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

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