routing.go 14.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 Vector Creations Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16 17
package routing

import (
18
	"encoding/json"
19
	"net/http"
20
	"strings"
21 22

	"github.com/gorilla/mux"
23
	"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
M
Mark Haines 已提交
24
	"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
25
	"github.com/matrix-org/dendrite/clientapi/auth/storage/devices"
26
	"github.com/matrix-org/dendrite/clientapi/jsonerror"
27
	"github.com/matrix-org/dendrite/clientapi/producers"
28
	"github.com/matrix-org/dendrite/clientapi/readers"
29
	"github.com/matrix-org/dendrite/clientapi/writers"
30
	"github.com/matrix-org/dendrite/common"
31
	"github.com/matrix-org/dendrite/common/config"
32
	"github.com/matrix-org/dendrite/roomserver/api"
33
	"github.com/matrix-org/gomatrixserverlib"
34 35 36
	"github.com/matrix-org/util"
)

37
const pathPrefixV1 = "/_matrix/client/api/v1"
38
const pathPrefixR0 = "/_matrix/client/r0"
39
const pathPrefixUnstable = "/_matrix/client/unstable"
40 41 42

// Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client
// to clients which need to make outbound HTTP requests.
43
func Setup(
E
Erik Johnston 已提交
44
	apiMux *mux.Router, cfg config.Dendrite,
45
	producer *producers.RoomserverProducer, queryAPI api.RoomserverQueryAPI,
46
	aliasAPI api.RoomserverAliasAPI,
47 48 49 50
	accountDB *accounts.Database,
	deviceDB *devices.Database,
	federation *gomatrixserverlib.FederationClient,
	keyRing gomatrixserverlib.KeyRing,
B
Brendan Abolivier 已提交
51
	userUpdateProducer *producers.UserUpdateProducer,
52
	syncProducer *producers.SyncAPIProducer,
53
) {
54 55

	apiMux.Handle("/_matrix/client/versions",
56
		common.MakeExternalAPI("versions", func(req *http.Request) util.JSONResponse {
57 58 59 60 61 62 63 64 65 66 67
			return util.JSONResponse{
				Code: 200,
				JSON: struct {
					Versions []string `json:"versions"`
				}{[]string{
					"r0.0.1",
					"r0.1.0",
					"r0.2.0",
				}},
			}
		}),
68
	).Methods("GET")
69

70
	r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
71
	v1mux := apiMux.PathPrefix(pathPrefixV1).Subrouter()
72 73
	unstableMux := apiMux.PathPrefix(pathPrefixUnstable).Subrouter()

74
	r0mux.Handle("/createRoom",
75
		common.MakeAuthAPI("createRoom", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
76
			return writers.CreateRoom(req, device, cfg, producer, accountDB, aliasAPI)
77
		}),
78
	).Methods("POST", "OPTIONS")
79 80 81 82
	r0mux.Handle("/join/{roomIDOrAlias}",
		common.MakeAuthAPI("join", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
			return writers.JoinRoomByIDOrAlias(
B
Brendan Abolivier 已提交
83
				req, device, vars["roomIDOrAlias"], cfg, federation, producer, queryAPI, aliasAPI, keyRing, accountDB,
84 85
			)
		}),
86
	).Methods("POST", "OPTIONS")
87 88 89 90 91 92
	r0mux.Handle("/rooms/{roomID}/{membership:(?:join|kick|ban|unban|leave|invite)}",
		common.MakeAuthAPI("membership", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
			return writers.SendMembership(req, accountDB, device, vars["roomID"], vars["membership"], cfg, queryAPI, producer)
		}),
	).Methods("POST", "OPTIONS")
E
Erik Johnston 已提交
93 94 95 96 97 98
	r0mux.Handle("/rooms/{roomID}/send/{eventType}",
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
			return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], nil, nil, cfg, queryAPI, producer)
		}),
	).Methods("POST", "OPTIONS")
99
	r0mux.Handle("/rooms/{roomID}/send/{eventType}/{txnID}",
100
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
101
			vars := mux.Vars(req)
E
Erik Johnston 已提交
102 103
			txnID := vars["txnID"]
			return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], &txnID, nil, cfg, queryAPI, producer)
