152 lines
5.0 KiB
Go
152 lines
5.0 KiB
Go
package config
|
||
|
||
import (
|
||
"time"
|
||
)
|
||
|
||
// SocialConfig 社交服务配置
|
||
type SocialConfig struct {
|
||
// TimeConstraints 时间约束配置
|
||
TimeConstraints TimeConstraints
|
||
|
||
// FriendLimit 好友数量限制配置(预留)
|
||
FriendLimit FriendLimitConfig
|
||
}
|
||
|
||
// TimeConstraints 时间约束配置
|
||
type TimeConstraints struct {
|
||
// RejectionCooldownDays 拒绝后的冷却期(天)
|
||
// 用户A被B拒绝后,需要等待N天才能再次发送好友请求
|
||
RejectionCooldownDays int
|
||
|
||
// RequestExpiryDays 好友请求过期时间(天)
|
||
// 超过N天未处理的请求会自动过期
|
||
RequestExpiryDays int
|
||
|
||
// RejectionCooldownMillis 拒绝后的冷却期(毫秒)
|
||
// 计算用,避免重复计算
|
||
RejectionCooldownMillis int64
|
||
|
||
// RequestExpiryMillis 好友请求过期时间(毫秒)
|
||
// 计算用,避免重复计算
|
||
RequestExpiryMillis int64
|
||
}
|
||
|
||
// FriendLimitConfig 好友数量限制配置
|
||
// 注意:目前暂不启用,后续可通过规则表根据用户等级、会员状态等动态限制
|
||
type FriendLimitConfig struct {
|
||
// Enabled 是否启用好友数量限制
|
||
Enabled bool
|
||
|
||
// DefaultLimit 默认好友数量限制
|
||
DefaultLimit int
|
||
|
||
// MaxLimit 最大好友数量限制(预留)
|
||
MaxLimit int
|
||
}
|
||
|
||
// GlobalSocialConfig 全局社交配置实例
|
||
var GlobalSocialConfig = &SocialConfig{
|
||
TimeConstraints: TimeConstraints{
|
||
RejectionCooldownDays: 7, // 7天冷却期
|
||
RequestExpiryDays: 30, // 30天过期时间
|
||
RejectionCooldownMillis: 7 * 24 * 60 * 60 * 1000,
|
||
RequestExpiryMillis: 30 * 24 * 60 * 60 * 1000,
|
||
},
|
||
FriendLimit: FriendLimitConfig{
|
||
Enabled: false, // 默认不启用
|
||
DefaultLimit: 0, // 不限制
|
||
MaxLimit: 10000, // 预留最大值
|
||
},
|
||
}
|
||
|
||
// ========== 时间相关辅助方法 ==========
|
||
|
||
// CalculateExpiryTime 计算过期时间(毫秒时间戳)
|
||
// createdAtMillis: 创建时间(毫秒时间戳)
|
||
// 返回:过期时间(毫秒时间戳)
|
||
func (c *SocialConfig) CalculateExpiryTime(createdAtMillis int64) int64 {
|
||
return createdAtMillis + c.TimeConstraints.RequestExpiryMillis
|
||
}
|
||
|
||
// IsExpired 检查请求是否已过期
|
||
// expiresAtMillis: 过期时间(毫秒时间戳)
|
||
// 返回:true=已过期,false=未过期
|
||
func (c *SocialConfig) IsExpired(expiresAtMillis int64) bool {
|
||
return time.Now().UnixMilli() > expiresAtMillis
|
||
}
|
||
|
||
// IsInCooldownPeriod 检查是否在冷却期内
|
||
// processedAtMillis: 处理时间(毫秒时间戳),即被拒绝的时间
|
||
// 返回:true=在冷却期内,false=已过冷却期
|
||
func (c *SocialConfig) IsInCooldownPeriod(processedAtMillis int64) bool {
|
||
cooldownEndTime := processedAtMillis + c.TimeConstraints.RejectionCooldownMillis
|
||
return time.Now().UnixMilli() < cooldownEndTime
|
||
}
|
||
|
||
// CalculateRemainingCooldownDays 计算剩余冷却天数
|
||
// processedAtMillis: 处理时间(毫秒时间戳),即被拒绝的时间
|
||
// 返回:剩余天数(向上取整)
|
||
func (c *SocialConfig) CalculateRemainingCooldownDays(processedAtMillis int64) int {
|
||
cooldownEndTime := processedAtMillis + c.TimeConstraints.RejectionCooldownMillis
|
||
now := time.Now().UnixMilli()
|
||
|
||
if now >= cooldownEndTime {
|
||
return 0
|
||
}
|
||
|
||
remainingMillis := cooldownEndTime - now
|
||
// 向上取整:1毫秒也算1天
|
||
remainingDays := (remainingMillis + 24*60*60*1000 - 1) / (24 * 60 * 60 * 1000)
|
||
return int(remainingDays)
|
||
}
|
||
|
||
// GetCooldownPeriodDays 获取冷却期天数配置
|
||
func (c *SocialConfig) GetCooldownPeriodDays() int {
|
||
return c.TimeConstraints.RejectionCooldownDays
|
||
}
|
||
|
||
// GetRequestExpiryDays 获取请求过期天数配置
|
||
func (c *SocialConfig) GetRequestExpiryDays() int {
|
||
return c.TimeConstraints.RequestExpiryDays
|
||
}
|
||
|
||
// ========== 好友数量限制相关方法 ==========
|
||
|
||
// IsFriendLimitEnabled 检查是否启用了好友数量限制
|
||
func (c *SocialConfig) IsFriendLimitEnabled() bool {
|
||
return c.FriendLimit.Enabled
|
||
}
|
||
|
||
// GetDefaultFriendLimit 获取默认好友数量限制
|
||
func (c *SocialConfig) GetDefaultFriendLimit() int {
|
||
return c.FriendLimit.DefaultLimit
|
||
}
|
||
|
||
// CheckFriendLimit 检查是否超过好友数量限制
|
||
// currentCount: 当前好友数量
|
||
// 返回:true=未超限,false=已超限
|
||
func (c *SocialConfig) CheckFriendLimit(currentCount int) bool {
|
||
if !c.FriendLimit.Enabled {
|
||
return true // 未启用限制,总是返回true
|
||
}
|
||
return currentCount < c.FriendLimit.DefaultLimit
|
||
}
|
||
|
||
// ========== 配置更新方法(预留) ==========
|
||
|
||
// UpdateTimeConstraints 更新时间约束配置
|
||
// 注意:后续可以从规则表或配置中心读取配置并调用此方法更新
|
||
func (c *SocialConfig) UpdateTimeConstraints(cooldownDays, expiryDays int) {
|
||
c.TimeConstraints.RejectionCooldownDays = cooldownDays
|
||
c.TimeConstraints.RequestExpiryDays = expiryDays
|
||
c.TimeConstraints.RejectionCooldownMillis = int64(cooldownDays) * 24 * 60 * 60 * 1000
|
||
c.TimeConstraints.RequestExpiryMillis = int64(expiryDays) * 24 * 60 * 60 * 1000
|
||
}
|
||
|
||
// UpdateFriendLimit 更新好友数量限制配置
|
||
func (c *SocialConfig) UpdateFriendLimit(enabled bool, defaultLimit int) {
|
||
c.FriendLimit.Enabled = enabled
|
||
c.FriendLimit.DefaultLimit = defaultLimit
|
||
}
|