social

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StateFriend         = 0
	StateInviteSent     = 1
	StateInviteReceived = 2
	StateBlocked        = 3
)

FriendState represents the states of a social edge.

View Source
const (
	NotificationCodeFriendRequest = -2
	NotificationCodeFriendAccept  = -3
	NotificationCodeFriendImport  = -1001 // UGE extension (Facebook/Steam import); -6 reserved for FriendJoinGame
	NotificationCodeFriendRemove  = -9
)

Notification codes for friend events (reference-engine aligned).

View Source
const (
	DefaultMaxFriends = 1000
	DefaultMaxPending = 100
	DefaultListLimit  = 100
	MaxListLimit      = 1000
)

Default limits (PRD-07).

View Source
const (
	GroupStateOpen   = 0
	GroupStateClosed = 1
)

GroupState represents whether a group is open or closed.

View Source
const (
	RoleSuperAdmin  = 0
	RoleAdmin       = 1
	RoleMember      = 2
	RoleJoinRequest = 3
	RoleBanned      = 4
)

GroupMemberState represents the membership roles.

View Source
const (
	NotificationCodeGroupAdd         = -4
	NotificationCodeGroupJoinRequest = -5
)

Group notification codes (reference-engine aligned).

View Source
const (
	GroupChannelMsgJoin    int16 = 3
	GroupChannelMsgAdd     int16 = 4
	GroupChannelMsgLeave   int16 = 5
	GroupChannelMsgKick    int16 = 6
	GroupChannelMsgPromote int16 = 7
	GroupChannelMsgBan     int16 = 8
	GroupChannelMsgDemote  int16 = 9
)

Group channel message codes (ADR-0013 / reference-aligned).

Variables

View Source
var (
	ErrCannotAddSelf       = errors.New("cannot add yourself as friend")
	ErrCannotBlockSelf     = errors.New("cannot block yourself")
	ErrFriendLimitReached  = errors.New("friend count limit reached")
	ErrPendingLimitReached = errors.New("pending friend request limit reached")
	ErrInvalidCursor       = errors.New("invalid friends cursor")
)
View Source
var ImportHTTPClient = &http.Client{Timeout: 15 * time.Second}

ImportHTTPClient is overridable for tests.

Functions

func AddFriend

func AddFriend(ctx context.Context, pool *pgxpool.Pool, sourceID, destinationID string) error

AddFriend sends a friend invite or accepts an incoming invite.

func AddFriendWithOpts

func AddFriendWithOpts(ctx context.Context, pool *pgxpool.Pool, sourceID, destinationID, metadata string, cfg Config, notifier FriendNotifier) error

AddFriendWithOpts is AddFriend with metadata, limits, and notifier.

func AddGroupUsers

func AddGroupUsers(ctx context.Context, pool *pgxpool.Pool, callerID, groupID string, userIDs []string, notifier FriendNotifier) error

AddGroupUsers adds users as members or accepts join requests (admin+). Empty callerID = authoritative.

func BanGroupUsers

func BanGroupUsers(ctx context.Context, pool *pgxpool.Pool, callerID, groupID string, userIDs []string) error

BanGroupUsers bans users: remove membership edges; insert unidirectional group→user state 4.

func BlockUser

func BlockUser(ctx context.Context, pool *pgxpool.Pool, sourceID, destinationID string) error

BlockUser blocks interaction with another user.

func DeleteFriend

func DeleteFriend(ctx context.Context, pool *pgxpool.Pool, sourceID, destinationID string, notifier FriendNotifier) error

DeleteFriend deletes friend relationships, rejects invites, or unblocks.

func DeleteGroup

func DeleteGroup(ctx context.Context, pool *pgxpool.Pool, callerID, id string) error

DeleteGroup soft-disables a group (superadmin). Empty callerID = authoritative hard cleanup of edges + disable.

func DemoteMember

func DemoteMember(ctx context.Context, pool *pgxpool.Pool, kickerID, userID, groupID string) error

DemoteMember lowers a user's role one step toward member.

func FetchFacebookFriendIDs

func FetchFacebookFriendIDs(ctx context.Context, accessToken string) ([]string, error)

FetchFacebookFriendIDs retrieves Facebook friend IDs for the given access token.

func FetchSteamFriendIDs

func FetchSteamFriendIDs(ctx context.Context, publisherKey, steamID string) ([]string, error)

FetchSteamFriendIDs retrieves Steam friend IDs for the given steam user.

