code.gitea.io/gitea@v1.22.3/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(ctx) 43 result.Subject.HTMLURL = n.Issue.HTMLURL() 44 result.Subject.State = n.Issue.State() 45 comment, err := n.Issue.GetLastComment(ctx) 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(ctx) 56 result.Subject.HTMLURL = n.Issue.HTMLURL() 57 result.Subject.State = n.Issue.State() 58 comment, err := n.Issue.GetLastComment(ctx) 59 if err == nil && comment != nil { 60 result.Subject.LatestCommentURL = comment.APIURL(ctx) 61 result.Subject.LatestCommentHTMLURL = comment.HTMLURL(ctx) 62 } 63 64 if err := n.Issue.LoadPullRequest(ctx); err == nil && 65 n.Issue.PullRequest != nil && 66 n.Issue.PullRequest.HasMerged { 67 result.Subject.State = "merged" 68 } 69 } 70 case activities_model.NotificationSourceCommit: 71 url := n.Repository.HTMLURL() + "/commit/" + url.PathEscape(n.CommitID) 72 result.Subject = &api.NotificationSubject{ 73 Type: api.NotifySubjectCommit, 74 Title: n.CommitID, 75 URL: url, 76 HTMLURL: url, 77 } 78 case activities_model.NotificationSourceRepository: 79 result.Subject = &api.NotificationSubject{ 80 Type: api.NotifySubjectRepository, 81 Title: n.Repository.FullName(), 82 // FIXME: this is a relative URL, rather useless and inconsistent, but keeping for backwards compat 83 URL: n.Repository.Link(), 84 HTMLURL: n.Repository.HTMLURL(), 85 } 86 } 87 88 return result 89 } 90 91 // ToNotifications convert list of Notification to api.NotificationThread list 92 func ToNotifications(ctx context.Context, nl activities_model.NotificationList) []*api.NotificationThread { 93 result := make([]*api.NotificationThread, 0, len(nl)) 94 for _, n := range nl { 95 result = append(result, ToNotificationThread(ctx, n)) 96 } 97 return result 98 }