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