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