group

package
v0.3.2 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: 7 Imported by: 0

Documentation

Overview

Package group 提供群组实体:成员角色、邀请/申请审核、公告、最大人数、封禁名单。

把 examples/clan 里用 relationship+tournament+wallet 现场组合的"公会"语义, 沉淀成有边界的一等包——clan 是 demo,group 是可复用的实体封装。

与 pkg/domain/relationship 的分工:

  • relationship 是图原语(二部有向图 + 状态编码),无群组语义;
  • group 是群组语义封装:owner/admin/member 角色、邀请/申请审核流、 公告、最大人数、banlist,内部用 relationship.Graph 存成员边 (source=groupID, destination=userID, State=角色)。

角色编码复用 relationship 的常量:StateOwner / StateAdmin / StateActive(member) / StatePending(申请中)。group 在其上加业务规则(如 owner 唯一、admin 可踢人、 banlist 用户不能加入)。

设计要点:Group 为一等实体,成员状态可流转。 零值不可用,用 New 构造。Store 并发安全。

Index

Constants

View Source
const (
	RoleMember  = relationship.StateActive  // 普通成员
	RoleAdmin   = relationship.StateAdmin   // 管理员
	RoleOwner   = relationship.StateOwner   // 拥有者(唯一)
	RolePending = relationship.StatePending // 申请中(待审核)
)

角色常量(复用 relationship 的状态编码,语义别名)。

Variables

View Source
var (
	ErrExists           = errors.New("group: already exists")
	ErrNotFound         = errors.New("group: not found")
	ErrAlreadyMember    = errors.New("group: already a member")
	ErrNotMember        = errors.New("group: not a member")
	ErrNotPending       = errors.New("group: not a pending request")
	ErrFull             = errors.New("group: max members reached")
	ErrBanned           = errors.New("group: user is banned")
	ErrNotBanned        = errors.New("group: user is not banned")
	ErrOwnerCannotLeave = errors.New("group: owner cannot leave, transfer first")
	ErrNoPermission     = errors.New("group: no permission")
	ErrCannotKickPeer   = errors.New("group: cannot kick a peer of equal or higher role")
)

错误定义。

Functions

This section is empty.

Types

type Group

type Group struct {
	ID           string
	Name         string
	OwnerID      string
	Announcement string
	MaxMembers   int
	CreatedAt    int64
}

Group 一个群组实体。

func (Group) String

func (g Group) String() string

String 便于日志。

type Option

type Option func(*config)

Option 配置 Store(预留,目前无选项)。

type Store

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

Store 管理群组集合及其成员关系。

func New

func New(opts ...Option) *Store

New 创建群组 Store。

func (*Store) Approve

func (s *Store) Approve(groupID, approver, userID string) error

Approve 审批通过申请:pending → member。仅 owner/admin 可操作。

func (*Store) Ban

func (s *Store) Ban(groupID, actor, userID string) error

Ban 封禁用户(加入 banlist,立即移除成员关系)。仅 owner/admin 可操作。 不能封禁同级或更高(admin 不能 ban owner/admin)。

func (*Store) Banned

func (s *Store) Banned(groupID string) ([]string, error)

Banned 返回封禁名单。

func (*Store) Create

func (s *Store) Create(g Group) error

Create 创建群组。owner 自动成为成员(StateOwner)。 MaxMembers<=0 视为不限。

func (*Store) Demote

func (s *Store) Demote(groupID, owner, userID string) error

Demote 降级 admin 为 member。仅 owner 可操作。

func (*Store) Info

func (s *Store) Info(groupID string) (Group, error)

Info 返回群组信息。

func (*Store) Join

func (s *Store) Join(groupID, userID string) error

Join 用户加入群组(直接加入,无需审核)。 banned 用户、已达 MaxMembers、已是成员则失败。

func (*Store) Kick

func (s *Store) Kick(groupID, actor, userID string) error

Kick 踢出成员。仅 owner/admin 可操作,且不能踢同级或更高(admin 不能踢 owner/admin)。

func (*Store) Leave

func (s *Store) Leave(groupID, userID string) error

Leave 用户主动退出。owner 不能退出(须先 TransferOwner)。

func (*Store) MemberCount

func (s *Store) MemberCount(groupID string) (int, error)

MemberCount 返回正式成员数(含 owner/admin/member,不含 pending)。

func (*Store) Members

func (s *Store) Members(groupID string) (owners, admins, members []string, err error)

Members 返回成员列表(按角色分组)。含 owner/admin/member(不含 pending)。

func (*Store) Pending

func (s *Store) Pending(groupID string) ([]string, error)

Pending 返回待审核申请列表。

func (*Store) Promote

func (s *Store) Promote(groupID, owner, userID string) error

Promote 提升为 admin。仅 owner 可操作。

func (*Store) Reject

func (s *Store) Reject(groupID, approver, userID string) error

Reject 拒绝申请:移除 pending 边。仅 owner/admin 可操作。

func (*Store) Request

func (s *Store) Request(groupID, userID string) error

Request 用户申请加入(进入待审核)。banned/已成员/满员则失败。 返回后由 admin/owner 用 Approve(groupID, userID) 审批。

func (*Store) Role

func (s *Store) Role(groupID, userID string) (role int, joinedAt int64, err error)

Role 查询某用户的角色。返回 (role, joinedAt, err)。

func (*Store) SetAnnouncement

func (s *Store) SetAnnouncement(groupID, actor, text string) error

SetAnnouncement 设置群公告。仅 owner/admin 可操作。

func (*Store) SetMaxMembers

func (s *Store) SetMaxMembers(groupID, owner string, max int) error

SetMaxMembers 设置最大人数。仅 owner 可操作。

func (*Store) TransferOwner

func (s *Store) TransferOwner(groupID, owner, newOwner string) error

TransferOwner 转让群主。仅 owner 可操作,新 owner 必须是 member/admin。

func (*Store) Unban

func (s *Store) Unban(groupID, actor, userID string) error

Unban 解除封禁。仅 owner/admin 可操作。

Jump to

Keyboard shortcuts

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