Class: Api::UsersController

Inherits:
ApplicationController show all
Includes:
Affiliation, ExternalLinks, Follow, Recommendations, Relations, Upload, Utils
Defined in:
app/controllers/api/users_controller.rb

Instance Method Summary collapse

Methods included from Affiliation

#affiliation_create, #affiliation_destroy, #affiliation_index, #affiliation_show, #affiliation_update

Methods included from Utils

#get_id_from_short_title, #is_admin, #is_member, #is_reviewer, #nickname_exist, #short_title_exist

Methods included from Upload

#remove_avatar, #remove_banner, #remove_document, #upload_avatar, #upload_banner, #upload_document

Methods included from Relations

#clap, #clappers, #follow, #review, #reviewed_object, #save, #saved_objects

Methods included from Recommendations

#recommended, #similar

Methods included from Follow

#followers, #following

Methods included from ExternalLinks

#create_link, #destroy_link, #index_link, #update_link

Methods included from Response

#json_response

Instance Method Details

#archiveObject



51
52
53
54
55
56
57
# File 'app/controllers/api/users_controller.rb', line 51

def archive
  if current_user.archived!
    current_user.roles.delete_all
    current_user.owned_relations.destroy_all
    json_response(current_user)
  end
end

#confirm_emailObject



105
106
107
108
109
110
111
112
113
# File 'app/controllers/api/users_controller.rb', line 105

def confirm_email
  @user = User.where(confirm_token: params[:token]).first
  if @user
    @user.validate_email
    redirect_to 'http://localhost:3000/newjogler' if @user.save && (ENV['RAILS_ENV'] == 'development')
  else
    render json: { data: 'Sorry. User does not exist' }, status: :not_found
  end
end

#createObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/api/users_controller.rb', line 59

def create
  @user = User.where(email: create_user_params[:email])

  render(json: { error: 'Password confirmation does not match the password' },
         status: :unprocessable_entity) && return if create_user_params[:password] != create_user_params[:password_confirmation]

  # Prevent users from signing up if they don't have salt key as param
  render(json: { error: 'Forbidden' },
    status: :forbidden) && return if params[:salt_key] != ENV['SALT_KEY']

  if @user.blank?
    @user = User.new(create_user_params)
    @user.uid = @user.email
    @user.provider = 'email'
    if @user.save
      render json: { msg: 'Thank you for signing up, please confirm your email address to continue' }, status: :created
    else
      json_response(@user.errors)
    end
  else
    render json: { error: 'User already exists' }, status: :unprocessable_entity
  end
end

#destroyObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/api/users_controller.rb', line 29

def destroy
  params.require(:id)
  
  id = params[:id]

  # check if user is moderator
  render status: :forbidden and return unless current_user.has_role?(:moderator)

  # delete roles, relations and posts
  user = User.find(id)
  user.roles.delete_all
  user.owned_relations.destroy_all
  user.posts.destroy_all

  # then destory user
  destroyed_user = User.destroy(id)

  render status: :ok and return if destroyed_user.errors.empty?

  render status: :internal_server_error
end

#indexObject



24
25
26
27
# File 'app/controllers/api/users_controller.rb', line 24

def index
  @pagy, @users = pagy(User.where(active_status: 'active').includes(%i[interests skills sash]).all)
  render json: @users
end

#mutualObject



177
178
179
180
# File 'app/controllers/api/users_controller.rb', line 177

def mutual
  users = current_user.follow_mutual(@user)
  render json: users
end

#projectsObject



115
116
117
118
# File 'app/controllers/api/users_controller.rb', line 115

def projects
  users_projects = current_user.projects
  render json: users_projects, each_serializer: Api::ProjectSerializer
end

#resend_confirmationObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/api/users_controller.rb', line 142

def resend_confirmation
  unless user_params[:email]
    return render json: {
      success: false,
      errors: ['You must provide an email address.']
    }, status: 400
  end

  @user = User.find_by(email: user_params[:email])

  errors = nil

  if @user
    if @user.confirmed_at.present?
      errors = ['User already confirmed']
    else
      @user.resend_confirmation_instructions
    end
  else
    errors = ["Unable to find user with email '#{user_params[:email]}'."]
  end

  if errors
    render json: {
      success: false,
      errors: errors
    }, status: 400
  else
    render json: {
      status: 'success',
      data: @user.as_json
    }
  end
end

#send_private_emailObject



130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/api/users_controller.rb', line 130

def send_private_email
  if current_user.direct_message_limit_reached?
    render json: { data: 'Message limit reached' }, status: :forbidden
  elsif params[:object].nil? || params[:content].nil?
    render json: { data: 'Something went wrong :(' }, status: :unprocessable_entity
  else
    current_user.increment!(:direct_message_count)
    PrivateEmailWorker.perform_async(current_user.id, @user.id, params[:object], params[:content])
    render json: { data: 'Message sent' }, status: :ok
  end
end

#showObject



83
84
85
86
87
88
89
90
# File 'app/controllers/api/users_controller.rb', line 83

def show
  serializer = if current_user && current_user == @user
                 Api::UserSerializerWithPrivateFields
               else
                 Api::UserSerializer
               end
  render json: @user, serializer: serializer
end

#updateObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/api/users_controller.rb', line 92

def update
  current_user.active_at! unless current_user.nil?

  if @user.update(user_params)
    @user.update_skills(params[:user][:skills]) unless params[:user][:skills].nil?
    @user.update_ressources(params[:user][:ressources]) unless params[:user][:ressources].nil?
    @user.update_interests(params[:user][:interests]) unless params[:user][:interests].nil?
    render json: { data: 'User updated' }, status: :ok
  else
    render json: { data: 'Something went wrong :(' }, status: :unprocessable_entity
  end
end

#user_objectObject



120
121
122
123
124
125
126
127
128
# File 'app/controllers/api/users_controller.rb', line 120

def user_object
  klass = params[:object_type].singularize.camelize.constantize
  serializer = 'Api::' + params[:object_type].singularize.camelize + 'Serializer'
  @results = klass.with_role(:owner, @user)
  @results += klass.with_role(:admin, @user)
  @results += klass.with_role(:member, @user)
  @results += klass.with_role(:reviewer, @user)
  render json: @results.uniq, each_serializer: serializer.constantize
end

#validate_tokenObject



182
183
184
# File 'app/controllers/api/users_controller.rb', line 182

def validate_token
  render json: current_user, status: :ok
end