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.

issue_reaction.go 7.1KB

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