router.ex 10.8 KB
Newer Older
lain's avatar
lain committed
1 2 3
defmodule Pleroma.Web.Router do
  use Pleroma.Web, :router

4
  alias Pleroma.{Repo, User, Web.Router}
5

lain's avatar
lain committed
6 7 8
  @instance Application.get_env(:pleroma, :instance)
  @federating Keyword.get(@instance, :federating)

9 10 11 12
  def user_fetcher(username) do
    {:ok, Repo.get_by(User, %{nickname: username})}
  end

lain's avatar
lain committed
13 14
  pipeline :api do
    plug :accepts, ["json"]
lain's avatar
lain committed
15
    plug :fetch_session
lain's avatar
lain committed
16
    plug Pleroma.Plugs.OAuthPlug
17
    plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true}
lain's avatar
lain committed
18 19
  end

20 21 22
  pipeline :authenticated_api do
    plug :accepts, ["json"]
    plug :fetch_session
23
    plug Pleroma.Plugs.OAuthPlug
24
    plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1}
lain's avatar
lain committed
25 26
  end

lain's avatar
lain committed
27 28 29 30 31 32 33
  pipeline :mastodon_html do
    plug :accepts, ["html"]
    plug :fetch_session
    plug Pleroma.Plugs.OAuthPlug
    plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true}
  end

34 35 36 37 38 39 40
  pipeline :pleroma_html do
    plug :accepts, ["html"]
    plug :fetch_session
    plug Pleroma.Plugs.OAuthPlug
    plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true}
  end

lain's avatar
lain committed
41 42 43 44
  pipeline :well_known do
    plug :accepts, ["xml", "xrd+xml"]
  end

lain's avatar
lain committed
45 46 47 48
  pipeline :config do
    plug :accepts, ["json", "xml"]
  end

49 50 51 52
  pipeline :oauth do
    plug :accepts, ["html", "json"]
  end

eal's avatar
eal committed
53 54
  pipeline :pleroma_api do
    plug :accepts, ["html", "json"]
Roger Braun's avatar
Roger Braun committed
55 56 57
  end

  scope "/api/pleroma", Pleroma.Web.TwitterAPI do
eal's avatar
eal committed
58
    pipe_through :pleroma_api
Roger Braun's avatar
Roger Braun committed
59 60
    get "/password_reset/:token", UtilController, :show_password_reset
    post "/password_reset", UtilController, :password_reset
eal's avatar
eal committed
61
    get "/emoji", UtilController, :emoji
Roger Braun's avatar
Roger Braun committed
62 63
  end

64 65 66 67
  scope "/", Pleroma.Web.TwitterAPI do
    pipe_through :pleroma_html
    get "/ostatus_subscribe", UtilController, :remote_follow
    post "/ostatus_subscribe", UtilController, :do_remote_follow
eal's avatar
eal committed
68
    post "/main/ostatus", UtilController, :remote_subscribe
69 70
  end

eal's avatar
eal committed
71 72 73 74 75
  scope "/api/pleroma", Pleroma.Web.TwitterAPI do
    pipe_through :authenticated_api
    post "/follow_import", UtilController, :follow_import
  end

76 77 78 79 80 81 82 83 84
  scope "/oauth", Pleroma.Web.OAuth do
    get "/authorize", OAuthController, :authorize
    post "/authorize", OAuthController, :create_authorization
    post "/token", OAuthController, :token_exchange
  end

  scope "/api/v1", Pleroma.Web.MastodonAPI do
    pipe_through :authenticated_api

85
    patch "/accounts/update_credentials", MastodonAPIController, :update_credentials
86
    get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials
Roger Braun's avatar
Roger Braun committed
87
    get "/accounts/relationships", MastodonAPIController, :relationships
lain's avatar
lain committed
88
    get "/accounts/search", MastodonAPIController, :account_search
89 90
    post "/accounts/:id/follow", MastodonAPIController, :follow
    post "/accounts/:id/unfollow", MastodonAPIController, :unfollow
