Skip to content

Commit b614077

Browse files
author
TeleGhost Dev
committed
Hotfix: Fix ChatID sync migration and real-time message updates
1 parent 7eb2a66 commit b614077

6 files changed

Lines changed: 52 additions & 13 deletions

File tree

app.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,12 @@ func (a *App) SetAppFocus(focused bool) {
420420
}
421421
}
422422

423-
// RequestProfileUpdate запрашивает обновление профиля у контакта
424-
func (a *App) RequestProfileUpdate(address string) error {
425-
if a.core != nil && a.core.Messenger != nil {
426-
return a.core.Messenger.SendProfileRequest(address)
423+
// RequestProfile запрашивает обновление профиля у контакта
424+
func (a *App) RequestProfile(address string) error {
425+
if a.core != nil {
426+
return a.core.RequestProfile(address)
427427
}
428-
return fmt.Errorf("messenger not initialized")
428+
return fmt.Errorf("core not initialized")
429429
}
430430

431431
// ExportReseed wraps AppCore.ExportReseed

frontend/src/App.svelte

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@
147147
// Более надежная проверка на принадлежность сообщения текущему чату
148148
const isCurrentChat = selectedContact && (
149149
msg.ChatID === selectedContact.ChatID ||
150+
msg.chat_id === selectedContact.ChatID ||
150151
msg.ChatID === selectedContact.ID ||
151152
msg.SenderID === selectedContact.PublicKey ||
152-
(msg.IsOutgoing && msg.ChatID === selectedContact.ChatID)
153+
msg.sender_id === selectedContact.PublicKey ||
154+
(msg.sender_addr && msg.sender_addr === selectedContact.I2PAddress) ||
155+
(msg.IsOutgoing && (msg.ChatID === selectedContact.ChatID || msg.chat_id === selectedContact.ChatID))
153156
);
154157
155158
if (isCurrentChat) {
@@ -185,7 +188,8 @@
185188
if (updated) {
186189
// Critical: Check if ChatID changed
187190
if (updated.ChatID !== selectedContact.ChatID) {
188-
console.log("[App] Selected contact updated (ChatID changed), reloading chat...");
191+
console.log("[App] Selected contact updated (ChatID changed), reloading chat from", selectedContact.ChatID, "to", updated.ChatID);
192+
// Update local reference immediately so new_message can match it while loadMessages is pending
189193
selectedContact = updated;
190194
await loadMessages(updated.ID);
191195
} else if (

internal/appcore/appcore.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ func (a *AppCore) formatProfileAvatarURL(userID, path string) string {
689689
}
690690

691691
// onProfileUpdate обрабатывает входящее обновление профиля от контакта
692-
func (a *AppCore) onProfileUpdate(senderPubKey, nickname, bio string, avatar []byte) {
692+
func (a *AppCore) onProfileUpdate(senderPubKey, nickname, bio string, avatar []byte, senderAddr string) {
693693
if a.Repo == nil {
694694
return
695695
}
@@ -701,7 +701,25 @@ func (a *AppCore) onProfileUpdate(senderPubKey, nickname, bio string, avatar []b
701701

702702
contact, _ := a.Repo.GetContactByPublicKey(a.Ctx, senderPubKey)
703703
if contact == nil {
704-
return
704+
// Try to find by address (important for b32-only contacts discovery)
705+
contact, _ = a.Repo.GetContactByAddress(a.Ctx, senderAddr)
706+
if contact != nil {
707+
oldChatID := contact.ChatID
708+
newChatID := identity.CalculateChatID(a.Identity.Keys.PublicKeyBase64, senderPubKey)
709+
710+
log.Printf("[AppCore] Discovered PublicKey via ProfileUpdate for %s (%s). Migrating ChatID: %s -> %s", contact.Nickname, senderAddr, oldChatID, newChatID)
711+
712+
contact.PublicKey = senderPubKey
713+
contact.ChatID = newChatID
714+
contact.UpdatedAt = time.Now()
715+
716+
if err := a.Repo.UpdateContactAndMigrateChatID(a.Ctx, contact, oldChatID, newChatID); err != nil {
717+
log.Printf("[AppCore] Failed to migrate ChatID via ProfileUpdate for %s: %v", contact.Nickname, err)
718+
}
719+
} else {
720+
// Not a contact we know, just return
721+
return
722+
}
705723
}
706724

707725
contact.Nickname = nickname
@@ -752,6 +770,14 @@ func (a *AppCore) onProfileRequest(requestorPubKey string) {
752770
}
753771
}
754772

773+
// RequestProfile запрашивает обновление профиля у контакта
774+
func (a *AppCore) RequestProfile(address string) error {
775+
if a.Messenger == nil {
776+
return fmt.Errorf("messenger not initialized")
777+
}
778+
return a.Messenger.SendProfileRequest(address)
779+
}
780+
755781
// ─── Обработчики входящих сообщений ─────────────────────────────────────────
756782

757783
// OnMessageReceived — обработчик входящих сообщений.
@@ -761,6 +787,7 @@ func (a *AppCore) OnMessageReceived(msg *core.Message, senderPubKey, senderAddr
761787
return
762788
}
763789

790+
msg.SenderAddr = senderAddr
764791
var contact *core.Contact
765792
contact, _ = a.Repo.GetContactByPublicKey(a.Ctx, senderPubKey)
766793
if contact == nil {

internal/core/models.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ type Message struct {
141141
// SenderID — ID отправителя (User.ID или Contact.ID)
142142
SenderID string `json:"sender_id" db:"sender_id"`
143143

144+
// SenderAddr — I2P адрес отправителя (для сопоставления до того как узнаем PubKey)
145+
SenderAddr string `json:"sender_addr,omitempty" db:"-"`
146+
144147
// Content — содержимое сообщения (расшифрованное)
145148
Content string `json:"content" db:"content"`
146149

internal/network/messenger/messenger.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type MessageHandler func(msg *core.Message, senderPubKey, senderAddr string)
4949
type ContactRequestHandler func(senderPubKey, nickname, i2pAddress string)
5050

5151
// ProfileUpdateHandler обработчик обновлений профиля
52-
type ProfileUpdateHandler func(senderPubKey, nickname, bio string, avatar []byte)
52+
type ProfileUpdateHandler func(senderPubKey, nickname, bio string, avatar []byte, senderAddr string)
5353

5454
// Service — мессенджер сервис
5555
type Service struct {
@@ -535,7 +535,7 @@ func (s *Service) handlePacket(packet *pb.Packet, remoteAddr string) {
535535
s.handleTextMessage(packet, senderPubKey, remoteAddr)
536536

537537
case pb.PacketType_PROFILE_UPDATE:
538-
s.handleProfileUpdate(packet, senderPubKey)
538+
s.handleProfileUpdate(packet, senderPubKey, remoteAddr)
539539

540540
case pb.PacketType_HANDSHAKE:
541541
s.handleHandshake(packet, senderPubKey)
@@ -735,7 +735,7 @@ func (s *Service) handleTextMessage(packet *pb.Packet, senderPubKey, remoteAddr
735735
}
736736

737737
// handleProfileUpdate обрабатывает обновление профиля
738-
func (s *Service) handleProfileUpdate(packet *pb.Packet, senderPubKey string) {
738+
func (s *Service) handleProfileUpdate(packet *pb.Packet, senderPubKey, senderAddr string) {
739739
profileUpdate := &pb.ProfileUpdate{}
740740
if err := proto.Unmarshal(packet.Payload, profileUpdate); err != nil {
741741
log.Printf("[Messenger] Failed to unmarshal ProfileUpdate: %v", err)
@@ -744,7 +744,7 @@ func (s *Service) handleProfileUpdate(packet *pb.Packet, senderPubKey string) {
744744

745745
log.Printf("[Messenger] Profile update from %s: %s", senderPubKey[:min(16, len(senderPubKey))], profileUpdate.Nickname)
746746
if s.profileHandler != nil {
747-
s.profileHandler(senderPubKey, profileUpdate.Nickname, profileUpdate.Bio, profileUpdate.Avatar)
747+
s.profileHandler(senderPubKey, profileUpdate.Nickname, profileUpdate.Bio, profileUpdate.Avatar, senderAddr)
748748
}
749749
}
750750

mobile/mobile.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ func dispatch(app *appcore.AppCore, method string, args []json.RawMessage) (inte
479479
parseArgs(args, &id)
480480
return nil, app.DeleteContact(id)
481481

482+
case "RequestProfile":
483+
var address string
484+
parseArgs(args, &address)
485+
return nil, app.RequestProfile(address)
486+
482487
// === Messages ===
483488
case "SendText":
484489
var contactID, text, replyToID string

0 commit comments

Comments
 (0)