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