routing.go 11.4 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 37
	"github.com/matrix-org/util"
)

const pathPrefixR0 = "/_matrix/client/r0"
38
const pathPrefixUnstable = "/_matrix/client/unstable"
39 40 41

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

	apiMux.Handle("/_matrix/client/versions",
		common.MakeAPI("versions", func(req *http.Request) util.JSONResponse {
			return util.JSONResponse{
				Code: 200,
				JSON: struct {
					Versions []string `json:"versions"`
				}{[]string{
					"r0.0.1",
					"r0.1.0",
					"r0.2.0",
				}},
			}
		}),
	)

69
	r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
70 71
	unstableMux := apiMux.PathPrefix(pathPrefixUnstable).Subrouter()

72
	r0mux.Handle("/createRoom",
73
		common.MakeAuthAPI("createRoom", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
74
			return writers.CreateRoom(req, device, cfg, producer, accountDB)
75 76
		}),
	)
77 78 79 80
	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 已提交
81
				req, device, vars["roomIDOrAlias"], cfg, federation, producer, queryAPI, aliasAPI, keyRing, accountDB,
82 83 84
			)
		}),
	)
85 86 87 88 89 90
	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")
91
	r0mux.Handle("/rooms/{roomID}/send/{eventType}/{txnID}",
92
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
93
			vars := mux.Vars(req)
94
			return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], vars["txnID"], nil, cfg, queryAPI, producer)
95
		}),
K
Kegsay 已提交
96
	)
97
	r0mux.Handle("/rooms/{roomID}/state/{eventType:[^/]+/?}",
98
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
K
Kegsay 已提交
99 100
			vars := mux.Vars(req)
			emptyString := ""
101 102 103 104 105
			eventType := vars["eventType"]
			// If there's a trailing slash, remove it
			if strings.HasSuffix(eventType, "/") {
				eventType = eventType[:len(eventType)-1]
			}
106
			return writers.SendEvent(req, device, vars["roomID"], eventType, "", &emptyString, cfg, queryAPI, producer)
107
		}),
K
Kegsay 已提交
108 109
	)
	r0mux.Handle("/rooms/{roomID}/state/{eventType}/{stateKey}",
110
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
K
Kegsay 已提交
111 112
			vars := mux.Vars(req)
			stateKey := vars["stateKey"]
113
			return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], "", &stateKey, cfg, queryAPI, producer)
114
		}),
115 116
	)

117
	r0mux.Handle("/register", common.MakeAPI("register", func(req *http.Request) util.JSONResponse {
118
		return writers.Register(req, accountDB, deviceDB)
119 120
	}))

121 122 123
	r0mux.Handle("/directory/room/{roomAlias}",
		common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
124
			return readers.DirectoryRoom(req, device, vars["roomAlias"], federation, &cfg, aliasAPI)
125
		}),
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	).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)
			return readers.RemoveLocalAlias(req, device, vars["roomAlias"], &cfg, aliasAPI)
		}),
	).Methods("DELETE")
141

B
Brendan Abolivier 已提交
142 143 144 145 146 147
	r0mux.Handle("/logout",
		common.MakeAuthAPI("logout", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			return readers.Logout(req, deviceDB, device)
		}),
	)

148 149 150
	// Stub endpoints required by Riot

	r0mux.Handle("/login",
151
		common.MakeAPI("login", func(req *http.Request) util.JSONResponse {
152
			return readers.Login(req, accountDB, deviceDB, cfg)
153
		}),
154 155 156
	)

	r0mux.Handle("/pushrules/",
157
		common.MakeAPI("push_rules", func(req *http.Request) util.JSONResponse {
158 159 160 161 162 163 164 165 166 167 168 169 170 171
			// TODO: Implement push rules API
			res := json.RawMessage(`{
					"global": {
						"content": [],
						"override": [],
						"room": [],
						"sender": [],
						"underride": []
					}
				}`)
			return util.JSONResponse{
				Code: 200,
				JSON: &res,
			}
172
		}),
173 174 175
	)

	r0mux.Handle("/user/{userID}/filter",
176
		common.MakeAPI("make_filter", func(req *http.Request) util.JSONResponse {
177 178 179 180 181
			// TODO: Persist filter and return filter ID
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
182
		}),
183 184 185
	)

	r0mux.Handle("/user/{userID}/filter/{filterID}",
186
		common.MakeAPI("filter", func(req *http.Request) util.JSONResponse {
187 188 189 190 191
			// TODO: Retrieve filter based on ID
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
192
		}),
193 194 195 196 197
	)

	// Riot user settings

	r0mux.Handle("/profile/{userID}",
198
		common.MakeAPI("profile", func(req *http.Request) util.JSONResponse {
B
Brendan Abolivier 已提交
199 200
			vars := mux.Vars(req)
			return readers.GetProfile(req, accountDB, vars["userID"])
201
		}),
202 203
	)

