1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPI do
8 alias Pleroma.Formatter
10 alias Pleroma.ThreadMute
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Utils
15 import Pleroma.Web.CommonAPI.Utils
17 def follow(follower, followed) do
18 with {:ok, follower} <- User.maybe_direct_follow(follower, followed),
19 {:ok, activity} <- ActivityPub.follow(follower, followed),
20 {:ok, follower, followed} <-
21 User.wait_and_refresh(
22 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
26 {:ok, follower, followed, activity}
30 def unfollow(follower, unfollowed) do
31 with {:ok, follower, _follow_activity} <- User.unfollow(follower, unfollowed),
32 {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed) do
37 def accept_follow_request(follower, followed) do
38 with {:ok, follower} <- User.maybe_follow(follower, followed),
39 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
40 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
45 object: follow_activity.data["id"],
52 def reject_follow_request(follower, followed) do
53 with %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
54 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
59 object: follow_activity.data["id"],
66 def delete(activity_id, user) do
67 with %Activity{data: %{"object" => _}} = activity <-
68 Activity.get_by_id_with_object(activity_id),
69 %Object{} = object <- Object.normalize(activity),
70 true <- User.superuser?(user) || user.ap_id == object.data["actor"],
71 {:ok, _} <- unpin(activity_id, user),
72 {:ok, delete} <- ActivityPub.delete(object) do
76 {:error, "Could not delete"}
80 def repeat(id_or_ap_id, user) do
81 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
82 object <- Object.normalize(activity),
83 nil <- Utils.get_existing_announce(user.ap_id, object) do
84 ActivityPub.announce(user, object)
87 {:error, "Could not repeat"}
91 def unrepeat(id_or_ap_id, user) do
92 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
93 object <- Object.normalize(activity) do
94 ActivityPub.unannounce(user, object)
97 {:error, "Could not unrepeat"}
101 def favorite(id_or_ap_id, user) do
102 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
103 object <- Object.normalize(activity),
104 nil <- Utils.get_existing_like(user.ap_id, object) do
105 ActivityPub.like(user, object)
108 {:error, "Could not favorite"}
112 def unfavorite(id_or_ap_id, user) do
113 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
114 object <- Object.normalize(activity) do
115 ActivityPub.unlike(user, object)
118 {:error, "Could not unfavorite"}
122 def vote(user, object, choices) do
123 with "Question" <- object.data["type"],
124 {:author, false} <- {:author, object.data["actor"] == user.ap_id},
125 {:existing_votes, []} <- {:existing_votes, Utils.get_existing_votes(user.ap_id, object)},
126 {options, max_count} <- get_options_and_max_count(object),
127 option_count <- Enum.count(options),
128 {:choice_check, {choices, true}} <-
129 {:choice_check, normalize_and_validate_choice_indices(choices, option_count)},
130 {:count_check, true} <- {:count_check, Enum.count(choices) <= max_count} do
132 Enum.map(choices, fn index ->
133 answer_data = make_answer_data(user, object, Enum.at(options, index)["name"])
135 ActivityPub.create(%{
136 to: answer_data["to"],
138 context: object.data["context"],
140 additional: %{"cc" => answer_data["cc"]}
144 object = Object.get_cached_by_ap_id(object.data["id"])
145 {:ok, answer_activities, object}
147 {:author, _} -> {:error, "Poll's author can't vote"}
148 {:existing_votes, _} -> {:error, "Already voted"}
149 {:choice_check, {_, false}} -> {:error, "Invalid indices"}
150 {:count_check, false} -> {:error, "Too many choices"}
154 defp get_options_and_max_count(object) do
155 if Map.has_key?(object.data, "anyOf") do
156 {object.data["anyOf"], Enum.count(object.data["anyOf"])}
158 {object.data["oneOf"], 1}
162 defp normalize_and_validate_choice_indices(choices, count) do
163 Enum.map_reduce(choices, true, fn index, valid ->
164 index = if is_binary(index), do: String.to_integer(index), else: index
165 {index, if(valid, do: index < count, else: valid)}
169 def get_visibility(%{"visibility" => visibility}, in_reply_to)
170 when visibility in ~w{public unlisted private direct},
171 do: {visibility, get_replied_to_visibility(in_reply_to)}
173 def get_visibility(_, in_reply_to) when not is_nil(in_reply_to) do
174 visibility = get_replied_to_visibility(in_reply_to)
175 {visibility, visibility}
178 def get_visibility(_, in_reply_to), do: {"public", get_replied_to_visibility(in_reply_to)}
180 def get_replied_to_visibility(nil), do: nil
182 def get_replied_to_visibility(activity) do
183 with %Object{} = object <- Object.normalize(activity) do
184 Pleroma.Web.ActivityPub.Visibility.get_visibility(object)
188 def post(user, %{"status" => status} = data) do
189 limit = Pleroma.Config.get([:instance, :limit])
191 with status <- String.trim(status),
192 attachments <- attachments_from_ids(data),
193 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
194 {visibility, in_reply_to_visibility} <- get_visibility(data, in_reply_to),
196 {:private_to_public, in_reply_to_visibility == "direct" && visibility != "direct"},
197 {content_html, mentions, tags} <-
204 mentioned_users <- for({_, mentioned_user} <- mentions, do: mentioned_user.ap_id),
205 addressed_users <- get_addressed_users(mentioned_users, data["to"]),
206 {poll, poll_emoji} <- make_poll_data(data),
207 {to, cc} <- get_to_and_cc(user, addressed_users, in_reply_to, visibility),
208 context <- make_context(in_reply_to),
209 cw <- data["spoiler_text"] || "",
210 sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}),
211 full_payload <- String.trim(status <> cw),
212 length when length in 1..limit <- String.length(full_payload),
231 Map.merge(Formatter.get_emoji_map(full_payload), poll_emoji)
240 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
242 Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
251 # Updates the emojis for a user based on their profile
254 with emoji <- emoji_from_profile(user),
255 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
256 info_cng <- User.Info.set_source_data(user.info, source_data),
257 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
258 {:ok, user} <- User.update_and_set_cache(change) do
265 ActivityPub.update(%{
267 to: [user.follower_address],
270 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
274 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
286 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
287 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
288 %{valid?: true} = info_changeset <-
289 User.Info.add_pinnned_activity(user.info, activity),
291 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
292 {:ok, _user} <- User.update_and_set_cache(changeset) do
295 %{errors: [pinned_activities: {err, _}]} ->
299 {:error, "Could not pin"}
303 def unpin(id_or_ap_id, user) do
304 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
305 %{valid?: true} = info_changeset <-
306 User.Info.remove_pinnned_activity(user.info, activity),
308 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
309 {:ok, _user} <- User.update_and_set_cache(changeset) do
312 %{errors: [pinned_activities: {err, _}]} ->
316 {:error, "Could not unpin"}
320 def add_mute(user, activity) do
321 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
324 {:error, _} -> {:error, "conversation is already muted"}
328 def remove_mute(user, activity) do
329 ThreadMute.remove_mute(user.id, activity.data["context"])
333 def thread_muted?(%{id: nil} = _user, _activity), do: false
335 def thread_muted?(user, activity) do
336 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
343 def bookmarked?(user, activity) do
344 with %Bookmark{} <- Bookmark.get(user.id, activity.id) do
352 def report(user, data) do
353 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
354 {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
355 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
356 {:ok, statuses} <- get_report_statuses(account, data),
359 context: Utils.generate_context_id(),
363 content: content_html,
364 forward: data["forward"] || false
368 {:error, err} -> {:error, err}
369 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
370 {:account, nil} -> {:error, "Account not found"}
374 def update_report_state(activity_id, state) do
375 with %Activity{} = activity <- Activity.get_by_id(activity_id),
376 {:ok, activity} <- Utils.update_report_state(activity, state) do
386 {:error, "Could not update state"}
390 def update_activity_scope(activity_id, opts \\ %{}) do
391 with %Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
392 {:ok, activity} <- toggle_sensitive(activity, opts),
393 {:ok, activity} <- set_visibility(activity, opts) do
404 defp toggle_sensitive(activity, %{"sensitive" => sensitive}) when sensitive in ~w(true false) do
405 toggle_sensitive(activity, %{"sensitive" => String.to_existing_atom(sensitive)})
408 defp toggle_sensitive(%Activity{object: object} = activity, %{"sensitive" => sensitive})
409 when is_boolean(sensitive) do
410 new_data = Map.put(object.data, "sensitive", sensitive)
414 |> Object.change(%{data: new_data})
415 |> Object.update_and_set_cache()
417 {:ok, Map.put(activity, :object, object)}
420 defp toggle_sensitive(activity, _), do: {:ok, activity}
422 defp set_visibility(activity, %{"visibility" => visibility}) do
423 Utils.update_activity_visibility(activity, visibility)
426 defp set_visibility(activity, _), do: {:ok, activity}
428 def hide_reblogs(user, muted) do
431 if ap_id not in user.info.muted_reblogs do
432 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
433 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
434 User.update_and_set_cache(changeset)
438 def show_reblogs(user, muted) do
441 if ap_id in user.info.muted_reblogs do
442 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
443 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
444 User.update_and_set_cache(changeset)