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