345 lines
8.4 KiB
Go
345 lines
8.4 KiB
Go
package friend
|
|
|
|
import (
|
|
"q5"
|
|
"f5"
|
|
"fmt"
|
|
"main/constant"
|
|
)
|
|
|
|
type friendMgr struct {
|
|
friendHash map[string]map[string]int32
|
|
blackHash map[string]map[string]int32
|
|
}
|
|
|
|
func (this *friendMgr) Init() {
|
|
this.friendHash = make(map[string]map[string]int32)
|
|
this.blackHash = make(map[string]map[string]int32)
|
|
this.loadFromDB()
|
|
}
|
|
|
|
func (this *friendMgr) UnInit() {
|
|
}
|
|
|
|
func (this *friendMgr) loadFromDB() {
|
|
this.loadFriendships()
|
|
this.loadBlacklist()
|
|
}
|
|
|
|
func (this *friendMgr) loadFriendships() {
|
|
f5.GetSysLog().Info("friendMgr.loadFriendships begin")
|
|
lastIdx := f5.GetJsStyleDb().SyncBatchLoadFullTable(
|
|
constant.FRIEND_DB,
|
|
"SELECT * FROM t_friend_relationship WHERE idx > %d AND deleted = 0",
|
|
func (ds *f5.DataSet) {
|
|
accountId1 := ds.GetByName("account_id1")
|
|
accountId2 := ds.GetByName("account_id2")
|
|
addTime := q5.ToInt32(ds.GetByName("add_time"))
|
|
this.addFriendShip(accountId1, accountId2, addTime)
|
|
},
|
|
func (err error) {
|
|
panic(fmt.Sprintf("friendMgr.loadFriendships dberror:%s", err))
|
|
})
|
|
f5.GetSysLog().Info("friendMgr.loadFriendships end lastIdx:%d friendNum:%d blackNum:%d",
|
|
lastIdx,
|
|
len(this.friendHash),
|
|
len(this.blackHash))
|
|
}
|
|
|
|
func (this *friendMgr) loadBlacklist() {
|
|
f5.GetSysLog().Info("friendMgr.loadBlacklist begin")
|
|
lastIdx := f5.GetJsStyleDb().SyncBatchLoadFullTable(
|
|
constant.FRIEND_DB,
|
|
"SELECT * FROM t_friend_relationship WHERE idx > %d AND deleted = 0",
|
|
func (ds *f5.DataSet) {
|
|
accountId := ds.GetByName("account_id")
|
|
blockId := ds.GetByName("block_id")
|
|
addTime := q5.ToInt32(ds.GetByName("add_time"))
|
|
this.addBlackList(accountId, blockId, addTime)
|
|
},
|
|
func (err error) {
|
|
panic(fmt.Sprintf("friendMgr.loadFriendships dberror:%s", err))
|
|
})
|
|
f5.GetSysLog().Info("friendMgr.loadBlacklist end lastIdx:%d friendNum:%d blackNum:%d",
|
|
lastIdx,
|
|
len(this.friendHash),
|
|
len(this.blackHash))
|
|
}
|
|
|
|
func (this *friendMgr) IsFriend(accountId1 string, accountId2 string) bool {
|
|
myFriends := this.getFriends(accountId1)
|
|
if myFriends != nil {
|
|
if _, ok := (*myFriends)[accountId2]; ok {
|
|
return ok
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (this *friendMgr) HasFriend(accountId string) bool {
|
|
return this.GetFriendNum(accountId) > 0
|
|
}
|
|
|
|
func (this *friendMgr) GetFriendNum(accountId string) int32 {
|
|
myFriends := this.getFriends(accountId)
|
|
if myFriends != nil {
|
|
return int32(len(*myFriends))
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (this *friendMgr) TraverseFriend(accountId string, cb func(string, int32) bool) {
|
|
myFriends := this.getFriends(accountId)
|
|
if myFriends != nil {
|
|
for a, t := range *myFriends {
|
|
if !cb(a, t) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *friendMgr) GetFriendList(accountId string) []string {
|
|
friendList := []string{}
|
|
this.TraverseFriend(
|
|
accountId,
|
|
func (friendId string, addTime int32) bool {
|
|
ele := q5.NewSliceElement(&friendList)
|
|
*ele = friendId
|
|
return true
|
|
})
|
|
return friendList
|
|
}
|
|
|
|
func (this *friendMgr) TraverseBlack(accountId string, cb func(string, int32) bool) {
|
|
myBlacks := this.getBlacks(accountId)
|
|
if myBlacks != nil {
|
|
for a, t := range *myBlacks {
|
|
if !cb(a, t) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *friendMgr) GetBlackList(accountId string) []string {
|
|
blackList := []string{}
|
|
this.TraverseBlack(
|
|
accountId,
|
|
func (blackId string, addTime int32) bool {
|
|
ele := q5.NewSliceElement(&blackList)
|
|
*ele = blackId
|
|
return true
|
|
})
|
|
return blackList
|
|
}
|
|
|
|
func (this *friendMgr) getFriends(accountId string) *map[string]int32 {
|
|
if friends, ok := this.friendHash[accountId]; ok {
|
|
return &friends
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *friendMgr) getBlacks(accountId string) *map[string]int32 {
|
|
if blacks, ok := this.blackHash[accountId]; ok {
|
|
return &blacks
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *friendMgr) AsyncGetApplyList(lastIdx int64, accountId string, cb func(int32, string, int64, []string)) {
|
|
f5.GetJsStyleDb().PageQuery(
|
|
constant.FRIEND_DB,
|
|
50,
|
|
0,
|
|
"SELECT * FROM t_friend_apply",
|
|
[]string{},
|
|
f5.GetDbFilter().Comp(
|
|
f5.GetDbFilter().GT("idx", q5.ToString(lastIdx)).And(),
|
|
f5.GetDbFilter().EQ("target_id", accountId).And(),
|
|
f5.GetDbFilter().EQ("status", "0"),
|
|
),
|
|
"",
|
|
func (err error, pg *f5.Pagination) {
|
|
var lastSinceId int64
|
|
if err != nil {
|
|
cb(500, "", lastSinceId, []string{})
|
|
}
|
|
if pg.Rows.Next() {
|
|
idx := q5.ToInt64(pg.Rows.GetByName("idx"))
|
|
if idx > lastSinceId {
|
|
lastSinceId = idx
|
|
} else {
|
|
panic(fmt.Sprintf("AsyncGetApply idx error:%s %s", idx, lastSinceId))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func (this *friendMgr) AsyncAddFriend(senderId string, targetId string, cb func(int32, string)) {
|
|
if this.IsFriend(senderId, targetId) {
|
|
cb(0, "")
|
|
return
|
|
}
|
|
nowTime := f5.GetApp().GetNowSeconds()
|
|
f5.GetJsStyleDb().Upsert(
|
|
constant.FRIEND_DB,
|
|
"t_friend_apply",
|
|
[][]string{
|
|
{"sender_id", senderId},
|
|
{"target_id", targetId},
|
|
},
|
|
[][]string{
|
|
{"status", q5.ToString(0)},
|
|
{"last_apply_time", q5.ToString(nowTime)},
|
|
},
|
|
[][]string{
|
|
{"sender_id", senderId},
|
|
{"target_id", targetId},
|
|
{"status", q5.ToString(0)},
|
|
{"last_apply_time", q5.ToString(nowTime)},
|
|
{"createtime", q5.ToString(nowTime)},
|
|
{"modifytime", q5.ToString(nowTime)},
|
|
},
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
cb(1, "")
|
|
return
|
|
}
|
|
this.asyncSetApplyStatus(senderId, targetId, 1)
|
|
cb(0, "")
|
|
})
|
|
}
|
|
|
|
func (this *friendMgr) AsyncAccpetApply(senderId string, targetId string, cb func(int32, string)) {
|
|
if senderId == targetId {
|
|
cb(1, "")
|
|
return
|
|
}
|
|
accountId1, accountId2 := this.sortAccounts(senderId, targetId)
|
|
queryCb := func () {
|
|
if this.IsFriend(senderId, targetId) {
|
|
this.asyncSetApplyStatus(senderId, targetId, 1)
|
|
cb(0, "")
|
|
return
|
|
} else {
|
|
nowTime := f5.GetApp().GetNowSeconds()
|
|
f5.GetJsStyleDb().Upsert(
|
|
constant.FRIEND_DB,
|
|
"t_friend_relationship",
|
|
[][]string{
|
|
{"account_id1", accountId1},
|
|
{"account_id2", accountId2},
|
|
},
|
|
[][]string{
|
|
{"add_time", q5.ToString(nowTime)},
|
|
{"deleted", "0"},
|
|
},
|
|
[][]string{
|
|
{"account_id1", accountId1},
|
|
{"account_id2", accountId2},
|
|
{"add_time", q5.ToString(nowTime)},
|
|
{"deleted", "0"},
|
|
{"createtime", q5.ToString(nowTime)},
|
|
{"modifytime", q5.ToString(nowTime)},
|
|
},
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
cb(1, "")
|
|
return
|
|
}
|
|
this.asyncSetApplyStatus(senderId, targetId, 1)
|
|
this.addFriendShip(accountId1, accountId2, int32(nowTime))
|
|
cb(0, "")
|
|
})
|
|
}
|
|
}
|
|
f5.GetJsStyleDb().OrmSelectOne(
|
|
constant.FRIEND_DB,
|
|
"t_friend_apply",
|
|
[][]string{
|
|
{"sender_id", senderId},
|
|
{"target_id", targetId},
|
|
{"status", "0"},
|
|
},
|
|
func (err error, ds *f5.DataSet) {
|
|
if err != nil {
|
|
cb(1, "")
|
|
return
|
|
}
|
|
queryCb()
|
|
})
|
|
}
|
|
|
|
func (this *friendMgr) AsyncRejectApply(senderId string, targetId string, cb func(int32, string)) {
|
|
|
|
}
|
|
|
|
func (this *friendMgr) AsynDeleteFriend(senderId string, targetId string, cb func(int32, string)) {
|
|
|
|
}
|
|
|
|
func (this *friendMgr) AsyncAddBlack(senderId string, targetId string, cb func(int32, string)) {
|
|
|
|
}
|
|
|
|
func (this *friendMgr) AsyncRemoveBlack(senderId string, targetId string, cb func(int32, string)) {
|
|
|
|
}
|
|
|
|
func (this *friendMgr) addFriendShip(accountId1 string, accountId2 string, addTime int32) {
|
|
{
|
|
friends := this.getFriends(accountId1)
|
|
if friends != nil {
|
|
this.friendHash[accountId1] = make(map[string]int32)
|
|
friends = this.getFriends(accountId1)
|
|
}
|
|
(*friends)[accountId2] = addTime
|
|
}
|
|
{
|
|
friends := this.getFriends(accountId2)
|
|
if friends != nil {
|
|
this.friendHash[accountId2] = make(map[string]int32)
|
|
friends = this.getFriends(accountId2)
|
|
}
|
|
(*friends)[accountId1] = addTime
|
|
}
|
|
}
|
|
|
|
func (this *friendMgr) addBlackList(accountId string, blockId string, addTime int32) {
|
|
{
|
|
blacks := this.getBlacks(accountId)
|
|
if blacks != nil {
|
|
this.blackHash[accountId] = make(map[string]int32)
|
|
blacks = this.getBlacks(accountId)
|
|
}
|
|
(*blacks)[blockId] = addTime
|
|
}
|
|
}
|
|
|
|
func (this *friendMgr) sortAccounts(senderId string, targetId string) (string, string) {
|
|
if senderId < targetId {
|
|
return senderId, targetId
|
|
} else {
|
|
return targetId, senderId
|
|
}
|
|
}
|
|
|
|
func (this *friendMgr) asyncSetApplyStatus(senderId string, targetId string, status int32) {
|
|
f5.GetJsStyleDb().Update(
|
|
constant.FRIEND_DB,
|
|
"t_friend_apply",
|
|
[][]string{
|
|
{"status", q5.ToString(status)},
|
|
{"modifytime", q5.ToString(f5.GetApp().GetNowSeconds())},
|
|
},
|
|
[][]string{
|
|
{"sender_id", senderId},
|
|
{"target_id", targetId},
|
|
},
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
|
|
})
|
|
}
|