Merge branch 'fix/blocked-user-boosts' into 'develop'
[akkoma] / lib / pleroma / web / common_api / utils.ex
1 defmodule Pleroma.Web.CommonAPI.Utils do
2 alias Pleroma.{Repo, Object, Formatter, User, Activity}
3 alias Pleroma.Web.ActivityPub.Utils
4 alias Calendar.Strftime
5
6 # This is a hack for twidere.
7 def get_by_id_or_ap_id(id) do
8 activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
9
10 if activity.data["type"] == "Create" do
11 activity
12 else
13 Activity.get_create_activity_by_object_ap_id(activity.data["object"])
14 end
15 end
16
17 def get_replied_to_activity(id) when not is_nil(id) do
18 Repo.get(Activity, id)
19 end
20
21 def get_replied_to_activity(_), do: nil
22
23 def attachments_from_ids(ids) do
24 Enum.map(ids || [], fn media_id ->
25 Repo.get(Object, media_id).data
26 end)
27 end
28
29 def to_for_user_and_mentions(user, mentions, inReplyTo, "public") do
30 to = ["https://www.w3.org/ns/activitystreams#Public"]
31
32 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
33 cc = [user.follower_address | mentioned_users]
34
35 if inReplyTo do
36 {to, Enum.uniq([inReplyTo.data["actor"] | cc])}
37 else
38 {to, cc}
39 end
40 end
41
42 def to_for_user_and_mentions(user, mentions, inReplyTo, "unlisted") do
43 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "public")
44 {cc, to}
45 end
46
47 def to_for_user_and_mentions(user, mentions, inReplyTo, "private") do
48 {to, cc} = to_for_user_and_mentions(user, mentions, inReplyTo, "direct")
49 {[user.follower_address | to], cc}
50 end
51
52 def to_for_user_and_mentions(user, mentions, inReplyTo, "direct") do
53 mentioned_users = Enum.map(mentions, fn {_, %{ap_id: ap_id}} -> ap_id end)
54
55 if inReplyTo do
56 {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
57 else
58 {mentioned_users, []}
59 end
60 end
61
62 def make_content_html(status, mentions, attachments, tags, no_attachment_links \\ false) do
63 status
64 |> String.replace("\r", "")
65 |> format_input(mentions, tags)
66 |> maybe_add_attachments(attachments, no_attachment_links)
67 end
68
69 def make_context(%Activity{data: %{"context" => context}}), do: context
70 def make_context(_), do: Utils.generate_context_id()
71
72 def maybe_add_attachments(text, attachments, _no_links = true), do: text
73
74 def maybe_add_attachments(text, attachments, _no_links) do
75 add_attachments(text, attachments)
76 end
77
78 def add_attachments(text, attachments) do
79 attachment_text =
80 Enum.map(attachments, fn
81 %{"url" => [%{"href" => href} | _]} ->
82 name = URI.decode(Path.basename(href))
83 "<a href=\"#{href}\" class='attachment'>#{shortname(name)}</a>"
84
85 _ ->
86 ""
87 end)
88
89 Enum.join([text | attachment_text], "<br>")
90 end
91
92 def format_input(text, mentions, tags) do
93 text
94 |> Formatter.html_escape()
95 |> String.replace("\n", "<br>")
96 |> (&{[], &1}).()
97 |> Formatter.add_links()
98 |> Formatter.add_user_links(mentions)
99 |> Formatter.add_hashtag_links(tags)
100 |> Formatter.finalize()
101 end
102
103 def add_tag_links(text, tags) do
104 tags =
105 tags
106 |> Enum.sort_by(fn {tag, _} -> -String.length(tag) end)
107
108 Enum.reduce(tags, text, fn {full, tag}, text ->
109 url = "#<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag}</a>"
110 String.replace(text, full, url)
111 end)
112 end
113
114 def make_note_data(
115 actor,
116 to,
117 context,
118 content_html,
119 attachments,
120 inReplyTo,
121 tags,
122 cw \\ nil,
123 cc \\ []
124 ) do
125 object = %{
126 "type" => "Note",
127 "to" => to,
128 "cc" => cc,
129 "content" => content_html,
130 "summary" => cw,
131 "context" => context,
132 "attachment" => attachments,
133 "actor" => actor,
134 "tag" => tags |> Enum.map(fn {_, tag} -> tag end)
135 }
136
137 if inReplyTo do
138 object
139 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
140 |> Map.put("inReplyToStatusId", inReplyTo.id)
141 else
142 object
143 end
144 end
145
146 def format_naive_asctime(date) do
147 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
148 end
149
150 def format_asctime(date) do
151 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
152 end
153
154 def date_to_asctime(date) do
155 with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do
156 format_asctime(date)
157 else
158 _e ->
159 ""
160 end
161 end
162
163 def to_masto_date(%NaiveDateTime{} = date) do
164 date
165 |> NaiveDateTime.to_iso8601()
166 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
167 end
168
169 def to_masto_date(date) do
170 try do
171 date
172 |> NaiveDateTime.from_iso8601!()
173 |> NaiveDateTime.to_iso8601()
174 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
175 rescue
176 _e -> ""
177 end
178 end
179
180 defp shortname(name) do
181 if String.length(name) < 30 do
182 name
183 else
184 String.slice(name, 0..30) <> "…"
185 end
186 end
187 end