diff --git a/database/frienddb.sql b/database/frienddb.sql index 9e2acdb9..c3fec924 100644 --- a/database/frienddb.sql +++ b/database/frienddb.sql @@ -15,7 +15,7 @@ CREATE TABLE `t_friend_ships` ( `idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id', `account1_id` varchar(60) NOT NULL DEFAULT '', `account2_id` varchar(60) NOT NULL DEFAULT '', -`is_delete_friendship` tinyint DEFAULT '0' COMMENT '是否已删除好友关系, 0 no, 1 deleted', +`is_friendship` tinyint DEFAULT '0' COMMENT '是否好友关系, 0 pending, 1 ok, 2 reject, 3 disband', `createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间', `modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间', PRIMARY KEY (`idx`), @@ -23,18 +23,6 @@ UNIQUE KEY `friendship` (`account1_id`,`account2_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin COMMENT "已经成为好友关系"; -drop table if exists `t_friend_pending_request`; -CREATE TABLE t_friend_pending_request ( -`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id', -sender_account_id varchar(60) NOT NULL DEFAULT '', -receiver_account_id varchar(60) NOT NULL DEFAULT '', -is_friendship tinyint DEFAULT '0' COMMENT '已经是好友关系, 0 pending, 1 ok, 2 reject, 3 deleted', -`createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间', -`modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间', -PRIMARY KEY (`idx`), -UNIQUE KEY `friend_req` (`sender_account_id`,`receiver_account_id`) -) COMMENT "等待验证的好友请求"; - drop table if exists t_friend_blacklist; CREATE TABLE `t_friend_blacklist` ( `idx` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增id', diff --git a/server/imserver/constant.go b/server/imserver/constant.go index c1e42a50..54475047 100644 --- a/server/imserver/constant.go +++ b/server/imserver/constant.go @@ -18,19 +18,22 @@ const ( // im server friend const ( - MaxFriendMembers = 200 - MaxPendingFriendReqs = 20 - MaxBlockedMembers = 50 - SearchWord = 42 // 搜索关键字 - MaxSearchResults = 20 // 搜索结果20条 - FriendReqsStatusDefault = 0 // 好友请求状态, 等待中 - FriendReqsStatusOk = 1 // 好友请求状态, 接受 - FriendReqsStatusReject = 2 // 好友请求状态, 拒绝 - FriendReqsStatusDeleted = 3 // 好友请求状态, 已删除 - FriendshipStatusOk = 0 // 好友关系状态 正常 - FriendshipStatusDeleted = 1 // 好友关系状态 已删除0 - BlacklistStatusDefault = 0 // 好友黑名单状态 是否已移除黑名单 默认0,添加进来 未移除 - BlacklistStatusIsRemoved = 1 // 好友黑名单状态 已移除黑名单 + MaxFriendMembers = 200 + MaxPendingFriendReqs = 20 + MaxBlockedMembers = 50 + SearchWord = 42 // 搜索关键字 + MaxSearchResults = 20 // 搜索结果20条 + + FriendshipStatusPending = 0 // 好友关系状态 等待中 pending + FriendshipStatusOK = 1 // 好友关系状态 接受 ok + FriendshipStatusReject = 2 // 好友关系状态 拒绝 reject + FriendshipStatusDeleted = 3 // 好友关系状态 已删除 disband + + BlacklistStatusDefault = 0 // 好友黑名单状态 是否已移除黑名单 默认0,添加进来 未移除 + BlacklistStatusIsRemoved = 1 // 好友黑名单状态 已移除黑名单 + + OnlineStatusOff = 0 // 在线状态 离线 + OnlineStatus = 1 // 在线状态 在线 ) const ( diff --git a/server/imserver/friends.go b/server/imserver/friends.go index 36e73789..79cc6952 100644 --- a/server/imserver/friends.go +++ b/server/imserver/friends.go @@ -2,30 +2,17 @@ package main type User struct { AccountId string - FriendRequest map[string]*FriendRequest // targetAccountId -> FriendRequest - FriendBlackList map[string]*FriendBlackList // targetAccountId -> FriendBlackList - Friendships []*Friendship + FriendBlackList map[string]*FriendBlackList // BlockedAccountId -> FriendBlackList + Friendships map[string]*Friendship // FriendAccountId -> Friendship } -type Friendship2 struct { +type Friendship struct { FriendAccountId string // friend's account id IsFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband - RequestTime int32 // send time + RequestTime int64 // send time } -// Friendship user1, user2 构成一个好友关系 -type Friendship struct { - User1 *User - User2 *User -} - -type FriendRequest struct { - AccountId string // target account id - IsFriendship int32 // 0 pending, 1 ok, 2 reject, 3 disband - RequestTime int32 // send time -} - -// FriendBlackList 直接存列表, 黑名单上限就50个 +// FriendBlackList 直接存列表, 黑名单上限50个 type FriendBlackList struct { AccountId string IsRemoved int32 // default: 0, isRemoved @@ -33,49 +20,34 @@ type FriendBlackList struct { func NewUser(accountId string) *User { user := &User{ - AccountId: accountId, - FriendRequest: make(map[string]*FriendRequest), + AccountId: accountId, + //FriendRequest: make(map[string]*FriendRequest), FriendBlackList: make(map[string]*FriendBlackList), - Friendships: make([]*Friendship, 0, MaxFriendMembers), + Friendships: make(map[string]*Friendship, MaxFriendMembers), } return user } -func (u *User) AddFriendRequest(account2Id string) { - friendRequest := &FriendRequest{ - AccountId: account2Id, - IsFriendship: 0, +func (u *User) AddFriendRequest(account2Id string, IsFriendship int32, requestTime int64) { + friendShip := &Friendship{ + FriendAccountId: account2Id, + IsFriendship: IsFriendship, + RequestTime: requestTime, } - 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) SetFriendRequest(account2Id string) { - friendRequest := u.FriendRequest[account2Id] - friendRequest.IsFriendship = 0 + u.Friendships[account2Id] = friendShip } func (u *User) RemoveFriendRequest(account2Id string) { - delete(u.FriendRequest, account2Id) + delete(u.Friendships, account2Id) } -func (u *User) IsInReq(targetAccountId string) bool { - friendRequest, exists := u.FriendRequest[targetAccountId] - return exists && friendRequest.IsFriendship != 1 +func (u *User) RemoveFriendShip(account2Id string) { + delete(u.Friendships, account2Id) +} + +func (u *User) IsInReq(FriendAccountId string) bool { + friendRequest, exists := u.Friendships[FriendAccountId] + return exists && friendRequest.IsFriendship == 0 } // AddBlacklist 加入黑名单 diff --git a/server/imserver/friendsdbmgr.go b/server/imserver/friendsdbmgr.go index 28a346a2..24c68bed 100644 --- a/server/imserver/friendsdbmgr.go +++ b/server/imserver/friendsdbmgr.go @@ -4,6 +4,7 @@ import ( "f5" "fmt" "q5" + "strings" ) func (fm *FriendsMgr) upsertFriendRequest(account1Id string, account2Id string, isFriendship string, cb func(error)) { @@ -31,7 +32,9 @@ func (fm *FriendsMgr) upsertFriendRequest(account1Id string, account2Id string, ) } -func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isDeleteFriendship int, cb func(error)) { +func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isFriendship int, requestTime int64, cb func(error)) { + account1Id, account2Id = SwapAccountIds(account1Id, account2Id) + where := [][]string{ {"account1_id", account1Id}, {"account2_id", account2Id}, @@ -39,10 +42,13 @@ func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isD insertKv := [][]string{ {"account1_id", account1Id}, {"account2_id", account2Id}, - {"is_delete_friendship", q5.ToString(isDeleteFriendship)}, + {"is_friendship", q5.ToString(isFriendship)}, + {"createtime", q5.ToString(requestTime)}, + {"modifytime", q5.ToString(requestTime)}, } updateKv := [][]string{ - {"is_delete_friendship", q5.ToString(isDeleteFriendship)}, + {"is_friendship", q5.ToString(isFriendship)}, + {"modifytime", q5.ToString(requestTime)}, } f5.GetJsStyleDb().Upsert( FRIEND_DB, @@ -189,8 +195,9 @@ func (fm *FriendsMgr) findUsersByUsername(username string, sinceId int64, cb fun } // loadFriendships 加载好友关系表 -func (fm *FriendsMgr) loadFriendships(user *User, where [][]string) { - fields := []string{"account1_id", "account2_id"} +func (fm *FriendsMgr) loadFriendships() { + fields := []string{"account1_id", "account2_id", "is_friendship", "createtime"} + var where [][]string f5.GetJsStyleDb().Select( FRIEND_DB, "t_friend_ships", @@ -203,40 +210,39 @@ func (fm *FriendsMgr) loadFriendships(user *User, where [][]string) { for rows.Next() { account1Id := q5.ToString(*rows.GetByIndex(0)) account2Id := q5.ToString(*rows.GetByIndex(1)) - if user.AccountId == account1Id { - user2 := NewUser(account2Id) - friendMgr.AddUser(account2Id, user2) - friendship1 := &Friendship{} - friendship1.User1 = user - friendship1.User2 = user2 - fm.AddFriendshipToMap(user.AccountId, friendship1) - cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) + isFriendship := q5.ToInt32(*rows.GetByIndex(2)) + requestTime := q5.ToInt64(*rows.GetByIndex(3)) + + user1 := NewUser(account1Id) + friendMgr.AddUser(account1Id, user1) + friendship1 := &Friendship{ + FriendAccountId: account2Id, + IsFriendship: isFriendship, + RequestTime: requestTime, } - if user.AccountId == account2Id { - user2 := NewUser(account1Id) - //friendMgr.AddUser(account1Id, user2) - friendship := &Friendship{} - friendship.User1 = user - friendship.User2 = user2 - fm.AddFriendshipToMap(account2Id, friendship) - cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {}) + user1.Friendships[account2Id] = friendship1 + + user2 := NewUser(account2Id) + friendMgr.AddUser(account2Id, user2) + friendship2 := &Friendship{ + FriendAccountId: account1Id, + IsFriendship: isFriendship, + RequestTime: requestTime, } + user2.Friendships[account1Id] = friendship2 + + cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {}) + cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) } }, ) } -// loadPendingRequests 加载等待验证好友请求 -func (fm *FriendsMgr) loadPendingRequests(user *User) { - fields := []string{"sender_account_id", "receiver_account_id", "createtime"} - // 还未成功好友关系的 请求列表 - where := [][]string{ - {"receiver_account_id", user.AccountId}, - {"is_friendship", "0"}, - } +func (fm *FriendsMgr) loadUserFriendships(user *User, where [][]string) { + fields := []string{"account1_id", "account2_id", "is_friendship", "createtime"} f5.GetJsStyleDb().Select( FRIEND_DB, - "t_friend_pending_request", + "t_friend_ships", fields, where, func(err error, rows *f5.DataSet) { @@ -244,32 +250,51 @@ func (fm *FriendsMgr) loadPendingRequests(user *User) { return } for rows.Next() { - senderAccountId := q5.ToString(*rows.GetByIndex(0)) - //receiverAccountId := q5.ToString(*rows.GetByIndex(1)) - requestTime := q5.ToInt32(*rows.GetByIndex(2)) + account1Id := q5.ToString(*rows.GetByIndex(0)) + account2Id := q5.ToString(*rows.GetByIndex(1)) + isFriendship := q5.ToInt32(*rows.GetByIndex(2)) + requestTime := q5.ToInt64(*rows.GetByIndex(3)) + if user.AccountId == account1Id { + friendship1 := &Friendship{ + FriendAccountId: account2Id, + IsFriendship: isFriendship, + RequestTime: requestTime, + } + user.Friendships[account2Id] = friendship1 - // load profile - cacheMgr.LoadPlayerProfile(senderAccountId, func(playerProfile *PlayerProfile) {}) - - friendRequest := &FriendRequest{ - AccountId: senderAccountId, - IsFriendship: 0, - RequestTime: requestTime, + user2 := NewUser(account2Id) + friendMgr.AddUser(account2Id, user2) + friendship2 := &Friendship{ + FriendAccountId: account1Id, + IsFriendship: isFriendship, + RequestTime: requestTime, + } + user2.Friendships[account1Id] = friendship2 + cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) + } + + if user.AccountId == account2Id { + friendUser := NewUser(account1Id) + friendMgr.AddUser(account1Id, friendUser) + friendship := &Friendship{ + FriendAccountId: account1Id, + IsFriendship: isFriendship, + RequestTime: requestTime, + } + user.Friendships[account1Id] = friendship + cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) } - user.FriendRequest[senderAccountId] = friendRequest } }, ) } // loadPendingRequests 加载等待验证好友请求 -func (fm *FriendsMgr) loadBlacklist(user *User) { +func (fm *FriendsMgr) loadBlacklist() { fields := []string{"account_id", "blocked_account_id"} where := [][]string{ - {"account_id", user.AccountId}, {"is_removed", "0"}, } - f5.GetJsStyleDb().Select( FRIEND_DB, "t_friend_blacklist", @@ -280,13 +305,22 @@ func (fm *FriendsMgr) loadBlacklist(user *User) { return } for rows.Next() { + account1Id := q5.ToString(*rows.GetByIndex(0)) account2Id := q5.ToString(*rows.GetByIndex(1)) friendBlackList := &FriendBlackList{ AccountId: account2Id, IsRemoved: 0, } + user := fm.GetUser(account1Id) user.FriendBlackList[account2Id] = friendBlackList } }, ) } + +func SwapAccountIds(account1Id, account2Id string) (string, string) { + if strings.Compare(account1Id, account2Id) > 0 { + account1Id, account2Id = account2Id, account1Id + } + return account1Id, account2Id +} diff --git a/server/imserver/friendsmgr.go b/server/imserver/friendsmgr.go index 11a4a6ed..5bbf14ed 100644 --- a/server/imserver/friendsmgr.go +++ b/server/imserver/friendsmgr.go @@ -4,8 +4,8 @@ import ( "cs" "f5" "q5" - "strings" "sync" + "time" ) type FriendsMgr struct { @@ -18,6 +18,7 @@ type FriendsMgr struct { func (fm *FriendsMgr) init() { fm.Users = make(map[string]*User) fm.UserCount = 0 + fm.loadFromDB() } func (fm *FriendsMgr) unInit() { @@ -25,6 +26,13 @@ func (fm *FriendsMgr) unInit() { // 2. Struct Data Persist to DB } +func (fm *FriendsMgr) loadFromDB() { + // 加载好友关系表 列表 + fm.loadFriendships() + // 加载黑名单列表 + // fm.loadBlacklist() +} + // SearchByAccountId 搜索指定用户 func (fm *FriendsMgr) SearchByAccountId(accountId string, cb func(errCode int32, errMsg string, playerProfile *PlayerProfile)) { friendMgr.findPlayer(accountId, func(err error, profile *PlayerProfile) { @@ -54,32 +62,22 @@ func (fm *FriendsMgr) SearchUsers(accountId, username string, sinceId int64, cb // AddFriendRequest 添加好友请求 By default, account1Id is me func (fm *FriendsMgr) AddFriendRequest(account1Id string, account2Id string, cb func(errCode int32, errMsg string)) { - //player2 := new(PlayerProfile) - //cacheMgr.GetPlayerProfile(account2Id, func(playerProfile *PlayerProfile) { - // if playerProfile != nil { - // player2 = playerProfile - // } else { - // cb(ERR_CODE_USERS_NO_EXISTS, "AddFriendRequest user no exists") - // return - // } - //}) - user1 := fm.GetUser(account1Id) user2 := fm.GetUser(account2Id) if user2 == nil { user2 = fm.LoadUser(account2Id) } - found1 := user1.IsInBlacklist(account2Id) - found2 := user2.IsInBlacklist(account1Id) - if found1 || found2 { - cb(ERR_CODE_USER_IN_BLACKLIST, "AddFriendRequest user in blacklist") - return - } + //found1 := user1.IsInBlacklist(account2Id) + //found2 := user2.IsInBlacklist(account1Id) + //if found1 || found2 { + // cb(ERR_CODE_USER_IN_BLACKLIST, "AddFriendRequest user in blacklist") + // return + //} // 已发送请求 - friendRequest, ok := user1.FriendRequest[account2Id] - if ok && friendRequest.IsFriendship == 1 { + friendship, exists := user1.Friendships[account2Id] + if exists && friendship.IsFriendship == 0 { cb(ERR_CODE_OK, "AddFriendRequest ok") return } @@ -96,18 +94,18 @@ func (fm *FriendsMgr) AddFriendRequest(account1Id string, account2Id string, cb cb(ERR_CODE_PENDING_REQUEST_IS_FULL, "AddFriendRequest pending request is full") return } - - fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusDefault), func(err error) { + requestTime := time.Now().Unix() + fm.upsertFriendShip(account1Id, account2Id, FriendshipStatusPending, requestTime, func(err error) { if err != nil { - cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "AddFriendRequest update pending request db error") - f5.GetSysLog().Info("AddFriendRequest update pending request db error") + cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "AddFriendRequest update friendship db error") + f5.GetSysLog().Info("AddFriendRequest update friendship db error") return } - user1.AddFriendRequest(account2Id) - user2.AddFriendRequest(account1Id) + user1.AddFriendRequest(account2Id, FriendshipStatusPending, requestTime) + user2.AddFriendRequest(account1Id, FriendshipStatusPending, requestTime) - // user2 profile add to cache + // Load user2 profile to cache cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) cb(ERR_CODE_OK, "AddFriendRequest ok") @@ -132,43 +130,31 @@ func (fm *FriendsMgr) AcceptFriendRequest(account1Id string, account2Id string, return } - // step1. update reqs - fm.upsertFriendRequest(account2Id, account1Id, q5.ToString(FriendReqsStatusOk), func(err error) { + requestTime := time.Now().Unix() + fm.upsertFriendShip(account1Id, account2Id, FriendshipStatusOK, requestTime, func(err error) { if err != nil { - cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "AcceptFriendRequest update pending request db error") + cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "AcceptFriendRequest update friendship db error") return } - fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusOk), func(err error) { - if err != nil { - cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "AcceptFriendRequest update pending request db error") - return - } - // step2. insert friendship - a1, a2 := SwapAccountIds(account1Id, account2Id) - fm.upsertFriendShip(a1, a2, FriendshipStatusOk, func(err error) { - if err != nil { - cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "AcceptFriendRequest update friendship db error") - return - } - // step3. create a new friendship - friendship := &Friendship{ - User1: fm.Users[account1Id], - User2: fm.Users[account2Id], - } - user1.Friendships = append(user1.Friendships, friendship) - user2.Friendships = append(user2.Friendships, friendship) - // step4. remove requests - user1.RemoveFriendRequest(account2Id) - user2.RemoveFriendRequest(account1Id) + friendship1 := &Friendship{ + FriendAccountId: account2Id, + IsFriendship: FriendshipStatusOK, + RequestTime: time.Now().Unix(), + } + user1.Friendships[account2Id] = friendship1 - // step5. add cache - cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {}) - cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) + friendship2 := &Friendship{ + FriendAccountId: account1Id, + IsFriendship: FriendshipStatusOK, + RequestTime: time.Now().Unix(), + } + user2.Friendships[account1Id] = friendship2 - cb(ERR_CODE_OK, "") - }) - }) + cacheMgr.LoadPlayerProfile(account1Id, func(playerProfile *PlayerProfile) {}) + cacheMgr.LoadPlayerProfile(account2Id, func(playerProfile *PlayerProfile) {}) + + cb(ERR_CODE_OK, "") }) } @@ -183,8 +169,8 @@ func (fm *FriendsMgr) RejectFriendRequest(account1Id string, account2Id string, // cb(ERR_CODE_USERS_NO_EXISTS, "RejectFriendRequest user no exists") // return //} - - fm.upsertFriendRequest(account2Id, account1Id, q5.ToString(FriendReqsStatusReject), func(err error) { + requestTime := time.Now().Unix() + fm.upsertFriendShip(account2Id, account1Id, FriendshipStatusReject, requestTime, func(err error) { if err != nil { cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "RejectFriendRequest update pending request db error") return @@ -208,44 +194,18 @@ func (fm *FriendsMgr) DeleteFriendShip(account1Id, account2Id string, cb func(er cb(ERR_CODE_USERS_NO_EXISTS, "DeleteFriendShip user no exists") return } + user1.RemoveFriendShip(account2Id) + user2.RemoveFriendShip(account1Id) - var found bool - found, friendShipIndex := fm.FindFriendshipIndex(account1Id, account2Id) - if found { - // 删除好友请求, upsert 不存在则新增,存在则替换值 - fm.upsertFriendRequest(account1Id, account2Id, q5.ToString(FriendReqsStatusDeleted), func(err error) { - if err != nil { - f5.GetSysLog().Info("DeleteFriendShip me, accountId:[%s-%s], db error:%s", account1Id, account2Id, err) - cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "DeleteFriendShip update pending request db error") - return - } - user1.Friendships = append(user1.Friendships[:friendShipIndex], user1.Friendships[friendShipIndex+1:]...) - - found2, friendShipIndex2 := fm.FindFriendshipIndex(account2Id, account1Id) - if found2 { - fm.upsertFriendRequest(account2Id, account1Id, q5.ToString(FriendReqsStatusDeleted), func(err error) { - if err != nil { - f5.GetSysLog().Info("DeleteFriendShip, accountId:[%s-%s], db error:%s", account1Id, account2Id, err) - cb(ERR_CODE_UPDATE_PENDING_REQUEST_DB_FAIL, "DeleteFriendShip update pending request db error") - return - } - user2.Friendships = append(user2.Friendships[:friendShipIndex2], user2.Friendships[friendShipIndex2+1:]...) - // Delete friendship DB - a1, a2 := SwapAccountIds(account1Id, account2Id) - fields := [][]string{{"is_delete_friendship", q5.ToString(FriendshipStatusDeleted)}} - fm.updateFriendShip(a1, a2, fields, func(err error) { - if err != nil { - f5.GetSysLog().Info("DeleteFriendShip, updateFriendShip accountId:[%s-%s], db error:%s", account1Id, account2Id, err) - cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "DeleteFriendShip update friendship db error") - return - } - - cb(ERR_CODE_OK, "") - }) - }) - } - }) - } + updateFields := [][]string{{"is_friendship", q5.ToString(FriendshipStatusDeleted)}} + fm.updateFriendShip(account1Id, account2Id, updateFields, func(err error) { + if err != nil { + f5.GetSysLog().Info("DeleteFriendShip, updateFriendShip accountId:[%s-%s], db error:%s", account1Id, account2Id, err) + cb(ERR_CODE_UPDATE_FRIENDSHIP_DB_FAIL, "DeleteFriendShip update friendship db error") + return + } + cb(ERR_CODE_OK, "") + }) } // AddBlacklist 加入黑名单 @@ -316,7 +276,13 @@ func (fm *FriendsMgr) GetFriendCount(accountId string) int { func (fm *FriendsMgr) GetFriendRequestCount(accountId string) int { user := fm.GetUser(accountId) if user != nil { - return len(user.FriendRequest) + count := 0 + for _, friendship := range user.Friendships { + if friendship.IsFriendship == 0 { + count++ + } + } + return count } return 0 } @@ -324,24 +290,20 @@ func (fm *FriendsMgr) GetFriendRequestCount(accountId string) int { func (fm *FriendsMgr) CleanFriendship(accountID string) { user := fm.GetUser(accountID) if user != nil { - user.Friendships = make([]*Friendship, 0, MaxFriendMembers) + user.Friendships = make(map[string]*Friendship, MaxFriendMembers) } } func (fm *FriendsMgr) AddFriendshipToMap(accountID string, friendship *Friendship) { user := fm.GetUser(accountID) - user.Friendships = append(user.Friendships, friendship) + user.Friendships[friendship.FriendAccountId] = friendship } func (fm *FriendsMgr) GetFriendByAccountId(account1Id, account2Id string) *User { user := fm.GetUser(account1Id) - for _, friendship := range user.Friendships { - if friendship.User1.AccountId == account2Id { - return friendship.User1 - } - if friendship.User2.AccountId == account2Id { - return friendship.User2 - } + friendship, exists := user.Friendships[account2Id] + if exists && friendship.IsFriendship == 1 { + return fm.GetUser(account2Id) } return nil } @@ -349,6 +311,8 @@ func (fm *FriendsMgr) GetFriendByAccountId(account1Id, account2Id string) *User func (fm *FriendsMgr) AddUser(accountId string, user *User) { if _, exists := fm.Users[accountId]; !exists { fm.Users[accountId] = user + //fm.UserCount = len(fm.Users) + fm.UserCount++ } } @@ -365,36 +329,15 @@ func (fm *FriendsMgr) LoadUser(accountId string) *User { user := NewUser(accountId) friendMgr.AddUser(accountId, user) // 加载我的好友关系表 - friendMgr.loadFriendships(user, [][]string{ + friendMgr.loadUserFriendships(user, [][]string{ {"account1_id", user.AccountId}, }) - friendMgr.loadFriendships(user, [][]string{ + friendMgr.loadUserFriendships(user, [][]string{ {"account2_id", user.AccountId}, }) - // 加载我的等待验证好友请求 - friendMgr.loadPendingRequests(user) // 加载我的黑名单列表 - friendMgr.loadBlacklist(user) + // friendMgr.loadBlacklist(user) return user } return existsUser } - -func (fm *FriendsMgr) FindFriendshipIndex(account1Id, account2Id string) (bool, int) { - user := fm.GetUser(account1Id) - // 通常 account1Id,指自己, account2Id 指对方 - User1Friendships := user.Friendships - for i, friendship := range User1Friendships { - if friendship.User1.AccountId == account2Id || friendship.User2.AccountId == account2Id { - return true, i - } - } - return false, -1 -} - -func SwapAccountIds(account1Id, account2Id string) (string, string) { - if strings.Compare(account1Id, account2Id) > 0 { - account1Id, account2Id = account2Id, account1Id - } - return account1Id, account2Id -} diff --git a/server/imserver/player.go b/server/imserver/player.go index 05f8e703..02673159 100644 --- a/server/imserver/player.go +++ b/server/imserver/player.go @@ -93,8 +93,8 @@ func (p *Player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPendin user := friendMgr.GetUser(accountId) rspMsg := &cs.SMListPendingFriendRequest{} - for targetAccountId, friendRequest := range user.FriendRequest { - if friendRequest.IsFriendship != FriendReqsStatusDefault { + for targetAccountId, friendRequest := range user.Friendships { + if friendRequest.IsFriendship != FriendshipStatusPending { continue } profile := cacheMgr.GetPlayerProfile(targetAccountId) @@ -130,9 +130,9 @@ func (p *Player) CMListFriend(hdr *f5.MsgHdr, msg *cs.CMListFriend) { rspMsg := &cs.SMListFriend{} rspMsg.Users = make([]*cs.MFUser, 0) for _, friendship := range user.Friendships { - friendAccountId := friendship.User2.AccountId - if friendship.User1.AccountId != accountId { - friendAccountId = friendship.User1.AccountId + friendAccountId := friendship.FriendAccountId + if friendAccountId == accountId || friendship.IsFriendship != FriendshipStatusOK { + continue } playerProfile := cacheMgr.GetPlayerProfile(friendAccountId) if playerProfile != nil { diff --git a/server/imserver/playermgr.go b/server/imserver/playermgr.go index 750fd72f..af22a565 100644 --- a/server/imserver/playermgr.go +++ b/server/imserver/playermgr.go @@ -92,13 +92,14 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` Info struct { - Activated string `json:"activated"` - RenameCount string `json:"rename_count"` - AccountID string `json:"account_id"` - Name string `json:"name"` - Avatar string `json:"head_id"` - AvatarHead string `json:"head_frame"` - Rank string `json:"rank"` + Activated string `json:"activated"` + RenameCount string `json:"rename_count"` + AccountID string `json:"account_id"` + Name string `json:"name"` + Avatar string `json:"head_id"` + AvatarHead string `json:"head_frame"` + Rank string `json:"rank"` + LastLoginTime string `json:"last_login_time"` } `json:"info"` }{} err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj) @@ -111,7 +112,6 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt return } accountId := msg.GetAccountId() - player := Player{ socket: hdr.GetSocket(), accountId: accountId, @@ -128,8 +128,8 @@ func (this *PlayerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt Avatar: q5.ToInt32(resObj.Info.Avatar), AvatarHead: q5.ToInt32(resObj.Info.AvatarHead), Rank: q5.ToInt32(resObj.Info.Rank), - OnlineStatus: 0, - LastLoginTime: 0, + OnlineStatus: OnlineStatus, + LastLoginTime: q5.ToInt32(resObj.Info.LastLoginTime), } cacheMgr.SetProfile(accountId, playerProfile) // Add friends diff --git a/third_party/q5 b/third_party/q5 index fe65489d..2ce4c46c 160000 --- a/third_party/q5 +++ b/third_party/q5 @@ -1 +1 @@ -Subproject commit fe65489de27eef96e2c1879cb19dd84ee172be9c +Subproject commit 2ce4c46c710cce553a5a059dda83fdf72a038a46