Handle users leaving from rooms

This commit is contained in:
Alex 2020-02-17 15:30:01 +01:00
parent 51f3c45e05
commit c740f76826
2 changed files with 39 additions and 10 deletions

View file

@ -182,3 +182,21 @@ func dbGetMxUser(protocol string, userId connector.UserID) (string, error) {
return user.MxUserID, nil return user.MxUserID, nil
} }
func dbIsPmRoom(mxRoomId string) *DbPmRoomMap {
var pm_room DbPmRoomMap
if db.First(&pm_room, DbPmRoomMap{MxRoomID: mxRoomId}).RecordNotFound() {
return nil
} else {
return &pm_room
}
}
func dbIsPublicRoom(mxRoomId string) *DbRoomMap {
var room DbRoomMap
if db.First(&room, DbRoomMap{MxRoomID: mxRoomId}).RecordNotFound() {
return nil
} else {
return &room
}
}

View file

@ -107,11 +107,7 @@ func handleTxnEvent(e *mxlib.Event) {
} }
// Look up if this is a private message room // Look up if this is a private message room
var pm_room DbPmRoomMap if pm_room := dbIsPmRoom(e.RoomId); pm_room != nil {
is_pm_room := !db.First(&pm_room, DbPmRoomMap{
MxRoomID: e.RoomId,
}).RecordNotFound()
if is_pm_room {
acct := FindAccount(pm_room.MxUserID, pm_room.AccountName) acct := FindAccount(pm_room.MxUserID, pm_room.AccountName)
if acct != nil && e.Sender == pm_room.MxUserID { if acct != nil && e.Sender == pm_room.MxUserID {
ev.Author = acct.Conn.User() ev.Author = acct.Conn.User()
@ -121,11 +117,7 @@ func handleTxnEvent(e *mxlib.Event) {
} }
// Look up if this is a regular room // Look up if this is a regular room
var room DbRoomMap if room := dbIsPublicRoom(e.RoomId); room != nil {
is_room := !db.First(&room, DbRoomMap{
MxRoomID: e.RoomId,
}).RecordNotFound()
if is_room {
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID) acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
if acct != nil { if acct != nil {
ev.Author = acct.Conn.User() ev.Author = acct.Conn.User()
@ -133,5 +125,24 @@ func handleTxnEvent(e *mxlib.Event) {
acct.Conn.Send(ev) acct.Conn.Send(ev)
} }
} }
} else if e.Type == "m.room.member" {
ms := e.Content["membership"].(string)
if ms == "leave" {
// If leaving a PM room, we must delete it
if pm_room := dbIsPmRoom(e.RoomId); pm_room != nil {
them_mx := userMxId(pm_room.Protocol, pm_room.UserID)
mxRoomLeaveAs(e.RoomId, them_mx)
db.Delete(pm_room)
}
// If leaving a public room, leave from server as well
if room := dbIsPublicRoom(e.RoomId); room != nil {
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
if acct != nil {
acct.Conn.Leave(room.RoomID)
// TODO: manage autojoin list, remove this room
}
}
}
} }
} }