lain's avatar
lain committed
91 92
    post "/accounts/:id/block", MastodonAPIController, :block
    post "/accounts/:id/unblock", MastodonAPIController, :unblock
93 94 95
    post "/accounts/:id/mute", MastodonAPIController, :relationship_noop
    post "/accounts/:id/unmute", MastodonAPIController, :relationship_noop

eal's avatar
eal committed
96 97
    post "/follows", MastodonAPIController, :follow

lain's avatar
lain committed
98 99
    get "/blocks", MastodonAPIController, :blocks

100 101 102
    get "/domain_blocks", MastodonAPIController, :empty_array
    get "/follow_requests", MastodonAPIController, :empty_array
    get "/mutes", MastodonAPIController, :empty_array
Roger Braun's avatar
Roger Braun committed
103

lain's avatar
lain committed
104 105
    get "/timelines/home", MastodonAPIController, :home_timeline

106 107
    get "/favourites", MastodonAPIController, :favourites

lain's avatar
lain committed
108
    post "/statuses", MastodonAPIController, :post_status
lain's avatar
lain committed
109
    delete "/statuses/:id", MastodonAPIController, :delete_status
lain's avatar
lain committed
110 111

    post "/statuses/:id/reblog", MastodonAPIController, :reblog_status
lain's avatar
lain committed
112
    post "/statuses/:id/favourite", MastodonAPIController, :fav_status
lain's avatar
lain committed
113
    post "/statuses/:id/unfavourite", MastodonAPIController, :unfav_status
114

115 116
    post "/notifications/clear", MastodonAPIController, :clear_notifications
    post "/notifications/dismiss", MastodonAPIController, :dismiss_notification
117
    get "/notifications", MastodonAPIController, :notifications
118
    get "/notifications/:id", MastodonAPIController, :get_notification
lain's avatar
lain committed
119 120

    post "/media", MastodonAPIController, :upload
lain's avatar
lain committed
121 122
  end

Roger Braun's avatar
Roger Braun committed
123 124 125
  scope "/api/v1", Pleroma.Web.MastodonAPI do
    pipe_through :api
    get "/instance", MastodonAPIController, :masto_instance
126
    get "/instance/peers", MastodonAPIController, :peers
Roger Braun's avatar
Roger Braun committed
127
    post "/apps", MastodonAPIController, :create_app
128
    get "/custom_emojis", MastodonAPIController, :custom_emojis
Roger Braun's avatar
Roger Braun committed
129 130

    get "/timelines/public", MastodonAPIController, :public_timeline
Roger Braun's avatar
Roger Braun committed
131
    get "/timelines/tag/:tag", MastodonAPIController, :hashtag_timeline
Roger Braun's avatar
Roger Braun committed
132 133 134

    get "/statuses/:id", MastodonAPIController, :get_status
    get "/statuses/:id/context", MastodonAPIController, :get_context
135 136
    get "/statuses/:id/favourited_by", MastodonAPIController, :favourited_by
    get "/statuses/:id/reblogged_by", MastodonAPIController, :reblogged_by
Roger Braun's avatar
Roger Braun committed
137 138

    get "/accounts/:id/statuses", MastodonAPIController, :user_statuses
139 140
    get "/accounts/:id/followers", MastodonAPIController, :followers
    get "/accounts/:id/following", MastodonAPIController, :following
Roger Braun's avatar
Roger Braun committed
141
    get "/accounts/:id", MastodonAPIController, :user
lain's avatar
lain committed
142 143

    get "/search", MastodonAPIController, :search
Roger Braun's avatar
Roger Braun committed
144 145
  end

lain's avatar
lain committed
146
  scope "/api", Pleroma.Web do
lain's avatar
lain committed
147
    pipe_through :config
dtluna's avatar
dtluna committed
148

dtluna's avatar
dtluna committed
149
    get "/help/test", TwitterAPI.UtilController, :help_test
lain's avatar
lain committed
150
    post "/help/test", TwitterAPI.UtilController, :help_test
dtluna's avatar
dtluna committed
151
    get "/statusnet/config", TwitterAPI.UtilController, :config
