From 4ad2a5f42fb9fe340d2131c76e91399f30733b76 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 1 Jul 2021 01:29:08 +0200 Subject: [PATCH 1/3] Introduce NotifySubjectType --- modules/convert/notification.go | 8 ++++---- modules/structs/notifications.go | 24 +++++++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/modules/convert/notification.go b/modules/convert/notification.go index cc941678b603..b0888ee09f0c 100644 --- a/modules/convert/notification.go +++ b/modules/convert/notification.go @@ -27,7 +27,7 @@ func ToNotificationThread(n *models.Notification) *api.NotificationThread { //handle Subject switch n.Source { case models.NotificationSourceIssue: - result.Subject = &api.NotificationSubject{Type: "Issue"} + result.Subject = &api.NotificationSubject{Type: api.NotifySubjectIssue} if n.Issue != nil { result.Subject.Title = n.Issue.Title result.Subject.URL = n.Issue.APIURL() @@ -38,7 +38,7 @@ func ToNotificationThread(n *models.Notification) *api.NotificationThread { } } case models.NotificationSourcePullRequest: - result.Subject = &api.NotificationSubject{Type: "Pull"} + result.Subject = &api.NotificationSubject{Type: api.NotifySubjectPull} if n.Issue != nil { result.Subject.Title = n.Issue.Title result.Subject.URL = n.Issue.APIURL() @@ -55,13 +55,13 @@ func ToNotificationThread(n *models.Notification) *api.NotificationThread { } case models.NotificationSourceCommit: result.Subject = &api.NotificationSubject{ - Type: "Commit", + Type: api.NotifySubjectCommit, Title: n.CommitID, URL: n.Repository.HTMLURL() + "/commit/" + n.CommitID, } case models.NotificationSourceRepository: result.Subject = &api.NotificationSubject{ - Type: "Repository", + Type: api.NotifySubjectRepository, Title: n.Repository.FullName(), URL: n.Repository.Link(), } diff --git a/modules/structs/notifications.go b/modules/structs/notifications.go index 8daa6de1686f..1481752f08ab 100644 --- a/modules/structs/notifications.go +++ b/modules/structs/notifications.go @@ -21,14 +21,28 @@ type NotificationThread struct { // NotificationSubject contains the notification subject (Issue/Pull/Commit) type NotificationSubject struct { - Title string `json:"title"` - URL string `json:"url"` - LatestCommentURL string `json:"latest_comment_url"` - Type string `json:"type" binding:"In(Issue,Pull,Commit)"` - State StateType `json:"state"` + Title string `json:"title"` + URL string `json:"url"` + LatestCommentURL string `json:"latest_comment_url"` + Type NotifySubjectType `json:"type"` + State StateType `json:"state"` } // NotificationCount number of unread notifications type NotificationCount struct { New int64 `json:"new"` } + +// NotifySubjectType represent type of notification subject +type NotifySubjectType string + +const ( + // NotifySubjectIssue an issue is subject of an notification + NotifySubjectIssue NotifySubjectType = "Issue" + // NotifySubjectPull an pull is subject of an notification + NotifySubjectPull NotifySubjectType = "Pull" + // NotifySubjectCommit an commit is subject of an notification + NotifySubjectCommit NotifySubjectType = "Commit" + // NotifySubjectRepository an repository is subject of an notification + NotifySubjectRepository NotifySubjectType = "Repository" +) From 6d1b97b3b03a595b171779bf26ee4bb2b5e42b9d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 1 Jul 2021 04:50:04 +0200 Subject: [PATCH 2/3] add binding back --- modules/structs/notifications.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/structs/notifications.go b/modules/structs/notifications.go index 1481752f08ab..675dcf76b127 100644 --- a/modules/structs/notifications.go +++ b/modules/structs/notifications.go @@ -24,7 +24,7 @@ type NotificationSubject struct { Title string `json:"title"` URL string `json:"url"` LatestCommentURL string `json:"latest_comment_url"` - Type NotifySubjectType `json:"type"` + Type NotifySubjectType `json:"type" binding:"In(Issue,Pull,Commit)"` State StateType `json:"state"` } From 4bd082feac0f82d436eb06ae5e11c264b50c933b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 1 Jul 2021 04:51:00 +0200 Subject: [PATCH 3/3] update swagger docs --- templates/swagger/v1_json.tmpl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 40dddb06a078..dfd08bcc68f8 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -15241,8 +15241,7 @@ "x-go-name": "Title" }, "type": { - "type": "string", - "x-go-name": "Type" + "$ref": "#/definitions/NotifySubjectType" }, "url": { "type": "string", @@ -15286,6 +15285,11 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "NotifySubjectType": { + "description": "NotifySubjectType represent type of notification subject", + "type": "string", + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "OAuth2Application": { "type": "object", "title": "OAuth2Application represents an OAuth2 application.",