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
7 alias Pleroma.ActivityExpiration
8 alias Pleroma.Conversation.Participation
9 alias Pleroma.Formatter
11 alias Pleroma.ThreadMute
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.ActivityPub.Visibility
17 import Pleroma.Web.Gettext
18 import Pleroma.Web.CommonAPI.Utils
20 require Pleroma.Constants
22 def follow(follower, followed) do
23 with {:ok, follower} <- User.maybe_direct_follow(follower, followed),
24 {:ok, activity} <- ActivityPub.follow(follower, followed),
25 {:ok, follower, followed} <-
26 User.wait_and_refresh(
27 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
31 {:ok, follower, followed, activity}
35 def unfollow(follower, unfollowed) do
36 with {:ok, follower, _follow_activity} <- User.unfollow(follower, unfollowed),
37 {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed),
38 {:ok, _unfollowed} <- User.unsubscribe(follower, unfollowed) do
43 def accept_follow_request(follower, followed) do
44 with {:ok, follower} <- User.follow(follower, followed),
45 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
46 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
51 object: follow_activity.data["id"],
58 def reject_follow_request(follower, followed) do
59 with %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
60 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
65 object: follow_activity.data["id"],
72 def delete(activity_id, user) do
73 with %Activity{data: %{"object" => _}} = activity <-
74 Activity.get_by_id_with_object(activity_id),
75 %Object{} = object <- Object.normalize(activity),
76 true <- User.superuser?(user) || user.ap_id == object.data["actor"],
77 {:ok, _} <- unpin(activity_id, user),
78 {:ok, delete} <- ActivityPub.delete(object) do
82 {:error, dgettext("errors", "Could not delete")}
86 def repeat(id_or_ap_id, user) do
87 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
88 object <- Object.normalize(activity),
89 nil <- Utils.get_existing_announce(user.ap_id, object) do
90 ActivityPub.announce(user, object)
93 {:error, dgettext("errors", "Could not repeat")}
97 def unrepeat(id_or_ap_id, user) do
98 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
99 object <- Object.normalize(activity) do
100 ActivityPub.unannounce(user, object)
103 {:error, dgettext("errors", "Could not unrepeat")}
107 def favorite(id_or_ap_id, user) do
108 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
109 object <- Object.normalize(activity),
110 nil <- Utils.get_existing_like(user.ap_id, object) do
111 ActivityPub.like(user, object)
114 {:error, dgettext("errors", "Could not favorite")}
118 def unfavorite(id_or_ap_id, user) do
119 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
120 object <- Object.normalize(activity) do
121 ActivityPub.unlike(user, object)
124 {:error, dgettext("errors", "Could not unfavorite")}
128 def vote(user, object, choices) do
129 with "Question" <- object.data["type"],
130 {:author, false} <- {:author, object.data["actor"] == user.ap_id},
131 {:existing_votes, []} <- {:existing_votes, Utils.get_existing_votes(user.ap_id, object)},
132 {options, max_count} <- get_options_and_max_count(object),
133 option_count <- Enum.count(options),
134 {:choice_check, {choices, true}} <-
135 {:choice_check, normalize_and_validate_choice_indices(choices, option_count)},
136 {:count_check, true} <- {:count_check, Enum.count(choices) <= max_count} do
138 Enum.map(choices, fn index ->
139 answer_data = make_answer_data(user, object, Enum.at(options, index)["name"])
142 ActivityPub.create(%{
143 to: answer_data["to"],
145 context: object.data["context"],
147 additional: %{"cc" => answer_data["cc"]}
153 object = Object.get_cached_by_ap_id(object.data["id"])
154 {:ok, answer_activities, object}
156 {:author, _} -> {:error, dgettext("errors", "Poll's author can't vote")}
157 {:existing_votes, _} -> {:error, dgettext("errors", "Already voted")}
158 {:choice_check, {_, false}} -> {:error, dgettext("errors", "Invalid indices")}
159 {:count_check, false} -> {:error, dgettext("errors", "Too many choices")}
163 defp get_options_and_max_count(object) do
164 if Map.has_key?(object.data, "anyOf") do
165 {object.data["anyOf"], Enum.count(object.data["anyOf"])}
167 {object.data["oneOf"], 1}
171 defp normalize_and_validate_choice_indices(choices, count) do
172 Enum.map_reduce(choices, true, fn index, valid ->
173 index = if is_binary(index), do: String.to_integer(index), else: index
174 {index, if(valid, do: index < count, else: valid)}
178 def get_visibility(_, _, %Participation{}) do
182 def get_visibility(%{"visibility" => visibility}, in_reply_to, _)
183 when visibility in ~w{public unlisted private direct},
184 do: {visibility, get_replied_to_visibility(in_reply_to)}
186 def get_visibility(%{"visibility" => "list:" <> list_id}, in_reply_to, _) do
187 visibility = {:list, String.to_integer(list_id)}
188 {visibility, get_replied_to_visibility(in_reply_to)}
191 def get_visibility(_, in_reply_to, _) when not is_nil(in_reply_to) do
192 visibility = get_replied_to_visibility(in_reply_to)
193 {visibility, visibility}
196 def get_visibility(_, in_reply_to, _), do: {"public", get_replied_to_visibility(in_reply_to)}
198 def get_replied_to_visibility(nil), do: nil
200 def get_replied_to_visibility(activity) do
201 with %Object{} = object <- Object.normalize(activity) do
202 Pleroma.Web.ActivityPub.Visibility.get_visibility(object)
206 defp check_expiry_date({:ok, nil} = res), do: res
208 defp check_expiry_date({:ok, in_seconds}) do
209 expiry = NaiveDateTime.utc_now() |> NaiveDateTime.add(in_seconds)
211 if ActivityExpiration.expires_late_enough?(expiry) do
214 {:error, "Expiry date is too soon"}
218 defp check_expiry_date(expiry_str) do
219 Ecto.Type.cast(:integer, expiry_str)
220 |> check_expiry_date()
223 def post(user, %{"status" => status} = data) do
224 limit = Pleroma.Config.get([:instance, :limit])
226 with status <- String.trim(status),
227 attachments <- attachments_from_ids(data),
228 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
229 in_reply_to_conversation <- Participation.get(data["in_reply_to_conversation_id"]),
230 {visibility, in_reply_to_visibility} <-
231 get_visibility(data, in_reply_to, in_reply_to_conversation),
233 {:private_to_public, in_reply_to_visibility == "direct" && visibility != "direct"},
234 {content_html, mentions, tags} <-
241 mentioned_users <- for({_, mentioned_user} <- mentions, do: mentioned_user.ap_id),
242 addressed_users <- get_addressed_users(mentioned_users, data["to"]),
243 {poll, poll_emoji} <- make_poll_data(data),
245 get_to_and_cc(user, addressed_users, in_reply_to, visibility, in_reply_to_conversation),
246 context <- make_context(in_reply_to, in_reply_to_conversation),
247 cw <- data["spoiler_text"] || "",
248 sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}),
249 {:ok, expires_at} <- check_expiry_date(data["expires_in"]),
250 full_payload <- String.trim(status <> cw),
251 :ok <- validate_character_limit(full_payload, attachments, limit),
270 Map.merge(Formatter.get_emoji_map(full_payload), poll_emoji)
272 preview? = Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
273 direct? = visibility == "direct"
281 additional: %{"cc" => cc, "directMessage" => direct?}
283 |> maybe_add_list_data(user, visibility)
284 |> ActivityPub.create(preview?)
287 with {:ok, activity} <- result do
288 {:ok, _} = ActivityExpiration.create(activity, expires_at)
294 {:private_to_public, true} ->
295 {:error, dgettext("errors", "The message visibility must be direct")}
305 # Updates the emojis for a user based on their profile
308 with emoji <- emoji_from_profile(user),
309 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
310 info_cng <- User.Info.set_source_data(user.info, source_data),
311 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
312 {:ok, user} <- User.update_and_set_cache(change) do
319 ActivityPub.update(%{
321 to: [Pleroma.Constants.as_public(), user.follower_address],
324 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
328 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
339 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
340 true <- Visibility.is_public?(activity),
341 %{valid?: true} = info_changeset <- User.Info.add_pinnned_activity(user.info, activity),
343 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
344 {:ok, _user} <- User.update_and_set_cache(changeset) do
347 %{errors: [pinned_activities: {err, _}]} ->
351 {:error, dgettext("errors", "Could not pin")}
355 def unpin(id_or_ap_id, user) do
356 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
357 %{valid?: true} = info_changeset <-
358 User.Info.remove_pinnned_activity(user.info, activity),
360 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
361 {:ok, _user} <- User.update_and_set_cache(changeset) do
364 %{errors: [pinned_activities: {err, _}]} ->
368 {:error, dgettext("errors", "Could not unpin")}
372 def add_mute(user, activity) do
373 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
376 {:error, _} -> {:error, dgettext("errors", "conversation is already muted")}
380 def remove_mute(user, activity) do
381 ThreadMute.remove_mute(user.id, activity.data["context"])
385 def thread_muted?(%{id: nil} = _user, _activity), do: false
387 def thread_muted?(user, activity) do
388 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
395 def report(user, data) do
396 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
397 {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
398 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
399 {:ok, statuses} <- get_report_statuses(account, data),
402 context: Utils.generate_context_id(),
406 content: content_html,
407 forward: data["forward"] || false
411 {:error, err} -> {:error, err}
412 {:account_id, %{}} -> {:error, dgettext("errors", "Valid `account_id` required")}
413 {:account, nil} -> {:error, dgettext("errors", "Account not found")}
417 def update_report_state(activity_id, state) do
418 with %Activity{} = activity <- Activity.get_by_id(activity_id),
419 {:ok, activity} <- Utils.update_report_state(activity, state) do
422 nil -> {:error, :not_found}
423 {:error, reason} -> {:error, reason}
424 _ -> {:error, dgettext("errors", "Could not update state")}
428 def update_activity_scope(activity_id, opts \\ %{}) do
429 with %Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
430 {:ok, activity} <- toggle_sensitive(activity, opts),
431 {:ok, activity} <- set_visibility(activity, opts) do
434 nil -> {:error, :not_found}
435 {:error, reason} -> {:error, reason}
439 defp toggle_sensitive(activity, %{"sensitive" => sensitive}) when sensitive in ~w(true false) do
440 toggle_sensitive(activity, %{"sensitive" => String.to_existing_atom(sensitive)})
443 defp toggle_sensitive(%Activity{object: object} = activity, %{"sensitive" => sensitive})
444 when is_boolean(sensitive) do
445 new_data = Map.put(object.data, "sensitive", sensitive)
449 |> Object.change(%{data: new_data})
450 |> Object.update_and_set_cache()
452 {:ok, Map.put(activity, :object, object)}
455 defp toggle_sensitive(activity, _), do: {:ok, activity}
457 defp set_visibility(activity, %{"visibility" => visibility}) do
458 Utils.update_activity_visibility(activity, visibility)
461 defp set_visibility(activity, _), do: {:ok, activity}
463 def hide_reblogs(user, muted) do
466 if ap_id not in user.info.muted_reblogs do
467 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
468 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
469 User.update_and_set_cache(changeset)
473 def show_reblogs(user, muted) do
476 if ap_id in user.info.muted_reblogs do
477 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
478 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
479 User.update_and_set_cache(changeset)