lain's avatar
lain committed
152 153 154
    get "/statusnet/version", TwitterAPI.UtilController, :version
  end

155 156 157
  @instance Application.get_env(:pleroma, :instance)
  @registrations_open Keyword.get(@instance, :registrations_open)

lain's avatar
lain committed
158 159
  scope "/api", Pleroma.Web do
    pipe_through :api
dtluna's avatar
dtluna committed
160

lain's avatar
lain committed
161
    get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
lain's avatar
lain committed
162
    get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_and_external_timeline
lain's avatar
lain committed
163
    get "/statuses/networkpublic_timeline", TwitterAPI.Controller, :public_and_external_timeline
164
    get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline
lain's avatar
lain committed
165
    get "/qvitter/statuses/user_timeline", TwitterAPI.Controller, :user_timeline
eal's avatar
eal committed
166
    get "/users/show", TwitterAPI.Controller, :show_user
dtluna's avatar
dtluna committed
167

eal's avatar
eal committed
168 169
    get "/statuses/followers", TwitterAPI.Controller, :followers
    get "/statuses/friends", TwitterAPI.Controller, :friends
lain's avatar
lain committed
170
    get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
171
    get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
dtluna's avatar
dtluna committed
172

173 174 175
    if @registrations_open do
      post "/account/register", TwitterAPI.Controller, :register
    end
lain's avatar
lain committed
176

lain's avatar
lain committed
177
    get "/search", TwitterAPI.Controller, :search
178
    get "/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline
179 180
  end

lain's avatar
lain committed
181
  scope "/api", Pleroma.Web do
182 183
    pipe_through :authenticated_api

dtluna's avatar
dtluna committed
184
    get "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
lain's avatar
lain committed
185
    post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
dtluna's avatar
dtluna committed
186

lain's avatar
lain committed
187
    post "/account/update_profile", TwitterAPI.Controller, :update_profile
lain's avatar
lain committed
188 189
    post "/account/update_profile_banner", TwitterAPI.Controller, :update_banner
    post "/qvitter/update_background_image", TwitterAPI.Controller, :update_background
lain's avatar
lain committed
190

191 192
    post "/account/most_recent_notification", TwitterAPI.Controller, :update_most_recent_notification

dtluna's avatar
dtluna committed
193
    get "/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline
lain's avatar
lain committed
194
    get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline
dtluna's avatar
dtluna committed
195
    get "/statuses/mentions", TwitterAPI.Controller, :mentions_timeline
dtluna's avatar
dtluna committed
196
    get "/statuses/mentions_timeline", TwitterAPI.Controller, :mentions_timeline
dtluna's avatar
dtluna committed
197 198 199

    post "/statuses/update", TwitterAPI.Controller, :status_update
    post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet
lain's avatar
lain committed
200
    post "/statuses/destroy/:id", TwitterAPI.Controller, :delete_post
dtluna's avatar
dtluna committed
201

lain's avatar
lain committed
202 203
    post "/friendships/create", TwitterAPI.Controller, :follow
    post "/friendships/destroy", TwitterAPI.Controller, :unfollow
eal's avatar
eal committed
204 205
    post "/blocks/create", TwitterAPI.Controller, :block
    post "/blocks/destroy", TwitterAPI.Controller, :unblock
dtluna's avatar
dtluna committed
206

lain's avatar
lain committed
207
    post "/statusnet/media/upload", TwitterAPI.Controller, :upload
208
    post "/media/upload", TwitterAPI.Controller, :upload_json
dtluna's avatar
dtluna committed
209

lain's avatar
lain committed
210
    post "/favorites/create/:id", TwitterAPI.Controller, :favorite
211
    post "/favorites/create", TwitterAPI.Controller, :favorite
lain's avatar
lain committed
212
    post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite
dtluna's avatar
dtluna committed
213

lain's avatar
lain committed
214
    post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar
lain's avatar
lain committed
215

eal's avatar
eal committed
216
    get "/friends/ids", TwitterAPI.Controller, :friends_ids
