Add avatar updating from incoming messages.
[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, Object}
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 entries = :xmerl_xpath.string('//entry', doc)
25
26 activities = Enum.map(entries, fn (entry) ->
27 {:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
28
29 case object_type do
30 'http://activitystrea.ms/schema/1.0/note' ->
31 with {:ok, activity} <- handle_note(entry, doc), do: activity
32 'http://activitystrea.ms/schema/1.0/comment' ->
33 with {:ok, activity} <- handle_note(entry, doc), do: activity
34 _ ->
35 Logger.error("Couldn't parse incoming document")
36 nil
37 end
38 end)
39 {:ok, activities}
40 end
41
42 def handle_note(entry, doc \\ nil) do
43 content_html = string_from_xpath("/entry/content[1]", entry)
44
45 uri = string_from_xpath("/entry/author/uri[1]", entry) || string_from_xpath("/feed/author/uri[1]", doc)
46 {:ok, actor} = find_or_make_user(uri)
47
48 context = (string_from_xpath("/entry/ostatus:conversation[1]", entry) || "") |> String.trim
49 context = if String.length(context) > 0 do
50 context
51 else
52 ActivityPub.generate_context_id
53 end
54
55 to = [
56 "https://www.w3.org/ns/activitystreams#Public"
57 ]
58
59 mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
60 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
61
62 to = to ++ mentions
63
64 date = string_from_xpath("/entry/published", entry)
65 id = string_from_xpath("/entry/id", entry)
66
67 object = %{
68 "id" => id,
69 "type" => "Note",
70 "to" => to,
71 "content" => content_html,
72 "published" => date,
73 "context" => context,
74 "actor" => actor.ap_id
75 }
76
77 inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@ref", entry)
78
79 object = if inReplyTo do
80 Map.put(object, "inReplyTo", inReplyTo)
81 else
82 object
83 end
84
85 # TODO: Bail out sooner and use transaction.
86 if Object.get_by_ap_id(id) do
87 {:error, "duplicate activity"}
88 else
89 ActivityPub.create(to, actor, context, object, %{}, date, false)
90 end
91 end
92
93 def find_make_or_update_user(doc) do
94 uri = string_from_xpath("//author/uri[1]", doc)
95 with {:ok, user} <- find_or_make_user(uri) do
96 avatar = make_avatar_object(doc)
97 if user.avatar != avatar do
98 change = Ecto.Changeset.change(user, %{avatar: avatar})
99 Repo.update(change)
100 else
101 {:ok, user}
102 end
103 end
104 end
105
106 def find_or_make_user(uri) do
107 query = from user in User,
108 where: user.local == false and fragment("? @> ?", user.info, ^%{uri: uri})
109
110 user = Repo.one(query)
111
112 if is_nil(user) do
113 make_user(uri)
114 else
115 {:ok, user}
116 end
117 end
118
119 def make_user(uri) do
120 with {:ok, info} <- gather_user_info(uri) do
121 data = %{
122 local: false,
123 name: info.name,
124 nickname: info.nickname <> "@" <> info.host,
125 ap_id: info.uri,
126 info: info,
127 avatar: info.avatar
128 }
129 # TODO: Make remote user changeset
130 # SHould enforce fqn nickname
131 Repo.insert(Ecto.Changeset.change(%User{}, data))
132 end
133 end
134
135 # TODO: Just takes the first one for now.
136 def make_avatar_object(author_doc) do
137 href = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@href", author_doc)
138 type = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@type", author_doc)
139
140 if href do
141 %{
142 "type" => "Image",
143 "url" =>
144 [%{
145 "type" => "Link",
146 "mediaType" => type,
147 "href" => href
148 }]
149 }
150 else
151 nil
152 end
153 end
154
155 def gather_user_info(username) do
156 with {:ok, webfinger_data} <- WebFinger.finger(username),
157 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data.topic) do
158 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put(:fqn, username)}
159 else e ->
160 Logger.debug("Couldn't gather info for #{username}")
161 {:error, e}
162 end
163 end
164 end