summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-01-14 16:37:19 +0100
committerAntoine GIRARD <sapk@users.noreply.github.com>2020-01-14 16:37:19 +0100
commit44de66bf50d1ab9a5acc298063cd942768092a19 (patch)
treefda1b76b954d720dfdfefded22883a51ab52e45e /models
parentce274d652f2be03364bc0d0a8cb1b4d1996b16c7 (diff)
downloadgitea-44de66bf50d1ab9a5acc298063cd942768092a19.tar.gz
gitea-44de66bf50d1ab9a5acc298063cd942768092a19.zip
[API] add endpoint to check notifications [Extend #9488] (#9595)
* introduce GET /notifications/new * add TEST * use Sprintf instead of path.Join * Error more verbose * return number of notifications if unreaded exist * 200 http status for available notifications
Diffstat (limited to 'models')
-rw-r--r--models/issue.go3
-rw-r--r--models/issue_comment.go3
-rw-r--r--models/notification.go17
3 files changed, 18 insertions, 5 deletions
diff --git a/models/issue.go b/models/issue.go
index 25765292ae..b6408365f7 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -7,7 +7,6 @@ package models
import (
"fmt"
- "path"
"regexp"
"sort"
"strconv"
@@ -324,7 +323,7 @@ func (issue *Issue) GetIsRead(userID int64) error {
// APIURL returns the absolute APIURL to this issue.
func (issue *Issue) APIURL() string {
- return issue.Repo.APIURL() + "/" + path.Join("issues", fmt.Sprint(issue.Index))
+ return fmt.Sprintf("%s/issues/%d", issue.Repo.APIURL(), issue.Index)
}
// HTMLURL returns the absolute URL to this issue.
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 8f54d9656a..699b8f0487 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -8,7 +8,6 @@ package models
import (
"fmt"
- "path"
"strings"
"code.gitea.io/gitea/modules/git"
@@ -249,7 +248,7 @@ func (c *Comment) APIURL() string {
return ""
}
- return c.Issue.Repo.APIURL() + "/" + path.Join("issues/comments", fmt.Sprint(c.ID))
+ return fmt.Sprintf("%s/issues/comments/%d", c.Issue.Repo.APIURL(), c.ID)
}
// IssueURL formats a URL-string to the issue
diff --git a/models/notification.go b/models/notification.go
index 8e9bca0dc6..403c53243d 100644
--- a/models/notification.go
+++ b/models/notification.go
@@ -8,6 +8,7 @@ import (
"fmt"
"path"
+ "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
@@ -294,6 +295,20 @@ func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, p
return
}
+// CountUnread count unread notifications for a user
+func CountUnread(user *User) int64 {
+ return countUnread(x, user.ID)
+}
+
+func countUnread(e Engine, userID int64) int64 {
+ exist, err := e.Where("user_id = ?", userID).And("status = ?", NotificationStatusUnread).Count(new(Notification))
+ if err != nil {
+ log.Error("countUnread", err)
+ return 0
+ }
+ return exist
+}
+
// APIFormat converts a Notification to api.NotificationThread
func (n *Notification) APIFormat() *api.NotificationThread {
result := &api.NotificationThread{
@@ -388,7 +403,7 @@ func (n *Notification) loadComment(e Engine) (err error) {
if n.Comment == nil && n.CommentID > 0 {
n.Comment, err = GetCommentByID(n.CommentID)
if err != nil {
- return fmt.Errorf("GetCommentByID [%d]: %v", n.CommentID, err)
+ return fmt.Errorf("GetCommentByID [%d] for issue ID [%d]: %v", n.CommentID, n.IssueID, err)
}
}
return nil