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