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.7KB

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