You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
6.0 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "bytes"
  7. "fmt"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/util"
  10. "github.com/go-xorm/xorm"
  11. "xorm.io/builder"
  12. )
  13. // Reaction represents a reactions on issues and comments.
  14. type Reaction struct {
  15. ID int64 `xorm:"pk autoincr"`
  16. Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
  17. IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
  18. CommentID int64 `xorm:"INDEX UNIQUE(s)"`
  19. UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
  20. User *User `xorm:"-"`
  21. CreatedUnix util.TimeStamp `xorm:"INDEX created"`
  22. }
  23. // FindReactionsOptions describes the conditions to Find reactions
  24. type FindReactionsOptions struct {
  25. IssueID int64
  26. CommentID int64
  27. }
  28. func (opts *FindReactionsOptions) toConds() builder.Cond {
  29. var cond = builder.NewCond()
  30. if opts.IssueID > 0 {
  31. cond = cond.And(builder.Eq{"reaction.issue_id": opts.IssueID})
  32. }
  33. if opts.CommentID > 0 {
  34. cond = cond.And(builder.Eq{"reaction.comment_id": opts.CommentID})
  35. }
  36. return cond
  37. }
  38. func findReactions(e Engine, opts FindReactionsOptions) ([]*Reaction, error) {
  39. reactions := make([]*Reaction, 0, 10)
  40. sess := e.Where(opts.toConds())
  41. return reactions, sess.
  42. Asc("reaction.issue_id", "reaction.comment_id", "reaction.created_unix", "reaction.id").
  43. Find(&reactions)
  44. }
  45. func createReaction(e *xorm.Session, opts *ReactionOptions) (*Reaction, error) {
  46. reaction := &Reaction{
  47. Type: opts.Type,
  48. UserID: opts.Doer.ID,
  49. IssueID: opts.Issue.ID,
  50. }
  51. if opts.Comment != nil {
  52. reaction.CommentID = opts.Comment.ID
  53. }
  54. if _, err := e.Insert(reaction); err != nil {
  55. return nil, err
  56. }
  57. return reaction, nil
  58. }
  59. // ReactionOptions defines options for creating or deleting reactions
  60. type ReactionOptions struct {
  61. Type string
  62. Doer *User
  63. Issue *Issue
  64. Comment *Comment
  65. }
  66. // CreateReaction creates reaction for issue or comment.
  67. func CreateReaction(opts *ReactionOptions) (reaction *Reaction, err error) {
  68. sess := x.NewSession()
  69. defer sess.Close()
  70. if err = sess.Begin(); err != nil {
  71. return nil, err
  72. }
  73. reaction, err = createReaction(sess, opts)
  74. if err != nil {
  75. return nil, err
  76. }
  77. if err = sess.Commit(); err != nil {
  78. return nil, err
  79. }
  80. return reaction, nil
  81. }
  82. // CreateIssueReaction creates a reaction on issue.
  83. func CreateIssueReaction(doer *User, issue *Issue, content string) (*Reaction, error) {
  84. return CreateReaction(&ReactionOptions{
  85. Type: content,
  86. Doer: doer,
  87. Issue: issue,
  88. })
  89. }
  90. // CreateCommentReaction creates a reaction on comment.
  91. func CreateCommentReaction(doer *User, issue *Issue, comment *Comment, content string) (*Reaction, error) {
  92. return CreateReaction(&ReactionOptions{
  93. Type: content,
  94. Doer: doer,
  95. Issue: issue,
  96. Comment: comment,
  97. })
  98. }
  99. func deleteReaction(e *xorm.Session, opts *ReactionOptions) error {
  100. reaction := &Reaction{
  101. Type: opts.Type,
  102. UserID: opts.Doer.ID,
  103. IssueID: opts.Issue.ID,
  104. }
  105. if opts.Comment != nil {
  106. reaction.CommentID = opts.Comment.ID
  107. }
  108. _, err := e.Delete(reaction)
  109. return err
  110. }
  111. // DeleteReaction deletes reaction for issue or comment.
  112. func DeleteReaction(opts *ReactionOptions) error {
  113. sess := x.NewSession()
  114. defer sess.Close()
  115. if err := sess.Begin(); err != nil {
  116. return err
  117. }
  118. if err := deleteReaction(sess, opts); err != nil {
  119. return err
  120. }
  121. return sess.Commit()
  122. }
  123. // DeleteIssueReaction deletes a reaction on issue.
  124. func DeleteIssueReaction(doer *User, issue *Issue, content string) error {
  125. return DeleteReaction(&ReactionOptions{
  126. Type: content,
  127. Doer: doer,
  128. Issue: issue,
  129. })
  130. }
  131. // DeleteCommentReaction deletes a reaction on comment.
  132. func DeleteCommentReaction(doer *User, issue *Issue, comment *Comment, content string) error {
  133. return DeleteReaction(&ReactionOptions{
  134. Type: content,
  135. Doer: doer,
  136. Issue: issue,
  137. Comment: comment,
  138. })
  139. }
  140. // ReactionList represents list of reactions
  141. type ReactionList []*Reaction
  142. // HasUser check if user has reacted
  143. func (list ReactionList) HasUser(userID int64) bool {
  144. if userID == 0 {
  145. return false
  146. }
  147. for _, reaction := range list {
  148. if reaction.UserID == userID {
  149. return true
  150. }
  151. }
  152. return false
  153. }
  154. // GroupByType returns reactions grouped by type
  155. func (list ReactionList) GroupByType() map[string]ReactionList {
  156. var reactions = make(map[string]ReactionList)
  157. for _, reaction := range list {
  158. reactions[reaction.Type] = append(reactions[reaction.Type], reaction)
  159. }
  160. return reactions
  161. }
  162. func (list ReactionList) getUserIDs() []int64 {
  163. userIDs := make(map[int64]struct{}, len(list))
  164. for _, reaction := range list {
  165. if _, ok := userIDs[reaction.UserID]; !ok {
  166. userIDs[reaction.UserID] = struct{}{}
  167. }
  168. }
  169. return keysInt64(userIDs)
  170. }
  171. func (list ReactionList) loadUsers(e Engine) ([]*User, error) {
  172. if len(list) == 0 {
  173. return nil, nil
  174. }
  175. userIDs := list.getUserIDs()
  176. userMaps := make(map[int64]*User, len(userIDs))
  177. err := e.
  178. In("id", userIDs).
  179. Find(&userMaps)
  180. if err != nil {
  181. return nil, fmt.Errorf("find user: %v", err)
  182. }
  183. for _, reaction := range list {
  184. if user, ok := userMaps[reaction.UserID]; ok {
  185. reaction.User = user
  186. } else {
  187. reaction.User = NewGhostUser()
  188. }
  189. }
  190. return valuesUser(userMaps), nil
  191. }
  192. // LoadUsers loads reactions' all users
  193. func (list ReactionList) LoadUsers() ([]*User, error) {
  194. return list.loadUsers(x)
  195. }
  196. // GetFirstUsers returns first reacted user display names separated by comma
  197. func (list ReactionList) GetFirstUsers() string {
  198. var buffer bytes.Buffer
  199. var rem = setting.UI.ReactionMaxUserNum
  200. for _, reaction := range list {
  201. if buffer.Len() > 0 {
  202. buffer.WriteString(", ")
  203. }
  204. buffer.WriteString(reaction.User.DisplayName())
  205. if rem--; rem == 0 {
  206. break
  207. }
  208. }
  209. return buffer.String()
  210. }
  211. // GetMoreUserCount returns count of not shown users in reaction tooltip
  212. func (list ReactionList) GetMoreUserCount() int {
  213. if len(list) <= setting.UI.ReactionMaxUserNum {
  214. return 0
  215. }
  216. return len(list) - setting.UI.ReactionMaxUserNum
  217. }