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