Merge branch 'feature/incoming_ostatus' of ssh.gitgud.io:lambadalambda/pleroma into...
[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 # wire up replies
35 def handle_note(doc) do
36 content_html = string_from_xpath("/entry/content[1]", doc)
37
38 [author] = :xmerl_xpath.string('/entry/author[1]', doc)
39 {:ok, actor} = find_or_make_user(author)
40
41 context = string_from_xpath("/entry/ostatus:conversation[1]", doc) |> String.trim
42 context = if String.length(context) > 0 do
43 context
44 else
45 ActivityPub.generate_context_id
46 end
47
48 to = [
49 "https://www.w3.org/ns/activitystreams#Public"
50 ]
51
52 mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', doc)
53 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
54
55 to = to ++ mentions
56
57 date = string_from_xpath("/entry/published", doc)
58
59 object = %{
60 "type" => "Note",
61 "to" => to,
62 "content" => content_html,
63 "published" => date,
64 "context" => context,
65 "actor" => actor.ap_id
66 }
67
68 inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@href", doc)
69
70 object = if inReplyTo do
71 Map.put(object, "inReplyTo", inReplyTo)
72 else
73 object
74 end
75
76 ActivityPub.create(to, actor, context, object, %{}, date)
77 end
78
79 def find_or_make_user(author_doc) do
80 {:xmlObj, :string, uri } = :xmerl_xpath.string('string(/author[1]/uri)', author_doc)
81
82 query = from user in User,
83 where: user.local == false and fragment("? @> ?", user.info, ^%{ostatus_uri: to_string(uri)})
84
85 user = Repo.one(query)
86
87 if is_nil(user) do
88 make_user(author_doc)
89 else
90 {:ok, user}
91 end
92 end
93
94 defp string_from_xpath(xpath, doc) do
95 {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
96
97 res = res
98 |> to_string
99 |> String.trim
100
101 if res == "", do: nil, else: res
102 end
103
104 def make_user(author_doc) do
105 author = string_from_xpath("/author[1]/uri", author_doc)
106 name = string_from_xpath("/author[1]/name", author_doc)
107 preferredUsername = string_from_xpath("/author[1]/poco:preferredUsername", author_doc)
108 displayName = string_from_xpath("/author[1]/poco:displayName", author_doc)
109 avatar = make_avatar_object(author_doc)
110
111 data = %{
112 local: false,
113 name: preferredUsername || name,
114 nickname: displayName || name,
115 ap_id: author,
116 info: %{
117 "ostatus_uri" => author,
118 "host" => URI.parse(author).host,
119 "system" => "ostatus"
120 },
121 avatar: avatar
122 }
123
124 Repo.insert(Ecto.Changeset.change(%User{}, data))
125 end
126
127 # TODO: Just takes the first one for now.
128 defp make_avatar_object(author_doc) do
129 href = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@href", author_doc)
130 type = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@type", author_doc)
131
132 if href do
133 %{
134 "type" => "Image",
135 "url" =>
136 [%{
137 "type" => "Link",
138 "mediaType" => type,
139 "href" => href
140 }]
141 }
142 else
143 nil
144 end
145 end
146 end