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, Utils}
10 alias Pleroma.Web.CommonAPI
13 Get the context for this note. Uses this:
14 1. The context of the parent activity
15 2. The conversation reference in the ostatus xml
16 3. A newly generated context id.
18 def get_context(entry, inReplyTo) do
20 (XML.string_from_xpath("//ostatus:conversation[1]", entry) ||
21 XML.string_from_xpath("//ostatus:conversation[1]/@ref", entry) || "")
24 with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
28 if String.length(context) > 0 do
31 Utils.generate_context_id()
36 def get_people_mentions(entry) do
38 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]',
41 |> Enum.map(fn person -> XML.string_from_xpath("@href", person) end)
44 def get_collection_mentions(entry) do
46 "http://activityschema.org/collection/public" ->
47 "https://www.w3.org/ns/activitystreams#Public"
54 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/collection"]',
57 |> Enum.map(fn collection -> XML.string_from_xpath("@href", collection) |> transmogrify.() end)
60 def get_mentions(entry) do
61 (get_people_mentions(entry) ++ get_collection_mentions(entry))
65 def get_emoji(entry) do
67 :xmerl_xpath.string('//link[@rel="emoji"]', entry)
68 |> Enum.reduce(%{}, fn emoji, acc ->
69 Map.put(acc, XML.string_from_xpath("@name", emoji), XML.string_from_xpath("@href", emoji))
76 def make_to_list(actor, mentions) do
78 actor.follower_address
82 def add_external_url(note, entry) do
83 url = XML.string_from_xpath("//link[@rel='alternate' and @type='text/html']/@href", entry)
84 Map.put(note, "external_url", url)
87 def fetch_replied_to_activity(entry, inReplyTo) do
88 with %Activity{} = activity <- Activity.get_create_by_object_ap_id(inReplyTo) do
92 with inReplyToHref when not is_nil(inReplyToHref) <-
93 XML.string_from_xpath("//thr:in-reply-to[1]/@href", entry),
94 {:ok, [activity | _]} <- OStatus.fetch_activity_from_url(inReplyToHref) do
102 # TODO: Clean this up a bit.
103 def handle_note(entry, doc \\ nil) do
104 with id <- XML.string_from_xpath("//id", entry),
105 activity when is_nil(activity) <- Activity.get_create_by_object_ap_id(id),
106 [author] <- :xmerl_xpath.string('//author[1]', doc),
107 {:ok, actor} <- OStatus.find_make_or_update_user(author),
108 content_html <- OStatus.get_content(entry),
109 cw <- OStatus.get_cw(entry),
110 inReplyTo <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry),
111 inReplyToActivity <- fetch_replied_to_activity(entry, inReplyTo),
112 inReplyTo <- (inReplyToActivity && inReplyToActivity.data["object"]["id"]) || inReplyTo,
113 attachments <- OStatus.get_attachments(entry),
114 context <- get_context(entry, inReplyTo),
115 tags <- OStatus.get_tags(entry),
116 mentions <- get_mentions(entry),
117 to <- make_to_list(actor, mentions),
118 date <- XML.string_from_xpath("//published", entry),
119 unlisted <- XML.string_from_xpath("//mastodon:scope", entry) == "unlisted",
120 cc <- if(unlisted, do: ["https://www.w3.org/ns/activitystreams#Public"], else: []),
122 CommonAPI.Utils.make_note_data(
132 note <- note |> Map.put("id", id) |> Map.put("tag", tags),
133 note <- note |> Map.put("published", date),
134 note <- note |> Map.put("emoji", get_emoji(entry)),
135 note <- add_external_url(note, entry),
136 note <- note |> Map.put("cc", cc),
137 # TODO: Handle this case in make_note_data
140 inReplyTo && !inReplyToActivity,
141 do: note |> Map.put("inReplyTo", inReplyTo),
144 ActivityPub.create(%{
151 additional: %{"cc" => cc}
154 %Activity{} = activity -> {:ok, activity}