API 参考取自源码中的 Go 文档注释,以英文书写。
package social
import "github.com/nsgo-dev/game/pkg/online/social"概述
Package social is experimental: its API and documented formats may change in a minor release. See the API stability policy. provides the in-memory cores of a game's social features on a server: the friend and block graph (Graph), groups with roles (Groups), parties that queue together (Parties) and chat channels with moderation and retention (Chat).
索引
- 变量
- func ChannelID(k ChannelKind, a, b string) string
- type ChannelKind
- type Chat
- func NewChat() *Chat
- func (c *Chat) Delete(ch, id, s string) bool
- func (c *Chat) Edit(ch, id, s string, v json.RawMessage) bool
- func (c *Chat) History(ch string, limit int) []Message
- func (c *Chat) HistoryBefore(ch, before string, limit int) []Message
- func (c *Chat) Join(ch, user string)
- func (c *Chat) Leave(ch, user string)
- func (c *Chat) Members(ch string) []string
- func (c *Chat) Send(ch, s string, v json.RawMessage) (Message, error)
- func (c *Chat) Sweep(now time.Time) int
- type Edge
- type EdgeState
- type Graph
- type Group
- type Groups
- func NewGroups() *Groups
- func (s *Groups) Create(owner, name string, max int, open bool) (*Group, error)
- func (s *Groups) Join(id uint64, account string) error
- func (s *Groups) Leave(id uint64, account string) error
- func (s *Groups) Promote(id uint64, actor, target string) error
- func (s *Groups) Search(prefix string) []Group
- type Message
- type Parties
- type Party
- type PartyMember
- type Role
变量
var (
ErrGroupNotFound = errors.New("social: group not found")
ErrGroupPermission = errors.New("social: permission denied")
ErrGroupBanned = errors.New("social: account banned")
ErrGroupFull = errors.New("social: group full")
)Errors returned by Groups.
var ErrBlocked = errors.New("social: account blocked")ErrBlocked is returned for a friend request between accounts when either blocks the other.
var ErrLimit = errors.New("social: friend limit")ErrLimit is returned when a friend request would pass MaxFriends or MaxPendingInvites.
var ErrPartyClosed = errors.New("social: party closed")ErrPartyClosed is returned by Join for a party that is not open.
var ErrPartyFull = errors.New("social: party full")ErrPartyFull is returned by Join for a party with MaxSize members.
函数
func ChannelID(k ChannelKind, a, b string) stringChannelID returns the ID of the channel of kind k for a and b: "kind:a" when b is empty and "kind:a:b" otherwise, with a direct channel's two accounts sorted so both sides name the same channel.
类型
type ChannelKind stringChannelKind is the kind of a chat channel: a match room, a group or a direct conversation between two accounts.
const (
RoomChannel ChannelKind = "room"
GroupChannel ChannelKind = "group"
DMChannel ChannelKind = "dm"
)type Chat struct {
BeforeSend func(Message) (Message, error)
MaxBytes int
Retention time.Duration
Now func() time.Time
Blocks func(a, b string) bool
// contains filtered or unexported fields
}Chat keeps channel members and messages in memory. BeforeSend, when set, may rewrite or refuse a message before it is stored, as a moderation filter; MaxBytes caps a message's content (4096 bytes from NewChat); Retention, when positive, is how long Sweep keeps messages; Now is the clock (time.Now when nil); Blocks, when set, reports whether its first account blocks its second, and refuses a direct message between them in either direction. A group channel takes messages only from the accounts that joined it, and a direct channel only from its two accounts. It is safe for concurrent use.
func NewChat() *ChatNewChat returns an empty chat with a 4096-byte message limit and no retention limit.
func (c *Chat) Delete(ch, id, s string) boolDelete marks message id in channel ch deleted when s sent it, keeping its place in the history, and reports whether it did.
func (c *Chat) Edit(ch, id, s string, v json.RawMessage) boolEdit replaces the content of message id in channel ch when s sent it and it is not deleted, and reports whether it did. Content over MaxBytes or not valid JSON is refused.
func (c *Chat) History(ch string, limit int) []MessageHistory returns the last limit messages of channel ch (50 when zero), oldest first, including deleted ones.
func (c *Chat) HistoryBefore(ch, before string, limit int) []MessageHistoryBefore returns up to limit messages (50 when zero) sent in channel ch before the message whose ID is before, or the last ones when before is empty or unknown, oldest first.
func (c *Chat) Join(ch, user string)Join adds user to channel ch. Empty names are ignored.
func (c *Chat) Members(ch string) []stringMembers returns the members of channel ch, sorted.
func (c *Chat) Send(ch, s string, v json.RawMessage) (Message, error)Send stores content v from sender s in channel ch, after BeforeSend, and returns the message. Content over MaxBytes or not valid JSON is refused, as is a sender who has not joined a group channel, is not one of a direct channel's two accounts, or is blocked from or blocks the other one.
func (c *Chat) Sweep(now time.Time) intSweep removes the messages sent Retention or longer before now and returns how many it removed; it does nothing without a positive Retention.
type Edge struct {
Account, Other string
State EdgeState
Updated time.Time
}Edge is Account's relation to Other and when it last changed. StateBlocked means Account blocks Other.
type EdgeState stringEdgeState is the relation of an account to another, as the first account sees it.
const (
StateFriend EdgeState = "friend"
StateInviteSent EdgeState = "invite_sent"
StateInviteReceived EdgeState = "invite_received"
StateBlocked EdgeState = "blocked"
)type Graph struct {
MaxFriends int
MaxPendingInvites int
// contains filtered or unexported fields
}Graph keeps the friend, invitation and block relations between accounts in memory. MaxFriends (100 from NewGraph) caps an account's friends; MaxPendingInvites is the cap on its pending invitations (50 from NewGraph). It is safe for concurrent use.
func (g *Graph) AddFriend(a, b string) errorAddFriend invites b on a's behalf, or makes them friends when b has already invited a; adding a friend or an invitee again changes nothing. It returns ErrBlocked when either blocks the other, and ErrLimit when a has MaxPendingInvites pending invitations (when positive) or when a or b would pass MaxFriends friends.
func (g *Graph) Block(a, b string)Block makes a block b, replacing any friendship or invitation between them; a block b already holds on a stays. Empty or equal accounts are ignored.
func (g *Graph) Blocked(a, b string) boolBlocked reports whether a blocks b or b blocks a.
func (g *Graph) Friends(a string, state EdgeState) []EdgeFriends returns a's relations in state, or all of them when state is empty, sorted by the other account. StateBlocked lists the accounts a blocks.
func (g *Graph) RemoveFriend(a, b string) boolRemoveFriend cancels a's invitation to b, declines b's invitation to a, or ends their friendship, on both sides, and reports whether there was one. It leaves a block in either direction in place.
func (g *Graph) Unblock(a, b string)Unblock lifts a's block on b. It cannot lift a block b holds on a.
type Group struct {
ID uint64
Name, Description, Lang string
Open bool
MaxMembers int
Members map[string]Role
}Group is a named group of accounts with their roles. A closed group puts accounts that join on Requested, and MaxMembers, when positive, caps its size.
type Groups struct {
// contains filtered or unexported fields
}Groups keeps groups in memory. It is safe for concurrent use.
func (s *Groups) Create(owner, name string, max int, open bool) (*Group, error)Create makes a group named name with owner as its Owner and returns a copy of it. An empty owner or name is refused.
func (s *Groups) Join(id uint64, account string) errorJoin adds account to group id, as a Member of an open group or Requested in a closed one; an account already in the group, or already Requested, keeps its role. It returns ErrGroupNotFound, ErrGroupBanned for a banned account and ErrGroupFull when an open group has MaxMembers owners, admins and members (requests do not count).
func (s *Groups) Leave(id uint64, account string) errorLeave removes account from group id, or withdraws its request. The owner cannot leave its group (ErrGroupPermission), and a banned account stays banned (ErrGroupBanned).
func (s *Groups) Promote(id uint64, actor, target string) errorPromote makes target, a Member of group id, an Admin; actor must be its Owner or an Admin. It returns ErrGroupPermission when actor may not promote or target is not a Member (an owner, an admin, a request or an account outside the group), and ErrGroupBanned for a banned target.
func (s *Groups) Search(prefix string) []GroupSearch returns copies of the groups whose names start with prefix, ignoring case, in creation order.
type Message struct {
ID, Channel, Sender string
Content json.RawMessage
Created, Updated time.Time
Deleted bool
}Message is one chat message: its ID in send order, its channel and sender, its JSON content, when it was sent and last edited, and whether it was deleted.
type Parties struct {
// contains filtered or unexported fields
}Parties keeps parties in memory. It is not safe for concurrent use.
func (p *Parties) Create(leader string, max int, open bool) *PartyCreate makes a party led by leader, holding at most max players (4 when below 1), and returns a copy of it.
func (p *Parties) Join(id, member string) (*Party, error)Join adds member to open party id, cancelling a pending matchmaking state, and returns a copy of the party; joining again changes nothing. It returns ErrPartyClosed for a closed party and ErrPartyFull for a full one.
func (p *Parties) Leave(id, member string) *PartyLeave removes member from party id, cancelling a pending matchmaking state, and returns a copy of the party, passing the lead to the earliest remaining member when the leader leaves. The last member to leave disbands the party, and Leave returns nil.
func (p *Parties) SetPending(id string, pending bool) boolSetPending marks party id as waiting on matchmaking or not, and reports whether the party exists.
type Party struct {
ID, Leader string
Open bool
MaxSize int
Members []PartyMember
Label string
Pending bool
}Party is a group of players who queue and play together: its ID, its leader, whether anyone may join, its size limit, its members, a label and whether it waits on matchmaking.
type PartyMember struct {
ID string
Joined uint64
}PartyMember is one member of a party and the order in which it joined.