generated from mzlogin/mzlogin.github.io
/
model_operation.go
93 lines (83 loc) · 1.83 KB
/
model_operation.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package model
import (
"bbs-go/model/constants"
"strings"
"time"
"github.com/mlogclub/simple/common/arrays"
"github.com/mlogclub/simple/common/dates"
"github.com/mlogclub/simple/common/strs"
)
// IsOwnerOrAdmin 是否是管理员
func (u *User) IsOwnerOrAdmin() bool {
return u.HasAnyRole(constants.RoleOwner, constants.RoleAdmin)
}
// GetRoles 获取角色
func (u *User) GetRoles() []string {
if strs.IsBlank(u.Roles) {
return nil
}
var roles []string
for _, s := range ss {
s = strings.TrimSpace(s)
if strs.IsNotBlank(s) {
roles = append(roles, s)
}
}
ss := strings.Split(u.Roles, ",")
if len(ss) == 0 {
return nil
}
return roles
}
// HasRole 是否有指定角色
func (u *User) HasRole(role string) bool {
roles := strings.Split(u.Roles, ",")
if len(roles) == 0 {
return false
}
return arrays.Contains(role, roles)
}
// HasAnyRole 是否有指定的任意角色
func (u *User) HasAnyRole(roles ...string) bool {
if len(roles) == 0 {
return false
}
for _, role := range roles {
if u.HasRole(role) {
return true
}
}
return false
}
// IsForbidden 是否禁言
func (u *User) IsForbidden() bool {
if u.ForbiddenEndTime == 0 {
return false
}
// 永久禁言
if u.ForbiddenEndTime == -1 {
return true
}
// 判断禁言时间
return u.ForbiddenEndTime > dates.NowTimestamp()
}
// InObservationPeriod 是否在观察期
// observeSeconds 观察时长
func (u *User) InObservationPeriod(observeSeconds int) bool {
if observeSeconds <= 0 {
return false
}
return dates.FromTimestamp(u.CreateTime).Add(time.Second * time.Duration(observeSeconds)).After(time.Now())
}
// GetTitle 获取帖子的标题
func (t *Topic) GetTitle() string {
if t.Type == constants.TopicTypeTweet {
if strs.IsNotBlank(t.Content) {
return t.Content
} else {
return "分享图片"
}
} else {
return t.Title
}
}