217
    get "/friendships/no_retweets/ids", TwitterAPI.Controller, :empty_array
218

219
    get "/mutes/users/ids", TwitterAPI.Controller, :empty_array
220 221

    get "/externalprofile/show", TwitterAPI.Controller, :external_profile
lain's avatar
lain committed
222
  end
lain's avatar
lain committed
223

lain's avatar
lain committed
224
  pipeline :ostatus do
lain's avatar
lain committed
225
    plug :accepts, ["xml", "atom", "html", "activity+json"]
lain's avatar
lain committed
226 227
  end

lain's avatar
lain committed
228
  scope "/", Pleroma.Web do
lain's avatar
lain committed
229 230
    pipe_through :ostatus

231
    get "/objects/:uuid", OStatus.OStatusController, :object
lain's avatar
lain committed
232
    get "/activities/:uuid", OStatus.OStatusController, :activity
lain's avatar
lain committed
233
    get "/notice/:id", OStatus.OStatusController, :notice
lain's avatar
lain committed
234
    get "/users/:nickname/feed", OStatus.OStatusController, :feed
235
    get "/users/:nickname", OStatus.OStatusController, :feed_redirect
lain's avatar
lain committed
236 237 238 239 240 241 242 243

    if @federating do
      post "/users/:nickname/salmon", OStatus.OStatusController, :salmon_incoming
      post "/push/hub/:nickname", Websub.WebsubController, :websub_subscription_request
      get "/push/subscriptions/:id", Websub.WebsubController, :websub_subscription_confirmation
      post "/push/subscriptions/:id", Websub.WebsubController, :websub_incoming
    end

lain's avatar
lain committed
244 245
  end

lain's avatar
lain committed
246 247
  pipeline :activitypub do
    plug :accepts, ["activity+json"]
248
    plug Pleroma.Web.Plugs.HTTPSignaturePlug
lain's avatar
lain committed
249 250
  end

lain's avatar
lain committed
251
  if @federating do
252 253 254 255 256
    scope "/", Pleroma.Web.ActivityPub do
      pipe_through :activitypub
      post "/users/:nickname/inbox", ActivityPubController, :inbox
      post "/inbox", ActivityPubController, :inbox
    end
lain's avatar
lain committed
257

lain's avatar
lain committed
258 259
    scope "/.well-known", Pleroma.Web do
      pipe_through :well_known
lain's avatar
lain committed
260

lain's avatar
lain committed
261 262 263
      get "/host-meta", WebFinger.WebFingerController, :host_meta
      get "/webfinger", WebFinger.WebFingerController, :webfinger
    end
lain's avatar
lain committed
264
  end
lain's avatar
lain committed
265

lain's avatar
lain committed
266
  scope "/", Pleroma.Web.MastodonAPI do
lain's avatar
lain committed
267 268
    pipe_through :mastodon_html

lain's avatar
lain committed
269 270 271 272
    get "/web/login", MastodonAPIController, :login
    post "/web/login", MastodonAPIController, :login_post
    get "/web/*path", MastodonAPIController, :index
    delete "/auth/sign_out", MastodonAPIController, :logout
lain's avatar
lain committed
273 274
  end

href's avatar
href committed
275 276 277 278 279 280 281 282
  pipeline :remote_media do
    plug :accepts, ["html"]
  end
  scope "/proxy/", Pleroma.Web.MediaProxy do
    pipe_through :remote_media
    get "/:sig/:url", MediaProxyController, :remote
  end

lain's avatar
lain committed
283 284 285 286 287 288 289
  scope "/", Fallback do
    get "/*path", RedirectController, :redirector
  end
end

defmodule Fallback.RedirectController do
  use Pleroma.Web, :controller
lain's avatar
lain committed
290 291 292 293 294 295 296
  def redirector(conn, _params) do
    if Mix.env != :test do
      conn
      |> put_resp_content_type("text/html")
      |> send_file(200, "priv/static/index.html")
    end
  end
lain's avatar
lain committed
297
end