-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
73 changed files
with
6,401 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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/timex" | ||
) | ||
|
||
// 目前列表是根据 display(status) 来判断是否能看到标题,state 决定是否能查看内容。 | ||
|
||
func TopicStatuses(u domain.Auth) []model.TopicStatus { | ||
if u.ID == 0 { | ||
return []model.TopicStatus{model.TopicStatusNormal} | ||
} | ||
|
||
if u.Permission.ManageTopicState || u.Permission.BanPost { | ||
return []model.TopicStatus{model.TopicStatusBan, model.TopicStatusNormal, model.TopicStatusReview} | ||
} | ||
|
||
return []model.TopicStatus{model.TopicStatusNormal} | ||
} | ||
|
||
func RewriteSubCommit(t model.SubComment) model.SubComment { | ||
switch t.State { | ||
case model.CommentStateDelete, model.CommentStatePrivate: | ||
t.Content = "" | ||
default: | ||
return t | ||
} | ||
|
||
return t | ||
} | ||
|
||
func RewriteCommit(t model.Comment) model.Comment { | ||
switch t.State { | ||
case model.CommentStateDelete, model.CommentStatePrivate: | ||
t.Content = "" | ||
default: | ||
return t | ||
} | ||
|
||
return t | ||
} | ||
|
||
func CanViewTopicContent(u domain.Auth, topic model.Topic) bool { | ||
if u.ID == 0 { | ||
return topic.State == model.CommentStateNone | ||
} | ||
|
||
if u.Permission.ManageTopicState || u.Permission.BanPost { | ||
return true | ||
} | ||
|
||
if u.ID == topic.CreatorID { | ||
return topic.State != model.CommentStateDelete | ||
} | ||
|
||
switch topic.State { | ||
case model.CommentStateNone, model.CommentStateReopen, | ||
model.CommentStateMerge, model.CommentStatePin, model.CommentStateSilent: | ||
return true | ||
case model.CommentStateClosed: | ||
return CanViewClosedTopic(u) | ||
case model.CommentStateDelete: | ||
return CanViewDeleteTopic(u) | ||
case model.CommentStatePrivate: | ||
return false | ||
} | ||
|
||
return false | ||
} | ||
|
||
const CanViewStateClosedTopic = timex.OneDay * 180 | ||
const CanViewStateDeleteTopic = timex.OneDay * 365 | ||
|
||
func CanViewDeleteTopic(a domain.Auth) bool { | ||
return a.RegisteredLongerThan(CanViewStateDeleteTopic) | ||
} | ||
|
||
func CanViewClosedTopic(a domain.Auth) bool { | ||
return a.RegisteredLongerThan(CanViewStateClosedTopic) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// 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/> | ||
|
||
// generate methods for dal comments dao. | ||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
//go:embed template/comments.go | ||
var template string | ||
|
||
//nolint:funlen | ||
func main() { | ||
for _, t := range []struct { | ||
Value string | ||
Name string | ||
GenStateStub bool | ||
}{ | ||
{ | ||
Value: "PersonComment", | ||
Name: "comment_person", | ||
GenStateStub: true, | ||
}, | ||
{ | ||
Value: "CharacterComment", | ||
Name: "comment_characters", | ||
GenStateStub: true, | ||
}, | ||
{ | ||
Value: "GroupTopicComment", | ||
Name: "comment_group", | ||
}, | ||
{ | ||
Value: "SubjectTopicComment", | ||
Name: "comment_subject", | ||
}, | ||
{ | ||
Value: "EpisodeComment", | ||
Name: "comment_episode", | ||
GenStateStub: true, | ||
}, | ||
{ | ||
Value: "IndexComment", | ||
Name: "comment_index", | ||
GenStateStub: true, | ||
}, | ||
} { | ||
name := t.Value | ||
|
||
content := template | ||
|
||
if t.GenStateStub { | ||
content += ` | ||
func (c *TypeComment) statStub() uint8 { | ||
return 0 | ||
} | ||
` | ||
} else { | ||
content += ` | ||
func (c *TypeComment) statStub() uint8 { | ||
return c.State | ||
} | ||
` | ||
} | ||
|
||
content = "// Code generated by internal/cmd/gen/dal/comment.go. DO NOT EDIT.\n//\n" + content | ||
|
||
content = strings.ReplaceAll(content, "TypeComment", name) | ||
|
||
fileName := t.Name + ".gen.go" | ||
err := ioutil.WriteFile(filepath.Join("./internal/dal/dao/", fileName), []byte(content), 0600) //nolint:gomnd | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 dao | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/bangumi/server/internal/model" | ||
) | ||
|
||
func (c *TypeComment) CreatorID() model.UserID { | ||
return model.UserID(c.UID) | ||
} | ||
|
||
func (c *TypeComment) IsSubComment() bool { | ||
return c.Related == 0 | ||
} | ||
|
||
func (c *TypeComment) CommentID() model.CommentID { | ||
return model.CommentID(c.ID) | ||
} | ||
|
||
func (c *TypeComment) CreateAt() time.Time { | ||
return time.Unix(int64(c.CreatedTime), 0) | ||
} | ||
|
||
func (c *TypeComment) GetState() uint8 { | ||
return c.statStub() | ||
} | ||
|
||
func (c *TypeComment) RelatedTo() model.CommentID { | ||
return model.CommentID(c.Related) | ||
} | ||
|
||
func (c *TypeComment) GetID() model.CommentID { | ||
return model.CommentID(c.ID) | ||
} | ||
|
||
func (c *TypeComment) GetContent() string { | ||
return c.Content | ||
} | ||
|
||
func (c *TypeComment) GetTopicID() uint32 { | ||
return c.TopicID | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.