Workaround for masto reply breakage.
[akkoma] / lib / pleroma / web / ostatus / handlers / note_handler.ex
1 defmodule Pleroma.Web.OStatus.NoteHandler do
2 require Logger
3 alias Pleroma.Web.{XML, OStatus}
4 alias Pleroma.{Object, User, Activity}
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Web.ActivityPub.Utils
7 alias Pleroma.Web.TwitterAPI
8
9 @doc """
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.
14 """
15 def get_context(entry, inReplyTo) do
16 context = (
17 XML.string_from_xpath("//ostatus:conversation[1]", entry)
18 || XML.string_from_xpath("//ostatus:conversation[1]/@ref", entry)
19 || "") |> String.trim
20
21 with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
22 context
23 else _e ->
24 if String.length(context) > 0 do
25 context
26 else
27 Utils.generate_context_id
28 end
29 end
30 end
31
32 def get_people_mentions(entry) do
33 :xmerl_xpath.string('//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
34 |> Enum.map(fn(person) -> XML.string_from_xpath("@href", person) end)
35 end
36
37 def get_collection_mentions(entry) do
38 transmogrify = fn
39 ("http://activityschema.org/collection/public") ->
40 "https://www.w3.org/ns/activitystreams#Public"
41 (group) ->
42 group
43 end
44
45 :xmerl_xpath.string('//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/collection"]', entry)
46 |> Enum.map(fn(collection) -> XML.string_from_xpath("@href", collection) |> transmogrify.() end)
47 end
48
49 def get_mentions(entry) do
50 (get_people_mentions(entry)
51 ++ get_collection_mentions(entry))
52 |> Enum.filter(&(&1))
53 end
54
55 def make_to_list(actor, mentions) do
56 [
57 actor.follower_address
58 ] ++ mentions
59 end
60
61 def add_external_url(note, entry) do
62 url = XML.string_from_xpath("//link[@rel='alternate' and @type='text/html']/@href", entry)
63 Map.put(note, "external_url", url)
64 end
65
66 def fetch_replied_to_activity(entry, inReplyTo) do
67 with %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(inReplyTo) do
68 activity
69 else
70 _e ->
71 with inReplyToHref when not is_nil(inReplyToHref) <- XML.string_from_xpath("//thr:in-reply-to[1]/@href", entry),
72 {:ok, [activity | _]} <- OStatus.fetch_activity_from_url(inReplyToHref) do
73 activity
74 else
75 _e -> nil
76 end
77 end
78 end
79
80 def handle_note(entry, doc \\ nil) do
81 with id <- XML.string_from_xpath("//id", entry),
82 activity when is_nil(activity) <- Activity.get_create_activity_by_object_ap_id(id),
83 [author] <- :xmerl_xpath.string('//author[1]', doc),
84 {:ok, actor} <- OStatus.find_make_or_update_user(author),
85 content_html <- OStatus.get_content(entry),
86 inReplyTo <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry),
87 inReplyToActivity <- fetch_replied_to_activity(entry, inReplyTo),
88 inReplyTo <- (inReplyToActivity && inReplyToActivity.data["object"]["id"]) || inReplyTo,
89 attachments <- OStatus.get_attachments(entry),
90 context <- get_context(entry, inReplyTo),
91 tags <- OStatus.get_tags(entry),
92 mentions <- get_mentions(entry),
93 to <- make_to_list(actor, mentions),
94 date <- XML.string_from_xpath("//published", entry),
95 note <- TwitterAPI.Utils.make_note_data(actor.ap_id, to, context, content_html, attachments, inReplyToActivity, []),
96 note <- note |> Map.put("id", id) |> Map.put("tag", tags),
97 note <- note |> Map.put("published", date),
98 note <- add_external_url(note, entry),
99 # TODO: Handle this case in make_note_data
100 note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note)
101 do
102 res = ActivityPub.create(to, actor, context, note, %{}, date, false)
103 User.update_note_count(actor)
104 res
105 else
106 %Activity{} = activity -> {:ok, activity}
107 e -> {:error, e}
108 end
109 end
110 end