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