Merge branch 'develop' into feature/activitypub
[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.CommonAPI
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 get_emoji(entry) do
56 try do
57 :xmerl_xpath.string('//link[@rel="emoji"]', entry)
58 |> Enum.reduce(%{}, fn(emoji, acc) ->
59 Map.put(acc, XML.string_from_xpath("@name", emoji), XML.string_from_xpath("@href", emoji))
60 end)
61 rescue
62 _e -> nil
63 end
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 fetch_replied_to_activity(entry, inReplyTo) do
78 with %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(inReplyTo) do
79 activity
80 else
81 _e ->
82 with inReplyToHref when not is_nil(inReplyToHref) <- XML.string_from_xpath("//thr:in-reply-to[1]/@href", entry),
83 {:ok, [activity | _]} <- OStatus.fetch_activity_from_url(inReplyToHref) do
84 activity
85 else
86 _e -> nil
87 end
88 end
89 end
90
91 def handle_note(entry, doc \\ nil) do
92 with id <- XML.string_from_xpath("//id", entry),
93 activity when is_nil(activity) <- Activity.get_create_activity_by_object_ap_id(id),
94 [author] <- :xmerl_xpath.string('//author[1]', doc),
95 {:ok, actor} <- OStatus.find_make_or_update_user(author),
96 content_html <- OStatus.get_content(entry),
97 cw <- OStatus.get_cw(entry),
98 inReplyTo <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry),
99 inReplyToActivity <- fetch_replied_to_activity(entry, inReplyTo),
100 inReplyTo <- (inReplyToActivity && inReplyToActivity.data["object"]["id"]) || inReplyTo,
101 attachments <- OStatus.get_attachments(entry),
102 context <- get_context(entry, inReplyTo),
103 tags <- OStatus.get_tags(entry),
104 mentions <- get_mentions(entry),
105 to <- make_to_list(actor, mentions),
106 date <- XML.string_from_xpath("//published", entry),
107 note <- CommonAPI.Utils.make_note_data(actor.ap_id, to, context, content_html, attachments, inReplyToActivity, [], cw),
108 note <- note |> Map.put("id", id) |> Map.put("tag", tags),
109 note <- note |> Map.put("published", date),
110 note <- note |> Map.put("emoji", get_emoji(entry)),
111 note <- add_external_url(note, entry),
112 # TODO: Handle this case in make_note_data
113 note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note)
114 do
115 res = ActivityPub.create(to, actor, context, note, %{}, date, false)
116 User.increase_note_count(actor)
117 res
118 else
119 %Activity{} = activity -> {:ok, activity}
120 e -> {:error, e}
121 end
122 end
123 end