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