1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OStatus.NoteHandler do
7 alias Pleroma.Web.OStatus
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.CommonAPI
16 Get the context for this note. Uses this:
17 1. The context of the parent activity
18 2. The conversation reference in the ostatus xml
19 3. A newly generated context id.
21 def get_context(entry, inReplyTo) do
23 (XML.string_from_xpath("//ostatus:conversation[1]", entry) ||
24 XML.string_from_xpath("//ostatus:conversation[1]/@ref", entry) || "")
27 with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
31 if String.length(context) > 0 do
34 Utils.generate_context_id()
39 def get_people_mentions(entry) do
41 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]',
44 |> Enum.map(fn person -> XML.string_from_xpath("@href", person) end)
47 def get_collection_mentions(entry) do
49 "http://activityschema.org/collection/public" ->
50 "https://www.w3.org/ns/activitystreams#Public"
57 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/collection"]',
60 |> Enum.map(fn collection -> XML.string_from_xpath("@href", collection) |> transmogrify.() end)
63 def get_mentions(entry) do
64 (get_people_mentions(entry) ++ get_collection_mentions(entry))
68 def get_emoji(entry) do
70 :xmerl_xpath.string('//link[@rel="emoji"]', entry)
71 |> Enum.reduce(%{}, fn emoji, acc ->
72 Map.put(acc, XML.string_from_xpath("@name", emoji), XML.string_from_xpath("@href", emoji))
79 def make_to_list(actor, mentions) do
81 actor.follower_address
85 def add_external_url(note, entry) do
86 url = XML.string_from_xpath("//link[@rel='alternate' and @type='text/html']/@href", entry)
87 Map.put(note, "external_url", url)
90 def fetch_replied_to_activity(entry, inReplyTo) do
91 with %Activity{} = activity <- Activity.get_create_by_object_ap_id(inReplyTo) do
95 with inReplyToHref when not is_nil(inReplyToHref) <-
96 XML.string_from_xpath("//thr:in-reply-to[1]/@href", entry),
97 {:ok, [activity | _]} <- OStatus.fetch_activity_from_url(inReplyToHref) do
105 # TODO: Clean this up a bit.
106 def handle_note(entry, doc \\ nil) do
107 with id <- XML.string_from_xpath("//id", entry),
108 activity when is_nil(activity) <- Activity.get_create_by_object_ap_id(id),
109 [author] <- :xmerl_xpath.string('//author[1]', doc),
110 {:ok, actor} <- OStatus.find_make_or_update_user(author),
111 content_html <- OStatus.get_content(entry),
112 cw <- OStatus.get_cw(entry),
113 inReplyTo <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry),
114 inReplyToActivity <- fetch_replied_to_activity(entry, inReplyTo),
115 inReplyTo <- (inReplyToActivity && inReplyToActivity.data["object"]["id"]) || inReplyTo,
116 attachments <- OStatus.get_attachments(entry),
117 context <- get_context(entry, inReplyTo),
118 tags <- OStatus.get_tags(entry),
119 mentions <- get_mentions(entry),
120 to <- make_to_list(actor, mentions),
121 date <- XML.string_from_xpath("//published", entry),
122 unlisted <- XML.string_from_xpath("//mastodon:scope", entry) == "unlisted",
123 cc <- if(unlisted, do: ["https://www.w3.org/ns/activitystreams#Public"], else: []),
125 CommonAPI.Utils.make_note_data(
135 note <- note |> Map.put("id", id) |> Map.put("tag", tags),
136 note <- note |> Map.put("published", date),
137 note <- note |> Map.put("emoji", get_emoji(entry)),
138 note <- add_external_url(note, entry),
139 note <- note |> Map.put("cc", cc),
140 # TODO: Handle this case in make_note_data
143 inReplyTo && !inReplyToActivity,
144 do: note |> Map.put("inReplyTo", inReplyTo),
147 ActivityPub.create(%{
154 additional: %{"cc" => cc}
157 %Activity{} = activity -> {:ok, activity}