Credo fixes: alias grouping/ordering
[akkoma] / lib / pleroma / web / ostatus / handlers / note_handler.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OStatus.NoteHandler do
6 require Logger
7 alias Pleroma.Web.{XML, OStatus}
8 alias Pleroma.{Object, Activity}
9 alias Pleroma.Web.ActivityPub.{ActivityPub, Utils}
10 alias Pleroma.Web.CommonAPI
11
12 @doc """
13 Get the context for this note. Uses this:
14 1. The context of the parent activity
15 2. The conversation reference in the ostatus xml
16 3. A newly generated context id.
17 """
18 def get_context(entry, inReplyTo) do
19 context =
20 (XML.string_from_xpath("//ostatus:conversation[1]", entry) ||
21 XML.string_from_xpath("//ostatus:conversation[1]/@ref", entry) || "")
22 |> String.trim()
23
24 with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
25 context
26 else
27 _e ->
28 if String.length(context) > 0 do
29 context
30 else
31 Utils.generate_context_id()
32 end
33 end
34 end
35
36 def get_people_mentions(entry) do
37 :xmerl_xpath.string(
38 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]',
39 entry
40 )
41 |> Enum.map(fn person -> XML.string_from_xpath("@href", person) end)
42 end
43
44 def get_collection_mentions(entry) do
45 transmogrify = fn
46 "http://activityschema.org/collection/public" ->
47 "https://www.w3.org/ns/activitystreams#Public"
48
49 group ->
50 group
51 end
52
53 :xmerl_xpath.string(
54 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/collection"]',
55 entry
56 )
57 |> Enum.map(fn collection -> XML.string_from_xpath("@href", collection) |> transmogrify.() end)
58 end
59
60 def get_mentions(entry) do
61 (get_people_mentions(entry) ++ get_collection_mentions(entry))
62 |> Enum.filter(& &1)
63 end
64
65 def get_emoji(entry) do
66 try do
67 :xmerl_xpath.string('//link[@rel="emoji"]', entry)
68 |> Enum.reduce(%{}, fn emoji, acc ->
69 Map.put(acc, XML.string_from_xpath("@name", emoji), XML.string_from_xpath("@href", emoji))
70 end)
71 rescue
72 _e -> nil
73 end
74 end
75
76 def make_to_list(actor, mentions) do
77 [
78 actor.follower_address
79 ] ++ mentions
80 end
81
82 def add_external_url(note, entry) do
83 url = XML.string_from_xpath("//link[@rel='alternate' and @type='text/html']/@href", entry)
84 Map.put(note, "external_url", url)
85 end
86
87 def fetch_replied_to_activity(entry, inReplyTo) do
88 with %Activity{} = activity <- Activity.get_create_by_object_ap_id(inReplyTo) do
89 activity
90 else
91 _e ->
92 with inReplyToHref when not is_nil(inReplyToHref) <-
93 XML.string_from_xpath("//thr:in-reply-to[1]/@href", entry),
94 {:ok, [activity | _]} <- OStatus.fetch_activity_from_url(inReplyToHref) do
95 activity
96 else
97 _e -> nil
98 end
99 end
100 end
101
102 # TODO: Clean this up a bit.
103 def handle_note(entry, doc \\ nil) do
104 with id <- XML.string_from_xpath("//id", entry),
105 activity when is_nil(activity) <- Activity.get_create_by_object_ap_id(id),
106 [author] <- :xmerl_xpath.string('//author[1]', doc),
107 {:ok, actor} <- OStatus.find_make_or_update_user(author),
108 content_html <- OStatus.get_content(entry),
109 cw <- OStatus.get_cw(entry),
110 inReplyTo <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry),
111 inReplyToActivity <- fetch_replied_to_activity(entry, inReplyTo),
112 inReplyTo <- (inReplyToActivity && inReplyToActivity.data["object"]["id"]) || inReplyTo,
113 attachments <- OStatus.get_attachments(entry),
114 context <- get_context(entry, inReplyTo),
115 tags <- OStatus.get_tags(entry),
116 mentions <- get_mentions(entry),
117 to <- make_to_list(actor, mentions),
118 date <- XML.string_from_xpath("//published", entry),
119 unlisted <- XML.string_from_xpath("//mastodon:scope", entry) == "unlisted",
120 cc <- if(unlisted, do: ["https://www.w3.org/ns/activitystreams#Public"], else: []),
121 note <-
122 CommonAPI.Utils.make_note_data(
123 actor.ap_id,
124 to,
125 context,
126 content_html,
127 attachments,
128 inReplyToActivity,
129 [],
130 cw
131 ),
132 note <- note |> Map.put("id", id) |> Map.put("tag", tags),
133 note <- note |> Map.put("published", date),
134 note <- note |> Map.put("emoji", get_emoji(entry)),
135 note <- add_external_url(note, entry),
136 note <- note |> Map.put("cc", cc),
137 # TODO: Handle this case in make_note_data
138 note <-
139 if(
140 inReplyTo && !inReplyToActivity,
141 do: note |> Map.put("inReplyTo", inReplyTo),
142 else: note
143 ) do
144 ActivityPub.create(%{
145 to: to,
146 actor: actor,
147 context: context,
148 object: note,
149 published: date,
150 local: false,
151 additional: %{"cc" => cc}
152 })
153 else
154 %Activity{} = activity -> {:ok, activity}
155 e -> {:error, e}
156 end
157 end
158 end