104
		}),
105
	).Methods("PUT", "OPTIONS")
106
	r0mux.Handle("/rooms/{roomID}/state/{eventType:[^/]+/?}",
107
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
K
Kegsay 已提交
108 109
			vars := mux.Vars(req)
			emptyString := ""
110 111 112 113 114
			eventType := vars["eventType"]
			// If there's a trailing slash, remove it
			if strings.HasSuffix(eventType, "/") {
				eventType = eventType[:len(eventType)-1]
			}
E
Erik Johnston 已提交
115
			return writers.SendEvent(req, device, vars["roomID"], eventType, nil, &emptyString, cfg, queryAPI, producer)
116
		}),
117
	).Methods("PUT", "OPTIONS")
K
Kegsay 已提交
118
	r0mux.Handle("/rooms/{roomID}/state/{eventType}/{stateKey}",
119
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
K
Kegsay 已提交
120 121
			vars := mux.Vars(req)
			stateKey := vars["stateKey"]
E
Erik Johnston 已提交
122
			return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], nil, &stateKey, cfg, queryAPI, producer)
123
		}),
124
	).Methods("PUT", "OPTIONS")
125

126
	r0mux.Handle("/register", common.MakeExternalAPI("register", func(req *http.Request) util.JSONResponse {
127 128 129
		return writers.Register(req, accountDB, deviceDB, &cfg)
	})).Methods("POST", "OPTIONS")

130
	v1mux.Handle("/register", common.MakeExternalAPI("register", func(req *http.Request) util.JSONResponse {
131
		return writers.LegacyRegister(req, accountDB, deviceDB, &cfg)
132
	})).Methods("POST", "OPTIONS")
133

M
Marcel 已提交
134 135 136 137
	r0mux.Handle("/register/available", common.MakeExternalAPI("registerAvailable", func(req *http.Request) util.JSONResponse {
		return writers.RegisterAvailable(req, accountDB)
	})).Methods("GET")

138 139 140
	r0mux.Handle("/directory/room/{roomAlias}",
		common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
E
Erik Johnston 已提交
141
			return readers.DirectoryRoom(req, vars["roomAlias"], federation, &cfg, aliasAPI)
142
		}),
143 144 145 146 147 148 149 150 151 152 153 154
	).Methods("GET")

	r0mux.Handle("/directory/room/{roomAlias}",
		common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
			return readers.SetLocalAlias(req, device, vars["roomAlias"], &cfg, aliasAPI)
		}),
	).Methods("PUT", "OPTIONS")

	r0mux.Handle("/directory/room/{roomAlias}",
		common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
E
Erik Johnston 已提交
155
			return readers.RemoveLocalAlias(req, device, vars["roomAlias"], aliasAPI)
156 157
		}),
	).Methods("DELETE")
158

B
Brendan Abolivier 已提交
159 160 161 162
	r0mux.Handle("/logout",
		common.MakeAuthAPI("logout", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			return readers.Logout(req, deviceDB, device)
		}),
163
	).Methods("POST", "OPTIONS")
B
Brendan Abolivier 已提交
164

165 166 167
	// Stub endpoints required by Riot

	r0mux.Handle("/login",
168
		common.MakeExternalAPI("login", func(req *http.Request) util.JSONResponse {
169
			return readers.Login(req, accountDB, deviceDB, cfg)
170
		}),
171
	).Methods("GET", "POST", "OPTIONS")
172 173

	r0mux.Handle("/pushrules/",
174
		common.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse {
175 176 177 178 179 180 181 182 183 184 185 186 187 188
			// TODO: Implement push rules API
			res := json.RawMessage(`{
					"global": {
						"content": [],
						"override": [],
						"room": [],
						"sender": [],
						"underride": []
					}
				}`)
			return util.JSONResponse{
				Code: 200,
				JSON: &res,
			}
189
		}),
190
	).Methods("GET")
