Fix hashtags.
[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 |> String.replace("\n", "<br>")
85 |> (&({[], &1})).()
86 |> Formatter.add_links
87 |> Formatter.add_user_links(mentions)
88 |> Formatter.add_hashtag_links(tags)
89 |> Formatter.finalize
90 end
91
92 def add_tag_links(text, tags) do
93 tags = tags
94 |> Enum.sort_by(fn ({tag, _}) -> -String.length(tag) end)
95
96 Enum.reduce(tags, text, fn({full, tag}, text) ->
97 url = "#<a href='#{Pleroma.Web.base_url}/tag/#{tag}' rel='tag'>#{tag}</a>"
98 String.replace(text, full, url)
99 end)
100 end
101
102 def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags, cw \\ nil, cc \\ []) do
103 object = %{
104 "type" => "Note",
105 "to" => to,
106 "cc" => cc,
107 "content" => content_html,
108 "summary" => cw,
109 "context" => context,
110 "attachment" => attachments,
111 "actor" => actor,
112 "tag" => tags |> Enum.map(fn ({_, tag}) -> tag end)
113 }
114
115 if inReplyTo do
116 object
117 |> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
118 |> Map.put("inReplyToStatusId", inReplyTo.id)
119 else
120 object
121 end
122 end
123
124 def format_naive_asctime(date) do
125 date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
126 end
127
128 def format_asctime(date) do
129 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
130 end
131
132 def date_to_asctime(date) do
133 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
134 format_asctime(date)
135 else _e ->
136 ""
137 end
138 end
139
140 def to_masto_date(%NaiveDateTime{} = date) do
141 date
142 |> NaiveDateTime.to_iso8601
143 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
144 end
145
146 def to_masto_date(date) do
147 try do
148 date
149 |> NaiveDateTime.from_iso8601!
150 |> NaiveDateTime.to_iso8601
151 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
152 rescue
153 _e -> ""
154 end
155 end
156
157 defp shortname(name) do
158 if String.length(name) < 30 do
159 name
160 else
161 String.slice(name, 0..30) <> "…"
162 end
163 end
164 end