f346cc9afa94871b5a73f44734d8508f54b8de0f
[akkoma] / lib / pleroma / web / ostatus / ostatus_controller.ex
1 defmodule Pleroma.Web.OStatus.OStatusController do
2 use Pleroma.Web, :controller
3
4 alias Pleroma.{User, Activity}
5 alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter}
6 alias Pleroma.Repo
7 alias Pleroma.Web.{OStatus, Federator}
8 alias Pleroma.Web.XML
9 alias Pleroma.Web.ActivityPub.ActivityPubController
10 alias Pleroma.Web.ActivityPub.ActivityPub
11
12 action_fallback(:errors)
13
14 def feed_redirect(conn, %{"nickname" => nickname}) do
15 with {_, %User{} = user} <- {:user, User.get_cached_by_nickname(nickname)} do
16 case get_format(conn) do
17 "html" -> Fallback.RedirectController.redirector(conn, nil)
18 "activity+json" -> ActivityPubController.call(conn, :user)
19 _ -> redirect(conn, external: OStatus.feed_path(user))
20 end
21 else
22 {:user, nil} -> {:error, :not_found}
23 end
24 end
25
26 def feed(conn, %{"nickname" => nickname} = params) do
27 with {_, %User{} = user} <- {:user, User.get_cached_by_nickname(nickname)} do
28 query_params =
29 Map.take(params, ["max_id"])
30 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
31
32 activities =
33 ActivityPub.fetch_public_activities(query_params)
34 |> Enum.reverse()
35
36 response =
37 user
38 |> FeedRepresenter.to_simple_form(activities, [user])
39 |> :xmerl.export_simple(:xmerl_xml)
40 |> to_string
41
42 conn
43 |> put_resp_content_type("application/atom+xml")
44 |> send_resp(200, response)
45 else
46 {:user, nil} -> {:error, :not_found}
47 end
48 end
49
50 defp decode_or_retry(body) do
51 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
52 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
53 {:ok, doc}
54 else
55 _e ->
56 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
57 doc <- XML.parse_document(decoded),
58 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
59 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
60 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
61 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
62 {:ok, doc}
63 end
64 end
65 end
66
67 def salmon_incoming(conn, _) do
68 {:ok, body, _conn} = read_body(conn)
69 {:ok, doc} = decode_or_retry(body)
70
71 Federator.enqueue(:incoming_doc, doc)
72
73 conn
74 |> send_resp(200, "")
75 end
76
77 def object(conn, %{"uuid" => uuid}) do
78 if get_format(conn) == "activity+json" do
79 ActivityPubController.call(conn, :object)
80 else
81 with id <- o_status_url(conn, :object, uuid),
82 {_, %Activity{} = activity} <-
83 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
84 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
85 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
86 case get_format(conn) do
87 "html" -> redirect(conn, to: "/notice/#{activity.id}")
88 _ -> represent_activity(conn, activity, user)
89 end
90 else
91 {:public?, false} ->
92 {:error, :not_found}
93
94 {:activity, nil} ->
95 {:error, :not_found}
96
97 e ->
98 e
99 end
100 end
101 end
102
103 def activity(conn, %{"uuid" => uuid}) do
104 with id <- o_status_url(conn, :activity, uuid),
105 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(id)},
106 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
107 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
108 case get_format(conn) do
109 "html" -> redirect(conn, to: "/notice/#{activity.id}")
110 _ -> represent_activity(conn, activity, user)
111 end
112 else
113 {:public?, false} ->
114 {:error, :not_found}
115
116 {:activity, nil} ->
117 {:error, :not_found}
118
119 e ->
120 e
121 end
122 end
123
124 def notice(conn, %{"id" => id}) do
125 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
126 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
127 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
128 case get_format(conn) do
129 "html" ->
130 conn
131 |> put_resp_content_type("text/html")
132 |> send_file(200, "priv/static/index.html")
133
134 _ ->
135 represent_activity(conn, activity, user)
136 end
137 else
138 {:public?, false} ->
139 {:error, :not_found}
140
141 {:activity, nil} ->
142 {:error, :not_found}
143
144 e ->
145 e
146 end
147 end
148
149 defp represent_activity(conn, activity, user) do
150 response =
151 activity
152 |> ActivityRepresenter.to_simple_form(user, true)
153 |> ActivityRepresenter.wrap_with_entry()
154 |> :xmerl.export_simple(:xmerl_xml)
155 |> to_string
156
157 conn
158 |> put_resp_content_type("application/atom+xml")
159 |> send_resp(200, response)
160 end
161
162 def errors(conn, {:error, :not_found}) do
163 conn
164 |> put_status(404)
165 |> text("Not found")
166 end
167
168 def errors(conn, _) do
169 conn
170 |> put_status(500)
171 |> text("Something went wrong")
172 end
173 end