Merge branch 'captcha' into 'develop'
[akkoma] / lib / pleroma / web / common_api / common_api.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
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
10
11 import Pleroma.Web.CommonAPI.Utils
12
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
18 {:ok, delete}
19 end
20 end
21
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)
27 else
28 _ ->
29 {:error, "Could not repeat"}
30 end
31 end
32
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)
37 else
38 _ ->
39 {:error, "Could not unrepeat"}
40 end
41 end
42
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)
48 else
49 _ ->
50 {:error, "Could not favorite"}
51 end
52 end
53
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)
58 else
59 _ ->
60 {:error, "Could not unfavorite"}
61 end
62 end
63
64 def get_visibility(%{"visibility" => visibility})
65 when visibility in ~w{public unlisted private direct},
66 do: visibility
67
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
70 nil ->
71 "public"
72
73 inReplyTo ->
74 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
75 end
76 end
77
78 def get_visibility(_), do: "public"
79
80 defp get_content_type(content_type) do
81 if Enum.member?(Pleroma.Config.get([:instance, :allowed_post_formats]), content_type) do
82 content_type
83 else
84 "text/plain"
85 end
86 end
87
88 def post(user, %{"status" => status} = data) do
89 visibility = get_visibility(data)
90 limit = Pleroma.Config.get([:instance, :limit])
91
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),
98 content_html <-
99 make_content_html(
100 status,
101 mentions,
102 attachments,
103 tags,
104 get_content_type(data["content_type"]),
105 Enum.member?([true, "true"], data["no_attachment_links"])
106 ),
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),
111 object <-
112 make_note_data(
113 user.ap_id,
114 to,
115 context,
116 content_html,
117 attachments,
118 inReplyTo,
119 tags,
120 cw,
121 cc
122 ),
123 object <-
124 Map.put(
125 object,
126 "emoji",
127 Formatter.get_emoji(status)
128 |> Enum.reduce(%{}, fn {name, file}, acc ->
129 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
130 end)
131 ) do
132 res =
133 ActivityPub.create(%{
134 to: to,
135 actor: user,
136 context: context,
137 object: object,
138 additional: %{"cc" => cc}
139 })
140
141 res
142 end
143 end
144
145 # Updates the emojis for a user based on their profile
146 def update(user) do
147 user =
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
153 user
154 else
155 _e ->
156 user
157 end
158
159 ActivityPub.update(%{
160 local: true,
161 to: [user.follower_address],
162 cc: [],
163 actor: user.ap_id,
164 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
165 })
166 end
167 end