Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions internal/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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} {
Comment thread
RealDoc06 marked this conversation as resolved.
Resolved
select {
case b.events <- ev:
default:
}
}
})
}
Expand Down
12 changes: 11 additions & 1 deletion internal/mtp/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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))
}
8 changes: 8 additions & 0 deletions internal/mtp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Comment thread
RealDoc06 marked this conversation as resolved.
Resolved
c.ReadOutbox = max(c.ReadOutbox, ev.Chat.ReadOutbox)
}
return nil
}
m.addMessage(ev.Message, &ev.Chat, ev.Kind == core.EventEdit)
Expand Down
23 changes: 23 additions & 0 deletions internal/tui/tui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
26 changes: 24 additions & 2 deletions internal/tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Loading