summaryrefslogtreecommitdiff
path: root/internal/client/ui
diff options
context:
space:
mode:
authorKyren223 <Kyren223@proton.me>2025-05-24 17:50:44 +0300
committerKyren223 <Kyren223@proton.me>2025-05-24 17:50:44 +0300
commit3e0210fd139cd1dcf2aa77722a41cab192b15f84 (patch)
tree0c1a7b3152a8121ec3f53559e187a9125d4f8bb9 /internal/client/ui
parentd10f09fa851c1916a7f9fec5de5cf059600e55b1 (diff)
Chat properly handles and displays the "new message separator"
Diffstat (limited to 'internal/client/ui')
-rw-r--r--internal/client/ui/core/chat/chat.go126
-rw-r--r--internal/client/ui/core/core.go1
2 files changed, 62 insertions, 65 deletions
diff --git a/internal/client/ui/core/chat/chat.go b/internal/client/ui/core/chat/chat.go
index 0d0f113..afc2c6a 100644
--- a/internal/client/ui/core/chat/chat.go
+++ b/internal/client/ui/core/chat/chat.go
@@ -125,8 +125,7 @@ type Model struct {
selectedMessage *data.Message
editingMessage *data.Message
- outdatedLastReadMsg *snowflake.ID
- keepPreviousLastRead bool
+ outdatedLastReadMsg *snowflake.ID
messagesHeight int
maxMessagesHeight int
@@ -143,27 +142,26 @@ func New() Model {
vi := viminput.New()
return Model{
- vi: vi,
- focus: false,
- locked: false,
- hasReadAccess: false,
- hasWriteAccess: false,
- networkIndex: -1,
- receiverIndex: -1,
- frequencyIndex: -1,
- base: SnapToBottom,
- index: Unselected,
- selectedMessage: nil,
- editingMessage: nil,
- outdatedLastReadMsg: nil,
- keepPreviousLastRead: false,
- messagesHeight: 0,
- maxMessagesHeight: -1,
- messagesCache: nil,
- prerender: "",
- width: -1,
- style: blurStyle(),
- borderStyle: ViBlurredBorder(),
+ vi: vi,
+ focus: false,
+ locked: false,
+ hasReadAccess: false,
+ hasWriteAccess: false,
+ networkIndex: -1,
+ receiverIndex: -1,
+ frequencyIndex: -1,
+ base: SnapToBottom,
+ index: Unselected,
+ selectedMessage: nil,
+ editingMessage: nil,
+ outdatedLastReadMsg: nil,
+ messagesHeight: 0,
+ maxMessagesHeight: -1,
+ messagesCache: nil,
+ prerender: "",
+ width: -1,
+ style: blurStyle(),
+ borderStyle: ViBlurredBorder(),
}
}
@@ -216,30 +214,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}
lastMsg := state.GetLastMessage(frequency.ID)
- if lastMsg != nil {
- if m.base == SnapToBottom && lastMsg != nil {
- // lastReadMsg := state.State.LastReadMessages[frequency.ID]
- // if m.keepPreviousLastRead && m.previousLastReadMsg != nil && lastReadMsg != nil &&
- // *lastReadMsg != *lastMsg && *lastReadMsg != *m.previousLastReadMsg {
- // m.keepPreviousLastRead = false
- // }
-
- state.State.LastReadMessages[frequency.ID] = lastMsg
- delete(state.State.RemoteNotifications, frequency.ID)
- } else if m.base == SnapToBottom {
- assert.Never("lastMsg is nil")
- }
- if m.base != SnapToBottom {
- // FIXME:
- // assert.Assert(
- // m.keepPreviousLastRead, "debug reached m.keep",
- // "m.base", m.base,
- // "lastMsg", lastMsg,
- // "lastReadMessage", state.State.LastReadMessages[frequency.ID],
- // "m.previousLastReadMessage", m.previousLastReadMsg,
- // )
- m.keepPreviousLastRead = false
- }
+ if lastMsg == nil {
+ m.outdatedLastReadMsg = nil
+ } else if m.base == SnapToBottom {
+ state.State.LastReadMessages[frequency.ID] = lastMsg
+ delete(state.State.RemoteNotifications, frequency.ID)
}
m.hasReadAccess = frequency.Perms != packet.PermNoAccess || member.IsAdmin
@@ -281,16 +260,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
}
lastMsg := state.GetLastMessage(receiverId)
- if m.base != SnapToBottom {
- m.keepPreviousLastRead = false
- } else if lastMsg != nil {
- lastReadMsg := state.State.LastReadMessages[receiverId]
- if m.keepPreviousLastRead && m.outdatedLastReadMsg != nil && lastReadMsg != nil &&
- *lastReadMsg != *lastMsg && *lastReadMsg != *m.outdatedLastReadMsg {
- m.keepPreviousLastRead = false
- }
-
+ if lastMsg == nil {
+ m.outdatedLastReadMsg = nil
+ } else if m.base == SnapToBottom {
state.State.LastReadMessages[receiverId] = lastMsg
+ delete(state.State.RemoteNotifications, receiverId)
}
m.hasReadAccess = true
@@ -836,7 +810,6 @@ func (m *Model) ResetBeforeSwitch() {
m.SetIndex(Unselected)
m.maxMessagesHeight = -1
m.outdatedLastReadMsg = nil
- m.keepPreviousLastRead = false
}()
networkId := state.NetworkId(m.networkIndex)
@@ -873,10 +846,6 @@ func (m *Model) RestoreAfterSwitch() tea.Cmd {
log.Println("Restoring frequency:", frequency.ID)
m.outdatedLastReadMsg = state.State.LastReadMessages[frequency.ID]
- lastMsg := state.GetLastMessage(frequency.ID)
- if m.outdatedLastReadMsg != nil && lastMsg != nil && *m.outdatedLastReadMsg != *lastMsg {
- m.keepPreviousLastRead = true
- }
if val, ok := msgs[frequency.ID]; ok {
m.vi.SetString(val.IncompleteMessage)
@@ -898,10 +867,6 @@ func (m *Model) RestoreAfterSwitch() tea.Cmd {
log.Println("Restoring signal:", receiverId)
m.outdatedLastReadMsg = state.State.LastReadMessages[receiverId]
- lastMsg := state.GetLastMessage(receiverId)
- if m.outdatedLastReadMsg != nil && lastMsg != nil && *m.outdatedLastReadMsg != *lastMsg {
- m.keepPreviousLastRead = true
- }
if val, ok := msgs[receiverId]; ok {
m.vi.SetString(val.IncompleteMessage)
@@ -1657,3 +1622,34 @@ func (m *Model) NewMsgSep(height, remainingHeight int) string {
return newMsgStyle.Render(line + NewText)
}
+
+func (m *Model) OnNewMessagesReceived(info *packet.MessagesInfo) {
+ var chatId *snowflake.ID = nil
+ if m.receiverIndex != -1 {
+ chatId = &state.Data.Signals[m.receiverIndex]
+ } else {
+ networkId := state.NetworkId(m.networkIndex)
+ if m.frequencyIndex != -1 && networkId != nil {
+ frequencies := state.State.Frequencies[*networkId]
+ chatId = &frequencies[m.frequencyIndex].ID
+ }
+ }
+ if chatId == nil {
+ return
+ }
+
+ for _, msg := range info.Messages {
+ if msg.SenderID == *state.UserID {
+ m.outdatedLastReadMsg = &msg.ID
+ return
+ }
+
+ if m.base == SnapToBottom {
+ lastMsg := state.GetLastMessage(*chatId)
+ if lastMsg != nil && m.outdatedLastReadMsg != nil &&
+ *lastMsg == *m.outdatedLastReadMsg {
+ m.outdatedLastReadMsg = &msg.ID
+ }
+ }
+ }
+}
diff --git a/internal/client/ui/core/core.go b/internal/client/ui/core/core.go
index fd91026..79c8c4f 100644
--- a/internal/client/ui/core/core.go
+++ b/internal/client/ui/core/core.go
@@ -328,6 +328,7 @@ func (m *Model) updateConnected(message tea.Msg) tea.Cmd {
}
case *packet.MessagesInfo:
+ m.chat.OnNewMessagesReceived(msg) // MUST BE BEFORE STATE UPDATE
state.UpdateMessages(msg)
case *packet.TrustInfo: