Add test 删除好友

This commit is contained in:
殷勇 2023-08-23 17:03:55 +08:00
parent 8165b22cb1
commit a22512d5cd
3 changed files with 53 additions and 18 deletions

View File

@ -15,20 +15,20 @@ CREATE TABLE `t_friend_ships` (
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id', `idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
`account1_id` varchar(60) NOT NULL DEFAULT '', `account1_id` varchar(60) NOT NULL DEFAULT '',
`account2_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 '创建时间', `createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间',
`modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间', `modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间',
PRIMARY KEY (`idx`), PRIMARY KEY (`idx`),
UNIQUE KEY `friendship` (`account1_id`,`account2_id`) UNIQUE KEY `friendship` (`account1_id`,`account2_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin
COMMENT "已经成为好友" COMMENT "已经成为好友关系";
;
drop table if exists `t_friend_pending_request`; drop table if exists `t_friend_pending_request`;
CREATE TABLE t_friend_pending_request ( CREATE TABLE t_friend_pending_request (
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id', `idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
sender_account_id varchar(60) NOT NULL DEFAULT '', sender_account_id varchar(60) NOT NULL DEFAULT '',
receiver_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 '创建时间', `createtime` int NOT NULL DEFAULT '0' COMMENT '创建时间',
`modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间', `modifytime` int NOT NULL DEFAULT '0' COMMENT '修改时间',
PRIMARY KEY (`idx`), PRIMARY KEY (`idx`),

View File

@ -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 加载所有用户 // loadUsers 加载所有用户
func (fm *FriendsMgr) loadUsers() { func (fm *FriendsMgr) loadUsers() {
fields := []string{"account_id", "name"} fields := []string{"account_id", "name"}

View File

@ -4,6 +4,7 @@ import (
"cs" "cs"
"errors" "errors"
"fmt" "fmt"
"q5"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -156,13 +157,8 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string)
// step1. update reqs // step1. update reqs
fm.insertFriendRequest(account2Id, account1Id, "1") fm.insertFriendRequest(account2Id, account1Id, "1")
// step2. insert friendship // step2. insert friendship
compareResult := strings.Compare(account1Id, account2Id) a1, a2 := swapMiniAccount(account1Id, account2Id)
if compareResult > 0 { fm.insertFriendShip(a1, a2)
temp := account1Id
account1Id = account2Id
account2Id = temp
}
fm.insertFriendShip(account1Id, account2Id)
// Create a new friendship // Create a new friendship
friendship := &Friendship{ friendship := &Friendship{
@ -179,7 +175,6 @@ func (fm *FriendsMgr) acceptFriendRequest(account1Id string, account2Id string)
// rejectFriendRequest 拒绝好友请求 // rejectFriendRequest 拒绝好友请求
func (fm *FriendsMgr) rejectFriendRequest(account1Id string, account2Id string) error { func (fm *FriendsMgr) rejectFriendRequest(account1Id string, account2Id string) error {
if fm.pendingReqs[account1Id] == nil { if fm.pendingReqs[account1Id] == nil {
return errors.New("no pending friend request to reject") return errors.New("no pending friend request to reject")
} }
@ -199,17 +194,19 @@ func (fm *FriendsMgr) rejectFriendRequest(account1Id string, account2Id string)
} }
// deleteFriendShip 删除好友 // deleteFriendShip 删除好友
func (fm *FriendsMgr) deleteFriendShip(account1ID, account2ID string) error { func (fm *FriendsMgr) deleteFriendShip(account1Id, account2Id string) error {
fm.mu.Lock() fm.mu.Lock()
defer fm.mu.Unlock() defer fm.mu.Unlock()
user1Friendships := fm.friendships[account1ID] user1Friendships := fm.friendships[account1Id]
user2Friendships := fm.friendships[account2ID] user2Friendships := fm.friendships[account2Id]
var found bool var found bool
for i, friendship := range user1Friendships { for i, friendship := range user1Friendships {
if friendship.User1.Username == account2ID || friendship.User2.Username == account2ID { if friendship.User1.AccountId == account2Id || friendship.User2.AccountId == account2Id {
fm.friendships[account1ID] = append(user1Friendships[:i], user1Friendships[i+1:]...) // 删除好友请求, insert和replace, 不存在则新增,存在则替换值
fm.insertFriendRequest(account1Id, account2Id, "3")
fm.friendships[account1Id] = append(user1Friendships[:i], user1Friendships[i+1:]...)
found = true found = true
break break
} }
@ -220,12 +217,19 @@ func (fm *FriendsMgr) deleteFriendShip(account1ID, account2ID string) error {
} }
for i, friendship := range user2Friendships { for i, friendship := range user2Friendships {
if friendship.User1.Username == account1ID || friendship.User2.Username == account1ID { if friendship.User1.AccountId == account1Id || friendship.User2.AccountId == account1Id {
fm.friendships[account2ID] = append(user2Friendships[:i], user2Friendships[i+1:]...) // 删除好友请求, insert和replace, 不存在则新增,存在则替换值
fm.insertFriendRequest(account2Id, account1Id, "3")
fm.friendships[account2Id] = append(user2Friendships[:i], user2Friendships[i+1:]...)
break break
} }
} }
// Delete friendship DB
a1, a2 := swapMiniAccount(account1Id, account2Id)
fields := [][]string{{"is_delete_friendship", q5.ToString(1)}}
fm.updateFriendShip(a1, a2, fields)
return nil return nil
} }
@ -377,3 +381,14 @@ func (fm *FriendsMgr) checkInBlackList(account1Id, account2Id string) error {
} }
return nil 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
}