Skip to content

Commit 4f5ff1e

Browse files
authored
move notification APIFormat (#13783)
1 parent 4569339 commit 4f5ff1e

File tree

5 files changed

+75
-63
lines changed

5 files changed

+75
-63
lines changed

models/notification.go

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"code.gitea.io/gitea/modules/log"
1212
"code.gitea.io/gitea/modules/setting"
13-
api "code.gitea.io/gitea/modules/structs"
1413
"code.gitea.io/gitea/modules/timeutil"
1514

1615
"xorm.io/builder"
@@ -332,56 +331,6 @@ func countUnread(e Engine, userID int64) int64 {
332331
return exist
333332
}
334333

335-
// APIFormat converts a Notification to api.NotificationThread
336-
func (n *Notification) APIFormat() *api.NotificationThread {
337-
result := &api.NotificationThread{
338-
ID: n.ID,
339-
Unread: !(n.Status == NotificationStatusRead || n.Status == NotificationStatusPinned),
340-
Pinned: n.Status == NotificationStatusPinned,
341-
UpdatedAt: n.UpdatedUnix.AsTime(),
342-
URL: n.APIURL(),
343-
}
344-
345-
//since user only get notifications when he has access to use minimal access mode
346-
if n.Repository != nil {
347-
result.Repository = n.Repository.APIFormat(AccessModeRead)
348-
}
349-
350-
//handle Subject
351-
switch n.Source {
352-
case NotificationSourceIssue:
353-
result.Subject = &api.NotificationSubject{Type: "Issue"}
354-
if n.Issue != nil {
355-
result.Subject.Title = n.Issue.Title
356-
result.Subject.URL = n.Issue.APIURL()
357-
result.Subject.State = n.Issue.State()
358-
comment, err := n.Issue.GetLastComment()
359-
if err == nil && comment != nil {
360-
result.Subject.LatestCommentURL = comment.APIURL()
361-
}
362-
}
363-
case NotificationSourcePullRequest:
364-
result.Subject = &api.NotificationSubject{Type: "Pull"}
365-
if n.Issue != nil {
366-
result.Subject.Title = n.Issue.Title
367-
result.Subject.URL = n.Issue.APIURL()
368-
result.Subject.State = n.Issue.State()
369-
comment, err := n.Issue.GetLastComment()
370-
if err == nil && comment != nil {
371-
result.Subject.LatestCommentURL = comment.APIURL()
372-
}
373-
}
374-
case NotificationSourceCommit:
375-
result.Subject = &api.NotificationSubject{
376-
Type: "Commit",
377-
Title: n.CommitID,
378-
}
379-
//unused until now
380-
}
381-
382-
return result
383-
}
384-
385334
// LoadAttributes load Repo Issue User and Comment if not loaded
386335
func (n *Notification) LoadAttributes() (err error) {
387336
return n.loadAttributes(x)
@@ -470,15 +419,6 @@ func (n *Notification) APIURL() string {
470419
// NotificationList contains a list of notifications
471420
type NotificationList []*Notification
472421

473-
// APIFormat converts a NotificationList to api.NotificationThread list
474-
func (nl NotificationList) APIFormat() []*api.NotificationThread {
475-
var result = make([]*api.NotificationThread, 0, len(nl))
476-
for _, n := range nl {
477-
result = append(result, n.APIFormat())
478-
}
479-
return result
480-
}
481-
482422
// LoadAttributes load Repo Issue User and Comment if not loaded
483423
func (nl NotificationList) LoadAttributes() (err error) {
484424
for i := 0; i < len(nl); i++ {

modules/convert/notification.go

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

routers/api/v1/notify/repo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/context"
14+
"code.gitea.io/gitea/modules/convert"
1415
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/routers/api/v1/utils"
1617
)
@@ -127,7 +128,7 @@ func ListRepoNotifications(ctx *context.APIContext) {
127128
return
128129
}
129130

130-
ctx.JSON(http.StatusOK, nl.APIFormat())
131+
ctx.JSON(http.StatusOK, convert.ToNotifications(nl))
131132
}
132133

133134
// ReadRepoNotifications mark notification threads as read on a specific repo

routers/api/v1/notify/threads.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/context"
13+
"code.gitea.io/gitea/modules/convert"
1314
)
1415

1516
// GetThread get notification by ID
@@ -44,7 +45,7 @@ func GetThread(ctx *context.APIContext) {
4445
return
4546
}
4647

47-
ctx.JSON(http.StatusOK, n.APIFormat())
48+
ctx.JSON(http.StatusOK, convert.ToNotificationThread(n))
4849
}
4950

5051
// ReadThread mark notification as read by ID

routers/api/v1/notify/user.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/context"
14+
"code.gitea.io/gitea/modules/convert"
1415
"code.gitea.io/gitea/routers/api/v1/utils"
1516
)
1617

@@ -87,7 +88,7 @@ func ListNotifications(ctx *context.APIContext) {
8788
return
8889
}
8990

90-
ctx.JSON(http.StatusOK, nl.APIFormat())
91+
ctx.JSON(http.StatusOK, convert.ToNotifications(nl))
9192
}
9293

9394
// ReadNotifications mark notification threads as read, unread, or pinned

0 commit comments

Comments
 (0)