routing.go 5.5 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 20 21
	"net/http"

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

const pathPrefixR0 = "/_matrix/client/r0"

// Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client
// to clients which need to make outbound HTTP requests.
40 41 42 43 44 45 46 47
func Setup(
	servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI,
	producer *producers.RoomserverProducer, queryAPI api.RoomserverQueryAPI,
	accountDB *accounts.Database,
	deviceDB *devices.Database,
	federation *gomatrixserverlib.FederationClient,
	keyRing gomatrixserverlib.KeyRing,
) {
48 49
	apiMux := mux.NewRouter()
	r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
50
	r0mux.Handle("/createRoom",
51 52
		common.MakeAuthAPI("createRoom", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			return writers.CreateRoom(req, device, cfg, producer)
53 54
		}),
	)
55 56 57 58 59 60 61 62
	r0mux.Handle("/join/{roomIDOrAlias}",
		common.MakeAuthAPI("join", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
			vars := mux.Vars(req)
			return writers.JoinRoomByIDOrAlias(
				req, device, vars["roomIDOrAlias"], cfg, federation, producer, queryAPI, keyRing,
			)
		}),
	)
63
	r0mux.Handle("/rooms/{roomID}/send/{eventType}/{txnID}",
64
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
65
			vars := mux.Vars(req)
66
			return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], vars["txnID"], nil, cfg, queryAPI, producer)
67
		}),
K
Kegsay 已提交
68 69
	)
	r0mux.Handle("/rooms/{roomID}/state/{eventType}",
70
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
K
Kegsay 已提交
71 72
			vars := mux.Vars(req)
			emptyString := ""
73
			return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], vars["txnID"], &emptyString, cfg, queryAPI, producer)
74
		}),
K
Kegsay 已提交
75 76
	)
	r0mux.Handle("/rooms/{roomID}/state/{eventType}/{stateKey}",
77
		common.MakeAuthAPI("send_message", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
K
Kegsay 已提交
78 79
			vars := mux.Vars(req)
			stateKey := vars["stateKey"]
80
			return writers.SendEvent(req, device, vars["roomID"], vars["eventType"], vars["txnID"], &stateKey, cfg, queryAPI, producer)
81
		}),
82 83
	)

84
	r0mux.Handle("/register", common.MakeAPI("register", func(req *http.Request) util.JSONResponse {
85 86 87
		return writers.Register(req, accountDB)
	}))

88 89 90
	// Stub endpoints required by Riot

	r0mux.Handle("/login",
91
		common.MakeAPI("login", func(req *http.Request) util.JSONResponse {
92
			return readers.Login(req, cfg)
93
		}),
94 95 96
	)

	r0mux.Handle("/pushrules/",
97
		common.MakeAPI("push_rules", func(req *http.Request) util.JSONResponse {
98 99 100 101 102 103 104 105 106 107 108 109 110 111
			// TODO: Implement push rules API
			res := json.RawMessage(`{
					"global": {
						"content": [],
						"override": [],
						"room": [],
						"sender": [],
						"underride": []
					}
				}`)
			return util.JSONResponse{
				Code: 200,
				JSON: &res,
			}
112
		}),
113 114 115
	)

	r0mux.Handle("/user/{userID}/filter",
116
		common.MakeAPI("make_filter", func(req *http.Request) util.JSONResponse {
117 118 119 120 121
			// TODO: Persist filter and return filter ID
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
122
		}),
123 124 125
	)

	r0mux.Handle("/user/{userID}/filter/{filterID}",
126
		common.MakeAPI("filter", func(req *http.Request) util.JSONResponse {
127 128 129 130 131
			// TODO: Retrieve filter based on ID
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
132
		}),
133 134 135 136 137
	)

	// Riot user settings

	r0mux.Handle("/profile/{userID}",
138
		common.MakeAPI("profile", func(req *http.Request) util.JSONResponse {
139 140 141 142 143
			// TODO: Get profile data for user ID
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
144
		}),
145 146 147
	)

	r0mux.Handle("/account/3pid",
148
		common.MakeAPI("account_3pid", func(req *http.Request) util.JSONResponse {
149 150 151 152 153 154
			// TODO: Get 3pid data for user ID
			res := json.RawMessage(`{"threepids":[]}`)
			return util.JSONResponse{
				Code: 200,
				JSON: &res,
			}
155
		}),
156 157 158 159
	)

	// Riot logs get flooded unless this is handled
	r0mux.Handle("/presence/{userID}/status",
160
		common.MakeAPI("presence", func(req *http.Request) util.JSONResponse {
161 162 163 164 165
			// TODO: Set presence (probably the responsibility of a presence server not clientapi)
			return util.JSONResponse{
				Code: 200,
				JSON: struct{}{},
			}
166
		}),
167 168
	)

169 170 171
	servMux.Handle("/metrics", prometheus.Handler())
	servMux.Handle("/api/", http.StripPrefix("/api", apiMux))
}