191 192

	r0mux.Handle("/user/{userID}/filter",
193
		common.MakeExternalAPI("make_filter", func(req *http.Request) util.JSONResponse {
194 195 196 197 198
			// TODO: Persist filter and return filter ID
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
199
		}),
200
	).Methods("POST", "OPTIONS")
201 202

	r0mux.Handle("/user/{userID}/filter/{filterID}",
203
		common.MakeExternalAPI("filter", func(req *http.Request) util.JSONResponse {
204 205 206 207 208
			// TODO: Retrieve filter based on ID
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
209
		}),
210
	).Methods("GET")
211 212 213 214

	// Riot user settings

	r0mux.Handle("/profile/{userID}",
215
		common.MakeExternalAPI("profile", func(req *http.Request) util.JSONResponse {
B
Brendan Abolivier 已提交
216 217
			vars := mux.Vars(req)
			return readers.GetProfile(req, accountDB, vars["userID"])
218
		}),
219
	).Methods("GET")
220

B
Brendan Abolivier 已提交
221
	r0mux.Handle("/profile/{userID}/avatar_url",
222
		common.MakeExternalAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse {
B
Brendan Abolivier 已提交
223 224 225 226 227 228 229 230
			vars := mux.Vars(req)
			return readers.GetAvatarURL(req, accountDB, vars["userID"])
		}),
	).Methods("GET")

	r0mux.Handle("/profile/{userID}/avatar_url",
		common.MakeAuthAPI("profile_avatar_url", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
231
			return readers.SetAvatarURL(req, accountDB, device, vars["userID"], userUpdateProducer, &cfg, producer, queryAPI)
B
Brendan Abolivier 已提交
232 233 234 235 236 237
		}),
	).Methods("PUT", "OPTIONS")
	// Browsers use the OPTIONS HTTP method to check if the CORS policy allows
	// PUT requests, so we need to allow this method

	r0mux.Handle("/profile/{userID}/displayname",
238
		common.MakeExternalAPI("profile_displayname", func(req *http.Request) util.JSONResponse {
B
Brendan Abolivier 已提交
239 240 241 242 243 244 245 246
			vars := mux.Vars(req)
			return readers.GetDisplayName(req, accountDB, vars["userID"])
		}),
	).Methods("GET")

	r0mux.Handle("/profile/{userID}/displayname",
		common.MakeAuthAPI("profile_displayname", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
247
			return readers.SetDisplayName(req, accountDB, device, vars["userID"], userUpdateProducer, &cfg, producer, queryAPI)
B
Brendan Abolivier 已提交
248 249 250 251 252
		}),
	).Methods("PUT", "OPTIONS")
	// Browsers use the OPTIONS HTTP method to check if the CORS policy allows
	// PUT requests, so we need to allow this method

253
	r0mux.Handle("/account/3pid",
254 255
		common.MakeAuthAPI("account_3pid", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			return readers.GetAssociated3PIDs(req, accountDB, device)
256
		}),
257 258 259 260
	).Methods("GET")

	r0mux.Handle("/account/3pid",
		common.MakeAuthAPI("account_3pid", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
261
			return readers.CheckAndSave3PIDAssociation(req, accountDB, device, cfg)
262 263 264 265 266 267 268 269 270 271
		}),
	).Methods("POST", "OPTIONS")

	unstableMux.Handle("/account/3pid/delete",
		common.MakeAuthAPI("account_3pid", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			return readers.Forget3PID(req, accountDB)
		}),
	).Methods("POST", "OPTIONS")

	r0mux.Handle("/{path:(?:account/3pid|register)}/email/requestToken",
272
		common.MakeExternalAPI("account_3pid_request_token", func(req *http.Request) util.JSONResponse {
273
			return readers.RequestEmailToken(req, accountDB, cfg)
274 275
		}),
	).Methods("POST", "OPTIONS")
276 277 278

	// Riot logs get flooded unless this is handled
	r0mux.Handle("/presence/{userID}/status",
279
		common.MakeExternalAPI("presence", func(req *http.Request) util.JSONResponse {
280 281 282 283 284
			// TODO: Set presence (probably the responsibility of a presence server not clientapi)
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
285
		}),
286
	).Methods("PUT", "OPTIONS")
