comment.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/beego/beego/v2/client/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. func (m *Comment) Update(cols ...string) error {
  57. o := orm.NewOrm()
  58. _, err := o.Update(m, cols...)
  59. return err
  60. }
  61. //Insert 添加一条评论.
  62. func (m *Comment) Insert() error {
  63. if m.DocumentId <= 0 {
  64. return errors.New("评论文档不存在")
  65. }
  66. if m.Content == "" {
  67. return ErrCommentContentNotEmpty
  68. }
  69. o := orm.NewOrm()
  70. if m.CommentId > 0 {
  71. comment := NewComment()
  72. //如果父评论不存在
  73. if err := o.Read(comment); err != nil {
  74. return err
  75. }
  76. }
  77. document := NewDocument()
  78. //如果评论的文档不存在
  79. if _, err := document.Find(m.DocumentId); err != nil {
  80. return err
  81. }
  82. book, err := NewBook().Find(document.BookId)
  83. //如果评论的项目不存在
  84. if err != nil {
  85. return err
  86. }
  87. //如果已关闭评论
  88. if book.CommentStatus == "closed" {
  89. return ErrCommentClosed
  90. }
  91. if book.CommentStatus == "registered_only" && m.MemberId <= 0 {
  92. return ErrPermissionDenied
  93. }
  94. //如果仅参与者评论
  95. if book.CommentStatus == "group_only" {
  96. if m.MemberId <= 0 {
  97. return ErrPermissionDenied
  98. }
  99. rel := NewRelationship()
  100. if _, err := rel.FindForRoleId(book.BookId, m.MemberId); err != nil {
  101. return ErrPermissionDenied
  102. }
  103. }
  104. if m.MemberId > 0 {
  105. member := NewMember()
  106. //如果用户不存在
  107. if _, err := member.Find(m.MemberId); err != nil {
  108. return ErrMemberNoExist
  109. }
  110. //如果用户被禁用
  111. if member.Status == 1 {
  112. return ErrMemberDisabled
  113. }
  114. } else if m.Author == "" {
  115. m.Author = "[匿名用户]"
  116. }
  117. m.BookId = book.BookId
  118. _, err = o.Insert(m)
  119. return err
  120. }