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.Formatter
9 alias Pleroma.ThreadMute
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Utils
14 import Pleroma.Web.CommonAPI.Utils
16 def follow(follower, followed) do
17 with {:ok, follower} <- User.maybe_direct_follow(follower, followed),
18 {:ok, activity} <- ActivityPub.follow(follower, followed),
19 {:ok, follower, followed} <-
20 User.wait_and_refresh(
21 Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
25 {:ok, follower, followed, activity}
29 def unfollow(follower, unfollowed) do
30 with {:ok, follower, _follow_activity} <- User.unfollow(follower, unfollowed),
31 {:ok, _activity} <- ActivityPub.unfollow(follower, unfollowed) do
36 def accept_follow_request(follower, followed) do
37 with {:ok, follower} <- User.maybe_follow(follower, followed),
38 %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
39 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
44 object: follow_activity.data["id"],
51 def reject_follow_request(follower, followed) do
52 with %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
53 {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
58 object: follow_activity.data["id"],
65 def delete(activity_id, user) do
66 with %Activity{data: %{"object" => _}} = activity <- Activity.get_by_id_with_object(activity_id),
67 %Object{} = object <- Object.normalize(activity),
68 true <- User.superuser?(user) || user.ap_id == object.data["actor"],
69 {:ok, _} <- unpin(activity_id, user),
70 {:ok, delete} <- ActivityPub.delete(object) do
75 def repeat(id_or_ap_id, user) do
76 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
77 object <- Object.normalize(activity),
78 nil <- Utils.get_existing_announce(user.ap_id, object) do
79 ActivityPub.announce(user, object)
82 {:error, "Could not repeat"}
86 def unrepeat(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) do
89 ActivityPub.unannounce(user, object)
92 {:error, "Could not unrepeat"}
96 def favorite(id_or_ap_id, user) do
97 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
98 object <- Object.normalize(activity),
99 nil <- Utils.get_existing_like(user.ap_id, object) do
100 ActivityPub.like(user, object)
103 {:error, "Could not favorite"}
107 def unfavorite(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) do
110 ActivityPub.unlike(user, object)
113 {:error, "Could not unfavorite"}
117 def get_visibility(%{"visibility" => visibility})
118 when visibility in ~w{public unlisted private direct},
121 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
122 case get_replied_to_activity(status_id) do
127 Pleroma.Web.MastodonAPI.StatusView.get_visibility(in_reply_to.data["object"])
131 def get_visibility(_), do: "public"
133 def post(user, %{"status" => status} = data) do
134 visibility = get_visibility(data)
135 limit = Pleroma.Config.get([:instance, :limit])
137 with status <- String.trim(status),
138 attachments <- attachments_from_ids(data),
139 in_reply_to <- get_replied_to_activity(data["in_reply_to_status_id"]),
140 {content_html, mentions, tags} <-
147 {to, cc} <- to_for_user_and_mentions(user, mentions, in_reply_to, visibility),
148 context <- make_context(in_reply_to),
149 cw <- data["spoiler_text"],
150 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
151 length when length in 1..limit <- String.length(full_payload),
168 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
169 |> Enum.reduce(%{}, fn {name, file}, acc ->
170 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
174 ActivityPub.create(%{
179 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
186 # Updates the emojis for a user based on their profile
189 with emoji <- emoji_from_profile(user),
190 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
191 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
192 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
193 {:ok, user} <- User.update_and_set_cache(change) do
200 ActivityPub.update(%{
202 to: [user.follower_address],
205 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
209 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
219 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
220 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
221 %{valid?: true} = info_changeset <-
222 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
224 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
225 {:ok, _user} <- User.update_and_set_cache(changeset) do
228 %{errors: [pinned_activities: {err, _}]} ->
232 {:error, "Could not pin"}
236 def unpin(id_or_ap_id, user) do
237 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
238 %{valid?: true} = info_changeset <-
239 Pleroma.User.Info.remove_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 unpin"}
253 def add_mute(user, activity) do
254 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
257 {:error, _} -> {:error, "conversation is already muted"}
261 def remove_mute(user, activity) do
262 ThreadMute.remove_mute(user.id, activity.data["context"])
266 def thread_muted?(%{id: nil} = _user, _activity), do: false
268 def thread_muted?(user, activity) do
269 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do
276 def report(user, data) do
277 with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
278 {:account, %User{} = account} <- {:account, User.get_by_id(account_id)},
279 {:ok, {content_html, _, _}} <- make_report_content_html(data["comment"]),
280 {:ok, statuses} <- get_report_statuses(account, data),
283 context: Utils.generate_context_id(),
287 content: content_html,
288 forward: data["forward"] || false
292 {:error, err} -> {:error, err}
293 {:account_id, %{}} -> {:error, "Valid `account_id` required"}
294 {:account, nil} -> {:error, "Account not found"}
298 def hide_reblogs(user, muted) do
301 if ap_id not in user.info.muted_reblogs do
302 info_changeset = User.Info.add_reblog_mute(user.info, ap_id)
303 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
304 User.update_and_set_cache(changeset)
308 def show_reblogs(user, muted) do
311 if ap_id in user.info.muted_reblogs do
312 info_changeset = User.Info.remove_reblog_mute(user.info, ap_id)
313 changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset)
314 User.update_and_set_cache(changeset)