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