func FriendMetadataUpdate

func FriendMetadataUpdate(ctx context.Context, pool *pgxpool.Pool, userID, friendID string, metadata map[string]any) error

FriendMetadataUpdate updates edge metadata for source→destination.

func GetUserRole

func GetUserRole(ctx context.Context, pool *pgxpool.Pool, userID, groupID string) (int, error)

GetUserRole retrieves the role of a user in a group.

func ImportFacebookFriends

func ImportFacebookFriends(ctx context.Context, pool *pgxpool.Pool, userID, username, token string, reset bool, notifier FriendNotifier) error

ImportFacebookFriends imports Facebook friends matching existing users.

func ImportFriendsFromProviderIDs

func ImportFriendsFromProviderIDs(ctx context.Context, pool *pgxpool.Pool, userID, username, column string, providerIDs []string, provider string, reset bool, notifier FriendNotifier) error

ImportFriendsFromProviderIDs is a test helper that skips external HTTP and matches by column.

func ImportSteamFriends

func ImportSteamFriends(ctx context.Context, pool *pgxpool.Pool, userID, username, steamToken string, reset bool, notifier FriendNotifier) error

ImportSteamFriends imports Steam friends matching existing users.

func IsGroupMember

func IsGroupMember(ctx context.Context, pool *pgxpool.Pool, userID, groupID string) (bool, error)

IsGroupMember reports whether user has membership state <= RoleMember.

func JoinGroup

func JoinGroup(ctx context.Context, pool *pgxpool.Pool, userID, groupID string, notifier FriendNotifier) error

JoinGroup joins an open group or queues a join request for a closed group.

func KickMember

func KickMember(ctx context.Context, pool *pgxpool.Pool, kickerID, userID, groupID string) error

KickMember removes a user from a group if kicker has proper authority. Also used to reject join requests (state 3) without changing edge_count.

func LeaveGroup

func LeaveGroup(ctx context.Context, pool *pgxpool.Pool, userID, groupID string) error

LeaveGroup removes a member from a group.

func ParseLegacyPositionCursor

func ParseLegacyPositionCursor(cursor string) int64

ParseLegacyPositionCursor parses a legacy integer position cursor.

func PromoteMember

func PromoteMember(ctx context.Context, pool *pgxpool.Pool, kickerID, userID, groupID string) error

PromoteMember raises a user's role (member→admin→superadmin) one step.

func ResolveUserIDs

func ResolveUserIDs(ctx context.Context, pool *pgxpool.Pool, ids, usernames []string) ([]string, error)

ResolveUserIDs resolves usernames to IDs and merges with provided IDs.

func UnblockUser

func UnblockUser(ctx context.Context, pool *pgxpool.Pool, sourceID, destinationID string) error

UnblockUser removes a block edge (same as delete when only block exists).

func UpdateGroup

func UpdateGroup(ctx context.Context, pool *pgxpool.Pool, callerID, id, name, description, avatarURL, langTag string, open bool, metadata string) error

UpdateGroup updates group metadata; callerID empty = authoritative.

func UpdateGroupMaxCount

func UpdateGroupMaxCount(ctx context.Context, pool *pgxpool.Pool, id string, maxCount int) error

UpdateGroupMaxCount sets max_count (runtime/console).

func UserExistsAndDoesNotBlock

func UserExistsAndDoesNotBlock(ctx context.Context, pool *pgxpool.Pool, checkUserID, blocksUserID string) (bool, error)

UserExistsAndDoesNotBlock returns true if checkUserID exists and has not blocked blocksUserID.

func UsersGetFriendStatus

func UsersGetFriendStatus(ctx context.Context, pool *pgxpool.Pool, userID string, friendIDs []string) (map[string]int, error)

UsersGetFriendStatus returns edge states between userID and the given friend IDs.

Types

type ChannelBroadcaster

type ChannelBroadcaster interface {
	BroadcastGroupChannelMessage(ctx context.Context, groupID string, code int16, content, senderID, username string)
}

ChannelBroadcaster emits group lifecycle events onto the group chat stream.

var DefaultChannelBroadcaster ChannelBroadcaster = noopChannelBroadcaster{}

DefaultChannelBroadcaster is set by the API/socket layer at startup.

type Config

type Config struct {
	MaxFriends int
	MaxPending int
}

Config holds friend limit configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns PRD defaults.

type CreateGroupParams

