1 | # Copyright (c) 2010-2011, Diaspora Inc. This file is |
---|
2 | # licensed under the Affero General Public License version 3 or later. See |
---|
3 | # the COPYRIGHT file. |
---|
4 | class StatusMessagesController < ApplicationController |
---|
5 | before_action :authenticate_user! |
---|
6 | |
---|
7 | before_action :remove_getting_started, only: :create |
---|
8 | |
---|
9 | respond_to :html, :mobile, :json |
---|
10 | |
---|
11 | layout "application", only: :bookmarklet |
---|
12 | |
---|
13 | # Called when a user clicks "Mention" on a profile page |
---|
14 | # @param person_id [Integer] The id of the person to be mentioned |
---|
15 | def new |
---|
16 | if params[:person_id] && fetch_person(params[:person_id]) |
---|
17 | @aspect = :profile |
---|
18 | @contact = current_user.contact_for(@person) |
---|
19 | if @contact |
---|
20 | @aspects_with_person = @contact.aspects.load |
---|
21 | @aspect_ids = @aspects_with_person.map(&:id) |
---|
22 | gon.aspect_ids = @aspect_ids |
---|
23 | render layout: nil |
---|
24 | else |
---|
25 | @aspects_with_person = [] |
---|
26 | end |
---|
27 | elsif request.format == :mobile |
---|
28 | @aspect = :all |
---|
29 | @aspects = current_user.aspects.load |
---|
30 | @aspect_ids = @aspects.map(&:id) |
---|
31 | gon.aspect_ids = @aspect_ids |
---|
32 | else |
---|
33 | redirect_to stream_path |
---|
34 | end |
---|
35 | end |
---|
36 | |
---|
37 | def bookmarklet |
---|
38 | @aspects = current_user.aspects |
---|
39 | @aspect_ids = current_user.aspect_ids |
---|
40 | |
---|
41 | gon.preloads[:bookmarklet] = { |
---|
42 | content: params[:content], |
---|
43 | title: params[:title], |
---|
44 | url: params[:url], |
---|
45 | notes: params[:notes] |
---|
46 | } |
---|
47 | end |
---|
48 | |
---|
49 | def create |
---|
50 | normalized_params = params.merge( |
---|
51 | services: normalize_services, |
---|
52 | aspect_ids: normalize_aspect_ids, |
---|
53 | public: normalize_public_flag |
---|
54 | ) |
---|
55 | status_message = StatusMessageCreationService.new(current_user).create(normalized_params) |
---|
56 | handle_mention_feedback(status_message) |
---|
57 | respond_to do |format| |
---|
58 | format.html { redirect_to :back } |
---|
59 | format.mobile { redirect_to stream_path } |
---|
60 | format.json { render json: PostPresenter.new(status_message, current_user), status: 201 } |
---|
61 | end |
---|
62 | rescue StandardError => error |
---|
63 | handle_create_error(error) |
---|
64 | end |
---|
65 | |
---|
66 | private |
---|
67 | |
---|
68 | def fetch_person(person_id) |
---|
69 | @person = Person.where(id: person_id).first |
---|
70 | end |
---|
71 | |
---|
72 | def handle_create_error(error) |
---|
73 | logger.debug error |
---|
74 | respond_to do |format| |
---|
75 | format.html { redirect_to :back } |
---|
76 | format.mobile { redirect_to stream_path } |
---|
77 | format.json { render text: error.message, status: 403 } |
---|
78 | end |
---|
79 | end |
---|
80 | |
---|
81 | def handle_mention_feedback(status_message) |
---|
82 | return unless comes_from_others_profile_page? |
---|
83 | flash[:notice] = t("status_messages.create.success", names: status_message.mentioned_people_names) |
---|
84 | end |
---|
85 | |
---|
86 | def comes_from_others_profile_page? |
---|
87 | coming_from_profile_page? && !own_profile_page? |
---|
88 | end |
---|
89 | |
---|
90 | def coming_from_profile_page? |
---|
91 | request.env["HTTP_REFERER"].include?("people") |
---|
92 | end |
---|
93 | |
---|
94 | def own_profile_page? |
---|
95 | request.env["HTTP_REFERER"].include?("/people/" + current_user.guid) |
---|
96 | end |
---|
97 | |
---|
98 | def normalize_services |
---|
99 | [*params[:services]].compact |
---|
100 | end |
---|
101 | |
---|
102 | def normalize_aspect_ids |
---|
103 | aspect_ids = [*params[:aspect_ids]] |
---|
104 | if aspect_ids.first == "all_aspects" |
---|
105 | current_user.aspect_ids |
---|
106 | else |
---|
107 | aspect_ids |
---|
108 | end |
---|
109 | end |
---|
110 | |
---|
111 | def normalize_public_flag |
---|
112 | [*params[:aspect_ids]].first == "public" |
---|
113 | end |
---|
114 | |
---|
115 | def remove_getting_started |
---|
116 | current_user.disable_getting_started |
---|
117 | end |
---|
118 | end |
---|