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