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