2023-09-05 19:39:27 +08:00

98 lines
2.4 KiB
Go

package main
import "q5"
// User 用户实体
type User struct {
accountId string
username string
friendRequest map[string]*FriendRequest // accountId -> FriendRequest
friendBlackList map[string]*FriendBlackList // accountId -> FriendBlackList
}
// Friendship user1, user2 构成一个好友关系
type Friendship struct {
user1 *User
user2 *User
}
type FriendRequest struct {
accountId string
isFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband
requestTime int32 // send time
}
// FriendBlackList 直接存列表, 黑名单上限就50个
type FriendBlackList struct {
accountId string
isRemoved int32 // default: 0, isRemoved
}
// RandomUsername generates a random username
func RandomUsername() string {
return q5.RandomString(6)
}
func (u *User) AddFriendRequest(account2Id string) {
if u.friendRequest == nil {
u.friendRequest = make(map[string]*FriendRequest, MaxPendingFriendReqs)
}
friendRequest := &FriendRequest{
accountId: account2Id,
isFriendship: 0,
}
u.friendRequest[account2Id] = friendRequest
}
func (u *User) GetFriendRequest(account2Id string) *FriendRequest {
if friendRequest, exists := u.friendRequest[account2Id]; exists {
return friendRequest
}
return nil
}
func (u *User) GetAllFriendRequestAccountIds() []string {
var accountIds []string
for accountId := range u.friendRequest {
accountIds = append(accountIds, accountId)
}
return accountIds
}
func (u *User) RemoveFriendRequest(account2Id string) {
delete(u.friendRequest, account2Id)
}
func (u *User) IsInReq(targetAccountId string) bool {
friendRequest, exists := u.friendRequest[targetAccountId]
return exists && friendRequest.isFriendship != 1
}
// AddBlacklist 加入黑名单
func (u *User) AddBlacklist(b *FriendBlackList) {
u.friendBlackList[b.accountId] = b
}
// GetBlacklist 获取黑名单
func (u *User) GetBlacklist(account2Id string) *FriendBlackList {
if friendBlackList, exists := u.friendBlackList[account2Id]; exists {
return friendBlackList
}
return nil
}
// IsInBlacklist 在黑名单中
func (u *User) IsInBlacklist(account2Id string) bool {
friendBlackList, exists := u.friendBlackList[account2Id]
return exists && friendBlackList.isRemoved == 0
}
func (u *User) RemoveBlacklist(account2Id string) {
delete(u.friendBlackList, account2Id)
}
// GetBlacklistCount 获取黑名单
func (u *User) GetBlacklistCount() int {
return len(u.friendBlackList)
}