Skip to content

Commit

Permalink
Merge pull request #75 from globekeeper/DEV-801/recent-locations-endp…
Browse files Browse the repository at this point in the history
…oint

✨ /location_sync endpoint for fetching recent locations in a room.
  • Loading branch information
Danieloni1 authored Jul 13, 2023
2 parents b63efd7 + 20bbac5 commit b2d30a7
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dendrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:

# run go test with go 1.19
test:
timeout-minutes: 10
timeout-minutes: 15
name: Unit tests
runs-on: ubuntu-latest
# Service containers to run with `container-job`
Expand Down
64 changes: 64 additions & 0 deletions syncapi/routing/location_sync.go
Original file line number Diff line number Diff line change
@@ -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 (
"database/sql"
"net/http"

"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/gomatrixserverlib/spec"
"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 util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: spec.InternalServerError{},
}
}
mr, err := snapshot.SelectAllMultiRoomDataInRoom(req.Context(), roomID)
if err != nil {
if err != sql.ErrNoRows {
util.GetLogger(req.Context()).WithError(err).Error("failed to select all most recent multiroom data for room")
return util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: spec.InternalServerError{},
}
}
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: getLocationSyncResponse{RecentLocations: mr},
}
}
10 changes: 10 additions & 0 deletions syncapi/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,14 @@ func Setup(
return GetMemberships(req, device, vars["roomID"], syncDB, rsAPI, true, &membership, nil, at)
}),
).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)
}
1 change: 1 addition & 0 deletions syncapi/storage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type DatabaseTransaction interface {
PresenceAfter(ctx context.Context, after types.StreamPosition, filter synctypes.EventFilter) (map[string]*types.PresenceInternal, error)
RelationsFor(ctx context.Context, roomID, eventID, relType, eventType string, from, to types.StreamPosition, backwards bool, limit int) (events []types.StreamEvent, prevBatch, nextBatch string, err error)
SelectMultiRoomData(ctx context.Context, r *types.Range, joinedRooms []string) (types.MultiRoom, error)
SelectAllMultiRoomDataInRoom(ctx context.Context, roomId string) (types.MultiRoom, error)
}

type Database interface {
Expand Down
30 changes: 29 additions & 1 deletion syncapi/storage/postgres/multiroomcast_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ WHERE v.room_id = ANY($1)
AND id > $2
AND id <= $3`

const selectAllMultiRoomCastInRoomSQL = `SELECT d.user_id, d.type, d.data, d.ts FROM syncapi_multiroom_data AS d
JOIN syncapi_multiroom_visibility AS v
ON d.user_id = v.user_id
AND concat(d.type, '.visibility') = v.type
WHERE v.room_id = $1`

type multiRoomStatements struct {
selectMultiRoomCast *sql.Stmt
selectMultiRoomCast *sql.Stmt
selectAllMultiRoomCastInRoom *sql.Stmt
}

func NewPostgresMultiRoomCastTable(db *sql.DB) (tables.MultiRoom, error) {
Expand All @@ -37,6 +44,7 @@ func NewPostgresMultiRoomCastTable(db *sql.DB) (tables.MultiRoom, error) {
}
return r, sqlutil.StatementList{
{&r.selectMultiRoomCast, selectMultiRoomCastSQL},
{&r.selectAllMultiRoomCastInRoom, selectAllMultiRoomCastInRoomSQL},
}.Prepare(db)
}

Expand All @@ -59,3 +67,23 @@ func (s *multiRoomStatements) SelectMultiRoomData(ctx context.Context, r *types.
}
return data, rows.Err()
}

func (s *multiRoomStatements) SelectAllMultiRoomDataInRoom(ctx context.Context, roomId string, txn *sql.Tx) ([]*types.MultiRoomDataRow, error) {
rows, err := sqlutil.TxStmt(txn, s.selectAllMultiRoomCastInRoom).QueryContext(ctx, roomId)
if err != nil {
return nil, err
}
data := make([]*types.MultiRoomDataRow, 0)
defer internal.CloseAndLogIfError(ctx, rows, "SelectAllMultiRoomDataInRoom: rows.close() failed")
var t time.Time
for rows.Next() {
r := types.MultiRoomDataRow{}
err = rows.Scan(&r.UserId, &r.Type, &r.Data, &t)
r.Timestamp = t.UnixMilli()
if err != nil {
return nil, fmt.Errorf("rows scan: %w", err)
}
data = append(data, &r)
}
return data, rows.Err()
}
19 changes: 19 additions & 0 deletions syncapi/storage/shared/storage_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,22 @@ func (d *DatabaseTransaction) SelectMultiRoomData(ctx context.Context, r *types.
return mr, nil

}

func (d *DatabaseTransaction) SelectAllMultiRoomDataInRoom(ctx context.Context, roomId string) (types.MultiRoom, error) {
rows, err := d.MultiRoom.SelectAllMultiRoomDataInRoom(ctx, roomId, d.txn)
if err != nil {
return nil, fmt.Errorf("select all multi room data in room: %w", err)
}
mr := make(types.MultiRoom, 3)
for _, row := range rows {
if mr[row.UserId] == nil {
mr[row.UserId] = make(map[string]types.MultiRoomData)
}
mr[row.UserId][row.Type] = types.MultiRoomData{
Content: row.Data,
OriginServerTs: row.Timestamp,
}
}
return mr, nil

}
1 change: 1 addition & 0 deletions syncapi/storage/tables/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,5 @@ type Relations interface {

type MultiRoom interface {
SelectMultiRoomData(ctx context.Context, r *types.Range, joinedRooms []string, txn *sql.Tx) ([]*types.MultiRoomDataRow, error)
SelectAllMultiRoomDataInRoom(ctx context.Context, roomId string, txn *sql.Tx) ([]*types.MultiRoomDataRow, error)
}

0 comments on commit b2d30a7

Please sign in to comment.