Merge branch 'develop' into dtluna/pleroma-refactor/1
[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 {:xmlObj, :string, verb } = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
29
30 case verb do
31 'http://activitystrea.ms/schema/1.0/share' ->
32 with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), do: [activity, retweeted_activity]
33 _ ->
34 case object_type do
35 'http://activitystrea.ms/schema/1.0/note' ->
36 with {:ok, activity} <- handle_note(entry, doc), do: activity
37 'http://activitystrea.ms/schema/1.0/comment' ->
38 with {:ok, activity} <- handle_note(entry, doc), do: activity
39 _ ->
40 Logger.error("Couldn't parse incoming document")
41 nil
42 end
43 end
44 end)
45 {:ok, activities}
46 end
47
48 def make_share(entry, doc, retweeted_activity) do
49 with {:ok, actor} <- find_make_or_update_user(doc),
50 %Object{} = object <- Object.get_cached_by_ap_id(retweeted_activity.data["object"]["id"]),
51 {:ok, activity, object} = ActivityPub.announce(actor, object, false) do
52 {:ok, activity}
53 end
54 end
55
56 def handle_share(entry, doc) do
57 with [object] <- :xmerl_xpath.string('/entry/activity:object', entry),
58 {:ok, retweeted_activity} <- handle_note(object, object),
59 {:ok, activity} <- make_share(entry, doc, retweeted_activity) do
60 {:ok, activity, retweeted_activity}
61 else
62 e -> {:error, e}
63 end
64 end
65
66 def get_attachments(entry) do
67 :xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
68 |> Enum.map(fn (enclosure) ->
69 with href when not is_nil(href) <- string_from_xpath("/link/@href", enclosure),
70 type when not is_nil(type) <- string_from_xpath("/link/@type", enclosure) do
71 %{
72 "type" => "Attachment",
73 "url" => [%{
74 "type" => "Link",
75 "mediaType" => type,
76 "href" => href
77 }]
78 }
79 end
80 end)
81 |> Enum.filter(&(&1))
82 end
83
84 def handle_note(entry, doc \\ nil) do
85 content_html = string_from_xpath("//content[1]", entry)
86
87 [author] = :xmerl_xpath.string('//author[1]', doc)
88 {:ok, actor} = find_make_or_update_user(author)
89 inReplyTo = string_from_xpath("//thr:in-reply-to[1]/@ref", entry)
90
91 context = (string_from_xpath("//ostatus:conversation[1]", entry) || "") |> String.trim
92
93 attachments = get_attachments(entry)
94
95 context = with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
96 context
97 else _e ->
98 if String.length(context) > 0 do
99 context
100 else
101 ActivityPub.generate_context_id
102 end
103 end
104
105 to = [
106 "https://www.w3.org/ns/activitystreams#Public"
107 ]
108
109 mentions = :xmerl_xpath.string('//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
110 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
111
112 to = to ++ mentions
113
114 date = string_from_xpath("//published", entry)
115 id = string_from_xpath("//id", entry)
116
117 object = %{
118 "id" => id,
119 "type" => "Note",
120 "to" => to,
121 "content" => content_html,
122 "published" => date,
123 "context" => context,
124 "actor" => actor.ap_id,
125 "attachment" => attachments
126 }
127
128 object = if inReplyTo do
129 Map.put(object, "inReplyTo", inReplyTo)
130 else
131 object
132 end
133
134 # TODO: Bail out sooner and use transaction.
135 if Object.get_by_ap_id(id) do
136 {:error, "duplicate activity"}
137 else
138 ActivityPub.create(to, actor, context, object, %{}, date, false)
139 end
140 end
141
142 def find_make_or_update_user(doc) do
143 uri = string_from_xpath("//author/uri[1]", doc)
144 with {:ok, user} <- find_or_make_user(uri) do
145 avatar = make_avatar_object(doc)
146 if user.avatar != avatar do
147 change = Ecto.Changeset.change(user, %{avatar: avatar})
148 Repo.update(change)
149 else
150 {:ok, user}
151 end
152 end
153 end
154
155 def find_or_make_user(uri) do
156 query = from user in User,
157 where: user.local == false and fragment("? @> ?", user.info, ^%{uri: uri})
158
159 user = Repo.one(query)
160
161 if is_nil(user) do
162 make_user(uri)
163 else
164 {:ok, user}
165 end
166 end
167
168 def make_user(uri) do
169 with {:ok, info} <- gather_user_info(uri) do
170 data = %{
171 local: false,
172 name: info["name"],
173 nickname: info["nickname"] <> "@" <> info["host"],
174 ap_id: info["uri"],
175 info: info,
176 avatar: info["avatar"]
177 }
178 # TODO: Make remote user changeset
179 # SHould enforce fqn nickname
180 Repo.insert(Ecto.Changeset.change(%User{}, data))
181 end
182 end
183
184 # TODO: Just takes the first one for now.
185 def make_avatar_object(author_doc) do
186 href = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@href", author_doc)
187 type = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@type", author_doc)
188
189 if href do
190 %{
191 "type" => "Image",
192 "url" =>
193 [%{
194 "type" => "Link",
195 "mediaType" => type,
196 "href" => href
197 }]
198 }
199 else
200 nil
201 end
202 end
203
204 def gather_user_info(username) do
205 with {:ok, webfinger_data} <- WebFinger.finger(username),
206 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data["topic"]) do
207 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put("fqn", username)}
208 else e ->
209 Logger.debug("Couldn't gather info for #{username}")
210 {:error, e}
211 end
212 end
213 end