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.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Formatter
14 import Pleroma.Web.CommonAPI.Utils
16 def delete(activity_id, user) do
17 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
18 %Object{} = object <- Object.normalize(object_id),
19 true <- user.info.is_moderator || user.ap_id == object.data["actor"],
20 {:ok, _} <- unpin(activity_id, user),
21 {:ok, delete} <- ActivityPub.delete(object) do
26 def repeat(id_or_ap_id, user) do
27 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
28 object <- Object.normalize(activity.data["object"]["id"]),
29 nil <- Utils.get_existing_announce(user.ap_id, object) do
30 ActivityPub.announce(user, object)
33 {:error, "Could not repeat"}
37 def unrepeat(id_or_ap_id, user) do
38 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
39 object <- Object.normalize(activity.data["object"]["id"]) do
40 ActivityPub.unannounce(user, object)
43 {:error, "Could not unrepeat"}
47 def favorite(id_or_ap_id, user) do
48 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
49 object <- Object.normalize(activity.data["object"]["id"]),
50 nil <- Utils.get_existing_like(user.ap_id, object) do
51 ActivityPub.like(user, object)
54 {:error, "Could not favorite"}
58 def unfavorite(id_or_ap_id, user) do
59 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
60 object <- Object.normalize(activity.data["object"]["id"]) do
61 ActivityPub.unlike(user, object)
64 {:error, "Could not unfavorite"}
68 def get_visibility(%{"visibility" => visibility})
69 when visibility in ~w{public unlisted private direct},
72 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
73 case get_replied_to_activity(status_id) do
78 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
82 def get_visibility(_), do: "public"
84 defp get_content_type(content_type) do
85 if Enum.member?(Pleroma.Config.get([:instance, :allowed_post_formats]), content_type) do
92 def post(user, %{"status" => status} = data) do
93 visibility = get_visibility(data)
94 limit = Pleroma.Config.get([:instance, :limit])
96 with status <- String.trim(status),
97 attachments <- attachments_from_ids(data["media_ids"]),
98 mentions <- Formatter.parse_mentions(status),
99 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
100 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
101 tags <- Formatter.parse_tags(status, data),
108 get_content_type(data["content_type"]),
113 "no_attachment_links",
114 Pleroma.Config.get([:instance, :no_attachment_links], false)
118 context <- make_context(inReplyTo),
119 cw <- data["spoiler_text"],
120 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
121 length when length in 1..limit <- String.length(full_payload),
138 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
139 |> Enum.reduce(%{}, fn {name, file}, acc ->
140 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
144 ActivityPub.create(%{
149 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
156 # Updates the emojis for a user based on their profile
159 with emoji <- emoji_from_profile(user),
160 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
161 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
162 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
163 {:ok, user} <- User.update_and_set_cache(change) do
170 ActivityPub.update(%{
172 to: [user.follower_address],
175 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
179 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
189 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
190 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
191 %{valid?: true} = info_changeset <-
192 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
194 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
195 {:ok, _user} <- User.update_and_set_cache(changeset) do
198 %{errors: [pinned_activities: {err, _}]} ->
202 {:error, "Could not pin"}
206 def unpin(id_or_ap_id, user) do
207 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
208 %{valid?: true} = info_changeset <-
209 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
211 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
212 {:ok, _user} <- User.update_and_set_cache(changeset) do
215 %{errors: [pinned_activities: {err, _}]} ->
219 {:error, "Could not unpin"}