store

package
v0.0.32 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatEntry

type ChatEntry struct {
	Chat        *telegram.Chat
	LastMessage *telegram.Message
	UnreadCount int32
	Pinned      bool
	Order       int64

	// UnreadReactionsCount is how many of the reader's messages carry a
	// reaction they have not seen. Like UnreadCount it comes from the
	// dialog and is kept live from there; unlike it, nothing draws it —
	// the chat view reads it to decide whether opening the chat has
	// reactions to clear.
	UnreadReactionsCount int32

	// UnreadMentionsCount is how many messages in the chat name the reader
	// and have not been opened: the @ on the chat's row. It comes from the
	// dialog, and after that this client keeps it live itself, since no
	// update carries it — up for each arriving mention, down for each one
	// this client clears.
	UnreadMentionsCount int32

	// Unresolved marks an entry this store INVENTED to hold a message for
	// a chat nobody had described yet — see [ChatStore.UpdateLastMessage].
	// It has an id and nothing else: no name, no type, no mute flag.
	//
	// The flag exists because "the title is empty" is not the same
	// question. A real chat can have an empty title (a deleted account
	// has no name at all), and a row for one of those must not spend the
	// rest of the session claiming to be loading.
	Unresolved bool

	// MemberCount is a group or channel's total, 0 when nobody has asked
	// yet. It is NOT part of a dialog — a dialog says who a chat is, not
	// how many people are in it — so it arrives from a separate full-info
	// call, and [ChatStore.Set] and [ChatStore.Merge] leave it alone.
	//
	// It lives here rather than in the two components that want it so
	// they cannot disagree, and so the second one to need it does not
	// make the call again.
	MemberCount int32
	// contains filtered or unexported fields
}

ChatEntry holds chat metadata and its position in the main list.

type ChatStore

type ChatStore struct {
	// contains filtered or unexported fields
}

ChatStore is a thread-safe in-memory cache of chats.

func NewChatStore

func NewChatStore() *ChatStore

func (*ChatStore) AddUnreadReaction added in v0.0.26

func (s *ChatStore) AddUnreadReaction(chatID int64)

AddUnreadReaction records that somebody reacted to one of the reader's messages in a chat and the reader has not seen it.

The update behind it says a reaction is unread, not how many messages carry one, so the count this keeps is only good for "some or none". That is all the chat view asks of it. A no-op for a chat the store does not know: a reaction describes a chat, it does not introduce one.

func (*ChatStore) ClearMentions added in v0.0.26

func (s *ChatStore) ClearMentions(chatID int64)

ClearMentions records that this client has just cleared every unread mention in a chat. A no-op for a chat the store does not know.

func (*ChatStore) Count

func (s *ChatStore) Count() int

Count returns the number of cached chats.

func (*ChatStore) Get

func (s *ChatStore) Get(chatID int64) (*ChatEntry, bool)

Get returns a chat entry by ID.

func (*ChatStore) MarkMentionsRead added in v0.0.26

func (s *ChatStore) MarkMentionsRead(chatID int64, n int)

MarkMentionsRead records that this client has just cleared n of a chat's unread mentions.

The count is the dialog's plus this client's own arithmetic, so it can be behind the server: a mention cleared on another device is still counted here. Clearing more than are counted therefore leaves none, not a negative number. A no-op for a chat the store does not know.

func (*ChatStore) MarkReactionsRead added in v0.0.26

func (s *ChatStore) MarkReactionsRead(chatID int64)

MarkReactionsRead records that this client has just cleared a chat's unread reactions. The clear covers the whole chat, so nothing is left. A no-op for a chat the store does not know.

func (*ChatStore) MarkReadUpTo added in v0.0.26

func (s *ChatStore) MarkReadUpTo(chatID int64, maxID int64)

MarkReadUpTo records that this client has just read a chat up to maxID.

The server does not reliably echo this session's own reads back, so without it the chat the reader is looking at kept its badge. Reading as far as the newest message seen leaves nothing unread; reading less leaves an unknown remainder, so the count is left for the next dialog reload to correct. A no-op for a chat the store does not know.

func (*ChatStore) MemberCount

func (s *ChatStore) MemberCount(chatID int64) int32

MemberCount is a chat's total membership, 0 when it is not known.

func (*ChatStore) Merge

func (s *ChatStore) Merge(chat *telegram.Chat)

Merge updates a chat entry from a PEER — a partial view.

Resolving a peer answers "who is this": the id, the type, the name, the username, the photo. It says nothing about the reader's relationship with the chat, because that lives in the dialog and in the account's notify settings, neither of which a peer lookup returns.

Handing such a chat to [Set] replaced the entry wholesale, so every field the peer lookup had no opinion about was overwritten with a zero value. Opening a chat calls GetChat, so opening a chat cleared its mute flag, its unread count, its pin and its last message — and the first symptom anybody noticed was desktop notifications from a chat they had muted, which had been muted right up until they read it.

The field list below is therefore explicit rather than "copy anything that looks set": a bool cannot tell "false" from "not known", and guessing from zero values is the same bug wearing a different hat. Muted IS in the list, because the fetch goes and asks for it — see Client.GetChat.

func (*ChatStore) OrderedChats

func (s *ChatStore) OrderedChats() []*ChatEntry

OrderedChats returns all chats: pinned first, then by last message date (descending).

func (*ChatStore) Set

func (s *ChatStore) Set(chat *telegram.Chat)

Set adds or updates a chat entry from a DIALOG — a complete view.

A dialog knows everything about a chat: who it is, and also whether it is muted, pinned, how many messages are unread and where it sits in the list. So this replaces. [Merge] is for the other kind of update, and mixing the two up is what silently unmuted every chat the reader opened.

func (*ChatStore) SetMemberCount

func (s *ChatStore) SetMemberCount(chatID int64, count int32)

SetMemberCount records a chat's total membership.

A zero is dropped rather than stored: every caller gets it from a full-info call, and a call that failed hands back the zero value. Storing that would replace a number this client knows with one it does not, and the header would go from "24 members" to silence on a refresh.

func (*ChatStore) SetMuted

func (s *ChatStore) SetMuted(chatID int64, muted bool)

SetMuted updates a chat's muted flag. It is a no-op if the chat is not yet known to the store.

func (*ChatStore) TotalUnread

func (s *ChatStore) TotalUnread() int32

TotalUnread is how many unread messages there are across every chat.

Summed on demand rather than kept as a running total: an incremental counter has to be right on every path that changes a chat's unread count, and the one that forgets leaves a number on screen that drifts further from the truth the longer the session runs.

func (*ChatStore) UpdateLastMessage

func (s *ChatStore) UpdateLastMessage(chatID int64, msg *telegram.Message)

UpdateLastMessage updates a chat's last message and sort order, and counts the message as unread when it is news — see [countsAsUnread] — and as an unread mention when it is news that names the reader.

func (*ChatStore) UpdateReadInbox

func (s *ChatStore) UpdateReadInbox(chatID int64, maxID int64, unreadCount int32)

UpdateReadInbox applies the server's read receipt for a chat: the unread count it reports, which is authoritative, and the read mark maxID, which [countsAsUnread] needs to tell a late message from a new one.

A receipt below the mark already held is ignored outright. Telegram's read mark never goes backwards, so such a receipt is stale, and so is its count — taking it brought back a badge a local read had just cleared.

func (*ChatStore) UpdateReadOutbox added in v0.0.26

func (s *ChatStore) UpdateReadOutbox(chatID int64, maxID int64)

UpdateReadOutbox records that the other side has read everything up to maxID — the fact a sent message's tick is drawn from. Only ever forward: receipts can arrive out of order, and an older one must not take a tick back. A no-op for a chat the store does not know; a receipt describes a chat, it does not introduce one.

type FileState

type FileState struct {
	File       *telegram.File
	LocalPath  string
	IsComplete bool
	Progress   float64 // 0.0 to 1.0
}

FileState tracks the download state of a file.

type FileStore

type FileStore struct {
	// contains filtered or unexported fields
}

FileStore tracks file download states, keeping the fileStoreCapacity most recently used. A download still in progress is never evicted, so the store can stand above its capacity by those, and only while they run.

func NewFileStore

func NewFileStore() *FileStore

func (*FileStore) Get

func (s *FileStore) Get(fileID string) (*FileState, bool)

Get returns the state of a file.

func (*FileStore) IsComplete

func (s *FileStore) IsComplete(fileID string) bool

IsComplete checks if a file download is complete.

func (*FileStore) LocalPath

func (s *FileStore) LocalPath(fileID string) string

LocalPath returns the local path of a downloaded file.

func (*FileStore) Progress

func (s *FileStore) Progress(fileID string) float64

Progress returns the download progress of a file (0.0 to 1.0).

func (*FileStore) Update

func (s *FileStore) Update(file *telegram.File)

Update processes a file update.

type MessageStore

type MessageStore struct {
	// contains filtered or unexported fields
}

MessageStore caches messages per chat.

func NewMessageStore

func NewMessageStore() *MessageStore

func (*MessageStore) Activate

func (s *MessageStore) Activate(chatID int64)

Activate makes chatID the one pageable chat. Its history may grow while the reader explicitly walks backwards; when the reader leaves, the previous chat is reduced to the same newest-message bound as every background chat.

func (*MessageStore) Append

func (s *MessageStore) Append(chatID int64, msg *telegram.Message)

Append adds a new message to the end of the chat's message list.

func (*MessageStore) Clear

func (s *MessageStore) Clear(chatID int64)

Clear removes all cached messages for a chat.

func (*MessageStore) Count

func (s *MessageStore) Count(chatID int64) int

Count returns the number of cached messages for a chat.

func (*MessageStore) Delete

func (s *MessageStore) Delete(chatID int64, messageIDs []int64)

Delete removes messages from the store.

func (*MessageStore) DeleteFromAll

func (s *MessageStore) DeleteFromAll(messageIDs []int64)

DeleteFromAll removes messages from every chat. Used for non-channel delete updates, which carry no peer (ChatId == 0).

func (*MessageStore) Get

func (s *MessageStore) Get(chatID int64) []*telegram.Message

Get returns all cached messages for a chat.

func (*MessageStore) GetByID added in v0.0.28

func (s *MessageStore) GetByID(chatID, messageID int64) (*telegram.Message, bool)

GetByID returns one cached message by its ID, and whether the chat holds it. It neither copies the history nor walks it, which is the point: a caller after a single message — the one a reply quotes, say — used to pay for [Get]'s copy of the whole chat to find it.

func (*MessageStore) MarkSendFailed added in v0.0.21

func (s *MessageStore) MarkSendFailed(chatID int64, messageID int64) bool

MarkSendFailed flags a locally echoed message as one whose send failed, and reports whether the row was still there to flag. The placeholder can legitimately be gone by the time a failure lands — the chat was cleared, or the cache trimmed past it — and that is not an error.

The flag is set here rather than by the caller mutating the message it found, because every other write to a stored message goes through the store's lock and this one has no reason to be the exception.

func (*MessageStore) Merge added in v0.0.26

func (s *MessageStore) Merge(chatID int64, msgs []*telegram.Message) []*telegram.Message

Merge folds a freshly fetched page into whatever is cached, wherever each message belongs, and returns the ones that were new.

[Prepend] is for paging backwards: everything it is handed is older than everything it has. A page fetched to catch up after a sync gap is the opposite shape — mostly newer than the cache, sometimes filling a hole in the middle of it, always overlapping what is already there — so it is placed by ID rather than by which end it arrived at. A message already cached is REPLACED by the server's copy, since an edit or a reaction that happened during the gap is on that copy and not on ours.

Local echoes (negative IDs, see [ReplaceMessageId]) stay at the bottom after everything confirmed: their IDs order nothing, and the row belongs under the newest message the server has, which is what the reader sent it after.

func (*MessageStore) OldestMessageId

func (s *MessageStore) OldestMessageId(chatID int64) int64

OldestMessageId returns the oldest cached message ID for a chat.

Locally echoed sends are skipped: their IDs are negative placeholders this client invented, and handing one to Telegram as the point to page backwards from asks for history either side of a message the server has never heard of.

func (*MessageStore) Prepend

func (s *MessageStore) Prepend(chatID int64, msgs []*telegram.Message) []*telegram.Message

Prepend adds older messages to the beginning of the chat's message list and returns the newly inserted messages that survived the cache policy.

Its contract is that the page is entirely older than everything held: that is what lets it place the page by which end it arrived at rather than by ID. Repeats of what is already held are fine and expected — the last page of a walk backwards is usually one — but anything genuinely newer is refused, and [Merge] is the method for that shape. See refusesLocked for why refusing rather than coping.

func (*MessageStore) ReplaceMessageId

func (s *MessageStore) ReplaceMessageId(chatID int64, oldID int64, newMsg *telegram.Message)

ReplaceMessageId replaces a temporary message ID with the real one (after send).

The update dispatcher races the send: the server's copy of the message can be appended here before the send call returns. Both halves of this therefore have to cope with the confirmed message already being present — the placeholder is dropped rather than swapped, because swapping it would leave the thread showing the same message twice.

func (*MessageStore) UpdateMessage

func (s *MessageStore) UpdateMessage(chatID int64, messageID int64, newMsg *telegram.Message)

UpdateMessage replaces a message in the store (for edits).

type Store

type Store struct {
	Chats    *ChatStore
	Messages *MessageStore
	Users    *UserStore
	Files    *FileStore
}

Store is the aggregate store holding all caches.

func NewStore

func NewStore() *Store

NewStore creates a new aggregate store.

type UserStore

type UserStore struct {
	// contains filtered or unexported fields
}

UserStore caches user information.

func NewUserStore

func NewUserStore() *UserStore

func (*UserStore) All

func (s *UserStore) All() []*telegram.User

All returns all cached users.

func (*UserStore) DisplayName

func (s *UserStore) DisplayName(userID int64) string

DisplayName returns a formatted display name for a user.

func (*UserStore) Get

func (s *UserStore) Get(userID int64) (*telegram.User, bool)

Get returns a cached user by ID.

func (*UserStore) IsOnline

func (s *UserStore) IsOnline(userID int64) bool

IsOnline checks if a user is currently online.

func (*UserStore) Set

func (s *UserStore) Set(user *telegram.User)

Set adds or updates a user in the cache.

func (*UserStore) UpdateStatus

func (s *UserStore) UpdateStatus(userID int64, status telegram.UserStatus)

UpdateStatus updates a user's online status.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL