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.

332 lines
8.4 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  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. }