Class: Api::V1::UsersController

Inherits:
BaseController show all
Includes:
TagsHelper
Defined in:
app/controllers/api/v1/users_controller.rb

Instance Attribute Summary

Attributes included from OpenidConnect::ProtectedResourceEndpoint

#current_token

Instance Method Summary collapse

Methods included from TagsHelper

#looking_for_tag_link, #normalize_tag_name, #tag_link

Methods included from OpenidConnect::ProtectedResourceEndpoint

#access_token?, #require_access_token

Instance Method Details

#blockObject


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/api/v1/users_controller.rb', line 88

def block
  person = Person.find_by!(guid: params[:user_id])
  service = BlockService.new(current_user)
  if request.request_method_symbol == :post
    begin
      service.block(person)
      head :created
    rescue ActiveRecord::RecordNotUnique
      render_error 409, "User is already blocked"
    end
  elsif request.request_method_symbol == :delete
    begin
      service.unblock(person)
      head :no_content
    rescue ActiveRecord::RecordNotFound
      render_error 410, "User is not blocked"
    end
  else
    raise AbstractController::ActionNotFound
  end
end

#contactsObject


55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/api/v1/users_controller.rb', line 55

def contacts
  if params.require(:user_id) != current_user.guid
    render_error 404, "User not found"
    return
  end

  contacts_query = aspects_service.all_contacts
  contacts_page = index_pager(contacts_query).response
  contacts_page[:data] = contacts_page[:data].map {|c| PersonPresenter.new(c.person).as_api_json }
  render_paged_api_response contacts_page
end

#photosObject


67
68
69
70
71
72
73
74
# File 'app/controllers/api/v1/users_controller.rb', line 67

def photos
  person = Person.find_by!(guid: params[:user_id])
  user_for_query = current_user if private_read?
  photos_query = Photo.visible(user_for_query, person, :all, Time.current)
  photos_page = time_pager(photos_query).response
  photos_page[:data] = photos_page[:data].map {|photo| PhotoPresenter.new(photo).as_api_json(true) }
  render_paged_api_response photos_page
end

#postsObject


76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/api/v1/users_controller.rb', line 76

def posts
  person = Person.find_by!(guid: params[:user_id])
  posts_query = if private_read?
                  current_user.posts_from(person, false)
                else
                  Post.where(author_id: person.id, public: true)
                end
  posts_page = time_pager(posts_query).response
  posts_page[:data] = posts_page[:data].map {|post| PostPresenter.new(post, current_user).as_api_response }
  render_paged_api_response posts_page
end

#showObject


32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/api/v1/users_controller.rb', line 32

def show
  person = if params.has_key?(:id)
             found_person = Person.find_by!(guid: params[:id])
             raise ActiveRecord::RecordNotFound unless found_person.searchable || access_token?("contacts:read")

             found_person
           else
             current_user.person
           end
  render json: PersonPresenter.new(person, current_user).profile_hash_as_api_json
end

#updateObject


44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/api/v1/users_controller.rb', line 44

def update
  params_to_update = profile_update_params
  if params_to_update && current_user.update_profile(params_to_update)
    render json: PersonPresenter.new(current_user.person, current_user).profile_hash_as_api_json
  else
    render_error 422, "Failed to update the user settings"
  end
rescue RuntimeError
  render_error 422, "Failed to update the user settings"
end