enter_room.go 2.07 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package group_c

import (
	"fmt"
	"git.hilo.cn/hilo-common/domain"
	"github.com/go-redis/redis/v8"
	"hilo-group/_const/redis_key/group_k"
	"strconv"
	"time"
)

// 用户进房
func ZAddUserEnterRoom(model *domain.Model, userId uint64, imGroupId string) {
	userKey := group_k.GetUserEnterRoomUserKey(userId)
	groupKey := group_k.GetUserEnterRoomGroupKey(imGroupId)
	if err := model.Redis.ZAdd(model, userKey, &redis.Z{
		Score:  float64(time.Now().Unix()),
		Member: imGroupId,
	}).Err(); err != nil {
		model.Log.Errorf("ZAddUserEnterRoom user fail:%v", err)
	}
	if err := model.Redis.ZAdd(model, groupKey, &redis.Z{
		Score:  float64(time.Now().Unix()),
		Member: userId,
	}).Err(); err != nil {
		model.Log.Errorf("ZAddUserEnterRoom room fail:%v", err)
	}
}

// 获取最近房间访客
iamhujiebin's avatar
iamhujiebin committed
31
// 15天
hujiebin's avatar
hujiebin committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
func GetLastRoomVisitors(model *domain.Model, imGroupId string) (userIds []uint64) {
	groupKey := group_k.GetUserEnterRoomGroupKey(imGroupId)
	res, err := model.Redis.ZRangeByScore(model, groupKey, &redis.ZRangeBy{
		Min: fmt.Sprintf("%d", time.Now().AddDate(0, 0, -15).Unix()),
		Max: "+inf",
	}).Result()
	if err != nil {
		return
	}
	for _, u := range res {
		uid, _ := strconv.ParseUint(u, 10, 64)
		if uid > 0 {
			userIds = append(userIds, uid)
		}
	}
	return
}

// 获取最近进入的房间
iamhujiebin's avatar
iamhujiebin committed
51
// 3个月
hujiebin's avatar
hujiebin committed
52 53 54 55
func GetUserRecentRooms(model *domain.Model, userId uint64) (imGroupIds []string) {
	userKey := group_k.GetUserEnterRoomUserKey(userId)
	var err error
	imGroupIds, err = model.Redis.ZRangeByScore(model, userKey, &redis.ZRangeBy{
iamhujiebin's avatar
iamhujiebin committed
56
		Min: fmt.Sprintf("%d", time.Now().AddDate(0, -3, 0).Unix()),
hujiebin's avatar
hujiebin committed
57 58 59 60 61 62 63 64 65 66 67 68
		Max: "+inf",
	}).Result()
	if err != nil {
		return
	}
	return
}

// 清理房间访客
// 15天前访问的
func RemoveRoomVisitors(model *domain.Model, imGroupId string) {
	groupKey := group_k.GetUserEnterRoomGroupKey(imGroupId)
hujiebin's avatar
hujiebin committed
69
	n, err := model.Redis.ZRemRangeByScore(model, groupKey, "-inf", fmt.Sprintf("%d", time.Now().AddDate(0, 0, -15).Unix())).Result()
hujiebin's avatar
hujiebin committed
70 71
	if err != nil {
		model.Log.Errorf("RemoveRoomVisitors fail:%v", err)
hujiebin's avatar
hujiebin committed
72 73
	} else {
		model.Log.Infof("RemoveRoomVisitors key:%v,n:%v", groupKey, n)
hujiebin's avatar
hujiebin committed
74 75
	}
}