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