Class: Api::V1::ConversationsController

Inherits:
BaseController show all
Includes:
ConversationsHelper
Defined in:
app/controllers/api/v1/conversations_controller.rb

Constant Summary collapse

BOOLEAN_TYPE =
ActiveModel::Type::Boolean.new

Instance Attribute Summary

Attributes included from OpenidConnect::ProtectedResourceEndpoint

#current_token

Instance Method Summary collapse

Methods included from ConversationsHelper

#conversation_class

Methods included from OpenidConnect::ProtectedResourceEndpoint

#access_token?, #require_access_token

Instance Method Details

#createObject


35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/api/v1/conversations_controller.rb', line 35

def create
  params.require(%i[subject body recipients])
  recipients = recipient_ids
  conversation = conversation_service.build(params[:subject], params[:body], recipients)
  raise ActiveRecord::RecordInvalid unless conversation_valid?(conversation, recipients)

  conversation.save!
  Diaspora::Federation::Dispatcher.defer_dispatch(current_user, conversation)
  render json: conversation_as_json(conversation), status: :created
rescue ActiveRecord::RecordInvalid, ActionController::ParameterMissing, ActiveRecord::RecordNotFound
  render_error 422, "Couldn't accept or process the conversation"
end

#destroyObject


58
59
60
61
62
# File 'app/controllers/api/v1/conversations_controller.rb', line 58

def destroy
  conversation = conversation_service.get_visibility(params[:id])
  conversation.destroy!
  head :no_content
end

#indexObject


18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/api/v1/conversations_controller.rb', line 18

def index
  mapped_params = {}
  mapped_params[:only_after] = params[:only_after] if params.has_key?(:only_after)

  mapped_params[:unread] = BOOLEAN_TYPE.cast(params[:only_unread]) if params.has_key?(:only_unread)

  conversations_query = conversation_service.all_for_user(mapped_params)
  conversations_page = pager(conversations_query, "conversations.created_at").response
  conversations_page[:data] = conversations_page[:data].map {|x| conversation_as_json(x) }
  render_paged_api_response conversations_page
end

#showObject


30
31
32
33
# File 'app/controllers/api/v1/conversations_controller.rb', line 30

def show
  conversation = conversation_service.find!(params[:id])
  render json: conversation_as_json(conversation)
end

#updateObject


48
49
50
51
52
53
54
55
56
# File 'app/controllers/api/v1/conversations_controller.rb', line 48

def update
  read = BOOLEAN_TYPE.cast(params.require(:read))
  conversation = conversation_service.find!(params[:id])
  conversation.update_read_for(current_user, read: read)

  render json: conversation_as_json(conversation)
rescue ActiveRecord::RecordInvalid, ActionController::ParameterMissing, ActiveRecord::RecordNotFound
  render_error 422, "Couldn't update the conversation"
end