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.

notification.go 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package convert
  4. import (
  5. "context"
  6. "net/url"
  7. activities_model "code.gitea.io/gitea/models/activities"
  8. "code.gitea.io/gitea/models/perm"
  9. access_model "code.gitea.io/gitea/models/perm/access"
  10. api "code.gitea.io/gitea/modules/structs"
  11. )
  12. // ToNotificationThread convert a Notification to api.NotificationThread
  13. func ToNotificationThread(ctx context.Context, n *activities_model.Notification) *api.NotificationThread {
  14. result := &api.NotificationThread{
  15. ID: n.ID,
  16. Unread: !(n.Status == activities_model.NotificationStatusRead || n.Status == activities_model.NotificationStatusPinned),
  17. Pinned: n.Status == activities_model.NotificationStatusPinned,
  18. UpdatedAt: n.UpdatedUnix.AsTime(),
  19. URL: n.APIURL(),
  20. }
  21. // since user only get notifications when he has access to use minimal access mode
  22. if n.Repository != nil {
  23. result.Repository = ToRepo(ctx, n.Repository, access_model.Permission{AccessMode: perm.AccessModeRead})
  24. // This permission is not correct and we should not be reporting it
  25. for repository := result.Repository; repository != nil; repository = repository.Parent {
  26. repository.Permissions = nil
  27. }
  28. }
  29. // handle Subject
  30. switch n.Source {
  31. case activities_model.NotificationSourceIssue:
  32. result.Subject = &api.NotificationSubject{Type: api.NotifySubjectIssue}
  33. if n.Issue != nil {
  34. result.Subject.Title = n.Issue.Title
  35. result.Subject.URL = n.Issue.APIURL(ctx)
  36. result.Subject.HTMLURL = n.Issue.HTMLURL()
  37. result.Subject.State = n.Issue.State()
  38. comment, err := n.Issue.GetLastComment(ctx)
  39. if err == nil && comment != nil {
  40. result.Subject.LatestCommentURL = comment.APIURL(ctx)
  41. result.Subject.LatestCommentHTMLURL = comment.HTMLURL(ctx)
  42. }
  43. }
  44. case activities_model.NotificationSourcePullRequest:
  45. result.Subject = &api.NotificationSubject{Type: api.NotifySubjectPull}
  46. if n.Issue != nil {
  47. result.Subject.Title = n.Issue.Title
  48. result.Subject.URL = n.Issue.APIURL(ctx)
  49. result.Subject.HTMLURL = n.Issue.HTMLURL()
  50. result.Subject.State = n.Issue.State()
  51. comment, err := n.Issue.GetLastComment(ctx)
  52. if err == nil && comment != nil {
  53. result.Subject.LatestCommentURL = comment.APIURL(ctx)
  54. result.Subject.LatestCommentHTMLURL = comment.HTMLURL(ctx)
  55. }
  56. if err := n.Issue.LoadPullRequest(ctx); err == nil &&
  57. n.Issue.PullRequest != nil &&
  58. n.Issue.PullRequest.HasMerged {
  59. result.Subject.State = "merged"
  60. }
  61. }
  62. case activities_model.NotificationSourceCommit:
  63. url := n.Repository.HTMLURL() + "/commit/" + url.PathEscape(n.CommitID)
  64. result.Subject = &api.NotificationSubject{
  65. Type: api.NotifySubjectCommit,
  66. Title: n.CommitID,
  67. URL: url,
  68. HTMLURL: url,
  69. }
  70. case activities_model.NotificationSourceRepository:
  71. result.Subject = &api.NotificationSubject{
  72. Type: api.NotifySubjectRepository,
  73. Title: n.Repository.FullName(),
  74. // FIXME: this is a relative URL, rather useless and inconsistent, but keeping for backwards compat
  75. URL: n.Repository.Link(),
  76. HTMLURL: n.Repository.HTMLURL(),
  77. }
  78. }
  79. return result
  80. }
  81. // ToNotifications convert list of Notification to api.NotificationThread list
  82. func ToNotifications(ctx context.Context, nl activities_model.NotificationList) []*api.NotificationThread {
  83. result := make([]*api.NotificationThread, 0, len(nl))
  84. for _, n := range nl {
  85. result = append(result, ToNotificationThread(ctx, n))
  86. }
  87. return result
  88. }