mirror of
https://github.com/hoernschen/dendrite.git
synced 2024-12-27 07:28:27 +00:00
Fix user already joined when using server notices (#2364)
This commit is contained in:
parent
658e82f8bc
commit
1bfe87aa56
1 changed files with 22 additions and 27 deletions
|
@ -21,6 +21,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/version"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/gomatrixserverlib/tokens"
|
"github.com/matrix-org/gomatrixserverlib/tokens"
|
||||||
|
@ -95,29 +96,16 @@ func SendServerNotice(
|
||||||
// get rooms for specified user
|
// get rooms for specified user
|
||||||
allUserRooms := []string{}
|
allUserRooms := []string{}
|
||||||
userRooms := api.QueryRoomsForUserResponse{}
|
userRooms := api.QueryRoomsForUserResponse{}
|
||||||
if err := rsAPI.QueryRoomsForUser(ctx, &api.QueryRoomsForUserRequest{
|
// Get rooms the user is either joined, invited or has left.
|
||||||
UserID: r.UserID,
|
for _, membership := range []string{"join", "invite", "leave"} {
|
||||||
WantMembership: "join",
|
if err := rsAPI.QueryRoomsForUser(ctx, &api.QueryRoomsForUserRequest{
|
||||||
}, &userRooms); err != nil {
|
UserID: r.UserID,
|
||||||
return util.ErrorResponse(err)
|
WantMembership: membership,
|
||||||
|
}, &userRooms); err != nil {
|
||||||
|
return util.ErrorResponse(err)
|
||||||
|
}
|
||||||
|
allUserRooms = append(allUserRooms, userRooms.RoomIDs...)
|
||||||
}
|
}
|
||||||
allUserRooms = append(allUserRooms, userRooms.RoomIDs...)
|
|
||||||
// get invites for specified user
|
|
||||||
if err := rsAPI.QueryRoomsForUser(ctx, &api.QueryRoomsForUserRequest{
|
|
||||||
UserID: r.UserID,
|
|
||||||
WantMembership: "invite",
|
|
||||||
}, &userRooms); err != nil {
|
|
||||||
return util.ErrorResponse(err)
|
|
||||||
}
|
|
||||||
allUserRooms = append(allUserRooms, userRooms.RoomIDs...)
|
|
||||||
// get left rooms for specified user
|
|
||||||
if err := rsAPI.QueryRoomsForUser(ctx, &api.QueryRoomsForUserRequest{
|
|
||||||
UserID: r.UserID,
|
|
||||||
WantMembership: "leave",
|
|
||||||
}, &userRooms); err != nil {
|
|
||||||
return util.ErrorResponse(err)
|
|
||||||
}
|
|
||||||
allUserRooms = append(allUserRooms, userRooms.RoomIDs...)
|
|
||||||
|
|
||||||
// get rooms of the sender
|
// get rooms of the sender
|
||||||
senderUserID := fmt.Sprintf("@%s:%s", cfgNotices.LocalPart, cfgClient.Matrix.ServerName)
|
senderUserID := fmt.Sprintf("@%s:%s", cfgNotices.LocalPart, cfgClient.Matrix.ServerName)
|
||||||
|
@ -145,7 +133,7 @@ func SendServerNotice(
|
||||||
|
|
||||||
var (
|
var (
|
||||||
roomID string
|
roomID string
|
||||||
roomVersion = gomatrixserverlib.RoomVersionV6
|
roomVersion = version.DefaultRoomVersion()
|
||||||
)
|
)
|
||||||
|
|
||||||
// create a new room for the user
|
// create a new room for the user
|
||||||
|
@ -194,14 +182,21 @@ func SendServerNotice(
|
||||||
// if we didn't get a createRoomResponse, we probably received an error, so return that.
|
// if we didn't get a createRoomResponse, we probably received an error, so return that.
|
||||||
return roomRes
|
return roomRes
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// we've found a room in common, check the membership
|
// we've found a room in common, check the membership
|
||||||
roomID = commonRooms[0]
|
roomID = commonRooms[0]
|
||||||
// re-invite the user
|
membershipRes := api.QueryMembershipForUserResponse{}
|
||||||
res, err := sendInvite(ctx, userAPI, senderDevice, roomID, r.UserID, "Server notice room", cfgClient, rsAPI, asAPI, time.Now())
|
err := rsAPI.QueryMembershipForUser(ctx, &api.QueryMembershipForUserRequest{UserID: r.UserID, RoomID: roomID}, &membershipRes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res
|
util.GetLogger(ctx).WithError(err).Error("unable to query membership for user")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
if !membershipRes.IsInRoom {
|
||||||
|
// re-invite the user
|
||||||
|
res, err := sendInvite(ctx, userAPI, senderDevice, roomID, r.UserID, "Server notice room", cfgClient, rsAPI, asAPI, time.Now())
|
||||||
|
if err != nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue