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