Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into develop
[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_html_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 = (XML.string_from_xpath("//ostatus:conversation[1]", entry) || "") |> String.trim
28
29 with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
30 context
31 else _e ->
32 if String.length(context) > 0 do
33 context
34 else
35 Utils.generate_context_id
36 end
37 end
38 end
39
40 def get_mentions(entry) do
41 :xmerl_xpath.string('//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
42 |> Enum.map(fn(person) -> XML.string_from_xpath("@href", person) end)
43 end
44
45 def make_to_list(actor, mentions) do
46 [
47 "https://www.w3.org/ns/activitystreams#Public",
48 User.ap_followers(actor)
49 ] ++ mentions
50 end
51
52 def handle_note(entry, doc \\ nil) do
53 with id <- XML.string_from_xpath("//id", entry),
54 activity when is_nil(activity) <- Activity.get_create_activity_by_object_ap_id(id),
55 [author] <- :xmerl_xpath.string('//author[1]', doc),
56 {:ok, actor} <- OStatus.find_make_or_update_user(author),
57 content_html <- OStatus.get_content(entry),
58 inReplyTo <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry),
59 _inReplyToActivity <- fetch_replied_to_activity(entry, inReplyTo),
60 inReplyToActivity <- Activity.get_create_activity_by_object_ap_id(inReplyTo),
61 attachments <- OStatus.get_attachments(entry),
62 context <- get_context(entry, inReplyTo),
63 tags <- OStatus.get_tags(entry),
64 mentions <- get_mentions(entry),
65 to <- make_to_list(actor, mentions),
66 date <- XML.string_from_xpath("//published", entry),
67 note <- TwitterAPI.Utils.make_note_data(actor.ap_id, to, context, content_html, attachments, inReplyToActivity, []),
68 note <- note |> Map.put("id", id) |> Map.put("tag", tags),
69 # TODO: Handle this case in make_note_data
70 note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note)
71 do
72 ActivityPub.create(to, actor, context, note, %{}, date, false)
73 else
74 %Activity{} = activity -> {:ok, activity}
75 e -> {:error, e}
76 end
77 end
78 end