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
77 def repeat(id_or_ap_id, user) do
78 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
79 object <- Object.normalize(activity),
80 nil <- Utils.get_existing_announce(user.ap_id, object) do
81 ActivityPub.announce(user, object)
84 {:error, "Could not repeat"}
88 def unrepeat(id_or_ap_id, user) do
89 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
90 object <- Object.normalize(activity) do
91 ActivityPub.unannounce(user, object)
94 {:error, "Could not unrepeat"}
98 def favorite(id_or_ap_id, user) do
99 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
100 object <- Object.normalize(activity),
101 nil <- Utils.get_existing_like(user.ap_id, object) do
102 ActivityPub.like(user, object)
105 {:error, "Could not favorite"}
109 def unfavorite(id_or_ap_id, user) do
110 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
111 object <- Object.normalize(activity) do
112 ActivityPub.unlike(user, object)
115 {:error, "Could not unfavorite"}
119 def get_visibility(%{"visibility" => visibility}, in_reply_to)
120 when visibility in ~w{public unlisted private direct},
121 do: {visibility, get_replied_to_visibility(in_reply_to)}
123 def get_visibility(_, in_reply_to) when not is_nil(in_reply_to) do
124 visibility = get_replied_to_visibility(in_reply_to)
125 {visibility, visibility}
128 def get_visibility(_, in_reply_to), do: {"public", get_replied_to_visibility(in_reply_to)}
130 def get_replied_to_visibility(nil), do: nil
132 def get_replied_to_visibility(activity) do
133 with %Object{} = object <- Object.normalize(activity) do
134 Pleroma.Web.ActivityPub.Visibility.get_visibility(object)
138 def post(user, %{"status" => status} = data) do
139 limit = Pleroma.Config.get([:instance, :limit])
141 with status <- String.trim(status),
142 attachments <- attachments_from_ids(data),
143 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
144 {visibility, in_reply_to_visibility} <- get_visibility(data, in_reply_to),
146 {:private_to_public, in_reply_to_visibility == "direct" && visibility != "direct"},
147 {content_html, mentions, tags} <-
154 {to, cc} <- to_for_user_and_mentions(user, mentions, in_reply_to, visibility),
155 context <- make_context(in_reply_to),
156 cw <- data["spoiler_text"] || "",
157 full_payload <- String.trim(status <> cw),
158 length when length in 1..limit <- String.length(full_payload),
175 Formatter.get_emoji_map(full_payload)
184 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
186 Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
195 # Updates the emojis for a user based on their profile
198 with emoji <- emoji_from_profile(user),
199 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
200 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
201 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
202 {:ok, user} <- User.update_and_set_cache(change) do
209 ActivityPub.update(%{
211 to: [user.follower_address],
214 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
218 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
230 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
231 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
232 %{valid?: true} = info_changeset <-
233 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
235 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
236 {:ok, _user} <- User.update_and_set_cache(changeset) do
239 %{errors: [pinned_activities: {err, _}]} ->
243 {:error, "Could not pin"}
247 def unpin(id_or_ap_id, user) do
248 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
249 %{valid?: true} = info_changeset <-
250 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
252 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
253 {:ok, _user} <- User.update_and_set_cache(changeset) do
256 %{errors: [pinned_activities: {err, _}]} ->
260 {:error, "Could not unpin"}
264 def add_mute(user, activity) do
265 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
268 {:error, _} -> {:error, "conversation is already muted"}
272 def remove_mute(user, activity) do
273 ThreadMute.remove_mute(user.id, activity.data["context"])
277 def thread_muted?(%{id: nil} = _user, _activity), do: false
279 def thread_muted?(user, activity) do
280 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
287 def bookmarked?(user, activity) do
288 with %Bookmark{} <- Bookmark.get(user.id, activity.id) do
296 def report(user, data) do
297 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
298 {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
299 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
300 {:ok, statuses} <- get_report_statuses(account, data),
303 context: Utils.generate_context_id(),
307 content: content_html,
308 forward: data["forward"] || false
312 {:error, err} -> {:error, err}
313 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
314 {:account, nil} -> {:error, "Account not found"}
318 def hide_reblogs(user, muted) do
321 if ap_id not in user.info.muted_reblogs do
322 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
323 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
324 User.update_and_set_cache(changeset)
328 def show_reblogs(user, muted) do
331 if ap_id in user.info.muted_reblogs do
332 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
333 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
334 User.update_and_set_cache(changeset)