Handle matrix left events correctly (hopefully)
This commit is contained in:
parent
d03091dd01
commit
5398e9fc0f
3 changed files with 39 additions and 25 deletions
22
account.go
22
account.go
|
@ -205,6 +205,15 @@ func (a *Account) addAutojoin(roomId RoomID) {
|
||||||
}).FirstOrCreate(&entry)
|
}).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 ----
|
// ---- Begin event handlers ----
|
||||||
|
|
||||||
func (a *Account) Joined(roomId RoomID) {
|
func (a *Account) Joined(roomId RoomID) {
|
||||||
|
@ -212,13 +221,13 @@ func (a *Account) Joined(roomId RoomID) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ezbrMessagef("Dropping Account.Joined %s: %s", roomId, err.Error())
|
a.ezbrMessagef("Dropping Account.Joined %s: %s", roomId, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
a.addAutojoin(roomId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Account) joinedInternal(roomId RoomID) error {
|
func (a *Account) joinedInternal(roomId RoomID) error {
|
||||||
a.JoinedRooms[roomId] = true
|
a.JoinedRooms[roomId] = true
|
||||||
|
|
||||||
|
a.addAutojoin(roomId)
|
||||||
|
|
||||||
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
|
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -240,18 +249,13 @@ func (a *Account) Left(roomId RoomID) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.ezbrMessagef("Dropping Account.Left %s: %s", roomId, err.Error())
|
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 {
|
func (a *Account) leftInternal(roomId RoomID) error {
|
||||||
delete(a.JoinedRooms, roomId)
|
delete(a.JoinedRooms, roomId)
|
||||||
|
|
||||||
|
a.delAutojoin(roomId)
|
||||||
|
|
||||||
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
|
mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
36
db.go
36
db.go
|
@ -237,8 +237,19 @@ func dbGetMxRoom(protocol string, roomId connector.RoomID) (string, error) {
|
||||||
return room.MxRoomID, nil
|
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) {
|
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)
|
dbLockSlot(slot_key)
|
||||||
defer dbUnlockSlot(slot_key)
|
defer dbUnlockSlot(slot_key)
|
||||||
|
@ -248,12 +259,7 @@ func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMx
|
||||||
}
|
}
|
||||||
|
|
||||||
var room DbPmRoomMap
|
var room DbPmRoomMap
|
||||||
must_create := db.First(&room, DbPmRoomMap{
|
must_create := db.First(&room, map_key).RecordNotFound()
|
||||||
MxUserID: usMxId,
|
|
||||||
Protocol: protocol,
|
|
||||||
AccountName: usAccount,
|
|
||||||
UserID: them,
|
|
||||||
}).RecordNotFound()
|
|
||||||
|
|
||||||
if must_create {
|
if must_create {
|
||||||
name := fmt.Sprintf("%s (%s)", them, protocol)
|
name := fmt.Sprintf("%s (%s)", them, protocol)
|
||||||
|
@ -264,12 +270,6 @@ func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMx
|
||||||
return "", err
|
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{
|
room = DbPmRoomMap{
|
||||||
MxUserID: usMxId,
|
MxUserID: usMxId,
|
||||||
Protocol: protocol,
|
Protocol: protocol,
|
||||||
|
@ -286,6 +286,16 @@ func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMx
|
||||||
return room.MxRoomID, nil
|
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) {
|
func dbGetMxUser(protocol string, userId connector.UserID) (string, error) {
|
||||||
slot_key := fmt.Sprintf("user:%s/%s", protocol, userId)
|
slot_key := fmt.Sprintf("user:%s/%s", protocol, userId)
|
||||||
|
|
||||||
|
|
|
@ -151,18 +151,18 @@ func handleTxnEvent(e *mxlib.Event) error {
|
||||||
ms := e.Content["membership"].(string)
|
ms := e.Content["membership"].(string)
|
||||||
if ms == "leave" {
|
if ms == "leave" {
|
||||||
if pm_room := dbIsPmRoom(e.RoomId); pm_room != nil {
|
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)
|
them_mx := userMxId(pm_room.Protocol, pm_room.UserID)
|
||||||
mx.RoomLeaveAs(e.RoomId, them_mx)
|
mx.RoomLeaveAs(e.RoomId, them_mx)
|
||||||
db.Delete(pm_room)
|
|
||||||
return nil
|
return nil
|
||||||
} else if room := dbIsPublicRoom(e.RoomId); room != nil {
|
} else if room := dbIsPublicRoom(e.RoomId); room != nil {
|
||||||
// If leaving a public room, leave from server as well
|
// If leaving a public room, leave from server as well
|
||||||
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
|
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
|
||||||
if acct != nil {
|
if acct != nil {
|
||||||
acct.Conn.Leave(room.RoomID)
|
acct.Conn.Leave(room.RoomID)
|
||||||
|
acct.delAutojoin(room.RoomID)
|
||||||
return nil
|
return nil
|
||||||
// TODO: manage autojoin list, remove this room
|
|
||||||
} else {
|
} 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))
|
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)
|
return fmt.Errorf("not joined %s on %s", room.RoomID, room.Protocol)
|
||||||
|
|
Loading…
Reference in a new issue