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

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