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