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