common api: add support for formatting messages outside of twitter-style plain text
[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 end
97
98 scope "/api/v1", Pleroma.Web.MastodonAPI do
99 pipe_through(:authenticated_api)
100
101 patch("/accounts/update_credentials", MastodonAPIController, :update_credentials)
102 get("/accounts/verify_credentials", MastodonAPIController, :verify_credentials)
103 get("/accounts/relationships", MastodonAPIController, :relationships)
104 get("/accounts/search", MastodonAPIController, :account_search)
105 post("/accounts/:id/follow", MastodonAPIController, :follow)
106 post("/accounts/:id/unfollow", MastodonAPIController, :unfollow)
107 post("/accounts/:id/block", MastodonAPIController, :block)
108 post("/accounts/:id/unblock", MastodonAPIController, :unblock)
109 post("/accounts/:id/mute", MastodonAPIController, :relationship_noop)
110 post("/accounts/:id/unmute", MastodonAPIController, :relationship_noop)
111
112 get("/follow_requests", MastodonAPIController, :follow_requests)
113 post("/follow_requests/:id/authorize", MastodonAPIController, :authorize_follow_request)
114 post("/follow_requests/:id/reject", MastodonAPIController, :reject_follow_request)
115
116 post("/follows", MastodonAPIController, :follow)
117
118 get("/blocks", MastodonAPIController, :blocks)
119
120 get("/mutes", MastodonAPIController, :empty_array)
121
122 get("/timelines/home", MastodonAPIController, :home_timeline)
123
124 get("/timelines/direct", MastodonAPIController, :dm_timeline)
125
126 get("/favourites", MastodonAPIController, :favourites)
127
128 post("/statuses", MastodonAPIController, :post_status)
129 delete("/statuses/:id", MastodonAPIController, :delete_status)
130
131 post("/statuses/:id/reblog", MastodonAPIController, :reblog_status)
132 post("/statuses/:id/unreblog", MastodonAPIController, :unreblog_status)
133 post("/statuses/:id/favourite", MastodonAPIController, :fav_status)
134 post("/statuses/:id/unfavourite", MastodonAPIController, :unfav_status)
135
136 post("/notifications/clear", MastodonAPIController, :clear_notifications)
137 post("/notifications/dismiss", MastodonAPIController, :dismiss_notification)
138 get("/notifications", MastodonAPIController, :notifications)
139 get("/notifications/:id", MastodonAPIController, :get_notification)
140
141 post("/media", MastodonAPIController, :upload)
142 put("/media/:id", MastodonAPIController, :update_media)
143
144 get("/lists", MastodonAPIController, :get_lists)
145 get("/lists/:id", MastodonAPIController, :get_list)
146 delete("/lists/:id", MastodonAPIController, :delete_list)
147 post("/lists", MastodonAPIController, :create_list)
148 put("/lists/:id", MastodonAPIController, :rename_list)
149 get("/lists/:id/accounts", MastodonAPIController, :list_accounts)
150 post("/lists/:id/accounts", MastodonAPIController, :add_to_list)
151 delete("/lists/:id/accounts", MastodonAPIController, :remove_from_list)
152
153 get("/domain_blocks", MastodonAPIController, :domain_blocks)
154 post("/domain_blocks", MastodonAPIController, :block_domain)
155 delete("/domain_blocks", MastodonAPIController, :unblock_domain)
156
157 get("/suggestions", MastodonAPIController, :suggestions)
158 end
159
160 scope "/api/web", Pleroma.Web.MastodonAPI do
161 pipe_through(:authenticated_api)
162
163 put("/settings", MastodonAPIController, :put_settings)
164 end
165
166 scope "/api/v1", Pleroma.Web.MastodonAPI do
167 pipe_through(:api)
168 get("/instance", MastodonAPIController, :masto_instance)
169 get("/instance/peers", MastodonAPIController, :peers)
170 post("/apps", MastodonAPIController, :create_app)
171 get("/custom_emojis", MastodonAPIController, :custom_emojis)
172
173 get("/timelines/public", MastodonAPIController, :public_timeline)
174 get("/timelines/tag/:tag", MastodonAPIController, :hashtag_timeline)
175 get("/timelines/list/:list_id", MastodonAPIController, :list_timeline)
176
177 get("/statuses/:id", MastodonAPIController, :get_status)
178 get("/statuses/:id/context", MastodonAPIController, :get_context)
179 get("/statuses/:id/card", MastodonAPIController, :empty_object)
180 get("/statuses/:id/favourited_by", MastodonAPIController, :favourited_by)
181 get("/statuses/:id/reblogged_by", MastodonAPIController, :reblogged_by)
182
183 get("/accounts/:id/statuses", MastodonAPIController, :user_statuses)
184 get("/accounts/:id/followers", MastodonAPIController, :followers)
185 get("/accounts/:id/following", MastodonAPIController, :following)
186 get("/accounts/:id", MastodonAPIController, :user)
187
188 get("/trends", MastodonAPIController, :empty_array)
189
190 get("/search", MastodonAPIController, :search)
191 end
192
193 scope "/api/v2", Pleroma.Web.MastodonAPI do
194 pipe_through(:api)
195 get("/search", MastodonAPIController, :search2)
196 end
197
198 scope "/api", Pleroma.Web do
199 pipe_through(:config)
200
201 get("/help/test", TwitterAPI.UtilController, :help_test)
202 post("/help/test", TwitterAPI.UtilController, :help_test)
203 get("/statusnet/config", TwitterAPI.UtilController, :config)
204 get("/statusnet/version", TwitterAPI.UtilController, :version)
205 end
206
207 scope "/api", Pleroma.Web do
208 pipe_through(:api)
209
210 get("/statuses/user_timeline", TwitterAPI.Controller, :user_timeline)
211 get("/qvitter/statuses/user_timeline", TwitterAPI.Controller, :user_timeline)
212 get("/users/show", TwitterAPI.Controller, :show_user)
213
214 get("/statuses/followers", TwitterAPI.Controller, :followers)
215 get("/statuses/friends", TwitterAPI.Controller, :friends)
216 get("/statuses/show/:id", TwitterAPI.Controller, :fetch_status)
217 get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation)
218
219 post("/account/register", TwitterAPI.Controller, :register)
220
221 get("/search", TwitterAPI.Controller, :search)
222 get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline)
223 end
224
225 scope "/api", Pleroma.Web do
226 if @public do
227 pipe_through(:api)
228 else
229 pipe_through(:authenticated_api)
230 end
231
232 get("/statuses/public_timeline", TwitterAPI.Controller, :public_timeline)
233
234 get(
235 "/statuses/public_and_external_timeline",
236 TwitterAPI.Controller,
237 :public_and_external_timeline
238 )
239
240 get("/statuses/networkpublic_timeline", TwitterAPI.Controller, :public_and_external_timeline)
241 end
242
243 scope "/api", Pleroma.Web do
244 pipe_through(:authenticated_api)
245
246 get("/account/verify_credentials", TwitterAPI.Controller, :verify_credentials)
247 post("/account/verify_credentials", TwitterAPI.Controller, :verify_credentials)
248
249 post("/account/update_profile", TwitterAPI.Controller, :update_profile)
250 post("/account/update_profile_banner", TwitterAPI.Controller, :update_banner)
251 post("/qvitter/update_background_image", TwitterAPI.Controller, :update_background)
252
253 post(
254 "/account/most_recent_notification",
255 TwitterAPI.Controller,
256 :update_most_recent_notification
257 )
258
259 get("/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline)
260 get("/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline)
261 get("/statuses/mentions", TwitterAPI.Controller, :mentions_timeline)
262 get("/statuses/mentions_timeline", TwitterAPI.Controller, :mentions_timeline)
263 get("/qvitter/statuses/notifications", TwitterAPI.Controller, :notifications)
264
265 post("/statuses/update", TwitterAPI.Controller, :status_update)
266 post("/statuses/retweet/:id", TwitterAPI.Controller, :retweet)
267 post("/statuses/unretweet/:id", TwitterAPI.Controller, :unretweet)
268 post("/statuses/destroy/:id", TwitterAPI.Controller, :delete_post)
269
270 get("/pleroma/friend_requests", TwitterAPI.Controller, :friend_requests)
271 post("/pleroma/friendships/approve", TwitterAPI.Controller, :approve_friend_request)
272 post("/pleroma/friendships/deny", TwitterAPI.Controller, :deny_friend_request)
273
274 post("/friendships/create", TwitterAPI.Controller, :follow)
275 post("/friendships/destroy", TwitterAPI.Controller, :unfollow)
276 post("/blocks/create", TwitterAPI.Controller, :block)
277 post("/blocks/destroy", TwitterAPI.Controller, :unblock)
278
279 post("/statusnet/media/upload", TwitterAPI.Controller, :upload)
280 post("/media/upload", TwitterAPI.Controller, :upload_json)
281
282 post("/favorites/create/:id", TwitterAPI.Controller, :favorite)
283 post("/favorites/create", TwitterAPI.Controller, :favorite)
284 post("/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite)
285
286 post("/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar)
287
288 get("/friends/ids", TwitterAPI.Controller, :friends_ids)
289 get("/friendships/no_retweets/ids", TwitterAPI.Controller, :empty_array)
290
291 get("/mutes/users/ids", TwitterAPI.Controller, :empty_array)
292 get("/qvitter/mutes", TwitterAPI.Controller, :raw_empty_array)
293
294 get("/externalprofile/show", TwitterAPI.Controller, :external_profile)
295 end
296
297 pipeline :ap_relay do
298 plug(:accepts, ["activity+json"])
299 end
300
301 pipeline :ostatus do
302 plug(:accepts, ["xml", "atom", "html", "activity+json"])
303 end
304
305 scope "/", Pleroma.Web do
306 pipe_through(:ostatus)
307
308 get("/objects/:uuid", OStatus.OStatusController, :object)
309 get("/activities/:uuid", OStatus.OStatusController, :activity)
310 get("/notice/:id", OStatus.OStatusController, :notice)
311 get("/users/:nickname/feed", OStatus.OStatusController, :feed)
312 get("/users/:nickname", OStatus.OStatusController, :feed_redirect)
313
314 if @federating do
315 post("/users/:nickname/salmon", OStatus.OStatusController, :salmon_incoming)
316 post("/push/hub/:nickname", Websub.WebsubController, :websub_subscription_request)
317 get("/push/subscriptions/:id", Websub.WebsubController, :websub_subscription_confirmation)
318 post("/push/subscriptions/:id", Websub.WebsubController, :websub_incoming)
319 end
320 end
321
322 pipeline :activitypub do
323 plug(:accepts, ["activity+json"])
324 plug(Pleroma.Web.Plugs.HTTPSignaturePlug)
325 end
326
327 scope "/", Pleroma.Web.ActivityPub do
328 # XXX: not really ostatus
329 pipe_through(:ostatus)
330
331 get("/users/:nickname/followers", ActivityPubController, :followers)
332 get("/users/:nickname/following", ActivityPubController, :following)
333 get("/users/:nickname/outbox", ActivityPubController, :outbox)
334 end
335
336 if @federating do
337 if @allow_relay do
338 scope "/relay", Pleroma.Web.ActivityPub do
339 pipe_through(:ap_relay)
340 get("/", ActivityPubController, :relay)
341 end
342 end
343
344 scope "/", Pleroma.Web.ActivityPub do
345 pipe_through(:activitypub)
346 post("/users/:nickname/inbox", ActivityPubController, :inbox)
347 post("/inbox", ActivityPubController, :inbox)
348 end
349
350 scope "/.well-known", Pleroma.Web do
351 pipe_through(:well_known)
352
353 get("/host-meta", WebFinger.WebFingerController, :host_meta)
354 get("/webfinger", WebFinger.WebFingerController, :webfinger)
355 get("/nodeinfo", Nodeinfo.NodeinfoController, :schemas)
356 end
357
358 scope "/nodeinfo", Pleroma.Web do
359 get("/:version", Nodeinfo.NodeinfoController, :nodeinfo)
360 end
361 end
362
363 scope "/", Pleroma.Web.MastodonAPI do
364 pipe_through(:mastodon_html)
365
366 get("/web/login", MastodonAPIController, :login)
367 post("/web/login", MastodonAPIController, :login_post)
368 get("/web/*path", MastodonAPIController, :index)
369 delete("/auth/sign_out", MastodonAPIController, :logout)
370 end
371
372 pipeline :remote_media do
373 plug(:accepts, ["html"])
374 end
375
376 scope "/proxy/", Pleroma.Web.MediaProxy do
377 pipe_through(:remote_media)
378 get("/:sig/:url", MediaProxyController, :remote)
379 end
380
381 scope "/", Fallback do
382 get("/registration/:token", RedirectController, :registration_page)
383 get("/*path", RedirectController, :redirector)
384 end
385 end
386
387 defmodule Fallback.RedirectController do
388 use Pleroma.Web, :controller
389
390 def redirector(conn, _params) do
391 if Mix.env() != :test do
392 conn
393 |> put_resp_content_type("text/html")
394 |> send_file(200, "priv/static/index.html")
395 end
396 end
397
398 def registration_page(conn, params) do
399 redirector(conn, params)
400 end
401 end