Handle matrix left events correctly (hopefully)

This commit is contained in:
Alex 2020-02-28 12:16:59 +01:00
parent d03091dd01
commit 5398e9fc0f
3 changed files with 39 additions and 25 deletions

View file

@ -205,6 +205,15 @@ func (a *Account) addAutojoin(roomId RoomID) {
}).FirstOrCreate(&entry)
}
func (a *Account) delAutojoin(roomId RoomID) {
db.Where(&DbJoinedRoom{
MxUserID: a.MatrixUser,
Protocol: a.Protocol,
AccountName: a.AccountName,
RoomID: roomId,
}).Delete(&DbJoinedRoom{})
}
// ---- Begin event handlers ----
func (a *Account) Joined(roomId RoomID) {
@ -212,13 +221,13 @@ func (a *Account) Joined(roomId RoomID) {
if err != nil {
a.ezbrMessagef("Dropping Account.Joined %s: %s", roomId, err.Error())
}
a.addAutojoin(roomId)
}
func (a *Account) joinedInternal(roomId RoomID) error {
a.JoinedRooms[roomId] = true
a.addAutojoin(roomId)
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return err
@ -240,18 +249,13 @@ func (a *Account) Left(roomId RoomID) {
if err != nil {
a.ezbrMessagef("Dropping Account.Left %s: %s", roomId, err.Error())
}
db.Where(&DbJoinedRoom{
MxUserID: a.MatrixUser,
Protocol: a.Protocol,
AccountName: a.AccountName,
RoomID: roomId,
}).Delete(&DbJoinedRoom{})
}
func (a *Account) leftInternal(roomId RoomID) error {
delete(a.JoinedRooms, roomId)
a.delAutojoin(roomId)
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
if err != nil {
return err

36
db.go
View file

@ -237,8 +237,19 @@ func dbGetMxRoom(protocol string, roomId connector.RoomID) (string, error) {
return room.MxRoomID, nil
}
func dbPmRoomSlotKey(room *DbPmRoomMap) string {
return fmt.Sprintf("pmroom:%s/%s/%s/%s",
room.Protocol, room.MxUserID, room.AccountName, room.UserID)
}
func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMxId string, usAccount string) (string, error) {
slot_key := fmt.Sprintf("pmroom:%s/%s/%s/%s", protocol, usMxId, usAccount, them)
map_key := &DbPmRoomMap{
MxUserID: usMxId,
Protocol: protocol,
AccountName: usAccount,
UserID: them,
}
slot_key := dbPmRoomSlotKey(map_key)
dbLockSlot(slot_key)
defer dbUnlockSlot(slot_key)
@ -248,12 +259,7 @@ func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMx
}
var room DbPmRoomMap
must_create := db.First(&room, DbPmRoomMap{
MxUserID: usMxId,
Protocol: protocol,
AccountName: usAccount,
UserID: them,
}).RecordNotFound()
must_create := db.First(&room, map_key).RecordNotFound()
if must_create {
name := fmt.Sprintf("%s (%s)", them, protocol)
@ -264,12 +270,6 @@ func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMx
return "", err
}
//err = mxRoomJoinAs(mx_room_id, themMxId)
//if err != nil {
// log.Printf("Could not join %s as %s", mx_room_id, themMxId)
// return "", err
//}
room = DbPmRoomMap{
MxUserID: usMxId,
Protocol: protocol,
@ -286,6 +286,16 @@ func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMx
return room.MxRoomID, nil
}
func dbDeletePmRoom(room *DbPmRoomMap) {
slot_key := dbPmRoomSlotKey(room)
dbLockSlot(slot_key)
defer dbUnlockSlot(slot_key)
db.Delete(room)
dbCache.Remove(slot_key)
}
func dbGetMxUser(protocol string, userId connector.UserID) (string, error) {
slot_key := fmt.Sprintf("user:%s/%s", protocol, userId)

View file

@ -151,18 +151,18 @@ func handleTxnEvent(e *mxlib.Event) error {
ms := e.Content["membership"].(string)
if ms == "leave" {
if pm_room := dbIsPmRoom(e.RoomId); pm_room != nil {
// If leaving a PM room, we must delete it
// If user leaves a PM room, we must drop it
dbDeletePmRoom(pm_room)
them_mx := userMxId(pm_room.Protocol, pm_room.UserID)
mx.RoomLeaveAs(e.RoomId, them_mx)
db.Delete(pm_room)
return nil
} else if room := dbIsPublicRoom(e.RoomId); room != nil {
// If leaving a public room, leave from server as well
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
if acct != nil {
acct.Conn.Leave(room.RoomID)
acct.delAutojoin(room.RoomID)
return nil
// TODO: manage autojoin list, remove this room
} else {
mx.RoomKick(e.RoomId, e.Sender, fmt.Sprintf("Not present in %s on %s, please talk with Easybridge to rejoin", room.RoomID, room.Protocol))
return fmt.Errorf("not joined %s on %s", room.RoomID, room.Protocol)