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
10 alias Pleroma.ThreadMute
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Formatter
15 import Pleroma.Web.CommonAPI.Utils
17 def delete(activity_id, user) do
18 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
19 %Object{} = object <- Object.normalize(object_id),
20 true <- user.info.is_moderator || user.ap_id == object.data["actor"],
21 {:ok, _} <- unpin(activity_id, user),
22 {:ok, delete} <- ActivityPub.delete(object) do
27 def repeat(id_or_ap_id, user) do
28 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
29 object <- Object.normalize(activity.data["object"]["id"]),
30 nil <- Utils.get_existing_announce(user.ap_id, object) do
31 ActivityPub.announce(user, object)
34 {:error, "Could not repeat"}
38 def unrepeat(id_or_ap_id, user) do
39 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
40 object <- Object.normalize(activity.data["object"]["id"]) do
41 ActivityPub.unannounce(user, object)
44 {:error, "Could not unrepeat"}
48 def favorite(id_or_ap_id, user) do
49 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
50 object <- Object.normalize(activity.data["object"]["id"]),
51 nil <- Utils.get_existing_like(user.ap_id, object) do
52 ActivityPub.like(user, object)
55 {:error, "Could not favorite"}
59 def unfavorite(id_or_ap_id, user) do
60 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
61 object <- Object.normalize(activity.data["object"]["id"]) do
62 ActivityPub.unlike(user, object)
65 {:error, "Could not unfavorite"}
69 def get_visibility(%{"visibility" => visibility})
70 when visibility in ~w{public unlisted private direct},
73 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
74 case get_replied_to_activity(status_id) do
79 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
83 def get_visibility(_), do: "public"
85 defp get_content_type(content_type) do
86 if Enum.member?(Pleroma.Config.get([:instance, :allowed_post_formats]), content_type) do
93 def post(user, %{"status" => status} = data) do
94 visibility = get_visibility(data)
95 limit = Pleroma.Config.get([:instance, :limit])
97 with status <- String.trim(status),
98 attachments <- attachments_from_ids(data["media_ids"]),
99 mentions <- Formatter.parse_mentions(status),
100 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
101 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
102 tags <- Formatter.parse_tags(status, data),
109 get_content_type(data["content_type"]),
114 "no_attachment_links",
115 Pleroma.Config.get([:instance, :no_attachment_links], false)
119 context <- make_context(inReplyTo),
120 cw <- data["spoiler_text"],
121 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
122 length when length in 1..limit <- String.length(full_payload),
139 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
140 |> Enum.reduce(%{}, fn {name, file}, acc ->
141 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
145 ActivityPub.create(%{
150 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
157 # Updates the emojis for a user based on their profile
160 with emoji <- emoji_from_profile(user),
161 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
162 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
163 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
164 {:ok, user} <- User.update_and_set_cache(change) do
171 ActivityPub.update(%{
173 to: [user.follower_address],
176 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
180 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
190 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
191 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
192 %{valid?: true} = info_changeset <-
193 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
195 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
196 {:ok, _user} <- User.update_and_set_cache(changeset) do
199 %{errors: [pinned_activities: {err, _}]} ->
203 {:error, "Could not pin"}
207 def unpin(id_or_ap_id, user) do
208 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
209 %{valid?: true} = info_changeset <-
210 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
212 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
213 {:ok, _user} <- User.update_and_set_cache(changeset) do
216 %{errors: [pinned_activities: {err, _}]} ->
220 {:error, "Could not unpin"}
224 def add_mute(user, activity) do
225 with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]) do
228 {:error, _} -> {:error, "conversation is already muted"}
232 def remove_mute(user, activity) do
233 ThreadMute.remove_mute(user.id, activity.data["context"])
237 def thread_muted?(%{id: nil} = _user, _activity), do: false
239 def thread_muted?(user, activity) do
240 with [] <- ThreadMute.check_muted(user.id, activity.data["context"]) do