/app/controllers/conversations_controller.rb

https://github.com/prellele/diaspora · Ruby · 121 lines · 97 code · 20 blank · 4 comment · 13 complexity · 151bcfa5198dd77ed3e07f57c79143d1 MD5 · raw file

  1. # frozen_string_literal: true
  2. class ConversationsController < ApplicationController
  3. before_action :authenticate_user!
  4. respond_to :html, :mobile, :json, :js
  5. def index
  6. @visibilities = ConversationVisibility.includes(:conversation)
  7. .order("conversations.updated_at DESC")
  8. .where(person_id: current_user.person_id)
  9. .paginate(page: params[:page], per_page: 15)
  10. if params[:conversation_id]
  11. @conversation = Conversation.joins(:conversation_visibilities)
  12. .where(conversation_visibilities: {
  13. person_id: current_user.person_id,
  14. conversation_id: params[:conversation_id]
  15. }).first
  16. if @conversation
  17. @first_unread_message_id = @conversation.first_unread_message(current_user).try(:id)
  18. @conversation.set_read(current_user)
  19. end
  20. end
  21. gon.contacts = contacts_data
  22. respond_with do |format|
  23. format.html { render "index", locals: {no_contacts: current_user.contacts.mutual.empty?} }
  24. format.json { render json: @visibilities.map(&:conversation), status: 200 }
  25. end
  26. end
  27. def create
  28. # Contacts autocomplete does not work the same way on mobile and desktop
  29. # Mobile returns contact ids array while desktop returns person id
  30. # This will have to be removed when mobile autocomplete is ported to Typeahead
  31. recipients_param, column = [%i(contact_ids id), %i(person_ids person_id)].find {|param, _| params[param].present? }
  32. if recipients_param
  33. person_ids = current_user.contacts.mutual.where(column => params[recipients_param].split(",")).pluck(:person_id)
  34. end
  35. unless person_ids.present?
  36. render plain: I18n.t("javascripts.conversation.create.no_recipient"), status: 422
  37. return
  38. end
  39. opts = params.require(:conversation).permit(:subject)
  40. opts[:participant_ids] = person_ids
  41. opts[:message] = { text: params[:conversation][:text] }
  42. @conversation = current_user.build_conversation(opts)
  43. if @conversation.save
  44. Diaspora::Federation::Dispatcher.defer_dispatch(current_user, @conversation)
  45. flash[:notice] = I18n.t("conversations.create.sent")
  46. render json: {id: @conversation.id}
  47. else
  48. render plain: I18n.t("conversations.create.fail"), status: 422
  49. end
  50. end
  51. def show
  52. respond_to do |format|
  53. format.html do
  54. redirect_to conversations_path(conversation_id: params[:id])
  55. return
  56. end
  57. if @conversation = current_user.conversations.where(id: params[:id]).first
  58. @first_unread_message_id = @conversation.first_unread_message(current_user).try(:id)
  59. @conversation.set_read(current_user)
  60. format.json { render :json => @conversation, :status => 200 }
  61. else
  62. redirect_to conversations_path
  63. end
  64. end
  65. end
  66. def raw
  67. @conversation = current_user.conversations.where(id: params[:conversation_id]).first
  68. if @conversation
  69. @first_unread_message_id = @conversation.first_unread_message(current_user).try(:id)
  70. @conversation.set_read(current_user)
  71. render partial: "conversations/show", locals: {conversation: @conversation}
  72. else
  73. head :not_found
  74. end
  75. end
  76. def new
  77. if !params[:modal] && !session[:mobile_view] && request.format.html?
  78. redirect_to conversations_path
  79. return
  80. end
  81. if session[:mobile_view] == true && request.format.html?
  82. @contacts_json = contacts_data.to_json
  83. @contact_ids = if params[:contact_id]
  84. current_user.contacts.find(params[:contact_id]).id
  85. elsif params[:aspect_id]
  86. current_user.aspects.find(params[:aspect_id]).contacts.pluck(:id).join(",")
  87. end
  88. render :layout => true
  89. else
  90. render :layout => false
  91. end
  92. end
  93. private
  94. def contacts_data
  95. current_user.contacts.mutual.joins(person: :profile)
  96. .pluck(*%w(contacts.id profiles.first_name profiles.last_name people.diaspora_handle))
  97. .map {|contact_id, *name_attrs|
  98. {value: contact_id, name: ERB::Util.h(Person.name_from_attrs(*name_attrs)) }
  99. }
  100. end
  101. end