Fix issue where a member is forced to leave a room when the invite is marked deleted #2838 (#55)

* fix #2838 by querying db directly for membership info

* fix lint error

* revert changes and use SelectMembershipForUser directly

* Remove extra membership checks

* Add a comment to explain why the membership check is necessary
This commit is contained in:
Tak Wai Wong 2022-11-02 12:34:07 -07:00 committed by GitHub
parent f2d6888452
commit 86f9b5ca89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"math"
"strconv" "strconv"
"time" "time"
@ -73,43 +74,28 @@ func (p *InviteStreamProvider) IncrementalSync(
return to return to
} }
for roomID := range retiredInvites { for roomID := range retiredInvites {
if req.Response.Rooms.Invite[roomID] != nil { membership, _, err := snapshot.SelectMembershipForUser(ctx, roomID, req.Device.UserID, math.MaxInt64)
continue // Skip if the user is an existing member of the room.
} // Otherwise, the NewLeaveResponse will eject the user from the room unintentionally
if req.Response.Rooms.Join[roomID] != nil { if membership == gomatrixserverlib.Join ||
err != nil {
continue continue
} }
joinedUsers, err := snapshot.AllJoinedUsersInRoom(ctx, []string{roomID}) lr := types.NewLeaveResponse()
if err != nil { h := sha256.Sum256(append([]byte(roomID), []byte(strconv.FormatInt(int64(to), 10))...))
continue lr.Timeline.Events = append(lr.Timeline.Events, gomatrixserverlib.ClientEvent{
} // fake event ID which muxes in the to position
EventID: "$" + base64.RawURLEncoding.EncodeToString(h[:]),
if !contains(joinedUsers[roomID], req.Device.UserID) { OriginServerTS: gomatrixserverlib.AsTimestamp(time.Now()),
lr := types.NewLeaveResponse() RoomID: roomID,
h := sha256.Sum256(append([]byte(roomID), []byte(strconv.FormatInt(int64(to), 10))...)) Sender: req.Device.UserID,
lr.Timeline.Events = append(lr.Timeline.Events, gomatrixserverlib.ClientEvent{ StateKey: &req.Device.UserID,
// fake event ID which muxes in the to position Type: "m.room.member",
EventID: "$" + base64.RawURLEncoding.EncodeToString(h[:]), Content: gomatrixserverlib.RawJSON(`{"membership":"leave"}`),
OriginServerTS: gomatrixserverlib.AsTimestamp(time.Now()), })
RoomID: roomID, req.Response.Rooms.Leave[roomID] = lr
Sender: req.Device.UserID,
StateKey: &req.Device.UserID,
Type: "m.room.member",
Content: gomatrixserverlib.RawJSON(`{"membership":"leave"}`),
})
req.Response.Rooms.Leave[roomID] = lr
}
} }
return maxID return maxID
} }
func contains(values []string, findVal string) bool {
for _, v := range values {
if v == findVal {
return true
}
}
return false
}