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..fe8fd02 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. Only the chat's ID is set, and 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..c16de18 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 = max(chat.ReadOutbox, m.ID) // read before answering + 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() - 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..6dc32ab 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,16 @@ 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 + } + (*fn)(core.Event{Kind: core.EventRead, Chat: core.Chat{ID: chatID, ReadOutbox: maxID}}) +} + 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..d2d906b 100644 --- a/internal/tui/tui_test.go +++ b/internal/tui/tui_test.go @@ -1124,3 +1124,26 @@ 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 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 lines[len(lines)-1].text + } + 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, read) { + t.Fatalf("read message %q, want a colored check", got) + } +} diff --git a/internal/tui/view.go b/internal/tui/view.go index 4107853..a26e4ce 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, 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) + 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. 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("✓") + " " + } + } + wrap := w - indent - 1 + if check != "" { + wrap -= 3 // " ✓ " + } + 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 {