mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-07 06:53:09 -06:00
✨ /location_sync endpoint for fetching recent locations in a room.
This commit is contained in:
parent
1124ed08b0
commit
6940728ffa
2
.github/workflows/dendrite.yml
vendored
2
.github/workflows/dendrite.yml
vendored
|
|
@ -76,7 +76,7 @@ jobs:
|
||||||
|
|
||||||
# run go test with go 1.19
|
# run go test with go 1.19
|
||||||
test:
|
test:
|
||||||
timeout-minutes: 10
|
timeout-minutes: 15
|
||||||
name: Unit tests
|
name: Unit tests
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
# Service containers to run with `container-job`
|
# Service containers to run with `container-job`
|
||||||
|
|
|
||||||
64
syncapi/routing/location_sync.go
Normal file
64
syncapi/routing/location_sync.go
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
package routing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
||||||
|
"github.com/matrix-org/dendrite/syncapi/types"
|
||||||
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
||||||
|
"github.com/matrix-org/util"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-rooms-roomid-joined-members
|
||||||
|
type getLocationSyncResponse struct {
|
||||||
|
RecentLocations types.MultiRoom `json:"recent_locations"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLocationSync return each VISIBLE user's most recent location update in the room.
|
||||||
|
// This is used for the case of newly joined users which require catching up on location events of users,
|
||||||
|
// but aren't able to retrieve certain location events from /sync, since they joined after them.
|
||||||
|
func GetLocationSync(
|
||||||
|
req *http.Request, device *userapi.Device, roomID string,
|
||||||
|
syncDB storage.Database, rsAPI api.SyncRoomserverAPI,
|
||||||
|
) util.JSONResponse {
|
||||||
|
snapshot, err := syncDB.NewDatabaseSnapshot(req.Context())
|
||||||
|
if err != nil {
|
||||||
|
logrus.WithError(err).Error("Failed to get snapshot for locations sync")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
id, err := snapshot.SelectMaxMultiRoomDataEventId(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("failed to get max multiroom data event id")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
mr, err := snapshot.SelectMultiRoomData(req.Context(), &types.Range{From: 0, To: id}, []string{roomID})
|
||||||
|
if err != nil {
|
||||||
|
if err != sql.ErrNoRows {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("failed to select multiroom data for room")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusOK,
|
||||||
|
JSON: getLocationSyncResponse{RecentLocations: mr},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -205,4 +205,14 @@ func Setup(
|
||||||
return GetMemberships(req, device, vars["roomID"], syncDB, rsAPI, true, &membership, nil, at)
|
return GetMemberships(req, device, vars["roomID"], syncDB, rsAPI, true, &membership, nil, at)
|
||||||
}),
|
}),
|
||||||
).Methods(http.MethodGet, http.MethodOptions)
|
).Methods(http.MethodGet, http.MethodOptions)
|
||||||
|
|
||||||
|
v3mux.Handle("/rooms/{roomID}/location_sync",
|
||||||
|
httputil.MakeAuthAPI("location_sync", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
|
||||||
|
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
|
||||||
|
if err != nil {
|
||||||
|
return util.ErrorResponse(err)
|
||||||
|
}
|
||||||
|
return GetLocationSync(req, device, vars["roomID"], syncDB, rsAPI)
|
||||||
|
}),
|
||||||
|
).Methods(http.MethodGet, http.MethodOptions)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,8 +25,12 @@ WHERE v.room_id = ANY($1)
|
||||||
AND id > $2
|
AND id > $2
|
||||||
AND id <= $3`
|
AND id <= $3`
|
||||||
|
|
||||||
|
const selectMaxMultiCastIDSQL = "" +
|
||||||
|
"SELECT MAX(id) FROM syncapi_multiroom_data"
|
||||||
|
|
||||||
type multiRoomStatements struct {
|
type multiRoomStatements struct {
|
||||||
selectMultiRoomCast *sql.Stmt
|
selectMultiRoomCast *sql.Stmt
|
||||||
|
selectMaxMultiCastID *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPostgresMultiRoomCastTable(db *sql.DB) (tables.MultiRoom, error) {
|
func NewPostgresMultiRoomCastTable(db *sql.DB) (tables.MultiRoom, error) {
|
||||||
|
|
@ -37,6 +41,7 @@ func NewPostgresMultiRoomCastTable(db *sql.DB) (tables.MultiRoom, error) {
|
||||||
}
|
}
|
||||||
return r, sqlutil.StatementList{
|
return r, sqlutil.StatementList{
|
||||||
{&r.selectMultiRoomCast, selectMultiRoomCastSQL},
|
{&r.selectMultiRoomCast, selectMultiRoomCastSQL},
|
||||||
|
{&r.selectMaxMultiCastID, selectMaxMultiCastIDSQL},
|
||||||
}.Prepare(db)
|
}.Prepare(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,3 +64,11 @@ func (s *multiRoomStatements) SelectMultiRoomData(ctx context.Context, r *types.
|
||||||
}
|
}
|
||||||
return data, rows.Err()
|
return data, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *multiRoomStatements) SelectMaxMultiRoomDataEventId(
|
||||||
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
) (id int64, err error) {
|
||||||
|
stmt := sqlutil.TxStmt(txn, s.selectMaxMultiCastID)
|
||||||
|
err = stmt.QueryRowContext(ctx).Scan(&id)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -824,3 +824,8 @@ func (d *DatabaseTransaction) SelectMultiRoomData(ctx context.Context, r *types.
|
||||||
return mr, nil
|
return mr, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DatabaseTransaction) SelectMaxMultiRoomDataEventId(ctx context.Context) (types.StreamPosition, error) {
|
||||||
|
id, err := d.MultiRoom.SelectMaxMultiRoomDataEventId(ctx, d.txn)
|
||||||
|
return types.StreamPosition(id), err
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -245,4 +245,5 @@ type Relations interface {
|
||||||
|
|
||||||
type MultiRoom interface {
|
type MultiRoom interface {
|
||||||
SelectMultiRoomData(ctx context.Context, r *types.Range, joinedRooms []string, txn *sql.Tx) ([]*types.MultiRoomDataRow, error)
|
SelectMultiRoomData(ctx context.Context, r *types.Range, joinedRooms []string, txn *sql.Tx) ([]*types.MultiRoomDataRow, error)
|
||||||
|
SelectMaxMultiRoomDataEventId(ctx context.Context, txn *sql.Tx) (id int64, err error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue