store

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 3 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

	// 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
}

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) 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) 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.

func (*ChatStore) UpdateReadInbox

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

UpdateReadInbox updates the unread count for a chat.

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.

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) OldestMessageId

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

OldestMessageId returns the oldest cached message ID for a chat.

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.

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).

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