Pleroma.Web.MastodonAPI.StatusView: Do not fail when URL isn’t a string
[akkoma] / lib / pleroma / web / common_api / common_api.ex
1 defmodule Pleroma.Web.CommonAPI do
2 alias Pleroma.{User, Repo, Activity, Object}
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Formatter
5
6 import Pleroma.Web.CommonAPI.Utils
7
8 def delete(activity_id, user) do
9 with %Activity{data: %{"object" => %{"id" => object_id}}} <- Repo.get(Activity, activity_id),
10 %Object{} = object <- Object.normalize(object_id),
11 true <- user.info["is_moderator"] || user.ap_id == object.data["actor"],
12 {:ok, delete} <- ActivityPub.delete(object),
13 {:ok, true} <- Cachex.del(:user_cache, "object:#{object_id}") do
14 {:ok, delete}
15 end
16 end
17
18 def repeat(id_or_ap_id, user) do
19 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
20 object <- Object.normalize(activity.data["object"]["id"]) do
21 ActivityPub.announce(user, object)
22 else
23 _ ->
24 {:error, "Could not repeat"}
25 end
26 end
27
28 def unrepeat(id_or_ap_id, user) do
29 with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
30 object <- Object.normalize(activity.data["object"]["id"]) do
31 ActivityPub.unannounce(user, object)
32 else
33 _ ->
34 {:error, "Could not unrepeat"}
35 end
36 end
37
38 def favorite(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.like(user, object)
42 else
43 _ ->
44 {:error, "Could not favorite"}
45 end
46 end
47
48 def unfavorite(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"]) do
51 ActivityPub.unlike(user, object)
52 else
53 _ ->
54 {:error, "Could not unfavorite"}
55 end
56 end
57
58 def get_visibility(%{"visibility" => visibility})
59 when visibility in ~w{public unlisted private direct},
60 do: visibility
61
62 def get_visibility(%{"in_reply_to_status_id" => status_id}) when not is_nil(status_id) do
63 case get_replied_to_activity(status_id) do
64 nil ->
65 "public"
66
67 inReplyTo ->
68 Pleroma.Web.MastodonAPI.StatusView.get_visibility(inReplyTo.data["object"])
69 end
70 end
71
72 def get_visibility(_), do: "public"
73
74 @instance Application.get_env(:pleroma, :instance)
75 @allowed_post_formats Keyword.get(@instance, :allowed_post_formats)
76
77 defp get_content_type(content_type) when content_type in @allowed_post_formats, do: content_type
78 defp get_content_type(_), do: "text/plain"
79
80 @limit Keyword.get(@instance, :limit)
81 def post(user, %{"status" => status} = data) do
82 visibility = get_visibility(data)
83
84 with status <- String.trim(status),
85 attachments <- attachments_from_ids(data["media_ids"]),
86 mentions <- Formatter.parse_mentions(status),
87 inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]),
88 {to, cc} <- to_for_user_and_mentions(user, mentions, inReplyTo, visibility),
89 tags <- Formatter.parse_tags(status, data),
90 content_html <-
91 make_content_html(
92 status,
93 mentions,
94 attachments,
95 tags,
96 get_content_type(data["content_type"]),
97 data["no_attachment_links"]
98 ),
99 context <- make_context(inReplyTo),
100 cw <- data["spoiler_text"],
101 full_payload <- String.trim(status <> (data["spoiler_text"] || "")),
102 length when length in 1..@limit <- String.length(full_payload),
103 object <-
104 make_note_data(
105 user.ap_id,
106 to,
107 context,
108 content_html,
109 attachments,
110 inReplyTo,
111 tags,
112 cw,
113 cc
114 ),
115 object <-
116 Map.put(
117 object,
118 "emoji",
119 Formatter.get_emoji(status)
120 |> Enum.reduce(%{}, fn {name, file}, acc ->
121 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
122 end)
123 ) do
124 res =
125 ActivityPub.create(%{
126 to: to,
127 actor: user,
128 context: context,
129 object: object,
130 additional: %{"cc" => cc}
131 })
132
133 res
134 end
135 end
136
137 def update(user) do
138 user =
139 with emoji <- emoji_from_profile(user),
140 source_data <- (user.info["source_data"] || %{}) |> Map.put("tag", emoji),
141 new_info <- Map.put(user.info, "source_data", source_data),
142 change <- User.info_changeset(user, %{info: new_info}),
143 {:ok, user} <- User.update_and_set_cache(change) do
144 user
145 else
146 _e ->
147 user
148 end
149
150 ActivityPub.update(%{
151 local: true,
152 to: [user.follower_address],
153 cc: [],
154 actor: user.ap_id,
155 object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
156 })
157 end
158 end