287

288
	r0mux.Handle("/voip/turnServer",
289
		common.MakeExternalAPI("turn_server", func(req *http.Request) util.JSONResponse {
290 291 292 293 294 295
			// TODO: Return credentials for a turn server if one is configured.
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
		}),
296
	).Methods("GET")
297 298

	unstableMux.Handle("/thirdparty/protocols",
299
		common.MakeExternalAPI("thirdparty_protocols", func(req *http.Request) util.JSONResponse {
300 301 302 303 304 305
			// TODO: Return the third party protcols
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
		}),
306
	).Methods("GET")
307 308

	r0mux.Handle("/rooms/{roomID}/initialSync",
309
		common.MakeExternalAPI("rooms_initial_sync", func(req *http.Request) util.JSONResponse {
310 311 312 313 314 315
			// TODO: Allow people to peek into rooms.
			return util.JSONResponse{
				Code: 403,
				JSON: jsonerror.GuestAccessForbidden("Guest access not implemented"),
			}
		}),
316
	).Methods("GET")
317 318

	r0mux.Handle("/user/{userID}/account_data/{type}",
319 320
		common.MakeAuthAPI("user_account_data", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
321
			return readers.SaveAccountData(req, accountDB, device, vars["userID"], "", vars["type"], syncProducer)
322
		}),
323
	).Methods("PUT", "OPTIONS")
324 325 326 327

	r0mux.Handle("/user/{userID}/rooms/{roomID}/account_data/{type}",
		common.MakeAuthAPI("user_account_data", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
328
			return readers.SaveAccountData(req, accountDB, device, vars["userID"], vars["roomID"], vars["type"], syncProducer)
329
		}),
330
	).Methods("PUT", "OPTIONS")
331

332 333 334
	r0mux.Handle("/rooms/{roomID}/members",
		common.MakeAuthAPI("rooms_members", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
E
Erik Johnston 已提交
335
			return readers.GetMemberships(req, device, vars["roomID"], false, cfg, queryAPI)
336
		}),
337
	).Methods("GET")
338 339 340 341

	r0mux.Handle("/rooms/{roomID}/joined_members",
		common.MakeAuthAPI("rooms_members", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
E
Erik Johnston 已提交
342
			return readers.GetMemberships(req, device, vars["roomID"], true, cfg, queryAPI)
343
		}),
344
	).Methods("GET")
345

346
	r0mux.Handle("/rooms/{roomID}/read_markers",
347
		common.MakeExternalAPI("rooms_read_markers", func(req *http.Request) util.JSONResponse {
348 349 350
			// TODO: return the read_markers.
			return util.JSONResponse{Code: 200, JSON: struct{}{}}
		}),
351
	).Methods("POST", "OPTIONS")
352 353

	r0mux.Handle("/rooms/{roomID}/typing/{userID}",
354
		common.MakeExternalAPI("rooms_typing", func(req *http.Request) util.JSONResponse {
355 356 357
			// TODO: handling typing
			return util.JSONResponse{Code: 200, JSON: struct{}{}}
		}),
358
	).Methods("PUT", "OPTIONS")
M
Mark Haines 已提交
359 360 361

	// Stub implementations for sytest
	r0mux.Handle("/events",
362
		common.MakeExternalAPI("events", func(req *http.Request) util.JSONResponse {
M
Mark Haines 已提交
363 364 365 366 367 368 369 370 371
			return util.JSONResponse{Code: 200, JSON: map[string]interface{}{
				"chunk": []interface{}{},
				"start": "",
				"end":   "",
			}}
		}),
	).Methods("GET")

	r0mux.Handle("/initialSync",
372
		common.MakeExternalAPI("initial_sync", func(req *http.Request) util.JSONResponse {
M
Mark Haines 已提交
373 374 375 376 377
			return util.JSONResponse{Code: 200, JSON: map[string]interface{}{
				"end": "",
			}}
		}),
	).Methods("GET")
378
}