better for leave room with command 'logout'

This commit is contained in:
zhaoYangguang 2021-02-20 14:54:00 +08:00
parent 403e28baa1
commit c2a347096b
4 changed files with 28 additions and 33 deletions

View File

@ -251,7 +251,7 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
if ce.User.Conn == nil { if ce.User.Conn == nil {
return return
} }
_ = ce.User.Conn.GetConversations("", ce.User.bridge.Config.Bridge.InitialChatSync) //_ = ce.User.Conn.GetConversations("", ce.User.bridge.Config.Bridge.InitialChatSync)
ce.User.Conn.LoggedIn = false ce.User.Conn.LoggedIn = false
username := "" username := ""
password := "" password := ""
@ -272,19 +272,19 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
} }
portals := ce.User.GetPortals() portals := ce.User.GetPortals()
newPortals := ce.User.GetPortalsNew() //newPortals := ce.User.GetPortalsNew()
allPortals := newPortals[0:] //allPortals := newPortals[0:]
for _, portal := range portals { //for _, portal := range portals {
var newPortalsHas bool // var newPortalsHas bool
for _, newPortal := range newPortals { // for _, newPortal := range newPortals {
if portal.Key == newPortal.Key { // if portal.Key == newPortal.Key {
newPortalsHas = true // newPortalsHas = true
} // }
} // }
if !newPortalsHas { // if !newPortalsHas {
allPortals = append(allPortals, portal) // allPortals = append(allPortals, portal)
} // }
} //}
leave := func(portal *Portal) { leave := func(portal *Portal) {
if len(portal.MXID) > 0 { if len(portal.MXID) > 0 {
@ -294,7 +294,7 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
}) })
} }
} }
for _, portal := range allPortals { for _, portal := range portals {
leave(portal) leave(portal)
} }
ce.User.Conn.Store = &skype.Store{ ce.User.Conn.Store = &skype.Store{

View File

@ -183,6 +183,17 @@ func (user *User) SetPortalKeys(newKeys []PortalKeyWithMeta) error {
return tx.Commit() return tx.Commit()
} }
func (user *User) CreateUserPortal(newKey PortalKeyWithMeta) {
user.log.Debugfln("Creating new portal %s for %s", newKey.PortalKey.JID, newKey.PortalKey.Receiver)
_, err := user.db.Exec(`INSERT INTO user_portal (user_jid, portal_jid, portal_receiver, in_community) VALUES ($1, $2, $3, $4)`,
user.jidPtr(),
newKey.PortalKey.JID, newKey.PortalKey.Receiver,
newKey.InCommunity)
if err != nil {
user.log.Warnfln("Failed to insert %s: %v", user.MXID, err)
}
}
func (user *User) IsInPortal(key PortalKey) bool { func (user *User) IsInPortal(key PortalKey) bool {
row := user.db.QueryRow(`SELECT EXISTS(SELECT 1 FROM user_portal WHERE user_jid=$1 AND portal_jid=$2 AND portal_receiver=$3)`, user.jidPtr(), &key.JID, &key.Receiver) row := user.db.QueryRow(`SELECT EXISTS(SELECT 1 FROM user_portal WHERE user_jid=$1 AND portal_jid=$2 AND portal_receiver=$3)`, user.jidPtr(), &key.JID, &key.Receiver)
var exists bool var exists bool

View File

@ -1237,7 +1237,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error {
_ = customPuppet.CustomIntent().EnsureJoined(portal.MXID) _ = customPuppet.CustomIntent().EnsureJoined(portal.MXID)
} }
} }
user.addPortalToCommunity(portal) inCommunity := user.addPortalToCommunity(portal)
if portal.IsPrivateChat() { if portal.IsPrivateChat() {
puppet := user.bridge.GetPuppetByJID(portal.Key.JID) puppet := user.bridge.GetPuppetByJID(portal.Key.JID)
user.addPuppetToCommunity(puppet) user.addPuppetToCommunity(puppet)
@ -1249,6 +1249,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error {
} }
} }
} }
user.CreateUserPortal(database.PortalKeyWithMeta{PortalKey: portal.Key, InCommunity: inCommunity})
err = portal.FillInitialHistory(user) err = portal.FillInitialHistory(user)
if err != nil { if err != nil {
portal.log.Errorln("Failed to fill history:", err) portal.log.Errorln("Failed to fill history:", err)

17
user.go
View File

@ -463,23 +463,6 @@ func (user *User) intPostLogin() {
} }
} }
func (user *User) HandleChatList(chats []skype.Conversation) {
user.log.Infoln("Chat list received")
chatMap := make(map[string]skype.Conversation)
//for _, chat := range user.Conn.Store.Chats {
// chatMap[chat.Jid] = chat
//}
for _, chat := range chats {
cid, _ := chat.Id.(string)
chatMap[cid] = chat
}
select {
case user.chatListReceived <- struct{}{}:
default:
}
go user.syncPortals(chatMap, false)
}
func (user *User) syncPortals(chatMap map[string]skype.Conversation, createAll bool) { func (user *User) syncPortals(chatMap map[string]skype.Conversation, createAll bool) {
if chatMap == nil { if chatMap == nil {
chatMap = user.Conn.Store.Chats chatMap = user.Conn.Store.Chats