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