B
Brendan Abolivier 已提交
204 205 206 207 208 209 210 211 212 213
	r0mux.Handle("/profile/{userID}/avatar_url",
		common.MakeAPI("profile_avatar_url", func(req *http.Request) util.JSONResponse {
			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)
214
			return readers.SetAvatarURL(req, accountDB, device, vars["userID"], userUpdateProducer, &cfg, producer, queryAPI)
B
Brendan Abolivier 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		}),
	).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",
		common.MakeAPI("profile_displayname", func(req *http.Request) util.JSONResponse {
			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)
230
			return readers.SetDisplayName(req, accountDB, device, vars["userID"], userUpdateProducer, &cfg, producer, queryAPI)
B
Brendan Abolivier 已提交
231 232 233 234 235
		}),
	).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

236
	r0mux.Handle("/account/3pid",
237
		common.MakeAPI("account_3pid", func(req *http.Request) util.JSONResponse {
238 239 240 241 242 243
			// TODO: Get 3pid data for user ID
			res := json.RawMessage(`{"threepids":[]}`)
			return util.JSONResponse{
				Code: 200,
				JSON: &res,
			}
244
		}),
245 246 247 248
	)

	// Riot logs get flooded unless this is handled
	r0mux.Handle("/presence/{userID}/status",
249
		common.MakeAPI("presence", func(req *http.Request) util.JSONResponse {
250 251 252 253 254
			// TODO: Set presence (probably the responsibility of a presence server not clientapi)
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
255
		}),
256 257
	)

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	r0mux.Handle("/voip/turnServer",
		common.MakeAPI("turn_server", func(req *http.Request) util.JSONResponse {
			// TODO: Return credentials for a turn server if one is configured.
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
		}),
	)

	unstableMux.Handle("/thirdparty/protocols",
		common.MakeAPI("thirdparty_protocols", func(req *http.Request) util.JSONResponse {
			// TODO: Return the third party protcols
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
		}),
	)

	r0mux.Handle("/rooms/{roomID}/initialSync",
		common.MakeAPI("rooms_initial_sync", func(req *http.Request) util.JSONResponse {
			// TODO: Allow people to peek into rooms.
			return util.JSONResponse{
				Code: 403,
				JSON: jsonerror.GuestAccessForbidden("Guest access not implemented"),
			}
		}),
	)

	r0mux.Handle("/user/{userID}/account_data/{type}",
289 290
		common.MakeAuthAPI("user_account_data", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
291
			return readers.SaveAccountData(req, accountDB, device, vars["userID"], "", vars["type"], syncProducer)
292 293 294 295 296 297
		}),
	)

	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)
298
			return readers.SaveAccountData(req, accountDB, device, vars["userID"], vars["roomID"], vars["type"], syncProducer)
299 300 301
		}),
	)

302 303 304
	r0mux.Handle("/rooms/{roomID}/members",
		common.MakeAuthAPI("rooms_members", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
305 306 307 308 309 310 311 312
			return readers.GetMemberships(req, device, vars["roomID"], false, accountDB, cfg, queryAPI)
		}),
	)

	r0mux.Handle("/rooms/{roomID}/joined_members",
		common.MakeAuthAPI("rooms_members", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
			return readers.GetMemberships(req, device, vars["roomID"], true, accountDB, cfg, queryAPI)
313 314 315
		}),
	)

316 317 318 319 320 321 322 323 324 325 326 327 328
	r0mux.Handle("/rooms/{roomID}/read_markers",
		common.MakeAPI("rooms_read_markers", func(req *http.Request) util.JSONResponse {
			// TODO: return the read_markers.
			return util.JSONResponse{Code: 200, JSON: struct{}{}}
		}),
	)

	r0mux.Handle("/rooms/{roomID}/typing/{userID}",
		common.MakeAPI("rooms_typing", func(req *http.Request) util.JSONResponse {
			// TODO: handling typing
			return util.JSONResponse{Code: 200, JSON: struct{}{}}
		}),
	)
329
}