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