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