Merge branch 'improve-server-config-examples' into 'develop'
[akkoma] / lib / pleroma / web / router.ex
1 defmodule Pleroma.Web.Router do
2 use Pleroma.Web, :router
3
4 alias Pleroma.{Repo, User, Web.Router}
5
6 @instance Application.get_env(:pleroma, :instance)
7 @federating Keyword.get(@instance, :federating)
8 @allow_relay Keyword.get(@instance, :allow_relay)
9 @public Keyword.get(@instance, :public)
10 @registrations_open Keyword.get(@instance, :registrations_open)
11
12 def user_fetcher(username_or_email) do
13 {
14 :ok,
15 cond do
16 # First, try logging in as if it was a name
17 user = Repo.get_by(User, %{nickname: username_or_email}) ->
18 user
19
20 # If we get nil, we try using it as an email
21 user = Repo.get_by(User, %{email: username_or_email}) ->
22 user
23 end
24 }
25 end
26
27 pipeline :api do
28 plug(:accepts, ["json"])
29 plug(:fetch_session)
30 plug(Pleroma.Plugs.OAuthPlug)
31 plug(Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true})
32 end
33
34 pipeline :authenticated_api do
35 plug(:accepts, ["json"])
36 plug(:fetch_session)
37 plug(Pleroma.Plugs.OAuthPlug)
38 plug(Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1})
39 end
40
41 pipeline :mastodon_html do
42 plug(:accepts, ["html"])
43 plug(:fetch_session)
44 plug(Pleroma.Plugs.OAuthPlug)
45 plug(Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true})
46 end
47
48 pipeline :pleroma_html do
49 plug(:accepts, ["html"])
50 plug(:fetch_session)
51 plug(Pleroma.Plugs.OAuthPlug)
52 plug(Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true})
53 end
54
55 pipeline :well_known do
56 plug(:accepts, ["json", "jrd+json", "xml", "xrd+xml"])
57 end
58
59 pipeline :config do
60 plug(:accepts, ["json", "xml"])
61 end
62
63 pipeline :oauth do
64 plug(:accepts, ["html", "json"])
65 end
66
67 pipeline :pleroma_api do
68 plug(:accepts, ["html", "json"])
69 end
70
71 scope "/api/pleroma", Pleroma.Web.TwitterAPI do
72 pipe_through(:pleroma_api)
73 get("/password_reset/:token", UtilController, :show_password_reset)
74 post("/password_reset", UtilController, :password_reset)
75 get("/emoji", UtilController, :emoji)
76 end
77
78 scope "/", Pleroma.Web.TwitterAPI do
79 pipe_through(:pleroma_html)
80 get("/ostatus_subscribe", UtilController, :remote_follow)
81 post("/ostatus_subscribe", UtilController, :do_remote_follow)
82 post("/main/ostatus", UtilController, :remote_subscribe)
83 end
84
85 scope "/api/pleroma", Pleroma.Web.TwitterAPI do
86 pipe_through(:authenticated_api)
87 post("/follow_import", UtilController, :follow_import)
88 post("/change_password", UtilController, :change_password)
89 post("/delete_account", UtilController, :delete_account)
90 end
91
92 scope "/oauth", Pleroma.Web.OAuth do
93 get("/authorize", OAuthController, :authorize)
94 post("/authorize", OAuthController, :create_authorization)
95 post("/token", OAuthController, :token_exchange)
96 post("/revoke", OAuthController, :token_revoke)
97 end
98
99 scope "/api/v1", Pleroma.Web.MastodonAPI do
100 pipe_through(:authenticated_api)
101
102 patch("/accounts/update_credentials", MastodonAPIController, :update_credentials)
103 get("/accounts/verify_credentials", MastodonAPIController, :verify_credentials)
104 get("/accounts/relationships", MastodonAPIController, :relationships)
105 get("/accounts/search", MastodonAPIController, :account_search)
106 post("/accounts/:id/follow", MastodonAPIController, :follow)
107 post("/accounts/:id/unfollow", MastodonAPIController, :unfollow)
108 post("/accounts/:id/block", MastodonAPIController, :block)
109 post("/accounts/:id/unblock", MastodonAPIController, :unblock)
110 post("/accounts/:id/mute", MastodonAPIController, :relationship_noop)
111 post("/accounts/:id/unmute", MastodonAPIController, :relationship_noop)
112
113 get("/follow_requests", MastodonAPIController, :follow_requests)
114 post("/follow_requests/:id/authorize", MastodonAPIController, :authorize_follow_request)
115 post("/follow_requests/:id/reject", MastodonAPIController, :reject_follow_request)
116
117 post("/follows", MastodonAPIController, :follow)
118
119 get("/blocks", MastodonAPIController, :blocks)
120
121 get("/mutes", MastodonAPIController, :empty_array)
122
123 get("/timelines/home", MastodonAPIController, :home_timeline)
124
125 get("/timelines/direct", MastodonAPIController, :dm_timeline)
126
127 get("/favourites", MastodonAPIController, :favourites)
128
129 post("/statuses", MastodonAPIController, :post_status)
130 delete("/statuses/:id", MastodonAPIController, :delete_status)
131
132 post("/statuses/:id/reblog", MastodonAPIController, :reblog_status)
133 post("/statuses/:id/unreblog", MastodonAPIController, :unreblog_status)
134 post("/statuses/:id/favourite", MastodonAPIController, :fav_status)
135 post("/statuses/:id/unfavourite", MastodonAPIController, :unfav_status)
136
137 post("/notifications/clear", MastodonAPIController, :clear_notifications)
138 post("/notifications/dismiss", MastodonAPIController, :dismiss_notification)
139 get("/notifications", MastodonAPIController, :notifications)
140 get("/notifications/:id", MastodonAPIController, :get_notification)
141
142 post("/media", MastodonAPIController, :upload)
143 put("/media/:id", MastodonAPIController, :update_media)
144
145 get("/lists", MastodonAPIController, :get_lists)
146 get("/lists/:id", MastodonAPIController, :get_list)
147 delete("/lists/:id", MastodonAPIController, :delete_list)
148 post("/lists", MastodonAPIController, :create_list)
149 put("/lists/:id", MastodonAPIController, :rename_list)
150 get("/lists/:id/accounts", MastodonAPIController, :list_accounts)
151 post("/lists/:id/accounts", MastodonAPIController, :add_to_list)
152 delete("/lists/:id/accounts", MastodonAPIController, :remove_from_list)
153
154 get("/domain_blocks", MastodonAPIController, :domain_blocks)
155 post("/domain_blocks", MastodonAPIController, :block_domain)
156 delete("/domain_blocks", MastodonAPIController, :unblock_domain)
157
158 get("/filters", MastodonAPIController, :get_filters)
159 post("/filters", MastodonAPIController, :create_filter)
160 get("/filters/:id", MastodonAPIController, :get_filter)
161 put("/filters/:id", MastodonAPIController, :update_filter)
162 delete("/filters/:id", MastodonAPIController, :delete_filter)
163
164 get("/suggestions", MastodonAPIController, :suggestions)
165
166 get("/filters", MastodonAPIController, :filters)
167 end
168
169 scope "/api/web", Pleroma.Web.MastodonAPI do
170 pipe_through(:authenticated_api)
171
172 put("/settings", MastodonAPIController, :put_settings)
173 end
174
175 scope "/api/v1", Pleroma.Web.MastodonAPI do
176 pipe_through(:api)
177 get("/instance", MastodonAPIController, :masto_instance)
178 get("/instance/peers", MastodonAPIController, :peers)
179 post("/apps", MastodonAPIController, :create_app)
180 get("/custom_emojis", MastodonAPIController, :custom_emojis)
181
182 get("/timelines/public", MastodonAPIController, :public_timeline)
183 get("/timelines/tag/:tag", MastodonAPIController, :hashtag_timeline)
184 get("/timelines/list/:list_id", MastodonAPIController, :list_timeline)
185
186 get("/statuses/:id", MastodonAPIController, :get_status)
187 get("/statuses/:id/context", MastodonAPIController, :get_context)
188 get("/statuses/:id/card", MastodonAPIController, :empty_object)
189 get("/statuses/:id/favourited_by", MastodonAPIController, :favourited_by)
190 get("/statuses/:id/reblogged_by", MastodonAPIController, :reblogged_by)
191
192 get("/accounts/:id/statuses", MastodonAPIController, :user_statuses)
193 get("/accounts/:id/followers", MastodonAPIController, :followers)
194 get("/accounts/:id/following", MastodonAPIController, :following)
195 get("/accounts/:id", MastodonAPIController, :user)
196
197 get("/trends", MastodonAPIController, :empty_array)
198
199 get("/search", MastodonAPIController, :search)
200 end
201
202 scope "/api/v2", Pleroma.Web.MastodonAPI do
203 pipe_through(:api)
204 get("/search", MastodonAPIController, :search2)
205 end
206
207 scope "/api", Pleroma.Web do
208 pipe_through(:config)
209
210 get("/help/test", TwitterAPI.UtilController, :help_test)
211 post("/help/test", TwitterAPI.UtilController, :help_test)
212 get("/statusnet/config", TwitterAPI.UtilController, :config)
213 get("/statusnet/version", TwitterAPI.UtilController, :version)
214 end
215
216 scope "/api", Pleroma.Web do
217 pipe_through(:api)
218
219 get("/statuses/user_timeline", TwitterAPI.Controller, :user_timeline)
220 get("/qvitter/statuses/user_timeline", TwitterAPI.Controller, :user_timeline)
221 get("/users/show", TwitterAPI.Controller, :show_user)
222
223 get("/statuses/followers", TwitterAPI.Controller, :followers)
224 get("/statuses/friends", TwitterAPI.Controller, :friends)
225 get("/statuses/show/:id", TwitterAPI.Controller, :fetch_status)
226 get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation)
227
228 post("/account/register", TwitterAPI.Controller, :register)
229
230 get("/search", TwitterAPI.Controller, :search)
231 get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline)
232 end
233
234 scope "/api", Pleroma.Web do
235 if @public do
236 pipe_through(:api)
237 else
238 pipe_through(:authenticated_api)
239 end
240
241 get("/statuses/public_timeline", TwitterAPI.Controller, :public_timeline)
242
243 get(
244 "/statuses/public_and_external_timeline",
245 TwitterAPI.Controller,
246 :public_and_external_timeline
247 )
248
249 get("/statuses/networkpublic_timeline", TwitterAPI.Controller, :public_and_external_timeline)
250 end
251
252 scope "/api", Pleroma.Web do
253 pipe_through(:authenticated_api)
254
255 get("/account/verify_credentials", TwitterAPI.Controller, :verify_credentials)
256 post("/account/verify_credentials", TwitterAPI.Controller, :verify_credentials)
257
258 post("/account/update_profile", TwitterAPI.Controller, :update_profile)
259 post("/account/update_profile_banner", TwitterAPI.Controller, :update_banner)
260 post("/qvitter/update_background_image", TwitterAPI.Controller, :update_background)
261
262 post(
263 "/account/most_recent_notification",
264 TwitterAPI.Controller,
265 :update_most_recent_notification
266 )
267
268 get("/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline)
269 get("/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline)
270 get("/statuses/mentions", TwitterAPI.Controller, :mentions_timeline)
271 get("/statuses/mentions_timeline", TwitterAPI.Controller, :mentions_timeline)
272 get("/qvitter/statuses/notifications", TwitterAPI.Controller, :notifications)
273
274 post("/statuses/update", TwitterAPI.Controller, :status_update)
275 post("/statuses/retweet/:id", TwitterAPI.Controller, :retweet)
276 post("/statuses/unretweet/:id", TwitterAPI.Controller, :unretweet)
277 post("/statuses/destroy/:id", TwitterAPI.Controller, :delete_post)
278
279 get("/pleroma/friend_requests", TwitterAPI.Controller, :friend_requests)
280 post("/pleroma/friendships/approve", TwitterAPI.Controller, :approve_friend_request)
281 post("/pleroma/friendships/deny", TwitterAPI.Controller, :deny_friend_request)
282
283 post("/friendships/create", TwitterAPI.Controller, :follow)
284 post("/friendships/destroy", TwitterAPI.Controller, :unfollow)
285 post("/blocks/create", TwitterAPI.Controller, :block)
286 post("/blocks/destroy", TwitterAPI.Controller, :unblock)
287
288 post("/statusnet/media/upload", TwitterAPI.Controller, :upload)
289 post("/media/upload", TwitterAPI.Controller, :upload_json)
290
291 post("/favorites/create/:id", TwitterAPI.Controller, :favorite)
292 post("/favorites/create", TwitterAPI.Controller, :favorite)
293 post("/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite)
294
295 post("/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar)
296
297 get("/friends/ids", TwitterAPI.Controller, :friends_ids)
298 get("/friendships/no_retweets/ids", TwitterAPI.Controller, :empty_array)
299
300 get("/mutes/users/ids", TwitterAPI.Controller, :empty_array)
301 get("/qvitter/mutes", TwitterAPI.Controller, :raw_empty_array)
302
303 get("/externalprofile/show", TwitterAPI.Controller, :external_profile)
304 end
305
306 pipeline :ap_relay do
307 plug(:accepts, ["activity+json"])
308 end
309
310 pipeline :ostatus do
311 plug(:accepts, ["xml", "atom", "html", "activity+json"])
312 end
313
314 scope "/", Pleroma.Web do
315 pipe_through(:ostatus)
316
317 get("/objects/:uuid", OStatus.OStatusController, :object)
318 get("/activities/:uuid", OStatus.OStatusController, :activity)
319 get("/notice/:id", OStatus.OStatusController, :notice)
320 get("/users/:nickname/feed", OStatus.OStatusController, :feed)
321 get("/users/:nickname", OStatus.OStatusController, :feed_redirect)
322
323 if @federating do
324 post("/users/:nickname/salmon", OStatus.OStatusController, :salmon_incoming)
325 post("/push/hub/:nickname", Websub.WebsubController, :websub_subscription_request)
326 get("/push/subscriptions/:id", Websub.WebsubController, :websub_subscription_confirmation)
327 post("/push/subscriptions/:id", Websub.WebsubController, :websub_incoming)
328 end
329 end
330
331 pipeline :activitypub do
332 plug(:accepts, ["activity+json"])
333 plug(Pleroma.Web.Plugs.HTTPSignaturePlug)
334 end
335
336 scope "/", Pleroma.Web.ActivityPub do
337 # XXX: not really ostatus
338 pipe_through(:ostatus)
339
340 get("/users/:nickname/followers", ActivityPubController, :followers)
341 get("/users/:nickname/following", ActivityPubController, :following)
342 get("/users/:nickname/outbox", ActivityPubController, :outbox)
343 end
344
345 if @federating do
346 if @allow_relay do
347 scope "/relay", Pleroma.Web.ActivityPub do
348 pipe_through(:ap_relay)
349 get("/", ActivityPubController, :relay)
350 end
351 end
352
353 scope "/", Pleroma.Web.ActivityPub do
354 pipe_through(:activitypub)
355 post("/users/:nickname/inbox", ActivityPubController, :inbox)
356 post("/inbox", ActivityPubController, :inbox)
357 end
358
359 scope "/.well-known", Pleroma.Web do
360 pipe_through(:well_known)
361
362 get("/host-meta", WebFinger.WebFingerController, :host_meta)
363 get("/webfinger", WebFinger.WebFingerController, :webfinger)
364 get("/nodeinfo", Nodeinfo.NodeinfoController, :schemas)
365 end
366
367 scope "/nodeinfo", Pleroma.Web do
368 get("/:version", Nodeinfo.NodeinfoController, :nodeinfo)
369 end
370 end
371
372 scope "/", Pleroma.Web.MastodonAPI do
373 pipe_through(:mastodon_html)
374
375 get("/web/login", MastodonAPIController, :login)
376 post("/web/login", MastodonAPIController, :login_post)
377 get("/web/*path", MastodonAPIController, :index)
378 delete("/auth/sign_out", MastodonAPIController, :logout)
379 end
380
381 pipeline :remote_media do
382 plug(:accepts, ["html"])
383 end
384
385 scope "/proxy/", Pleroma.Web.MediaProxy do
386 pipe_through(:remote_media)
387 get("/:sig/:url", MediaProxyController, :remote)
388 end
389
390 scope "/", Fallback do
391 get("/registration/:token", RedirectController, :registration_page)
392 get("/*path", RedirectController, :redirector)
393 end
394 end
395
396 defmodule Fallback.RedirectController do
397 use Pleroma.Web, :controller
398
399 def redirector(conn, _params) do
400 if Mix.env() != :test do
401 conn
402 |> put_resp_content_type("text/html")
403 |> send_file(200, "priv/static/index.html")
404 end
405 end
406
407 def registration_page(conn, params) do
408 redirector(conn, params)
409 end
410 end