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
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.Federator
14 alias Pleroma.Web.OStatus
18 Get the context for this note. Uses this:
19 1. The context of the parent activity
20 2. The conversation reference in the ostatus xml
21 3. A newly generated context id.
23 def get_context(entry, in_reply_to) do
25 (XML.string_from_xpath("//ostatus:conversation[1]", entry) ||
26 XML.string_from_xpath("//ostatus:conversation[1]/@ref", entry) || "")
29 with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(in_reply_to) do
33 if String.length(context) > 0 do
36 Utils.generate_context_id()
41 def get_people_mentions(entry) do
43 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]',
46 |> Enum.map(fn person -> XML.string_from_xpath("@href", person) end)
49 def get_collection_mentions(entry) do
51 "http://activityschema.org/collection/public" ->
52 "https://www.w3.org/ns/activitystreams#Public"
59 '//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/collection"]',
62 |> Enum.map(fn collection -> XML.string_from_xpath("@href", collection) |> transmogrify.() end)
65 def get_mentions(entry) do
66 (get_people_mentions(entry) ++ get_collection_mentions(entry))
70 def get_emoji(entry) do
72 :xmerl_xpath.string('//link[@rel="emoji"]', entry)
73 |> Enum.reduce(%{}, fn emoji, acc ->
74 Map.put(acc, XML.string_from_xpath("@name", emoji), XML.string_from_xpath("@href", emoji))
81 def make_to_list(actor, mentions) do
83 actor.follower_address
87 def add_external_url(note, entry) do
88 url = XML.string_from_xpath("//link[@rel='alternate' and @type='text/html']/@href", entry)
89 Map.put(note, "external_url", url)
92 def fetch_replied_to_activity(entry, in_reply_to, options \\ []) do
93 with %Activity{} = activity <- Activity.get_create_by_object_ap_id(in_reply_to) do
97 with true <- Federator.allowed_incoming_reply_depth?(options[:depth]),
98 in_reply_to_href when not is_nil(in_reply_to_href) <-
99 XML.string_from_xpath("//thr:in-reply-to[1]/@href", entry),
100 {:ok, [activity | _]} <- OStatus.fetch_activity_from_url(in_reply_to_href, options) do
108 # TODO: Clean this up a bit.
109 def handle_note(entry, doc \\ nil, options \\ []) do
110 with id <- XML.string_from_xpath("//id", entry),
111 activity when is_nil(activity) <- Activity.get_create_by_object_ap_id_with_object(id),
112 [author] <- :xmerl_xpath.string('//author[1]', doc),
113 {:ok, actor} <- OStatus.find_make_or_update_user(author),
114 content_html <- OStatus.get_content(entry),
115 cw <- OStatus.get_cw(entry),
116 in_reply_to <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry),
117 options <- Keyword.put(options, :depth, (options[:depth] || 0) + 1),
118 in_reply_to_activity <- fetch_replied_to_activity(entry, in_reply_to, options),
119 in_reply_to_object <-
120 (in_reply_to_activity && Object.normalize(in_reply_to_activity)) || nil,
121 in_reply_to <- (in_reply_to_object && in_reply_to_object.data["id"]) || in_reply_to,
122 attachments <- OStatus.get_attachments(entry),
123 context <- get_context(entry, in_reply_to),
124 tags <- OStatus.get_tags(entry),
125 mentions <- get_mentions(entry),
126 to <- make_to_list(actor, mentions),
127 date <- XML.string_from_xpath("//published", entry),
128 unlisted <- XML.string_from_xpath("//mastodon:scope", entry) == "unlisted",
129 cc <- if(unlisted, do: ["https://www.w3.org/ns/activitystreams#Public"], else: []),
131 CommonAPI.Utils.make_note_data(
137 in_reply_to_activity,
141 note <- note |> Map.put("id", id) |> Map.put("tag", tags),
142 note <- note |> Map.put("published", date),
143 note <- note |> Map.put("emoji", get_emoji(entry)),
144 note <- add_external_url(note, entry),
145 note <- note |> Map.put("cc", cc),
146 # TODO: Handle this case in make_note_data
149 in_reply_to && !in_reply_to_activity,
150 do: note |> Map.put("inReplyTo", in_reply_to),
153 ActivityPub.create(%{
160 additional: %{"cc" => cc}
163 %Activity{} = activity -> {:ok, activity}