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

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