From e94ea17dad32329c0a8e8f0b6b019da68f61b5f4 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 9 Jul 2023 17:26:34 +0300 Subject: [PATCH] Create portals for recent chats automatically --- ROADMAP.md | 4 ++-- user.go | 27 ++++++++++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 6916727..7620990 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -16,7 +16,7 @@ * [ ] Typing notifications * [ ] Read receipts * Misc - * [ ] Automatic portal creation - * [ ] After login + * [x] Automatic portal creation + * [x] After login * [x] When receiving message * [ ] Private chat creation by inviting Matrix ghost of remote user to new room diff --git a/user.go b/user.go index ba0ce47..2902f7f 100644 --- a/user.go +++ b/user.go @@ -541,20 +541,17 @@ func (user *User) HandleEvent(event interface{}) { user.zlog.Err(err).Msg("Failed to update session in database") } case *binary.Conversation: - portal := user.GetPortalByID(v.GetConversationID()) - if portal.MXID != "" { - portal.UpdateMetadata(user, v) - } else { - err := portal.CreateMatrixRoom(user, v) - if err != nil { - user.zlog.Err(err).Msg("Error creating Matrix room from conversation event") - } - } + user.syncConversation(v) case *binary.Message: portal := user.GetPortalByID(v.GetConversationID()) portal.messages <- PortalMessage{evt: v, source: user} case *events.ClientReady: user.zlog.Trace().Any("data", v).Msg("Client is ready!") + go func() { + for _, conv := range v.Conversations { + user.syncConversation(conv) + } + }() case *events.BrowserActive: user.zlog.Trace().Any("data", v).Msg("Browser active") default: @@ -562,6 +559,18 @@ func (user *User) HandleEvent(event interface{}) { } } +func (user *User) syncConversation(v *binary.Conversation) { + portal := user.GetPortalByID(v.GetConversationID()) + if portal.MXID != "" { + portal.UpdateMetadata(user, v) + } else { + err := portal.CreateMatrixRoom(user, v) + if err != nil { + user.zlog.Err(err).Msg("Error creating Matrix room from conversation event") + } + } +} + func (user *User) updateChatMute(portal *Portal, mutedUntil time.Time) { intent := user.DoublePuppetIntent if intent == nil || len(portal.MXID) == 0 {