-
Notifications
You must be signed in to change notification settings - Fork 61
/
rule_topic.go
120 lines (98 loc) · 3.27 KB
/
rule_topic.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package auth
import (
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/generic/slice"
"github.com/bangumi/server/internal/pkg/gtime"
)
// ListTopicDisplays 在帖子列表能看到哪些状态的帖子。
func ListTopicDisplays(u domain.Auth) []model.TopicDisplay {
if u.ID == 0 {
return []model.TopicDisplay{model.TopicDisplayNormal}
}
if u.Permission.ManageTopicState || u.Permission.BanPost {
return []model.TopicDisplay{model.TopicDisplayBan, model.TopicDisplayNormal, model.TopicDisplayReview}
}
return []model.TopicDisplay{model.TopicDisplayNormal}
}
func RewriteSubCommit(t model.SubComment) model.SubComment {
switch t.State {
case model.CommentStateUserDelete, model.CommentStateAdminDelete:
t.Content = ""
default:
return t
}
return t
}
func RewriteCommit(t model.Comment) model.Comment {
switch t.State {
case model.CommentStateUserDelete, model.CommentStateAdminDelete:
t.Content = ""
default:
return t
}
return t
}
func RewriteCommentTree(comments []model.Comment) []model.Comment {
var newComments = make([]model.Comment, len(comments))
for i, comment := range comments {
comment.SubComments = slice.Map(comment.SubComments, RewriteSubCommit)
newComments[i] = RewriteCommit(comment)
}
return newComments
}
func CanViewTopicContent(u domain.Auth, topic model.Topic) bool {
if u.ID == 0 {
// 未登录用户只能看到正常帖子
return topic.Display == model.TopicDisplayNormal && (topic.State == model.CommentStateNone ||
topic.State == model.CommentStateAdminReopen)
}
// 登录用户
// 管理员啥都能看
if u.Permission.ManageTopicState || u.Permission.BanPost {
return true
}
if u.ID == topic.CreatorID {
if topic.Display == model.TopicDisplayReview {
return true
}
}
// 非管理员看不到删除和review的帖子
if topic.Display != model.TopicDisplayNormal {
return false
}
// 注册时间决定
switch topic.State {
case model.CommentStateNone, model.CommentStateAdminReopen,
model.CommentStateAdminMerge, model.CommentStateAdminPin, model.CommentStateAdminSilentTopic:
return true
case model.CommentStateAdminCloseTopic:
return CanViewClosedTopic(u)
case model.CommentStateUserDelete:
return CanViewDeleteTopic(u)
case model.CommentStateAdminDelete:
return false
}
return false
}
const CanViewStateClosedTopic = gtime.OneDay * 180
const CanViewStateDeleteTopic = gtime.OneDay * 365
func CanViewDeleteTopic(a domain.Auth) bool {
return a.RegisteredLongerThan(CanViewStateDeleteTopic)
}
func CanViewClosedTopic(a domain.Auth) bool {
return a.RegisteredLongerThan(CanViewStateClosedTopic)
}