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

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