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, delete} <- ActivityPub.delete(object) do
22 def repeat(id_or_ap_id, user) do
23 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
24 object <- Object.normalize(activity.data["object"]["id"]),
25 nil <- Utils.get_existing_announce(user.ap_id, object) do
26 ActivityPub.announce(user, object)
29 {:error, "Could not repeat"}
33 def unrepeat(id_or_ap_id, user) do
34 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
35 object <- Object.normalize(activity.data["object"]["id"]) do
36 ActivityPub.unannounce(user, object)
39 {:error, "Could not unrepeat"}
43 def favorite(id_or_ap_id, user) do
44 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
45 object <- Object.normalize(activity.data["object"]["id"]),
46 nil <- Utils.get_existing_like(user.ap_id, object) do
47 ActivityPub.like(user, object)
50 {:error, "Could not favorite"}
54 def unfavorite(id_or_ap_id, user) do
55 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
56 object <- Object.normalize(activity.data["object"]["id"]) do
57 ActivityPub.unlike(user, object)
60 {:error, "Could not unfavorite"}
64 def get_visibility(%{"visibility" => visibility})
65 when visibility in ~w{public unlisted private direct},
68 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
69 case get_replied_to_activity(status_id) do
74 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
78 def get_visibility(_), do: "public"
80 defp get_content_type(content_type) do
81 if Enum.member?(Pleroma.Config.get([:instance, :allowed_post_formats]), content_type) do
88 def post(user, %{"status" => status} = data) do
89 visibility = get_visibility(data)
90 limit = Pleroma.Config.get([:instance, :limit])
92 with status <- String.trim(status),
93 attachments <- attachments_from_ids(data["media_ids"]),
94 mentions <- Formatter.parse_mentions(status),
95 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
96 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
97 tags <- Formatter.parse_tags(status, data),
104 get_content_type(data["content_type"]),
105 Enum.member?([true, "true"], data["no_attachment_links"])
107 context <- make_context(inReplyTo),
108 cw <- data["spoiler_text"],
109 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
110 length when length in 1..limit <- String.length(full_payload),
127 (Formatter.get_emoji(status) ++ Formatter.get_emoji(data["spoiler_text"]))
128 |> Enum.reduce(%{}, fn {name, file}, acc ->
129 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
133 ActivityPub.create(%{
138 additional: %{"cc" => cc}
145 # Updates the emojis for a user based on their profile
148 with emoji <- emoji_from_profile(user),
149 source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
150 info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
151 change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
152 {:ok, user} <- User.update_and_set_cache(change) do
159 ActivityPub.update(%{
161 to: [user.follower_address],
164 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})