This commit is contained in:
殷勇 2023-09-05 19:39:27 +08:00
parent 6b9bcbaed1
commit bc98f0b5be
4 changed files with 41 additions and 34 deletions

View File

@ -19,6 +19,7 @@ type Friendship struct {
type FriendRequest struct { type FriendRequest struct {
accountId string accountId string
isFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband isFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband
requestTime int32 // send time
} }
// FriendBlackList 直接存列表, 黑名单上限就50个 // FriendBlackList 直接存列表, 黑名单上限就50个

View File

@ -118,11 +118,13 @@ func (fm *FriendsMgr) loadUsersResult(err error, rows *f5.DataSet) {
fmt.Printf("loadUsersResult err:%v \n", err) fmt.Printf("loadUsersResult err:%v \n", err)
return return
} }
fm.users = make(map[string]*User, 100) fm.users = make(map[string]*User)
for rows.Next() { for rows.Next() {
user := &User{ user := &User{
accountId: q5.ToString(*rows.GetByIndex(0)), accountId: q5.ToString(*rows.GetByIndex(0)),
username: q5.ToString(*rows.GetByIndex(1)), username: q5.ToString(*rows.GetByIndex(1)),
friendRequest: make(map[string]*FriendRequest, MaxPendingFriendReqs),
friendBlackList: make(map[string]*FriendBlackList, MaxBlockedMembers),
} }
fm.users[user.accountId] = user fm.users[user.accountId] = user
} }
@ -153,17 +155,24 @@ func (fm *FriendsMgr) loadFriendshipsResult(err error, rows *f5.DataSet) {
if user1 == nil || user2 == nil { if user1 == nil || user2 == nil {
continue continue
} }
friendship := &Friendship{}
friendship.user1 = user1 // add user1 friendship
friendship.user2 = user2 friendship1 := &Friendship{}
fm.addFriendshipToMap(user1.accountId, friendship) friendship1.user1 = user1
fm.addFriendshipToMap(user2.accountId, friendship) friendship1.user2 = user2
fm.addFriendshipToMap(user1.accountId, friendship1)
// add user2 friendship
friendship2 := &Friendship{}
friendship2.user1 = user2
friendship2.user2 = user1
fm.addFriendshipToMap(user2.accountId, friendship2)
} }
} }
// loadPendingRequests 加载等待验证好友请求 // loadPendingRequests 加载等待验证好友请求
func (fm *FriendsMgr) loadPendingRequests() { func (fm *FriendsMgr) loadPendingRequests() {
fields := []string{"sender_account_id", "receiver_account_id"} fields := []string{"sender_account_id", "receiver_account_id", "createtime"}
f5.GetJsStyleDb().Select( f5.GetJsStyleDb().Select(
FRIEND_DB, FRIEND_DB,
"t_friend_pending_request", "t_friend_pending_request",
@ -183,6 +192,7 @@ func (fm *FriendsMgr) loadPendingRequestsResult(err error, rows *f5.DataSet) {
for rows.Next() { for rows.Next() {
senderAccountId := q5.ToString(*rows.GetByIndex(0)) senderAccountId := q5.ToString(*rows.GetByIndex(0))
receiverAccountId := q5.ToString(*rows.GetByIndex(1)) receiverAccountId := q5.ToString(*rows.GetByIndex(1))
requestTime := q5.ToInt32(*rows.GetByIndex(2))
user := fm.users[receiverAccountId] user := fm.users[receiverAccountId]
if user != nil { if user != nil {
if user.friendRequest == nil { if user.friendRequest == nil {
@ -191,6 +201,7 @@ func (fm *FriendsMgr) loadPendingRequestsResult(err error, rows *f5.DataSet) {
friendRequest := &FriendRequest{ friendRequest := &FriendRequest{
accountId: senderAccountId, accountId: senderAccountId,
isFriendship: 0, isFriendship: 0,
requestTime: requestTime,
} }
user.friendRequest[receiverAccountId] = friendRequest user.friendRequest[receiverAccountId] = friendRequest
} }

View File

@ -99,25 +99,23 @@ func (fm *FriendsMgr) addFriendRequest(account1Id string, account2Id string, cb
found1 := user1.IsInBlacklist(account2Id) found1 := user1.IsInBlacklist(account2Id)
found2 := user2.IsInBlacklist(account1Id) found2 := user2.IsInBlacklist(account1Id)
if found1 == true || found2 == true { if found1 || found2 {
cb(1, "xxx") cb(1, "xxx")
} }
// 已发送请求 // 已发送请求
if _, ok := user1.friendRequest[account2Id]; ok { friendRequest, ok := user1.friendRequest[account2Id]
if ok && friendRequest.isFriendship == 1 {
cb(1, "xxx") cb(1, "xxx")
} }
// 自己好友已满 // 好友已满
if fm.getFriendCount(account1Id) >= MaxFriendMembers { if fm.getFriendCount(account1Id) >= MaxFriendMembers || fm.getFriendCount(account2Id) >= MaxFriendMembers {
cb(1, "xxx") cb(1, "xxx")
} }
// 对方好友已满
if fm.getFriendCount(account2Id) >= MaxFriendMembers { // 待请求已满
cb(1, "xxx") if fm.getFriendRequestCount(account1Id) >= MaxPendingFriendReqs || fm.getFriendRequestCount(account2Id) >= MaxPendingFriendReqs {
}
// 对方待请求已满
if fm.getFriendRequestCount(account2Id) >= MaxPendingFriendReqs {
cb(1, "xxx") cb(1, "xxx")
} }
@ -228,10 +226,11 @@ func (fm *FriendsMgr) deleteFriendShip(account1Id, account2Id string, cb func(er
// getFriendCount 好友数量 // getFriendCount 好友数量
func (fm *FriendsMgr) getFriendCount(accountId string) int { func (fm *FriendsMgr) getFriendCount(accountId string) int {
if friends, ok := fm.friendships[accountId]; ok { return len(fm.friendships[accountId])
return len(friends) //if friends, ok := fm.friendships[accountId]; ok {
} // return len(friends)
return 0 //}
//return 0
} }
// getFriendRequestCount 等待请求数量 // getFriendRequestCount 等待请求数量
@ -245,7 +244,6 @@ func (fm *FriendsMgr) getFriendRequestCount(accountId string) int {
// listFriend 我的好友列表 // listFriend 我的好友列表
func (fm *FriendsMgr) listFriends(accountId string) []*User { func (fm *FriendsMgr) listFriends(accountId string) []*User {
// By default, Users member data count:10
var users []*User var users []*User
for _, friendship := range fm.friendships[accountId] { for _, friendship := range fm.friendships[accountId] {
if friendship.user1.accountId != accountId { if friendship.user1.accountId != accountId {
@ -313,11 +311,8 @@ func (fm *FriendsMgr) removeBlacklist(account1Id string, account2Id string, cb f
} }
func (fm *FriendsMgr) addFriendshipToMap(accountID string, friendship *Friendship) { func (fm *FriendsMgr) addFriendshipToMap(accountID string, friendship *Friendship) {
if fm.friendships[accountID] == nil { user1Friendships := fm.friendships[accountID]
fm.friendships[accountID] = []*Friendship{friendship} user1Friendships = append(user1Friendships, friendship)
} else {
fm.friendships[accountID] = append(fm.friendships[accountID], friendship)
}
} }
func (fm *FriendsMgr) clearExpiredCaches() { func (fm *FriendsMgr) clearExpiredCaches() {

View File

@ -191,7 +191,7 @@ func (gm *GuildMgr) Approve(operatorAccountId, accountId string, cb func(errCode
} }
// 公会干部及以上仅可操作 // 公会干部及以上仅可操作
err := gm.checkOperatorPerm(operatorMember, GuildMemberLevelViceLeader) err := gm.CheckOperatorPerm(operatorMember, GuildMemberLevelViceLeader)
if err != nil { if err != nil {
cb(1, "XXX") cb(1, "XXX")
} }
@ -244,7 +244,7 @@ func (gm *GuildMgr) Reject(operatorAccountId, accountId string, cb func(errCode
cb(1, "XXX") cb(1, "XXX")
} }
// 公会干部及以上仅可操作 // 公会干部及以上仅可操作
err := gm.checkOperatorPerm(operatorMember, GuildMemberLevelViceLeader) err := gm.CheckOperatorPerm(operatorMember, GuildMemberLevelViceLeader)
if err != nil { if err != nil {
cb(1, "XXX") cb(1, "XXX")
} }
@ -324,7 +324,7 @@ func (gm *GuildMgr) DismissMember(operatorAccountId, accountId string, cb func(e
} }
// 公会干部及以上仅可操作 // 公会干部及以上仅可操作
err := gm.checkOperatorPerm(operatorMember, GuildMemberLevelViceLeader) err := gm.CheckOperatorPerm(operatorMember, GuildMemberLevelViceLeader)
if err != nil { if err != nil {
cb(1, "XXX") cb(1, "XXX")
} }
@ -560,9 +560,9 @@ func (gm *GuildMgr) RandomGuilds() []*Guild {
return results return results
} }
func (gm *GuildMgr) checkOperatorPerm(operatorMember *GuildMember, level int) error { func (gm *GuildMgr) CheckOperatorPerm(operatorMember *GuildMember, level int) error {
if operatorMember.level > level { if operatorMember.level > level {
return fmt.Errorf("checkOperatorPerm: no permission[%s-%d]", operatorMember.accountId, operatorMember.level) return fmt.Errorf("CheckOperatorPerm: no permission[%s-%d]", operatorMember.accountId, operatorMember.level)
} }
return nil return nil
} }