From c740f76826c0291005111c8554c256e8f491e7b9 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 17 Feb 2020 15:30:01 +0100 Subject: Handle users leaving from rooms --- appservice/db.go | 18 ++++++++++++++++++ appservice/server.go | 31 +++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/appservice/db.go b/appservice/db.go index cbd1d11..e5b2d02 100644 --- a/appservice/db.go +++ b/appservice/db.go @@ -182,3 +182,21 @@ func dbGetMxUser(protocol string, userId connector.UserID) (string, error) { 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 + } +} diff --git a/appservice/server.go b/appservice/server.go index 79a8f1a..00f23f1 100644 --- a/appservice/server.go +++ b/appservice/server.go @@ -107,11 +107,7 @@ func handleTxnEvent(e *mxlib.Event) { } // Look up if this is a private message room - var pm_room DbPmRoomMap - is_pm_room := !db.First(&pm_room, DbPmRoomMap{ - MxRoomID: e.RoomId, - }).RecordNotFound() - if is_pm_room { + if pm_room := dbIsPmRoom(e.RoomId); pm_room != nil { acct := FindAccount(pm_room.MxUserID, pm_room.AccountName) if acct != nil && e.Sender == pm_room.MxUserID { ev.Author = acct.Conn.User() @@ -121,11 +117,7 @@ func handleTxnEvent(e *mxlib.Event) { } // Look up if this is a regular room - var room DbRoomMap - is_room := !db.First(&room, DbRoomMap{ - MxRoomID: e.RoomId, - }).RecordNotFound() - if is_room { + if room := dbIsPublicRoom(e.RoomId); room != nil { acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID) if acct != nil { ev.Author = acct.Conn.User() @@ -133,5 +125,24 @@ func handleTxnEvent(e *mxlib.Event) { 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 + } + } + } } } -- cgit v1.2.3