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