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.

notification.go 2.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2020 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 convert
  5. import (
  6. "code.gitea.io/gitea/models"
  7. api "code.gitea.io/gitea/modules/structs"
  8. )
  9. // ToNotificationThread convert a Notification to api.NotificationThread
  10. func ToNotificationThread(n *models.Notification) *api.NotificationThread {
  11. result := &api.NotificationThread{
  12. ID: n.ID,
  13. Unread: !(n.Status == models.NotificationStatusRead || n.Status == models.NotificationStatusPinned),
  14. Pinned: n.Status == models.NotificationStatusPinned,
  15. UpdatedAt: n.UpdatedUnix.AsTime(),
  16. URL: n.APIURL(),
  17. }
  18. //since user only get notifications when he has access to use minimal access mode
  19. if n.Repository != nil {
  20. result.Repository = ToRepo(n.Repository, models.AccessModeRead)
  21. }
  22. //handle Subject
  23. switch n.Source {
  24. case models.NotificationSourceIssue:
  25. result.Subject = &api.NotificationSubject{Type: "Issue"}
  26. if n.Issue != nil {
  27. result.Subject.Title = n.Issue.Title
  28. result.Subject.URL = n.Issue.APIURL()
  29. result.Subject.State = n.Issue.State()
  30. comment, err := n.Issue.GetLastComment()
  31. if err == nil && comment != nil {
  32. result.Subject.LatestCommentURL = comment.APIURL()
  33. }
  34. }
  35. case models.NotificationSourcePullRequest:
  36. result.Subject = &api.NotificationSubject{Type: "Pull"}
  37. if n.Issue != nil {
  38. result.Subject.Title = n.Issue.Title
  39. result.Subject.URL = n.Issue.APIURL()
  40. result.Subject.State = n.Issue.State()
  41. comment, err := n.Issue.GetLastComment()
  42. if err == nil && comment != nil {
  43. result.Subject.LatestCommentURL = comment.APIURL()
  44. }
  45. }
  46. case models.NotificationSourceCommit:
  47. result.Subject = &api.NotificationSubject{
  48. Type: "Commit",
  49. Title: n.CommitID,
  50. URL: n.Repository.HTMLURL() + "/commit/" + n.CommitID,
  51. }
  52. case models.NotificationSourceRepository:
  53. result.Subject = &api.NotificationSubject{
  54. Type: "Repository",
  55. Title: n.Repository.FullName(),
  56. URL: n.Repository.Link(),
  57. }
  58. }
  59. return result
  60. }
  61. // ToNotifications convert list of Notification to api.NotificationThread list
  62. func ToNotifications(nl models.NotificationList) []*api.NotificationThread {
  63. var result = make([]*api.NotificationThread, 0, len(nl))
  64. for _, n := range nl {
  65. result = append(result, ToNotificationThread(n))
  66. }
  67. return result
  68. }