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
6 alias Pleroma.{User, Repo, Activity, Object}
7 alias Pleroma.Web.ActivityPub.ActivityPub
8 alias Pleroma.Web.ActivityPub.Utils
9 alias Pleroma.Formatter
11 import Pleroma.Web.CommonAPI.Utils
13 def delete(activity_id, user) do
14 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
15 %Object{} = object <- Object.normalize(object_id),
16 true <- user.info.is_moderator || user.ap_id == object.data["actor"],
17 {:ok, _} <- unpin(activity_id, user),
18 {:ok, delete} <- ActivityPub.delete(object) do
23 def repeat(id_or_ap_id, user) do
24 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
25 object <- Object.normalize(activity.data["object"]["id"]),
26 nil <- Utils.get_existing_announce(user.ap_id, object) do
27 ActivityPub.announce(user, object)
30 {:error, "Could not repeat"}
34 def unrepeat(id_or_ap_id, user) do
35 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
36 object <- Object.normalize(activity.data["object"]["id"]) do
37 ActivityPub.unannounce(user, object)
40 {:error, "Could not unrepeat"}
44 def favorite(id_or_ap_id, user) do
45 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
46 object <- Object.normalize(activity.data["object"]["id"]),
47 nil <- Utils.get_existing_like(user.ap_id, object) do
48 ActivityPub.like(user, object)
51 {:error, "Could not favorite"}
55 def unfavorite(id_or_ap_id, user) do
56 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
57 object <- Object.normalize(activity.data["object"]["id"]) do
58 ActivityPub.unlike(user, object)
61 {:error, "Could not unfavorite"}
65 def get_visibility(%{"visibility" => visibility})
66 when visibility in ~w{public unlisted private direct},
69 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
70 case get_replied_to_activity(status_id) do
75 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
79 def get_visibility(_), do: "public"
81 defp get_content_type(content_type) do
82 if Enum.member?(Pleroma.Config.get([:instance, :allowed_post_formats]), content_type) do
89 def post(user, %{"status" => status} = data) do
90 visibility = get_visibility(data)
91 limit = Pleroma.Config.get([:instance, :limit])
93 with status <- String.trim(status),
94 attachments <- attachments_from_ids(data["media_ids"]),
95 mentions <- Formatter.parse_mentions(status),
96 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
97 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
98 tags <- Formatter.parse_tags(status, data),
105 get_content_type(data["content_type"]),
110 "no_attachment_links",
111 Pleroma.Config.get([:instance, :no_attachment_links], false)
115 context <- make_context(inReplyTo),
116 cw <- data["spoiler_text"],
117 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
118 length when length in 1..limit <- String.length(full_payload),
135 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
136 |> Enum.reduce(%{}, fn {name, file}, acc ->
137 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
141 ActivityPub.create(%{
146 additional: %{"cc" => cc, "directMessage" => visibility == "direct"}
153 # Updates the emojis for a user based on their profile
156 with emoji <- emoji_from_profile(user),
157 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
158 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
159 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
160 {:ok, user} <- User.update_and_set_cache(change) do
167 ActivityPub.update(%{
169 to: [user.follower_address],
172 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
176 def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do
186 } = activity <- get_by_id_or_ap_id(id_or_ap_id),
187 true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"),
188 %{valid?: true} = info_changeset <-
189 Pleroma.User.Info.add_pinnned_activity(user.info, activity),
191 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
192 {:ok, _user} <- User.update_and_set_cache(changeset) do
195 %{errors: [pinned_activities: {err, _}]} ->
199 {:error, "Could not pin"}
203 def unpin(id_or_ap_id, user) do
204 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
205 %{valid?: true} = info_changeset <-
206 Pleroma.User.Info.remove_pinnned_activity(user.info, activity),
208 Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset),
209 {:ok, _user} <- User.update_and_set_cache(changeset) do
212 %{errors: [pinned_activities: {err, _}]} ->
216 {:error, "Could not unpin"}