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