space

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const SpaceChannelPrefix = "s"

Variables

View Source
var SystemBots = map[string]bool{
	"botfather":    true,
	"u_10000":      true,
	"fileHelper":   true,
	"notification": true,
}

SystemBots 是所有 Space 都可见的系统级 Bot UID。

关键场景 (YUJ-216 / GH#1280):

  • 会话同步(POST /v1/conversation/sync)带 X-Space-ID 时,按 Space 过滤 普通会话。系统 Bot(botfather 等)与 Space 无关,必须始终对客户端可见, 哪怕用户在当前 Space 下尚未与 Bot 产生任何消息(首次进入 Space、 本地缓存丢失等)。Web 端在前端做兜底,移动端没有,所以后端 需要在 sync 响应中保底注入 SystemBot entry。
  • 目前系统 Bot 固定为 {botfather, u_10000, fileHelper}。未来加入 BUILDER/ADMIN bot 时,只需在此 map 中追加,或改为从配置加载。 对外接口通过 SystemBotList() 暴露,便于统一遍历。

Functions

func BuildChannelID

func BuildChannelID(spaceID, peerID string) string

BuildChannelID 构建 Space 内的 channel_id 个人空间返回原始 peerID

func CheckBothMembers

func CheckBothMembers(session *dbr.Session, spaceID string, uid1, uid2 string) (bool, error)

CheckBothMembers checks if both uid1 and uid2 are active members of the given Space. The Space itself must also be active (space.status=1); a disabled Space returns false even if both users still have space_member rows, matching CheckMembership's semantics.

func CheckBotsInSpace

func CheckBotsInSpace(session *dbr.Session, spaceID string, botUIDs map[string]bool) (map[string]bool, error)

CheckBotsInSpace 查询给定 Bot UID 中哪些是指定 Space 的成员。 返回在 Space 中的 Bot UID 集合。DB 查询失败时返回 error。

func CheckMembership

func CheckMembership(session *dbr.Session, spaceID string, uid string) (bool, error)

CheckMembership checks if uid is an active member of the given Space. Also verifies the Space itself is active (space.status=1).

func DMSpacePresenceAnySet added in v1.8.0

func DMSpacePresenceAnySet(session *dbr.Session, fakeChannelIDs []string) (map[string]bool, error)

DMSpacePresenceAnySet returns the subset of fakeChannelIDs that have at least one dm_space_presence row in ANY Space, as a set keyed by fake_channel_id. Combined with DMSpacePresenceSet(ids, spaceID) a caller can derive "tracked, but belongs exclusively to other Spaces" — the positive, durable evidence required before hiding a DM from the default-Space catch-all (issue #484 follow-up). Empty input returns an empty set without querying.

func DMSpacePresenceSet added in v1.8.0

func DMSpacePresenceSet(session *dbr.Session, fakeChannelIDs []string, spaceID string) (map[string]bool, error)

DMSpacePresenceSet returns the subset of fakeChannelIDs that have at least one message tagged with spaceID, as a set keyed by fake_channel_id. Empty input (or empty spaceID) returns an empty set without querying.

func GetBotUIDs

func GetBotUIDs(session *dbr.Session, uids []string) (map[string]bool, error)

GetBotUIDs 从给定 UID 列表中查询哪些是 Bot(robot=1),排除系统 Bot。 返回 Bot UID 集合。DB 查询失败时返回 error。

func GetGroupSpaceMap

func GetGroupSpaceMap(groupNos []string, getGroups func([]string) ([]GroupSpaceInfo, error)) (map[string]string, error)

GetGroupSpaceMap 批量查询群的 space_id,返回 groupNo -> spaceID 映射。 需要传入一个能执行 GetGroups 的回调。

func GetSpaceID

func GetSpaceID(c *wkhttp.Context) string

GetSpaceID 从 gin context 读取 space_id。

func GetSpaceName

func GetSpaceName(session *dbr.Session, spaceID string) (string, error)

GetSpaceName returns the name of an active Space by ID. Returns an empty string (without error) when the Space does not exist, has status=0, or spaceID is empty — callers can treat "no space info" uniformly.

Used by H5 invite / join landing pages (YUJ-168 / GH #1243) to show "来自 {space_name}" below the group name so that external-group invitees have a trust anchor before they tap "加入群聊".

func HaveCommonSpace

func HaveCommonSpace(session *dbr.Session, uid1, uid2 string) (bool, error)

HaveCommonSpace checks if uid1 and uid2 share at least one active Space membership. Used to prevent cross-Space existence probing in user search.

func InvalidateMembershipCache

func InvalidateMembershipCache(redisConn *redis.Conn, spaceID, uid string)

InvalidateMembershipCache 删除指定用户在指定 Space 的成员缓存。

func IsSystemBot

func IsSystemBot(uid string) bool

IsSystemBot 判断 UID 是否为系统 Bot。语义等价于 SystemBots[uid], 提供函数版本是为将来切换到配置/DB 后不破坏调用方签名。

func ParseChannelID

func ParseChannelID(channelID string) (spaceID, peerID string)

ParseChannelID 从 channel_id 解析 space_id 和 peer_id 优先用已知 spaceId 列表做快速匹配,回退用正则 s[0-9a-f]{32}_ 精确匹配

func RegisterSpaceIDs

func RegisterSpaceIDs(ids []string)

RegisterSpaceIDs 注册已知的 spaceId 列表(启动时从 DB 加载)

func SpaceMiddleware

func SpaceMiddleware(ctx *config.Context) wkhttp.HandlerFunc

SpaceMiddleware 是 opt-in 中间件,route group 级别注入。 从请求提取 space_id(query param 优先,header X-Space-ID 其次), 无 space_id 则跳过,有则校验用户是否属于该 Space。

func SystemBotList

func SystemBotList() []string

SystemBotList 以稳定顺序返回所有系统 Bot UID。

稳定顺序有两个价值:

  1. sync 响应里保底注入 SystemBot entry 时,保证跨请求顺序一致, 方便客户端/测试做幂等比对。
  2. 未来系统 Bot 列表改为配置驱动时,调用方无需感知底层容器类型。

func UpsertDMSpacePresence added in v1.8.0

func UpsertDMSpacePresence(session *dbr.Session, fakeChannelID, spaceID string, ts int64) error

UpsertDMSpacePresence records that fakeChannelID has a message in spaceID. Idempotent; last_timestamp only moves forward. Best-effort by contract — callers (webhook ingest) log and continue on error and must NOT let a failure here roll back message persistence.

Types

type GroupSpaceInfo

type GroupSpaceInfo struct {
	GroupNo string
	SpaceID string
}

GroupSpaceInfo 用于 GetGroupSpaceMap 回调的最小接口。

type InMemoryMembershipCache

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

InMemoryMembershipCache 基于内存的 MembershipCache 实现,用于测试。

func NewInMemoryMembershipCache

func NewInMemoryMembershipCache() *InMemoryMembershipCache

func (*InMemoryMembershipCache) Clear

func (c *InMemoryMembershipCache) Clear()

Clear 清除所有缓存条目(测试用)。

func (*InMemoryMembershipCache) Get

func (c *InMemoryMembershipCache) Get(spaceID, uid string) (bool, bool)

func (*InMemoryMembershipCache) Set

func (c *InMemoryMembershipCache) Set(spaceID, uid string, isMember bool, ttl time.Duration)

type MembershipCache

type MembershipCache interface {
	// Get 返回缓存的成员身份。found=false 表示缓存未命中。
	Get(spaceID, uid string) (isMember bool, found bool)
	// Set 写入缓存,ttl 为过期时间。
	Set(spaceID, uid string, isMember bool, ttl time.Duration)
}

MembershipCache 缓存成员身份校验结果的接口。

type MembershipChecker

type MembershipChecker func(spaceID string, uid string) (bool, error)

MembershipChecker 校验用户是否属于 Space 的函数签名。

type RedisMembershipCache

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

RedisMembershipCache 基于 Redis 的 MembershipCache 实现。

func NewRedisMembershipCache

func NewRedisMembershipCache(redisConn *redis.Conn) *RedisMembershipCache

NewRedisMembershipCache 创建基于 Redis 的缓存。

func (*RedisMembershipCache) Get

func (c *RedisMembershipCache) Get(spaceID, uid string) (bool, bool)

func (*RedisMembershipCache) Set

func (c *RedisMembershipCache) Set(spaceID, uid string, isMember bool, ttl time.Duration)

Jump to

Keyboard shortcuts

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