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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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
  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 *xorm.Session, opts *ReactionOptions) error {
  157. reaction := &Reaction{
  158. Type: opts.Type,
  159. UserID: opts.Doer.ID,
  160. IssueID: opts.Issue.ID,
  161. }
  162. if opts.Comment != nil {
  163. reaction.CommentID = opts.Comment.ID
  164. }
  165. _, err := e.Where("original_author_id = 0").Delete(reaction)
  166. return err
  167. }
  168. // DeleteReaction deletes reaction for issue or comment.
  169. func DeleteReaction(opts *ReactionOptions) error {
  170. sess := x.NewSession()
  171. defer sess.Close()
  172. if err := sess.Begin(); err != nil {
  173. return err
  174. }
  175. if err := deleteReaction(sess, opts); err != nil {
  176. return err
  177. }
  178. return sess.Commit()
  179. }
  180. // DeleteIssueReaction deletes a reaction on issue.
  181. func DeleteIssueReaction(doer *User, issue *Issue, content string) error {
  182. return DeleteReaction(&ReactionOptions{
  183. Type: content,
  184. Doer: doer,
  185. Issue: issue,
  186. })
  187. }
  188. // DeleteCommentReaction deletes a reaction on comment.
  189. func DeleteCommentReaction(doer *User, issue *Issue, comment *Comment, content string) error {
  190. return DeleteReaction(&ReactionOptions{
  191. Type: content,
  192. Doer: doer,
  193. Issue: issue,
  194. Comment: comment,
  195. })
  196. }
  197. // LoadUser load user of reaction
  198. func (r *Reaction) LoadUser() (*User, error) {
  199. if r.User != nil {
  200. return r.User, nil
  201. }
  202. user, err := getUserByID(x, r.UserID)
  203. if err != nil {
  204. return nil, err
  205. }
  206. r.User = user
  207. return user, nil
  208. }
  209. // ReactionList represents list of reactions
  210. type ReactionList []*Reaction
  211. // HasUser check if user has reacted
  212. func (list ReactionList) HasUser(userID int64) bool {
  213. if userID == 0 {
  214. return false
  215. }
  216. for _, reaction := range list {
  217. if reaction.OriginalAuthor == "" && reaction.UserID == userID {
  218. return true
  219. }
  220. }
  221. return false
  222. }
  223. // GroupByType returns reactions grouped by type
  224. func (list ReactionList) GroupByType() map[string]ReactionList {
  225. var reactions = make(map[string]ReactionList)
  226. for _, reaction := range list {
  227. reactions[reaction.Type] = append(reactions[reaction.Type], reaction)
  228. }
  229. return reactions
  230. }
  231. func (list ReactionList) getUserIDs() []int64 {
  232. userIDs := make(map[int64]struct{}, len(list))
  233. for _, reaction := range list {
  234. if reaction.OriginalAuthor != "" {
  235. continue
  236. }
  237. if _, ok := userIDs[reaction.UserID]; !ok {
  238. userIDs[reaction.UserID] = struct{}{}
  239. }
  240. }
  241. return keysInt64(userIDs)
  242. }
  243. func (list ReactionList) loadUsers(e Engine, repo *Repository) ([]*User, error) {
  244. if len(list) == 0 {
  245. return nil, nil
  246. }
  247. userIDs := list.getUserIDs()
  248. userMaps := make(map[int64]*User, len(userIDs))
  249. err := e.
  250. In("id", userIDs).
  251. Find(&userMaps)
  252. if err != nil {
  253. return nil, fmt.Errorf("find user: %v", err)
  254. }
  255. for _, reaction := range list {
  256. if reaction.OriginalAuthor != "" {
  257. reaction.User = NewReplaceUser(fmt.Sprintf("%s(%s)", reaction.OriginalAuthor, repo.OriginalServiceType.Name()))
  258. } else if user, ok := userMaps[reaction.UserID]; ok {
  259. reaction.User = user
  260. } else {
  261. reaction.User = NewGhostUser()
  262. }
  263. }
  264. return valuesUser(userMaps), nil
  265. }
  266. // LoadUsers loads reactions' all users
  267. func (list ReactionList) LoadUsers(repo *Repository) ([]*User, error) {
  268. return list.loadUsers(x, repo)
  269. }
  270. // GetFirstUsers returns first reacted user display names separated by comma
  271. func (list ReactionList) GetFirstUsers() string {
  272. var buffer bytes.Buffer
  273. var rem = setting.UI.ReactionMaxUserNum
  274. for _, reaction := range list {
  275. if buffer.Len() > 0 {
  276. buffer.WriteString(", ")
  277. }
  278. buffer.WriteString(reaction.User.DisplayName())
  279. if rem--; rem == 0 {
  280. break
  281. }
  282. }
  283. return buffer.String()
  284. }
  285. // GetMoreUserCount returns count of not shown users in reaction tooltip
  286. func (list ReactionList) GetMoreUserCount() int {
  287. if len(list) <= setting.UI.ReactionMaxUserNum {
  288. return 0
  289. }
  290. return len(list) - setting.UI.ReactionMaxUserNum
  291. }