chat

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package chat 提供频道聊天消息:按 channel 持久化 + 自增 message_id 游标分页。

与 pkg/domain/notification 互补:notification 是"给个人的离线信"(按 userID 游标), chat 是"给频道的历史信"(按 channelID 游标)。IM 频道消息需要持久化 + 历史拉取 + 游标分页,区别于 pkg/match 的实时(不持久)与 pkg/router 的投递(不存历史)。

channel message:

  • 每条消息有 channel 内单调递增的 message_id(游标);
  • Before(messageID, limit) 返回该 ID 之前的历史(翻页往前看);
  • 频道有容量上限,超限删最旧(message_id 不因截断回退,保持单调);
  • 实时投递与持久化解耦:本包只管存历史,实时扇出由 pkg/router 负责。

零值不可用,用 New 构造。Store 并发安全。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	ID        int64  // 全局唯一 ID(Store 分配)
	ChannelID string // 频道 ID
	MsgID     int64  // 频道内单调序号(游标分页用)
	UserID    string // 发送者
	Content   string // 消息内容(业务可自定义编码,这里用 string 简化)
	CreatedAt int64  // 创建时间(UnixNano)
}

Message 一条频道消息。

type Option

type Option func(*config)

Option 配置 Store。

func WithMaxPerChannel

func WithMaxPerChannel(n int) Option

WithMaxPerChannel 设置每频道最大保留条数,超限删最旧。默认 1000。

type Store

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

Store 频道消息存储:按 channel 持久化 + 游标分页。

func New

func New(opts ...Option) *Store

New 创建 Store。

func (*Store) After

func (s *Store) After(channelID string, afterID int64, limit int) []Message

After 返回 channelID 中 msgID > afterID 的消息(拉取新消息)。 afterID<0 表示从最新开始(返回最新 limit 条)。 返回按 msgID 升序(旧→新),便于客户端追加到列表尾部。 limit<=0 默认 50。

func (*Store) Before

func (s *Store) Before(channelID string, beforeID int64, limit int) []Message

Before 返回 channelID 中 msgID < beforeID 的历史(翻页往前看)。 beforeID<=0 表示返回最新的 limit 条。 返回按 msgID 降序(最新在前),便于客户端"加载更早消息"。 limit<=0 默认 50。

func (*Store) Count

func (s *Store) Count(channelID string) int

Count 返回频道当前消息数。

func (*Store) Delete

func (s *Store) Delete(channelID string, id int64) bool

Delete 删除频道某条消息(by global ID)。返回是否删除成功。

func (*Store) LastMsgID

func (s *Store) LastMsgID(channelID string) int64

LastMsgID 返回频道最新 msgID(无消息返回 0)。用于客户端增量拉取的游标基点。

func (*Store) Latest

func (s *Store) Latest(channelID string, limit int) []Message

Latest 返回频道最新的 limit 条(按 msgID 降序)。

func (*Store) Post

func (s *Store) Post(channelID, userID, content string, nowNano int64) *Message

Post 投递一条消息到频道,返回存库后的 Message(ID/MsgID 已填充)。 channelID/userID 为空则返回 nil 不存。

Jump to

Keyboard shortcuts

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