Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into feature...
[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, User, 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 inReplyTo <- (inReplyToActivity && inReplyToActivity.data["object"]["id"]) || inReplyTo,
110 attachments <- OStatus.get_attachments(entry),
111 context <- get_context(entry, inReplyTo),
112 tags <- OStatus.get_tags(entry),
113 mentions <- get_mentions(entry),
114 to <- make_to_list(actor, mentions),
115 date <- XML.string_from_xpath("//published", entry),
116 unlisted <- XML.string_from_xpath("//mastodon:scope", entry) == "unlisted",
117 cc <- if(unlisted, do: ["https://www.w3.org/ns/activitystreams#Public"], else: []),
118 note <-
119 CommonAPI.Utils.make_note_data(
120 actor.ap_id,
121 to,
122 context,
123 content_html,
124 attachments,
125 inReplyToActivity,
126 [],
127 cw
128 ),
129 note <- note |> Map.put("id", id) |> Map.put("tag", tags),
130 note <- note |> Map.put("published", date),
131 note <- note |> Map.put("emoji", get_emoji(entry)),
132 note <- add_external_url(note, entry),
133 note <- note |> Map.put("cc", cc),
134 # TODO: Handle this case in make_note_data
135 note <-
136 if(
137 inReplyTo && !inReplyToActivity,
138 do: note |> Map.put("inReplyTo", inReplyTo),
139 else: note
140 ) do
141 ActivityPub.create(%{
142 to: to,
143 actor: actor,
144 context: context,
145 object: note,
146 published: date,
147 local: false,
148 additional: %{"cc" => cc}
149 })
150 else
151 %Activity{} = activity -> {:ok, activity}
152 e -> {:error, e}
153 end
154 end
155 end