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

issue_reaction.go 6.3 kB

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