Skip to content

Commit

Permalink
feat: topics and comments (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 authored Jul 17, 2022
1 parent d2bf1da commit abf8803
Show file tree
Hide file tree
Showing 73 changed files with 6,401 additions and 180 deletions.
13 changes: 13 additions & 0 deletions etc/mock.task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tasks:
- PersonService
- GroupRepo
- CollectionRepo
- TopicRepo

base-mock:
deps:
Expand Down Expand Up @@ -284,3 +285,15 @@ tasks:
vars:
SRC_DIR: ./internal/domain
INTERFACE: CollectionRepo

TopicRepo:
sources:
- internal/domain/topic.go
- ./internal/pkg/tools/go.mod
generates:
- internal/mocks/TopicRepo.go
cmds:
- task: base-mock
vars:
SRC_DIR: ./internal/domain
INTERFACE: TopicRepo
96 changes: 96 additions & 0 deletions internal/auth/rule_topic.go
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)
}
92 changes: 92 additions & 0 deletions internal/cmd/gen/dal/comment.go
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)
}
}
}
57 changes: 57 additions & 0 deletions internal/cmd/gen/dal/template/comments.go
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@
// 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 generic
package dao

type TypeComment struct {
Content string
ID uint32
TopicID uint32
UID uint32
Related uint32
CreatedTime uint32
}

func (c TypeComment) statStub() uint8 {
return 0
}
59 changes: 58 additions & 1 deletion internal/cmd/gen/gorm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func main() {
modelMember := g.GenerateModelAs("chii_members", "Member",
gen.FieldRename("uid", "ID"),
gen.FieldType("uid", userIDTypeString),
gen.FieldRename("SIGN", "Sign"),
gen.FieldType("regdate", "int64"),
gen.FieldType("password_crypt", "[]byte"),
gen.FieldType("groupid", "uint8"),
Expand Down Expand Up @@ -373,6 +372,64 @@ func main() {
gen.FieldRename("gmb_dateline", "CreatedTime"),
))

g.ApplyBasic(g.GenerateModelAs("chii_subject_topics", "SubjectTopic",
gen.FieldTrimPrefix("sbj_tpc_"),
gen.FieldRename("sbj_tpc_subject_id", "SubjectID"),
gen.FieldRename("sbj_tpc_dateline", "CreatedTime"),
gen.FieldRename("sbj_tpc_lastpost", "UpdatedTime"),
gen.FieldRename("sbj_tpc_display", "Status"),
gen.FieldType("sbj_tpc_state", "uint8"),
gen.FieldType("sbj_tpc_display", "uint8"),
))

g.ApplyBasic(g.GenerateModelAs("chii_group_topics", "GroupTopic",
gen.FieldTrimPrefix("grp_tpc_"),
gen.FieldRename("grp_tpc_gid", "GroupID"),
gen.FieldRename("grp_tpc_dateline", "CreatedTime"),
gen.FieldRename("grp_tpc_lastpost", "UpdatedTime"),
gen.FieldRename("grp_tpc_display", "Status"),
gen.FieldType("grp_tpc_state", "uint8"),
gen.FieldType("grp_tpc_display", "uint8"),
))

g.ApplyBasic(g.GenerateModelAs("chii_subject_posts", "SubjectTopicComment",
gen.FieldTrimPrefix("sbj_pst_"),
gen.FieldRename("sbj_pst_mid", "TopicID"),
gen.FieldType("sbj_pst_state", "uint8"),
gen.FieldRename("sbj_pst_dateline", "CreatedTime"),
))

g.ApplyBasic(g.GenerateModelAs("chii_group_posts", "GroupTopicComment",
gen.FieldTrimPrefix("grp_pst_"),
gen.FieldRename("grp_pst_mid", "TopicID"),
gen.FieldType("grp_pst_state", "uint8"),
gen.FieldRename("grp_pst_dateline", "CreatedTime"),
))

g.ApplyBasic(g.GenerateModelAs("chii_ep_comments", "EpisodeComment",
gen.FieldTrimPrefix("ep_pst_"),
gen.FieldRename("ep_pst_mid", "TopicID"),
gen.FieldRename("ep_pst_dateline", "CreatedTime"),
))

g.ApplyBasic(g.GenerateModelAs("chii_crt_comments", "CharacterComment",
gen.FieldTrimPrefix("crt_pst_"),
gen.FieldRename("crt_pst_mid", "TopicID"),
gen.FieldRename("crt_pst_dateline", "CreatedTime"),
))

g.ApplyBasic(g.GenerateModelAs("chii_index_comments", "IndexComment",
gen.FieldTrimPrefix("idx_pst_"),
gen.FieldRename("idx_pst_mid", "TopicID"),
gen.FieldRename("idx_pst_dateline", "CreatedTime"),
))

g.ApplyBasic(g.GenerateModelAs("chii_prsn_comments", "PersonComment",
gen.FieldTrimPrefix("prsn_pst_"),
gen.FieldRename("prsn_pst_mid", "TopicID"),
gen.FieldRename("prsn_pst_dateline", "CreatedTime"),
))

// execute the action of code generation
g.Execute()
}
22 changes: 22 additions & 0 deletions internal/dal/dao/chii_crt_comments.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit abf8803

Please sign in to comment.