diff --git a/database/frienddb.sql b/database/frienddb.sql index f14cc19f..15a46284 100644 --- a/database/frienddb.sql +++ b/database/frienddb.sql @@ -15,20 +15,20 @@ 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', `createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间', `modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间', PRIMARY KEY (`idx`), UNIQUE KEY `friendship` (`account1_id`,`account2_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin -COMMENT "已经成为好友" -; +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 no', +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`), diff --git a/server/imserver/friendsdbmgr.go b/server/imserver/friendsdbmgr.go index 181b4e9a..88f0e08f 100644 --- a/server/imserver/friendsdbmgr.go +++ b/server/imserver/friendsdbmgr.go @@ -68,6 +68,26 @@ func (fm *FriendsMgr) insertFriendShip(account1Id string, account2Id string) { ) } +func (fm *FriendsMgr) updateFriendShip(account1Id string, account2Id string, fields [][]string) { + where := [][]string{ + {"account1_id", account1Id}, + {"account2_id", account2Id}, + } + f5.GetJsStyleDb().Update( + FRIEND_DB, + "t_friend_ships", + fields, + where, + func(err error, lastInsertId int64, rowsAffected int64) { + if err != nil { + fmt.Printf("error:%v\n", err) + } + fmt.Printf("lastInsertId:%d\n", lastInsertId) + fmt.Printf("rowsAffected:%d\n", rowsAffected) + }, + ) +} + // loadUsers 加载所有用户 func (fm *FriendsMgr) loadUsers() { fields := []string{"account_id", "name"} diff --git a/server/imserver/friendsmgr.go b/server/imserver/friendsmgr.go index 712e3d7a..fe346254 100644 --- a/server/imserver/friendsmgr.go +++ b/server/imserver/friendsmgr.go @@ -4,6 +4,7 @@ import ( "cs" "errors" "fmt" + "q5" "strings" "sync" "time" @@ -156,13 +157,8 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string) // step1. update reqs fm.insertFriendRequest(account2Id, account1Id, "1") // step2. insert friendship - compareResult := strings.Compare(account1Id, account2Id) - if compareResult > 0 { - temp := account1Id - account1Id = account2Id - account2Id = temp - } - fm.insertFriendShip(account1Id, account2Id) + a1, a2 := swapMiniAccount(account1Id, account2Id) + fm.insertFriendShip(a1, a2) // Create a new friendship friendship := &Friendship{ @@ -179,7 +175,6 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string) // rejectFriendRequest 拒绝好友请求 func (fm *FriendsMgr) rejectFriendRequest(account1Id string, account2Id string) error { - if fm.pendingReqs[account1Id] == nil { return errors.New("no pending friend request to reject") } @@ -199,17 +194,19 @@ func (fm *FriendsMgr) rejectFriendRequest(account1Id string, account2Id string) } // deleteFriendShip 删除好友 -func (fm *FriendsMgr) deleteFriendShip(account1ID, account2ID string) error { +func (fm *FriendsMgr) deleteFriendShip(account1Id, account2Id string) error { fm.mu.Lock() defer fm.mu.Unlock() - user1Friendships := fm.friendships[account1ID] - user2Friendships := fm.friendships[account2ID] + user1Friendships := fm.friendships[account1Id] + user2Friendships := fm.friendships[account2Id] var found bool for i, friendship := range user1Friendships { - if friendship.User1.Username == account2ID || friendship.User2.Username == account2ID { - fm.friendships[account1ID] = append(user1Friendships[:i], user1Friendships[i+1:]...) + if friendship.User1.AccountId == account2Id || friendship.User2.AccountId == account2Id { + // 删除好友请求, insert和replace, 不存在则新增,存在则替换值 + fm.insertFriendRequest(account1Id, account2Id, "3") + fm.friendships[account1Id] = append(user1Friendships[:i], user1Friendships[i+1:]...) found = true break } @@ -220,12 +217,19 @@ func (fm *FriendsMgr) deleteFriendShip(account1ID, account2ID string) error { } for i, friendship := range user2Friendships { - if friendship.User1.Username == account1ID || friendship.User2.Username == account1ID { - fm.friendships[account2ID] = append(user2Friendships[:i], user2Friendships[i+1:]...) + if friendship.User1.AccountId == account1Id || friendship.User2.AccountId == account1Id { + // 删除好友请求, insert和replace, 不存在则新增,存在则替换值 + fm.insertFriendRequest(account2Id, account1Id, "3") + fm.friendships[account2Id] = append(user2Friendships[:i], user2Friendships[i+1:]...) break } } + // Delete friendship DB + a1, a2 := swapMiniAccount(account1Id, account2Id) + fields := [][]string{{"is_delete_friendship", q5.ToString(1)}} + fm.updateFriendShip(a1, a2, fields) + return nil } @@ -377,3 +381,14 @@ func (fm *FriendsMgr) checkInBlackList(account1Id, account2Id string) error { } return nil } + +func swapMiniAccount(account1Id, account2Id string) (string, string) { + // step2. insert friendship + compareResult := strings.Compare(account1Id, account2Id) + if compareResult > 0 { + temp := account1Id + account1Id = account2Id + account2Id = temp + } + return account1Id, account2Id +}