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 get_visibility(%{"visibility" => visibility}, in_reply_to)
123 when visibility in ~w{public unlisted private direct},
124 do: {visibility, get_replied_to_visibility(in_reply_to)}
126 def get_visibility(%{"visibility" => "list:" <> list_id}, in_reply_to) do
127 visibility = {:list, String.to_integer(list_id)}
128 {visibility, get_replied_to_visibility(in_reply_to)}
131 def get_visibility(_, in_reply_to) when not is_nil(in_reply_to) do
132 visibility = get_replied_to_visibility(in_reply_to)
133 {visibility, visibility}
136 def get_visibility(_, in_reply_to), do: {"public", get_replied_to_visibility(in_reply_to)}
138 def get_replied_to_visibility(nil), do: nil
140 def get_replied_to_visibility(activity) do
141 with %Object{} = object <- Object.normalize(activity) do
142 Pleroma.Web.ActivityPub.Visibility.get_visibility(object)
146 def post(user, %{"status" => status} = data) do
147 limit = Pleroma.Config.get([:instance, :limit])
149 with status <- String.trim(status),
150 attachments <- attachments_from_ids(data),
151 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
152 {visibility, in_reply_to_visibility} <- get_visibility(data, in_reply_to),
154 {:private_to_public, in_reply_to_visibility == "direct" && visibility != "direct"},
155 {content_html, mentions, tags} <-
162 {to, cc} <- to_for_user_and_mentions(user, mentions, in_reply_to, visibility),
163 bcc <- bcc_for_list(user, visibility),
164 context <- make_context(in_reply_to),
165 cw <- data["spoiler_text"] || "",
166 full_payload <- String.trim(status <> cw),
167 length when length in 1..limit <- String.length(full_payload),
184 Formatter.get_emoji_map(full_payload)
192 additional: %{"cc" => cc, "bcc" => bcc, "directMessage" => visibility == "direct"}
194 Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
201 # Updates the emojis for a user based on their profile
204 with emoji <- emoji_from_profile(user),
205 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
206 info_cng <- User.Info.set_source_data(user.info, source_data),
207 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
208 {:ok, user} <- User.update_and_set_cache(change) do
215 ActivityPub.update(%{
217 to: [user.follower_address],
220 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
224 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
236 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
237 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
238 %{valid?: true} = info_changeset <-
239 User.Info.add_pinnned_activity(user.info, activity),
241 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
242 {:ok, _user} <- User.update_and_set_cache(changeset) do
245 %{errors: [pinned_activities: {err, _}]} ->
249 {:error, "Could not pin"}
253 def unpin(id_or_ap_id, user) do
254 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
255 %{valid?: true} = info_changeset <-
256 User.Info.remove_pinnned_activity(user.info, activity),
258 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
259 {:ok, _user} <- User.update_and_set_cache(changeset) do
262 %{errors: [pinned_activities: {err, _}]} ->
266 {:error, "Could not unpin"}
270 def add_mute(user, activity) do
271 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
274 {:error, _} -> {:error, "conversation is already muted"}
278 def remove_mute(user, activity) do
279 ThreadMute.remove_mute(user.id, activity.data["context"])
283 def thread_muted?(%{id: nil} = _user, _activity), do: false
285 def thread_muted?(user, activity) do
286 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
293 def bookmarked?(user, activity) do
294 with %Bookmark{} <- Bookmark.get(user.id, activity.id) do
302 def report(user, data) do
303 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
304 {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
305 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
306 {:ok, statuses} <- get_report_statuses(account, data),
309 context: Utils.generate_context_id(),
313 content: content_html,
314 forward: data["forward"] || false
318 {:error, err} -> {:error, err}
319 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
320 {:account, nil} -> {:error, "Account not found"}
324 def update_report_state(activity_id, state) do
325 with %Activity{} = activity <- Activity.get_by_id(activity_id),
326 {:ok, activity} <- Utils.update_report_state(activity, state) do
336 {:error, "Could not update state"}
340 def update_activity_scope(activity_id, opts \\ %{}) do
341 with %Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
342 {:ok, activity} <- toggle_sensitive(activity, opts),
343 {:ok, activity} <- set_visibility(activity, opts) do
354 defp toggle_sensitive(activity, %{"sensitive" => sensitive}) when sensitive in ~w(true false) do
355 toggle_sensitive(activity, %{"sensitive" => String.to_existing_atom(sensitive)})
358 defp toggle_sensitive(%Activity{object: object} = activity, %{"sensitive" => sensitive})
359 when is_boolean(sensitive) do
360 new_data = Map.put(object.data, "sensitive", sensitive)
364 |> Object.change(%{data: new_data})
365 |> Object.update_and_set_cache()
367 {:ok, Map.put(activity, :object, object)}
370 defp toggle_sensitive(activity, _), do: {:ok, activity}
372 defp set_visibility(activity, %{"visibility" => visibility}) do
373 Utils.update_activity_visibility(activity, visibility)
376 defp set_visibility(activity, _), do: {:ok, activity}
378 def hide_reblogs(user, muted) do
381 if ap_id not in user.info.muted_reblogs do
382 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
383 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
384 User.update_and_set_cache(changeset)
388 def show_reblogs(user, muted) do
391 if ap_id in user.info.muted_reblogs do
392 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
393 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
394 User.update_and_set_cache(changeset)