type CreateGroupParams struct {
	Name        string
	Description string
	AvatarURL   string
	LangTag     string
	Metadata    string
	Open        bool
	MaxCount    int
}

CreateGroupParams configures group creation.

type FacebookFriendProfile

type FacebookFriendProfile struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

FacebookFriendProfile is a minimal Facebook friend profile.

type Friend

type Friend struct {
	User       FriendUser
	State      int
	UpdateTime time.Time
	Metadata   string
	Position   int64
}

Friend represents a friend record with edge metadata.

func GetFriends

func GetFriends(ctx context.Context, pool *pgxpool.Pool, userID string) ([]Friend, error)

GetFriends returns all active mutual friends of a user.

func ListFriends

func ListFriends(ctx context.Context, pool *pgxpool.Pool, userID string, filterState int, limit int, cursor string) ([]Friend, string, error)

ListFriends returns friend records filterable by state, with DB keyset pagination.

type FriendNotifier

type FriendNotifier interface {
	Notify(ctx context.Context, userID, subject, content string, code int16, senderID string) error
}

FriendNotifier dispatches friend-related notifications.

var DefaultNotifier FriendNotifier = NoopNotifier{}

DefaultNotifier is used when callers pass nil.

type FriendOfFriend

type FriendOfFriend struct {
	Referrer string
	User     FriendUser
}

FriendOfFriend is a friends-of-friends discovery result.

func ListFriendsOfFriends

func ListFriendsOfFriends(ctx context.Context, pool *pgxpool.Pool, userID string, limit int, cursor string) ([]FriendOfFriend, string, error)

ListFriendsOfFriends returns friends-of-friends discovery results.

type FriendUser

type FriendUser struct {
	ID          string
	Username    string
	DisplayName string
	AvatarURL   string
	LangTag     string
	Location    string
	Timezone    string
	Metadata    string
	CreateTime  time.Time
	UpdateTime  time.Time
	EdgeCount   int32
}

FriendUser holds profile fields returned in friend lists.

type Group

type Group struct {
	ID          string
	CreatorID   string
	Name        string
	Description string
	AvatarURL   string
	LangTag     string
	Metadata    string
	State       int
	EdgeCount   int
	MaxCount    int
	CreateTime  time.Time
	UpdateTime  time.Time
	DisableTime time.Time
}

Group represents a guild/clan record.

func CreateGroup

func CreateGroup(ctx context.Context, pool *pgxpool.Pool, creatorID, name, description, avatarURL, langTag string) (*Group, error)

CreateGroup creates a new group and designates the creator as SuperAdmin. Legacy helper: open=true, max_count=100.

func CreateGroupWithParams

func CreateGroupWithParams(ctx context.Context, pool *pgxpool.Pool, creatorID string, p CreateGroupParams) (*Group, error)

CreateGroupWithParams creates a group with full options.

func GroupsGetID

func GroupsGetID(ctx context.Context, pool *pgxpool.Pool, ids []string) ([]*Group, error)

GroupsGetID returns groups by IDs (active only).

func GroupsGetRandom

func GroupsGetRandom(ctx context.Context, pool *pgxpool.Pool, count int) ([]*Group, error)

GroupsGetRandom returns up to count random active groups.

func ListGroups

func ListGroups(ctx context.Context, pool *pgxpool.Pool, name, langTag string, open *bool, members int, limit int, cursor string) ([]*Group, string, error)

ListGroups searches active groups with filters and pagination.

type GroupMember

type GroupMember struct {
	UserID   string
	Username string
	Role     int
}

func ListGroupMembers

func ListGroupMembers(ctx context.Context, pool *pgxpool.Pool, groupID string, limit int, cursor string) ([]GroupMember, string, error)

ListGroupMembers lists users belonging to a group (members only by default; includeRequests adds state 3).

type NoopNotifier

type NoopNotifier struct{}

NoopNotifier is a no-op FriendNotifier used in tests.

func (NoopNotifier) Notify

type SteamFriendProfile

type SteamFriendProfile struct {
	SteamID uint64 `json:"steamid"`
}

SteamFriendProfile is a minimal Steam friend profile.

type UserGroupRelation

type UserGroupRelation struct {
	Group *Group
	Role  int
}

func ListUserGroups

func ListUserGroups(ctx context.Context, pool *pgxpool.Pool, userID string, limit int, cursor string) ([]UserGroupRelation, string, error)

ListUserGroups lists all groups a user belongs to.

Jump to

Keyboard shortcuts

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