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