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