Insert meta tags into static index.html on the fly for OStatus#notice
[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, Object}
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.ObjectView
10 alias Pleroma.Web.ActivityPub.ActivityPubController
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Router.Helpers, as: Routes
13
14 plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
15 action_fallback(:errors)
16
17 def feed_redirect(conn, %{"nickname" => nickname}) do
18 case get_format(conn) do
19 "html" ->
20 Fallback.RedirectController.redirector(conn, nil)
21
22 "activity+json" ->
23 ActivityPubController.call(conn, :user)
24
25 _ ->
26 with %User{} = user <- User.get_cached_by_nickname(nickname) do
27 redirect(conn, external: OStatus.feed_path(user))
28 else
29 nil -> {:error, :not_found}
30 end
31 end
32 end
33
34 def feed(conn, %{"nickname" => nickname} = params) do
35 with %User{} = user <- User.get_cached_by_nickname(nickname) do
36 query_params =
37 Map.take(params, ["max_id"])
38 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
39
40 activities =
41 ActivityPub.fetch_public_activities(query_params)
42 |> Enum.reverse()
43
44 response =
45 user
46 |> FeedRepresenter.to_simple_form(activities, [user])
47 |> :xmerl.export_simple(:xmerl_xml)
48 |> to_string
49
50 conn
51 |> put_resp_content_type("application/atom+xml")
52 |> send_resp(200, response)
53 else
54 nil -> {:error, :not_found}
55 end
56 end
57
58 defp decode_or_retry(body) do
59 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
60 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
61 {:ok, doc}
62 else
63 _e ->
64 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
65 doc <- XML.parse_document(decoded),
66 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
67 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
68 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
69 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
70 {:ok, doc}
71 end
72 end
73 end
74
75 def salmon_incoming(conn, _) do
76 {:ok, body, _conn} = read_body(conn)
77 {:ok, doc} = decode_or_retry(body)
78
79 Federator.enqueue(:incoming_doc, doc)
80
81 conn
82 |> send_resp(200, "")
83 end
84
85 def object(conn, %{"uuid" => uuid}) do
86 if get_format(conn) == "activity+json" do
87 ActivityPubController.call(conn, :object)
88 else
89 with id <- o_status_url(conn, :object, uuid),
90 {_, %Activity{} = activity} <-
91 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
92 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
93 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
94 case get_format(conn) do
95 "html" -> redirect(conn, to: "/notice/#{activity.id}")
96 _ -> represent_activity(conn, nil, activity, user)
97 end
98 else
99 {:public?, false} ->
100 {:error, :not_found}
101
102 {:activity, nil} ->
103 {:error, :not_found}
104
105 e ->
106 e
107 end
108 end
109 end
110
111 def activity(conn, %{"uuid" => uuid}) do
112 with id <- o_status_url(conn, :activity, uuid),
113 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
114 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
115 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
116 case format = get_format(conn) do
117 "html" -> redirect(conn, to: "/notice/#{activity.id}")
118 _ -> represent_activity(conn, format, activity, user)
119 end
120 else
121 {:public?, false} ->
122 {:error, :not_found}
123
124 {:activity, nil} ->
125 {:error, :not_found}
126
127 e ->
128 e
129 end
130 end
131
132 def notice(conn, %{"id" => id}) do
133 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
134 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
135 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
136 case format = get_format(conn) do
137 "html" ->
138 serve_static_with_meta(conn, activity)
139
140 _ ->
141 represent_activity(conn, format, activity, user)
142 end
143 else
144 {:public?, false} ->
145 {:error, :not_found}
146
147 {:activity, nil} ->
148 {:error, :not_found}
149
150 e ->
151 e
152 end
153 end
154
155 defp serve_static_with_meta(conn, activity) do
156 {:ok, index_content } = File.read(Application.app_dir(:pleroma, "priv/static/index.html"))
157 links = OStatus.metadata(request_url(conn))
158 response = String.replace(index_content, "<!--server-generated-meta-->", links)
159 conn
160 |> put_resp_content_type("text/html")
161 |> send_resp(200, response)
162 end
163
164 defp represent_activity(
165 conn,
166 "activity+json",
167 %Activity{data: %{"type" => "Create"}} = activity,
168 _user
169 ) do
170 object = Object.normalize(activity.data["object"])
171
172 conn
173 |> put_resp_header("content-type", "application/activity+json")
174 |> json(ObjectView.render("object.json", %{object: object}))
175 end
176
177 defp represent_activity(_conn, "activity+json", _, _) do
178 {:error, :not_found}
179 end
180
181 defp represent_activity(conn, _, activity, user) do
182 response =
183 activity
184 |> ActivityRepresenter.to_simple_form(user, true)
185 |> ActivityRepresenter.wrap_with_entry()
186 |> :xmerl.export_simple(:xmerl_xml)
187 |> to_string
188
189 conn
190 |> put_resp_content_type("application/atom+xml")
191 |> send_resp(200, response)
192 end
193
194 def errors(conn, {:error, :not_found}) do
195 conn
196 |> put_status(404)
197 |> text("Not found")
198 end
199
200 def errors(conn, _) do
201 conn
202 |> put_status(500)
203 |> text("Something went wrong")
204 end
205 end