Get messages from before user left the room (#2824)

This is going to make `Can get rooms/{roomId}/messages for a departed
room (SPEC-216)` pass, since we now only grep events from before the
user left the room.
This commit is contained in:
Till 2022-10-24 17:03:04 +02:00 committed by GitHub
parent a553fe7705
commit 7506e3303e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 9 deletions

View File

@ -83,18 +83,18 @@ func OnIncomingMessagesRequest(
defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err) defer sqlutil.EndTransactionWithCheck(snapshot, &succeeded, &err)
// check if the user has already forgotten about this room // check if the user has already forgotten about this room
isForgotten, roomExists, err := checkIsRoomForgotten(req.Context(), roomID, device.UserID, rsAPI) membershipResp, err := getMembershipForUser(req.Context(), roomID, device.UserID, rsAPI)
if err != nil { if err != nil {
return jsonerror.InternalServerError() return jsonerror.InternalServerError()
} }
if !roomExists { if !membershipResp.RoomExists {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusForbidden, Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("room does not exist"), JSON: jsonerror.Forbidden("room does not exist"),
} }
} }
if isForgotten { if membershipResp.IsRoomForgotten {
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusForbidden, Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("user already forgot about this room"), JSON: jsonerror.Forbidden("user already forgot about this room"),
@ -195,6 +195,20 @@ func OnIncomingMessagesRequest(
} }
} }
// If the user already left the room, grep events from before that
if membershipResp.Membership == gomatrixserverlib.Leave {
var token types.TopologyToken
token, err = snapshot.EventPositionInTopology(req.Context(), membershipResp.EventID)
if err != nil {
return util.JSONResponse{
Code: http.StatusInternalServerError,
}
}
if backwardOrdering {
from = token
}
}
mReq := messagesReq{ mReq := messagesReq{
ctx: req.Context(), ctx: req.Context(),
db: db, db: db,
@ -283,17 +297,16 @@ func (m *messagesResp) applyLazyLoadMembers(
} }
} }
func checkIsRoomForgotten(ctx context.Context, roomID, userID string, rsAPI api.SyncRoomserverAPI) (forgotten bool, exists bool, err error) { func getMembershipForUser(ctx context.Context, roomID, userID string, rsAPI api.SyncRoomserverAPI) (resp api.QueryMembershipForUserResponse, err error) {
req := api.QueryMembershipForUserRequest{ req := api.QueryMembershipForUserRequest{
RoomID: roomID, RoomID: roomID,
UserID: userID, UserID: userID,
} }
resp := api.QueryMembershipForUserResponse{}
if err := rsAPI.QueryMembershipForUser(ctx, &req, &resp); err != nil { if err := rsAPI.QueryMembershipForUser(ctx, &req, &resp); err != nil {
return false, false, err return api.QueryMembershipForUserResponse{}, err
} }
return resp.IsRoomForgotten, resp.RoomExists, nil return resp, nil
} }
// retrieveEvents retrieves events from the local database for a request on // retrieveEvents retrieves events from the local database for a request on
@ -313,7 +326,11 @@ func (r *messagesReq) retrieveEvents() (
} }
var events []*gomatrixserverlib.HeaderedEvent var events []*gomatrixserverlib.HeaderedEvent
util.GetLogger(r.ctx).WithField("start", start).WithField("end", end).Infof("Fetched %d events locally", len(streamEvents)) util.GetLogger(r.ctx).WithFields(logrus.Fields{
"start": r.from,
"end": r.to,
"backwards": r.backwardOrdering,
}).Infof("Fetched %d events locally", len(streamEvents))
// There can be two reasons for streamEvents to be empty: either we've // There can be two reasons for streamEvents to be empty: either we've
// reached the oldest event in the room (or the most recent one, depending // reached the oldest event in the room (or the most recent one, depending

View File

@ -753,3 +753,5 @@ When user joins a room the state is included in a gapped sync
Messages that notify from another user increment notification_count Messages that notify from another user increment notification_count
Messages that highlight from another user increment unread highlight count Messages that highlight from another user increment unread highlight count
Notifications can be viewed with GET /notifications Notifications can be viewed with GET /notifications
Can get rooms/{roomId}/messages for a departed room (SPEC-216)
Local device key changes appear in /keys/changes