CommentModel.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/astaxie/beego/orm"
  6. "github.com/mindoc-org/mindoc/conf"
  7. )
  8. //Comment struct
  9. type Comment struct {
  10. CommentId int `orm:"pk;auto;unique;column(comment_id)" json:"comment_id"`
  11. Floor int `orm:"column(floor);type(unsigned);default(0)" json:"floor"`
  12. BookId int `orm:"column(book_id);type(int)" json:"book_id"`
  13. // DocumentId 评论所属的文档.
  14. DocumentId int `orm:"column(document_id);type(int)" json:"document_id"`
  15. // Author 评论作者.
  16. Author string `orm:"column(author);size(100)" json:"author"`
  17. //MemberId 评论用户ID.
  18. MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
  19. // IPAddress 评论者的IP地址
  20. IPAddress string `orm:"column(ip_address);size(100)" json:"ip_address"`
  21. // 评论日期.
  22. CommentDate time.Time `orm:"type(datetime);column(comment_date);auto_now_add" json:"comment_date"`
  23. //Content 评论内容.
  24. Content string `orm:"column(content);size(2000)" json:"content"`
  25. // Approved 评论状态:0 待审核/1 已审核/2 垃圾评论/ 3 已删除
  26. Approved int `orm:"column(approved);type(int)" json:"approved"`
  27. // UserAgent 评论者浏览器内容
  28. UserAgent string `orm:"column(user_agent);size(500)" json:"user_agent"`
  29. // Parent 评论所属父级
  30. ParentId int `orm:"column(parent_id);type(int);default(0)" json:"parent_id"`
  31. AgreeCount int `orm:"column(agree_count);type(int);default(0)" json:"agree_count"`
  32. AgainstCount int `orm:"column(against_count);type(int);default(0)" json:"against_count"`
  33. }
  34. // TableName 获取对应数据库表名.
  35. func (m *Comment) TableName() string {
  36. return "comments"
  37. }
  38. // TableEngine 获取数据使用的引擎.
  39. func (m *Comment) TableEngine() string {
  40. return "INNODB"
  41. }
  42. func (m *Comment) TableNameWithPrefix() string {
  43. return conf.GetDatabasePrefix() + m.TableName()
  44. }
  45. func NewComment() *Comment {
  46. return &Comment{}
  47. }
  48. func (m *Comment) Find(id int) (*Comment, error) {
  49. if id <= 0 {
  50. return m, ErrInvalidParameter
  51. }
  52. o := orm.NewOrm()
  53. err := o.Read(m)
  54. return m, err
  55. }
  56. // 根据文档id查询文档评论
  57. func (m *Comment) QueryCommentByDocumentId(doc_id, page, pagesize int) (comments []Comment, count int64) {
  58. o := orm.NewOrm()
  59. offset := (page - 1) * pagesize
  60. o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).OrderBy("comment_date").Offset(offset).Limit(pagesize).All(&comments)
  61. count, _ = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).Count()
  62. return
  63. }
  64. func (m *Comment) Update(cols ...string) error {
  65. o := orm.NewOrm()
  66. _, err := o.Update(m, cols...)
  67. return err
  68. }
  69. //Insert 添加一条评论.
  70. func (m *Comment) Insert() error {
  71. if m.DocumentId <= 0 {
  72. return errors.New("评论文档不存在")
  73. }
  74. if m.Content == "" {
  75. return ErrCommentContentNotEmpty
  76. }
  77. o := orm.NewOrm()
  78. if m.CommentId > 0 {
  79. comment := NewComment()
  80. //如果父评论不存在
  81. if err := o.Read(comment); err != nil {
  82. return err
  83. }
  84. }
  85. document := NewDocument()
  86. //如果评论的文档不存在
  87. if _, err := document.Find(m.DocumentId); err != nil {
  88. return err
  89. }
  90. book, err := NewBook().Find(document.BookId)
  91. //如果评论的项目不存在
  92. if err != nil {
  93. return err
  94. }
  95. //如果已关闭评论
  96. if book.CommentStatus == "closed" {
  97. return ErrCommentClosed
  98. }
  99. if book.CommentStatus == "registered_only" && m.MemberId <= 0 {
  100. return ErrPermissionDenied
  101. }
  102. //如果仅参与者评论
  103. if book.CommentStatus == "group_only" {
  104. if m.MemberId <= 0 {
  105. return ErrPermissionDenied
  106. }
  107. rel := NewRelationship()
  108. if _, err := rel.FindForRoleId(book.BookId, m.MemberId); err != nil {
  109. return ErrPermissionDenied
  110. }
  111. }
  112. if m.MemberId > 0 {
  113. member := NewMember()
  114. //如果用户不存在
  115. if _, err := member.Find(m.MemberId); err != nil {
  116. return ErrMemberNoExist
  117. }
  118. //如果用户被禁用
  119. if member.Status == 1 {
  120. return ErrMemberDisabled
  121. }
  122. } else if m.Author == "" {
  123. m.Author = "[匿名用户]"
  124. }
  125. m.BookId = book.BookId
  126. _, err = o.Insert(m)
  127. return err
  128. }