2017-04-21 18:20:35 +08:00
|
|
|
|
package models
|
|
|
|
|
|
2017-04-23 20:33:21 +08:00
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2018-01-26 17:17:38 +08:00
|
|
|
|
"time"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
2017-04-23 20:33:21 +08:00
|
|
|
|
)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
2024-08-26 13:38:58 +08:00
|
|
|
|
// Comment struct
|
2017-04-21 18:20:35 +08:00
|
|
|
|
type Comment struct {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
CommentId int `orm:"pk;auto;unique;column(comment_id)" json:"comment_id"`
|
|
|
|
|
Floor int `orm:"column(floor);type(unsigned);default(0)" json:"floor"`
|
|
|
|
|
BookId int `orm:"column(book_id);type(int)" json:"book_id"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// DocumentId 评论所属的文档.
|
2018-01-26 17:17:38 +08:00
|
|
|
|
DocumentId int `orm:"column(document_id);type(int)" json:"document_id"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// Author 评论作者.
|
2018-01-26 17:17:38 +08:00
|
|
|
|
Author string `orm:"column(author);size(100)" json:"author"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
//MemberId 评论用户ID.
|
2018-01-26 17:17:38 +08:00
|
|
|
|
MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// IPAddress 评论者的IP地址
|
2018-01-26 17:17:38 +08:00
|
|
|
|
IPAddress string `orm:"column(ip_address);size(100)" json:"ip_address"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// 评论日期.
|
2018-01-26 17:17:38 +08:00
|
|
|
|
CommentDate time.Time `orm:"type(datetime);column(comment_date);auto_now_add" json:"comment_date"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
//Content 评论内容.
|
2018-01-26 17:17:38 +08:00
|
|
|
|
Content string `orm:"column(content);size(2000)" json:"content"`
|
2017-04-23 20:33:21 +08:00
|
|
|
|
// Approved 评论状态:0 待审核/1 已审核/2 垃圾评论/ 3 已删除
|
2018-01-26 17:17:38 +08:00
|
|
|
|
Approved int `orm:"column(approved);type(int)" json:"approved"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// UserAgent 评论者浏览器内容
|
2018-01-26 17:17:38 +08:00
|
|
|
|
UserAgent string `orm:"column(user_agent);size(500)" json:"user_agent"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// Parent 评论所属父级
|
2024-08-26 13:38:58 +08:00
|
|
|
|
ParentId int `orm:"column(parent_id);type(int);default(0)" json:"parent_id"`
|
|
|
|
|
AgreeCount int `orm:"column(agree_count);type(int);default(0)" json:"agree_count"`
|
|
|
|
|
AgainstCount int `orm:"column(against_count);type(int);default(0)" json:"against_count"`
|
|
|
|
|
Index int `orm:"-" json:"index"`
|
|
|
|
|
ShowDel int `orm:"-" json:"show_del"`
|
|
|
|
|
Avatar string `orm:"-" json:"avatar"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
|
func (m *Comment) TableName() string {
|
|
|
|
|
return "comments"
|
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
|
func (m *Comment) TableEngine() string {
|
|
|
|
|
return "INNODB"
|
|
|
|
|
}
|
2017-04-23 20:33:21 +08:00
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *Comment) TableNameWithPrefix() string {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewComment() *Comment {
|
|
|
|
|
return &Comment{}
|
|
|
|
|
}
|
2021-04-03 17:40:08 +08:00
|
|
|
|
|
2021-04-04 16:44:14 +08:00
|
|
|
|
// 是否有权限删除
|
|
|
|
|
func (m *Comment) CanDelete(user_memberid int, user_bookrole conf.BookRole) bool {
|
|
|
|
|
return user_memberid == m.MemberId || user_bookrole == conf.BookFounder || user_bookrole == conf.BookAdmin
|
2017-04-23 20:33:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-03 17:40:08 +08:00
|
|
|
|
// 根据文档id查询文档评论
|
2022-05-18 11:17:03 +08:00
|
|
|
|
func (m *Comment) QueryCommentByDocumentId(doc_id, page, pagesize int, member *Member) (comments []*Comment, count int64, ret_page int) {
|
2021-04-04 16:44:14 +08:00
|
|
|
|
doc, err := NewDocument().Find(doc_id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-03 17:40:08 +08:00
|
|
|
|
o := orm.NewOrm()
|
2021-04-03 22:44:43 +08:00
|
|
|
|
count, _ = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).Count()
|
2022-05-18 11:17:03 +08:00
|
|
|
|
if -1 == page { // 请求最后一页
|
2021-04-03 22:44:43 +08:00
|
|
|
|
var total int = int(count)
|
2022-05-18 11:17:03 +08:00
|
|
|
|
if total%pagesize == 0 {
|
2021-04-03 22:44:43 +08:00
|
|
|
|
page = total / pagesize
|
|
|
|
|
} else {
|
2022-05-18 11:17:03 +08:00
|
|
|
|
page = total/pagesize + 1
|
2021-04-03 22:44:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-03 17:40:08 +08:00
|
|
|
|
offset := (page - 1) * pagesize
|
2021-04-03 22:44:43 +08:00
|
|
|
|
ret_page = page
|
2021-04-03 17:40:08 +08:00
|
|
|
|
o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).OrderBy("comment_date").Offset(offset).Limit(pagesize).All(&comments)
|
2021-04-04 16:44:14 +08:00
|
|
|
|
|
2022-05-18 11:17:03 +08:00
|
|
|
|
// 需要判断未登录的情况
|
|
|
|
|
var bookRole conf.BookRole
|
|
|
|
|
if member != nil {
|
|
|
|
|
bookRole, _ = NewRelationship().FindForRoleId(doc.BookId, member.MemberId)
|
|
|
|
|
}
|
2021-04-03 22:44:43 +08:00
|
|
|
|
for i := 0; i < len(comments); i++ {
|
2022-05-18 11:17:03 +08:00
|
|
|
|
comments[i].Index = (i + 1) + (page-1)*pagesize
|
|
|
|
|
if member != nil && comments[i].CanDelete(member.MemberId, bookRole) {
|
2021-04-03 22:44:43 +08:00
|
|
|
|
comments[i].ShowDel = 1
|
2024-08-26 13:38:58 +08:00
|
|
|
|
comments[i].Avatar = member.Avatar
|
2021-04-03 22:44:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-03 17:40:08 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func (m *Comment) Update(cols ...string) error {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
|
_, err := o.Update(m, cols...)
|
2017-04-23 20:33:21 +08:00
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 13:38:58 +08:00
|
|
|
|
// Insert 添加一条评论.
|
2017-04-23 20:33:21 +08:00
|
|
|
|
func (m *Comment) Insert() error {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if m.DocumentId <= 0 {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
return errors.New("评论文档不存在")
|
|
|
|
|
}
|
|
|
|
|
if m.Content == "" {
|
|
|
|
|
return ErrCommentContentNotEmpty
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
|
|
if m.CommentId > 0 {
|
|
|
|
|
comment := NewComment()
|
|
|
|
|
//如果父评论不存在
|
|
|
|
|
if err := o.Read(comment); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document := NewDocument()
|
|
|
|
|
//如果评论的文档不存在
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if _, err := document.Find(m.DocumentId); err != nil {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
book, err := NewBook().Find(document.BookId)
|
2017-04-23 20:33:21 +08:00
|
|
|
|
//如果评论的项目不存在
|
2017-04-25 20:05:59 +08:00
|
|
|
|
if err != nil {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
//如果已关闭评论
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if book.CommentStatus == "closed" {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
return ErrCommentClosed
|
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if book.CommentStatus == "registered_only" && m.MemberId <= 0 {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
return ErrPermissionDenied
|
|
|
|
|
}
|
|
|
|
|
//如果仅参与者评论
|
|
|
|
|
if book.CommentStatus == "group_only" {
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if m.MemberId <= 0 {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
return ErrPermissionDenied
|
|
|
|
|
}
|
|
|
|
|
rel := NewRelationship()
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if _, err := rel.FindForRoleId(book.BookId, m.MemberId); err != nil {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
return ErrPermissionDenied
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if m.MemberId > 0 {
|
|
|
|
|
member := NewMember()
|
|
|
|
|
//如果用户不存在
|
2018-01-26 17:17:38 +08:00
|
|
|
|
if _, err := member.Find(m.MemberId); err != nil {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
return ErrMemberNoExist
|
|
|
|
|
}
|
|
|
|
|
//如果用户被禁用
|
|
|
|
|
if member.Status == 1 {
|
|
|
|
|
return ErrMemberDisabled
|
|
|
|
|
}
|
2018-01-26 17:17:38 +08:00
|
|
|
|
} else if m.Author == "" {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
m.Author = "[匿名用户]"
|
|
|
|
|
}
|
|
|
|
|
m.BookId = book.BookId
|
2018-01-26 17:17:38 +08:00
|
|
|
|
_, err = o.Insert(m)
|
2017-04-23 20:33:21 +08:00
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-04-03 22:44:43 +08:00
|
|
|
|
|
|
|
|
|
// 删除一条评论
|
|
|
|
|
func (m *Comment) Delete() error {
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
_, err := o.Delete(m)
|
|
|
|
|
return err
|
2021-04-04 16:44:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Comment) Find(id int, cols ...string) (*Comment, error) {
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
if err := o.QueryTable(m.TableNameWithPrefix()).Filter("comment_id", id).One(m, cols...); err != nil {
|
|
|
|
|
return m, err
|
|
|
|
|
}
|
|
|
|
|
return m, nil
|
2022-05-18 11:17:03 +08:00
|
|
|
|
}
|