1 defmodule Pleroma.Web.OStatus.NoteHandler do
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
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.
15 def get_context(entry, inReplyTo) do
17 (XML.string_from_xpath("//ostatus:conversation[1]", entry) ||
18 XML.string_from_xpath("//ostatus:conversation[1]/@ref", entry) || "")
21 with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
25 if String.length(context) > 0 do
28 Utils.generate_context_id()
33 def get_people_mentions(entry) do
35 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]',
38 |> Enum.map(fn person -> XML.string_from_xpath("@href", person) end)
41 def get_collection_mentions(entry) do
43 "http://activityschema.org/collection/public" ->
44 "https://www.w3.org/ns/activitystreams#Public"
51 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/collection"]',
54 |> Enum.map(fn collection -> XML.string_from_xpath("@href", collection) |> transmogrify.() end)
57 def get_mentions(entry) do
58 (get_people_mentions(entry) ++ get_collection_mentions(entry))
62 def get_emoji(entry) 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))
73 def make_to_list(actor, mentions) do
75 actor.follower_address
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)
84 def fetch_replied_to_activity(entry, inReplyTo) do
85 with %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(inReplyTo) do
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
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: []),
119 CommonAPI.Utils.make_note_data(
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
137 inReplyTo && !inReplyToActivity,
138 do: note |> Map.put("inReplyTo", inReplyTo),
141 ActivityPub.create(%{
148 additional: %{"cc" => cc}
151 %Activity{} = activity -> {:ok, activity}