Refactor XML parsing.
[akkoma] / lib / pleroma / web / ostatus / ostatus.ex
1 defmodule Pleroma.Web.OStatus do
2 import Ecto.Query
3 import Pleroma.Web.XML
4 require Logger
5
6 alias Pleroma.{Repo, User, Web}
7 alias Pleroma.Web.ActivityPub.ActivityPub
8
9 def feed_path(user) do
10 "#{user.ap_id}/feed.atom"
11 end
12
13 def pubsub_path(user) do
14 "#{Web.base_url}/push/hub/#{user.nickname}"
15 end
16
17 def salmon_path(user) do
18 "#{user.ap_id}/salmon"
19 end
20
21 def handle_incoming(xml_string) do
22 doc = parse_document(xml_string)
23
24 {:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', doc)
25
26 case object_type do
27 'http://activitystrea.ms/schema/1.0/note' ->
28 handle_note(doc)
29 _ ->
30 Logger.error("Couldn't parse incoming document")
31 end
32 end
33
34 # TODO
35 # wire up replies
36 def handle_note(doc) do
37 content_html = string_from_xpath("/entry/content[1]", doc)
38
39 [author] = :xmerl_xpath.string('/entry/author[1]', doc)
40 {:ok, actor} = find_or_make_user(author)
41
42 context = string_from_xpath("/entry/ostatus:conversation[1]", doc) |> String.trim
43 context = if String.length(context) > 0 do
44 context
45 else
46 ActivityPub.generate_context_id
47 end
48
49 to = [
50 "https://www.w3.org/ns/activitystreams#Public"
51 ]
52
53 mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', doc)
54 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
55
56 to = to ++ mentions
57
58 date = string_from_xpath("/entry/published", doc)
59
60 object = %{
61 "type" => "Note",
62 "to" => to,
63 "content" => content_html,
64 "published" => date,
65 "context" => context,
66 "actor" => actor.ap_id
67 }
68
69 inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@href", doc)
70
71 object = if inReplyTo do
72 Map.put(object, "inReplyTo", inReplyTo)
73 else
74 object
75 end
76
77 ActivityPub.create(to, actor, context, object, %{}, date)
78 end
79
80 def find_or_make_user(author_doc) do
81 {:xmlObj, :string, uri } = :xmerl_xpath.string('string(/author[1]/uri)', author_doc)
82
83 query = from user in User,
84 where: user.local == false and fragment("? @> ?", user.info, ^%{ostatus_uri: to_string(uri)})
85
86 user = Repo.one(query)
87
88 if is_nil(user) do
89 make_user(author_doc)
90 else
91 {:ok, user}
92 end
93 end
94
95 def make_user(author_doc) do
96 author = string_from_xpath("/author[1]/uri", author_doc)
97 name = string_from_xpath("/author[1]/name", author_doc)
98 preferredUsername = string_from_xpath("/author[1]/poco:preferredUsername", author_doc)
99 displayName = string_from_xpath("/author[1]/poco:displayName", author_doc)
100 avatar = make_avatar_object(author_doc)
101
102 data = %{
103 local: false,
104 name: preferredUsername || name,
105 nickname: displayName || name,
106 ap_id: author,
107 info: %{
108 "ostatus_uri" => author,
109 "host" => URI.parse(author).host,
110 "system" => "ostatus"
111 },
112 avatar: avatar
113 }
114
115 Repo.insert(Ecto.Changeset.change(%User{}, data))
116 end
117
118 # TODO: Just takes the first one for now.
119 defp make_avatar_object(author_doc) do
120 href = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@href", author_doc)
121 type = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@type", author_doc)
122
123 if href do
124 %{
125 "type" => "Image",
126 "url" =>
127 [%{
128 "type" => "Link",
129 "mediaType" => type,
130 "href" => href
131 }]
132 }
133 else
134 nil
135 end
136 end
137 end