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 7.3 kB

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