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.{XML, OStatus}
8 alias Pleroma.{Object, Activity}
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.ActivityPub.Utils
11 alias Pleroma.Web.CommonAPI
14 Get the context for this note. Uses this:
15 1. The context of the parent activity
16 2. The conversation reference in the ostatus xml
17 3. A newly generated context id.
19 def get_context(entry, inReplyTo) do
21 (XML.string_from_xpath("//ostatus:conversation[1]", entry) ||
22 XML.string_from_xpath("//ostatus:conversation[1]/@ref", entry) || "")
25 with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
29 if String.length(context) > 0 do
32 Utils.generate_context_id()
37 def get_people_mentions(entry) do
39 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]',
42 |> Enum.map(fn person -> XML.string_from_xpath("@href", person) end)
45 def get_collection_mentions(entry) do
47 "http://activityschema.org/collection/public" ->
48 "https://www.w3.org/ns/activitystreams#Public"
55 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/collection"]',
58 |> Enum.map(fn collection -> XML.string_from_xpath("@href", collection) |> transmogrify.() end)
61 def get_mentions(entry) do
62 (get_people_mentions(entry) ++ get_collection_mentions(entry))
66 def get_emoji(entry) do
68 :xmerl_xpath.string('//link[@rel="emoji"]', entry)
69 |> Enum.reduce(%{}, fn emoji, acc ->
70 Map.put(acc, XML.string_from_xpath("@name", emoji), XML.string_from_xpath("@href", emoji))
77 def make_to_list(actor, mentions) do
79 actor.follower_address
83 def add_external_url(note, entry) do
84 url = XML.string_from_xpath("//link[@rel='alternate' and @type='text/html']/@href", entry)
85 Map.put(note, "external_url", url)
88 def fetch_replied_to_activity(entry, inReplyTo) do
89 with %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(inReplyTo) do
93 with inReplyToHref when not is_nil(inReplyToHref) <-
94 XML.string_from_xpath("//thr:in-reply-to[1]/@href", entry),
95 {:ok, [activity | _]} <- OStatus.fetch_activity_from_url(inReplyToHref) do
103 # TODO: Clean this up a bit.
104 def handle_note(entry, doc \\ nil) do
105 with id <- XML.string_from_xpath("//id", entry),
106 activity when is_nil(activity) <- Activity.get_create_activity_by_object_ap_id(id),
107 [author] <- :xmerl_xpath.string('//author[1]', doc),
108 {:ok, actor} <- OStatus.find_make_or_update_user(author),
109 content_html <- OStatus.get_content(entry),
110 cw <- OStatus.get_cw(entry),
111 inReplyTo <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry),
112 inReplyToActivity <- fetch_replied_to_activity(entry, inReplyTo),
113 inReplyTo <- (inReplyToActivity && inReplyToActivity.data["object"]["id"]) || inReplyTo,
114 attachments <- OStatus.get_attachments(entry),
115 context <- get_context(entry, inReplyTo),
116 tags <- OStatus.get_tags(entry),
117 mentions <- get_mentions(entry),
118 to <- make_to_list(actor, mentions),
119 date <- XML.string_from_xpath("//published", entry),
120 unlisted <- XML.string_from_xpath("//mastodon:scope", entry) == "unlisted",
121 cc <- if(unlisted, do: ["https://www.w3.org/ns/activitystreams#Public"], else: []),
123 CommonAPI.Utils.make_note_data(
133 note <- note |> Map.put("id", id) |> Map.put("tag", tags),
134 note <- note |> Map.put("published", date),
135 note <- note |> Map.put("emoji", get_emoji(entry)),
136 note <- add_external_url(note, entry),
137 note <- note |> Map.put("cc", cc),
138 # TODO: Handle this case in make_note_data
141 inReplyTo && !inReplyToActivity,
142 do: note |> Map.put("inReplyTo", inReplyTo),
145 ActivityPub.create(%{
152 additional: %{"cc" => cc}
155 %Activity{} = activity -> {:ok, activity}