From ffec186b0c56e1c3425c6c6b647611a7b10ebc5c Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:17:12 +0100 Subject: [PATCH 1/3] feat(tui): show read checks on sent messages One check when sent, two once the other side has read it, updated live. Co-Authored-By: Claude Opus 5.5 --- internal/cli/cli.go | 3 +++ internal/core/core.go | 6 ++++++ internal/demo/demo.go | 18 +++++++++++++++--- internal/mtp/backend.go | 14 +++++++++++++- internal/mtp/client.go | 8 ++++++++ internal/tui/tui.go | 13 +++++++++++-- internal/tui/tui_test.go | 18 ++++++++++++++++++ internal/tui/view.go | 26 ++++++++++++++++++++++++-- 8 files changed, 98 insertions(+), 8 deletions(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index b5bf2bc..d0ab18a 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -716,6 +716,9 @@ func (a *app) watch(ctx context.Context, be core.Backend, opts watchOptions) (wa } return } + if ev.Kind == core.EventRead { + return // watch reports messages only + } mu.Lock() defer mu.Unlock() if catchingUp { diff --git a/internal/core/core.go b/internal/core/core.go index e0da985..5e02ad4 100644 --- a/internal/core/core.go +++ b/internal/core/core.go @@ -35,6 +35,9 @@ type Chat struct { Last *Message `json:"last,omitempty"` // PhotoID identifies the current profile photo, 0 when there is none. PhotoID int64 `json:"-"` + // ReadOutbox is the newest message sent by the account that the other + // side has read, by anyone in a group. Older ones are read too. + ReadOutbox int `json:"-"` } // Message is one message of a chat. Media and service messages carry a short @@ -175,6 +178,9 @@ const ( // EventReady is sent once, when the subscription is live: every message // from then on is reported. It carries no chat or message. EventReady EventKind = "ready" + // EventRead tells that the other side read the account's messages up to + // Chat.ReadOutbox. It carries no message. + EventRead EventKind = "read" ) // Event is a live update. Chat describes the chat the message belongs to, so diff --git a/internal/demo/demo.go b/internal/demo/demo.go index 67e3e81..b7ec0dc 100644 --- a/internal/demo/demo.go +++ b/internal/demo/demo.go @@ -153,6 +153,13 @@ func New() *Backend { msgs := b.messages[chat.ID] msgs[len(msgs)-1].Buttons = keyboards[chat.ID] chat.Last = &msgs[len(msgs)-1] + // They read everything up to their last answer, so a message of + // the account after it shows as not read yet. + for _, m := range msgs { + if !m.Out { + chat.ReadOutbox = m.ID + } + } b.chats = append(b.chats, chat) } // Pinned chats first, then most recent first, like the real chat list. @@ -246,11 +253,16 @@ func (b *Backend) Send(_ context.Context, chatID int64, req core.SendRequest) (c time.AfterFunc(time.Second, func() { b.mu.Lock() b.messages[chatID] = append(b.messages[chatID], reply) + chat, _ := b.chat(chatID) // b.chats may have moved since + chat.ReadOutbox = m.ID // read before answering + read := core.Event{Kind: core.EventRead, Chat: *chat} ev := core.Event{Kind: core.EventMessage, Chat: *chat, Message: reply} b.mu.Unlock() - select { - case b.events <- ev: - default: + for _, ev := range []core.Event{read, ev} { + select { + case b.events <- ev: + default: + } } }) } diff --git a/internal/mtp/backend.go b/internal/mtp/backend.go index e3ef49f..5820156 100644 --- a/internal/mtp/backend.go +++ b/internal/mtp/backend.go @@ -105,7 +105,7 @@ func (b *backend) Chats(ctx context.Context, q core.ChatQuery) ([]core.Chat, err if !known { chat.Title = "Unavailable chat" // banned from it, or it was deleted } - chat.Unread, chat.Pinned = d.UnreadCount, d.Pinned + chat.Unread, chat.Pinned, chat.ReadOutbox = d.UnreadCount, d.Pinned, d.ReadOutboxMaxID if m, ok := last[key{id, d.TopMessage}]; ok { chat.Last = &m req.OffsetID, req.OffsetDate = m.ID, int(m.Date.Unix()) @@ -645,6 +645,18 @@ func (b *backend) emit(kind core.EventKind, e tg.Entities, raw tg.MessageClass) (*fn)(core.Event{Kind: kind, Chat: chat, Message: msg}) } +// emitRead forwards that the other side of a chat read the account's +// messages up to maxID. +func (b *backend) emitRead(chatID int64, maxID int) { + fn := b.onEvent.Load() + if fn == nil { + return + } + chat, _ := b.peers.chat(chatID) + chat.ID, chat.ReadOutbox = chatID, maxID + (*fn)(core.Event{Kind: core.EventRead, Chat: chat}) +} + func newSender(api *tg.Client) *message.Sender { return message.NewSender(api).WithUploader(uploader.NewUploader(api)) } diff --git a/internal/mtp/client.go b/internal/mtp/client.go index 291a41c..c8df592 100644 --- a/internal/mtp/client.go +++ b/internal/mtp/client.go @@ -145,6 +145,14 @@ func Run(ctx context.Context, opts Options, fn func(context.Context, core.Backen b.emit(core.EventEdit, e, msg) return nil }) + dispatcher.OnReadHistoryOutbox(func(_ context.Context, _ tg.Entities, u *tg.UpdateReadHistoryOutbox) error { + b.emitRead(chatIDOf(u.Peer), u.MaxID) + return nil + }) + dispatcher.OnReadChannelOutbox(func(_ context.Context, _ tg.Entities, u *tg.UpdateReadChannelOutbox) error { + b.emitRead(channelChatID(u.ChannelID), u.MaxID) + return nil + }) err = fn(ctx, b) if saveErr := b.peers.save(); err == nil { diff --git a/internal/tui/tui.go b/internal/tui/tui.go index 0e9db29..494a092 100644 --- a/internal/tui/tui.go +++ b/internal/tui/tui.go @@ -435,7 +435,10 @@ func (m *model) update(msg tea.Msg) tea.Cmd { } before := len(m.chats) for _, c := range msg.chats { - if m.chat(c.ID) == nil { // pages can overlap when chats move + if have := m.chat(c.ID); have != nil { // pages can overlap when chats move + // A chat a live message added first lacks what was read. + have.ReadOutbox = max(have.ReadOutbox, c.ReadOutbox) + } else { m.chats = append(m.chats, c) } } @@ -551,7 +554,13 @@ func (m *model) update(msg tea.Msg) tea.Cmd { case eventMsg: ev := core.Event(msg) - if ev.Kind == core.EventReady { + switch ev.Kind { + case core.EventReady: + return nil + case core.EventRead: + if c := m.chat(ev.Chat.ID); c != nil { + c.ReadOutbox = max(c.ReadOutbox, ev.Chat.ReadOutbox) + } return nil } m.addMessage(ev.Message, &ev.Chat, ev.Kind == core.EventEdit) diff --git a/internal/tui/tui_test.go b/internal/tui/tui_test.go index 72414c8..63b0446 100644 --- a/internal/tui/tui_test.go +++ b/internal/tui/tui_test.go @@ -1124,3 +1124,21 @@ func TestClickPressesBotButtons(t *testing.T) { t.Fatalf("an answer to an earlier press replaced the note with %q", m.note) } } + +// A message of the account shows one check until the other side reads it. +func TestReadReceiptTurnsIntoTwoChecks(t *testing.T) { + m := loaded(t, 100, 30) + open(t, m, slices.IndexFunc(m.visible(), func(c *core.Chat) bool { return c.Title == "Dmitri Volkov" })) + last := func() string { + lines := m.messageLines(m.histories[m.openID], m.conversationWidth()) + return ansi.Strip(lines[len(lines)-1].text) + } + if got := last(); !strings.HasSuffix(got, " ✓ ") { + t.Fatalf("unread message %q, want one check", got) + } + h := m.histories[m.openID] + m.Update(eventMsg(core.Event{Kind: core.EventRead, Chat: core.Chat{ID: m.openID, ReadOutbox: h.msgs[len(h.msgs)-1].ID}})) + if got := last(); !strings.HasSuffix(got, " ✓✓ ") { + t.Fatalf("read message %q, want two checks", got) + } +} diff --git a/internal/tui/view.go b/internal/tui/view.go index 4107853..cd7ac9a 100644 --- a/internal/tui/view.go +++ b/internal/tui/view.go @@ -430,10 +430,14 @@ type key struct { // day, then "HH:MM Name: text" wrapped with a hanging indent, and the picture // below when it is loaded. Spaced, a gap sets apart a new sender or a pause, // and each day; otherwise messages are packed. Gaps and dates belong to no -// message. The message being replied to or edited is marked. +// message. The message being replied to or edited is marked. Messages of the +// account end with a check, two once read, except where nobody reads them: +// channels and Saved Messages. func (m *model) messageLines(h *history, w int) []msgLine { const indent = photoIndent pad := strings.Repeat(" ", indent) + chat := m.chat(m.openID) + receipts := chat != nil && chat.Kind != core.KindChannel && chat.ID != m.be.Self().ID byID := make(map[int]*core.Message, len(h.msgs)) var lines []msgLine var day string @@ -492,11 +496,29 @@ func (m *model) messageLines(h *history, w int) []msgLine { text += m.st.muted.Render(" (edited)") } - for j, l := range strings.Split(ansi.Wrap(text, max(10, w-indent-1), ""), "\n") { + // The check sits at the right edge of the last line, in room the + // text leaves for it. + var check string + if receipts && msg.Out && !msg.Service { + check = m.st.muted.Render("✓") + " " + if msg.ID <= chat.ReadOutbox { + check = m.st.accent.Render("✓✓") + " " + } + } + wrap := w - indent - 1 + if check != "" { + wrap -= 4 // " ✓✓ " + } + wrapped := strings.Split(ansi.Wrap(text, max(10, wrap), ""), "\n") + for j, l := range wrapped { prefix := padded if j == 0 { prefix = mark + m.st.muted.Render(msg.Date.Format("15:04")) + " " } + if j == len(wrapped)-1 && check != "" { + l = spread(prefix+l, check, w) + prefix = "" + } lines = append(lines, msgLine{text: prefix + l, msg: i}) } if pic.id != 0 { From bd5d9211e3b7b457d5324a28694003920f35afb7 Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:20:29 +0100 Subject: [PATCH 2/3] feat(tui): one read check, colored once read Co-Authored-By: Claude Opus 5.5 --- internal/core/core.go | 2 +- internal/demo/demo.go | 4 ++-- internal/mtp/backend.go | 4 +--- internal/tui/tui_test.go | 19 ++++++++++++------- internal/tui/view.go | 12 ++++++------ 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/internal/core/core.go b/internal/core/core.go index 5e02ad4..fe8fd02 100644 --- a/internal/core/core.go +++ b/internal/core/core.go @@ -179,7 +179,7 @@ const ( // from then on is reported. It carries no chat or message. EventReady EventKind = "ready" // EventRead tells that the other side read the account's messages up to - // Chat.ReadOutbox. It carries no message. + // Chat.ReadOutbox. Only the chat's ID is set, and no message. EventRead EventKind = "read" ) diff --git a/internal/demo/demo.go b/internal/demo/demo.go index b7ec0dc..0338bb7 100644 --- a/internal/demo/demo.go +++ b/internal/demo/demo.go @@ -253,8 +253,8 @@ func (b *Backend) Send(_ context.Context, chatID int64, req core.SendRequest) (c time.AfterFunc(time.Second, func() { b.mu.Lock() b.messages[chatID] = append(b.messages[chatID], reply) - chat, _ := b.chat(chatID) // b.chats may have moved since - chat.ReadOutbox = m.ID // read before answering + chat, _ := b.chat(chatID) // b.chats may have moved since + chat.ReadOutbox = max(chat.ReadOutbox, m.ID) // read before answering read := core.Event{Kind: core.EventRead, Chat: *chat} ev := core.Event{Kind: core.EventMessage, Chat: *chat, Message: reply} b.mu.Unlock() diff --git a/internal/mtp/backend.go b/internal/mtp/backend.go index 5820156..6dc32ab 100644 --- a/internal/mtp/backend.go +++ b/internal/mtp/backend.go @@ -652,9 +652,7 @@ func (b *backend) emitRead(chatID int64, maxID int) { if fn == nil { return } - chat, _ := b.peers.chat(chatID) - chat.ID, chat.ReadOutbox = chatID, maxID - (*fn)(core.Event{Kind: core.EventRead, Chat: chat}) + (*fn)(core.Event{Kind: core.EventRead, Chat: core.Chat{ID: chatID, ReadOutbox: maxID}}) } func newSender(api *tg.Client) *message.Sender { diff --git a/internal/tui/tui_test.go b/internal/tui/tui_test.go index 63b0446..d2d906b 100644 --- a/internal/tui/tui_test.go +++ b/internal/tui/tui_test.go @@ -1125,20 +1125,25 @@ func TestClickPressesBotButtons(t *testing.T) { } } -// A message of the account shows one check until the other side reads it. -func TestReadReceiptTurnsIntoTwoChecks(t *testing.T) { +// A message of the account shows a muted check until the other side reads +// it, then a colored one. +func TestReadReceiptColorsTheCheck(t *testing.T) { m := loaded(t, 100, 30) open(t, m, slices.IndexFunc(m.visible(), func(c *core.Chat) bool { return c.Title == "Dmitri Volkov" })) + sent, read := m.st.muted.Render("✓")+" ", m.st.accent.Render("✓")+" " + if sent == read { + t.Fatal("sent and read checks look the same") + } last := func() string { lines := m.messageLines(m.histories[m.openID], m.conversationWidth()) - return ansi.Strip(lines[len(lines)-1].text) + return lines[len(lines)-1].text } - if got := last(); !strings.HasSuffix(got, " ✓ ") { - t.Fatalf("unread message %q, want one check", got) + if got := last(); !strings.HasSuffix(got, sent) { + t.Fatalf("unread message %q, want a muted check", got) } h := m.histories[m.openID] m.Update(eventMsg(core.Event{Kind: core.EventRead, Chat: core.Chat{ID: m.openID, ReadOutbox: h.msgs[len(h.msgs)-1].ID}})) - if got := last(); !strings.HasSuffix(got, " ✓✓ ") { - t.Fatalf("read message %q, want two checks", got) + if got := last(); !strings.HasSuffix(got, read) { + t.Fatalf("read message %q, want a colored check", got) } } diff --git a/internal/tui/view.go b/internal/tui/view.go index cd7ac9a..a26e4ce 100644 --- a/internal/tui/view.go +++ b/internal/tui/view.go @@ -431,8 +431,8 @@ type key struct { // below when it is loaded. Spaced, a gap sets apart a new sender or a pause, // and each day; otherwise messages are packed. Gaps and dates belong to no // message. The message being replied to or edited is marked. Messages of the -// account end with a check, two once read, except where nobody reads them: -// channels and Saved Messages. +// account end with a check, colored once read, except where nobody reads +// them: channels and Saved Messages. func (m *model) messageLines(h *history, w int) []msgLine { const indent = photoIndent pad := strings.Repeat(" ", indent) @@ -496,18 +496,18 @@ func (m *model) messageLines(h *history, w int) []msgLine { text += m.st.muted.Render(" (edited)") } - // The check sits at the right edge of the last line, in room the - // text leaves for it. + // The check sits at the right edge of the last line. The text of + // the message wraps narrower to keep room for it. var check string if receipts && msg.Out && !msg.Service { check = m.st.muted.Render("✓") + " " if msg.ID <= chat.ReadOutbox { - check = m.st.accent.Render("✓✓") + " " + check = m.st.accent.Render("✓") + " " } } wrap := w - indent - 1 if check != "" { - wrap -= 4 // " ✓✓ " + wrap -= 3 // " ✓ " } wrapped := strings.Split(ansi.Wrap(text, max(10, wrap), ""), "\n") for j, l := range wrapped { From f26bd5c29af1d979a2a69a4237cb6a68710ac5d8 Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:21:43 +0100 Subject: [PATCH 3/3] fix(demo): read events carry only the chat ID, as on Telegram Co-Authored-By: Claude Opus 5.5 --- internal/demo/demo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/demo/demo.go b/internal/demo/demo.go index 0338bb7..c16de18 100644 --- a/internal/demo/demo.go +++ b/internal/demo/demo.go @@ -255,7 +255,7 @@ func (b *Backend) Send(_ context.Context, chatID int64, req core.SendRequest) (c b.messages[chatID] = append(b.messages[chatID], reply) chat, _ := b.chat(chatID) // b.chats may have moved since chat.ReadOutbox = max(chat.ReadOutbox, m.ID) // read before answering - read := core.Event{Kind: core.EventRead, Chat: *chat} + read := core.Event{Kind: core.EventRead, Chat: core.Chat{ID: chatID, ReadOutbox: chat.ReadOutbox}} ev := core.Event{Kind: core.EventMessage, Chat: *chat, Message: reply} b.mu.Unlock() for _, ev := range []core.Event{read, ev} {