]> source.dussan.org Git - gitea.git/commitdiff
Extend Notifications API and return pinned notifications by default (#12164) (#12232)
authorzeripath <art27@cantab.net>
Mon, 13 Jul 2020 20:52:05 +0000 (21:52 +0100)
committerGitHub <noreply@github.com>
Mon, 13 Jul 2020 20:52:05 +0000 (21:52 +0100)
Backport #12164

This PR extends the notifications API to allow specific notification statuses to be searched for and to allow setting of notifications to statuses other than read.

By default unread and pinned statuses will be returned when querying for notifications - however pinned statuses will not be marked as read.

Close #12152

Signed-off-by: Andrew Thornton art27@cantab.net
integrations/api_notification_test.go
integrations/eventsource_test.go
models/notification.go
routers/api/v1/notify/repo.go
routers/api/v1/notify/threads.go
routers/api/v1/notify/user.go
templates/swagger/v1_json.tmpl

index 3296604a081fc002b0511df59aa943ef937c4ba1..42041734200b03cb5ceabb9af46a528d31a0823c 100644 (file)
@@ -55,7 +55,7 @@ func TestAPINotification(t *testing.T) {
        assert.EqualValues(t, false, apiNL[2].Pinned)
 
        // -- GET /repos/{owner}/{repo}/notifications --
-       req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?token=%s", user2.Name, repo1.Name, token))
+       req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?status-types=unread&token=%s", user2.Name, repo1.Name, token))
        resp = session.MakeRequest(t, req, http.StatusOK)
        DecodeJSON(t, resp, &apiNL)
 
@@ -92,7 +92,7 @@ func TestAPINotification(t *testing.T) {
        assert.True(t, new.New > 0)
 
        // -- mark notifications as read --
-       req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s", token))
+       req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?status-types=unread&token=%s", token))
        resp = session.MakeRequest(t, req, http.StatusOK)
        DecodeJSON(t, resp, &apiNL)
        assert.Len(t, apiNL, 2)
@@ -101,7 +101,7 @@ func TestAPINotification(t *testing.T) {
        req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s&token=%s", user2.Name, repo1.Name, lastReadAt, token))
        resp = session.MakeRequest(t, req, http.StatusResetContent)
 
-       req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s", token))
+       req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?status-types=unread&token=%s", token))
        resp = session.MakeRequest(t, req, http.StatusOK)
        DecodeJSON(t, resp, &apiNL)
        assert.Len(t, apiNL, 1)
index bc154531476a5bee80a55215393804d0ab70b479..caaf8c2cefff89cc720d48c494861741d9d9cd9f 100644 (file)
@@ -59,7 +59,7 @@ func TestEventSourceManagerRun(t *testing.T) {
        var apiNL []api.NotificationThread
 
        // -- mark notifications as read --
-       req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s", token))
+       req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?status-types=unread&token=%s", token))
        resp := session.MakeRequest(t, req, http.StatusOK)
 
        DecodeJSON(t, resp, &apiNL)
@@ -69,7 +69,7 @@ func TestEventSourceManagerRun(t *testing.T) {
        req = NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/notifications?last_read_at=%s&token=%s", user2.Name, repo1.Name, lastReadAt, token))
        resp = session.MakeRequest(t, req, http.StatusResetContent)
 
-       req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s", token))
+       req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/notifications?token=%s&status-types=unread", token))
        resp = session.MakeRequest(t, req, http.StatusOK)
        DecodeJSON(t, resp, &apiNL)
        assert.Len(t, apiNL, 1)
index 1c378a1350da0fe123107408436b0c90473161c4..9258b68f22b1a6f7a6c99e193ec6c82ac2e8707f 100644 (file)
@@ -72,7 +72,7 @@ type FindNotificationOptions struct {
        UserID            int64
        RepoID            int64
        IssueID           int64
-       Status            NotificationStatus
+       Status            []NotificationStatus
        UpdatedAfterUnix  int64
        UpdatedBeforeUnix int64
 }
@@ -89,8 +89,8 @@ func (opts *FindNotificationOptions) ToCond() builder.Cond {
        if opts.IssueID != 0 {
                cond = cond.And(builder.Eq{"notification.issue_id": opts.IssueID})
        }
-       if opts.Status != 0 {
-               cond = cond.And(builder.Eq{"notification.status": opts.Status})
+       if len(opts.Status) > 0 {
+               cond = cond.And(builder.In("notification.status", opts.Status))
        }
        if opts.UpdatedAfterUnix != 0 {
                cond = cond.And(builder.Gte{"notification.updated_unix": opts.UpdatedAfterUnix})
index 10c00c5467a21ff1aa913dd3d4b97a6167880c05..ca9aea2565770c78c06b2a5ddf03728676a6e5e3 100644 (file)
@@ -11,9 +11,37 @@ import (
 
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/modules/context"
+       "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/routers/api/v1/utils"
 )
 
+func statusStringToNotificationStatus(status string) models.NotificationStatus {
+       switch strings.ToLower(strings.TrimSpace(status)) {
+       case "unread":
+               return models.NotificationStatusUnread
+       case "read":
+               return models.NotificationStatusRead
+       case "pinned":
+               return models.NotificationStatusPinned
+       default:
+               return 0
+       }
+}
+
+func statusStringsToNotificationStatuses(statuses []string, defaultStatuses []string) []models.NotificationStatus {
+       if len(statuses) == 0 {
+               statuses = defaultStatuses
+       }
+       results := make([]models.NotificationStatus, 0, len(statuses))
+       for _, status := range statuses {
+               notificationStatus := statusStringToNotificationStatus(status)
+               if notificationStatus > 0 {
+                       results = append(results, notificationStatus)
+               }
+       }
+       return results
+}
+
 // ListRepoNotifications list users's notification threads on a specific repo
 func ListRepoNotifications(ctx *context.APIContext) {
        // swagger:operation GET /repos/{owner}/{repo}/notifications notification notifyGetRepoList
@@ -39,6 +67,14 @@ func ListRepoNotifications(ctx *context.APIContext) {
        //   description: If true, show notifications marked as read. Default value is false
        //   type: string
        //   required: false
+       // - name: status-types
+       //   in: query
+       //   description: "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread & pinned"
+       //   type: array
+       //   collectionFormat: multi
+       //   items:
+       //     type: string
+       //   required: false
        // - name: since
        //   in: query
        //   description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
@@ -75,9 +111,10 @@ func ListRepoNotifications(ctx *context.APIContext) {
                UpdatedBeforeUnix: before,
                UpdatedAfterUnix:  since,
        }
-       qAll := strings.Trim(ctx.Query("all"), " ")
-       if qAll != "true" {
-               opts.Status = models.NotificationStatusUnread
+
+       if !ctx.QueryBool("all") {
+               statuses := ctx.QueryStrings("status-types")
+               opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
        }
        nl, err := models.GetNotifications(opts)
        if err != nil {
@@ -97,7 +134,7 @@ func ListRepoNotifications(ctx *context.APIContext) {
 func ReadRepoNotifications(ctx *context.APIContext) {
        // swagger:operation PUT /repos/{owner}/{repo}/notifications notification notifyReadRepoList
        // ---
-       // summary: Mark notification threads as read on a specific repo
+       // summary: Mark notification threads as read, pinned or unread on a specific repo
        // consumes:
        // - application/json
        // produces:
@@ -113,6 +150,24 @@ func ReadRepoNotifications(ctx *context.APIContext) {
        //   description: name of the repo
        //   type: string
        //   required: true
+       // - name: all
+       //   in: query
+       //   description: If true, mark all notifications on this repo. Default value is false
+       //   type: string
+       //   required: false
+       // - name: status-types
+       //   in: query
+       //   description: "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread."
+       //   type: array
+       //   collectionFormat: multi
+       //   items:
+       //     type: string
+       //   required: false
+       // - name: to-status
+       //   in: query
+       //   description: Status to mark notifications as. Defaults to read.
+       //   type: string
+       //   required: false
        // - name: last_read_at
        //   in: query
        //   description: Describes the last point that notifications were checked. Anything updated since this time will not be updated.
@@ -135,11 +190,17 @@ func ReadRepoNotifications(ctx *context.APIContext) {
                        lastRead = tmpLastRead.Unix()
                }
        }
+
        opts := models.FindNotificationOptions{
                UserID:            ctx.User.ID,
                RepoID:            ctx.Repo.Repository.ID,
                UpdatedBeforeUnix: lastRead,
-               Status:            models.NotificationStatusUnread,
+       }
+
+       if !ctx.QueryBool("all") {
+               statuses := ctx.QueryStrings("status-types")
+               opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
+               log.Error("%v", opts.Status)
        }
        nl, err := models.GetNotifications(opts)
        if err != nil {
@@ -147,8 +208,13 @@ func ReadRepoNotifications(ctx *context.APIContext) {
                return
        }
 
+       targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
+       if targetStatus == 0 {
+               targetStatus = models.NotificationStatusRead
+       }
+
        for _, n := range nl {
-               err := models.SetNotificationStatus(n.ID, ctx.User, models.NotificationStatusRead)
+               err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
                if err != nil {
                        ctx.InternalServerError(err)
                        return
index d0119e9938eec1b9d17daeba82e06b9aa0027fd5..86ae2dca318213a35448f824c6168b314d8c92cb 100644 (file)
@@ -62,6 +62,12 @@ func ReadThread(ctx *context.APIContext) {
        //   description: id of notification thread
        //   type: string
        //   required: true
+       // - name: to-status
+       //   in: query
+       //   description: Status to mark notifications as
+       //   type: string
+       //   default: read
+       //   required: false
        // responses:
        //   "205":
        //     "$ref": "#/responses/empty"
@@ -75,7 +81,12 @@ func ReadThread(ctx *context.APIContext) {
                return
        }
 
-       err := models.SetNotificationStatus(n.ID, ctx.User, models.NotificationStatusRead)
+       targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
+       if targetStatus == 0 {
+               targetStatus = models.NotificationStatusRead
+       }
+
+       err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
        if err != nil {
                ctx.InternalServerError(err)
                return
index 7f731e25d4a5fe820ea29abcf5a5fb24e8fb289c..08a2f084153053b833107d109f29973985547331 100644 (file)
@@ -29,6 +29,14 @@ func ListNotifications(ctx *context.APIContext) {
        //   description: If true, show notifications marked as read. Default value is false
        //   type: string
        //   required: false
+       // - name: status-types
+       //   in: query
+       //   description: "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread & pinned."
+       //   type: array
+       //   collectionFormat: multi
+       //   items:
+       //     type: string
+       //   required: false
        // - name: since
        //   in: query
        //   description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
@@ -64,9 +72,9 @@ func ListNotifications(ctx *context.APIContext) {
                UpdatedBeforeUnix: before,
                UpdatedAfterUnix:  since,
        }
-       qAll := strings.Trim(ctx.Query("all"), " ")
-       if qAll != "true" {
-               opts.Status = models.NotificationStatusUnread
+       if !ctx.QueryBool("all") {
+               statuses := ctx.QueryStrings("status-types")
+               opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread", "pinned"})
        }
        nl, err := models.GetNotifications(opts)
        if err != nil {
@@ -82,11 +90,11 @@ func ListNotifications(ctx *context.APIContext) {
        ctx.JSON(http.StatusOK, nl.APIFormat())
 }
 
-// ReadNotifications mark notification threads as read
+// ReadNotifications mark notification threads as read, unread, or pinned
 func ReadNotifications(ctx *context.APIContext) {
        // swagger:operation PUT /notifications notification notifyReadList
        // ---
-       // summary: Mark notification threads as read
+       // summary: Mark notification threads as read, pinned or unread
        // consumes:
        // - application/json
        // produces:
@@ -98,6 +106,24 @@ func ReadNotifications(ctx *context.APIContext) {
        //   type: string
        //   format: date-time
        //   required: false
+       // - name: all
+       //   in: query
+       //   description: If true, mark all notifications on this repo. Default value is false
+       //   type: string
+       //   required: false
+       // - name: status-types
+       //   in: query
+       //   description: "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread."
+       //   type: array
+       //   collectionFormat: multi
+       //   items:
+       //     type: string
+       //   required: false
+       // - name: to-status
+       //   in: query
+       //   description: Status to mark notifications as, Defaults to read.
+       //   type: string
+       //   required: false
        // responses:
        //   "205":
        //     "$ref": "#/responses/empty"
@@ -117,7 +143,10 @@ func ReadNotifications(ctx *context.APIContext) {
        opts := models.FindNotificationOptions{
                UserID:            ctx.User.ID,
                UpdatedBeforeUnix: lastRead,
-               Status:            models.NotificationStatusUnread,
+       }
+       if !ctx.QueryBool("all") {
+               statuses := ctx.QueryStrings("status-types")
+               opts.Status = statusStringsToNotificationStatuses(statuses, []string{"unread"})
        }
        nl, err := models.GetNotifications(opts)
        if err != nil {
@@ -125,8 +154,13 @@ func ReadNotifications(ctx *context.APIContext) {
                return
        }
 
+       targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
+       if targetStatus == 0 {
+               targetStatus = models.NotificationStatusRead
+       }
+
        for _, n := range nl {
-               err := models.SetNotificationStatus(n.ID, ctx.User, models.NotificationStatusRead)
+               err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
                if err != nil {
                        ctx.InternalServerError(err)
                        return
index 5058166ab7d4cebaca3c3ef42b072dde5ffc3aaf..12c732a9a975b84f07c42b145a11383845db430c 100644 (file)
             "name": "all",
             "in": "query"
           },
+          {
+            "type": "array",
+            "items": {
+              "type": "string"
+            },
+            "collectionFormat": "multi",
+            "description": "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread \u0026 pinned.",
+            "name": "status-types",
+            "in": "query"
+          },
           {
             "type": "string",
             "format": "date-time",
         "tags": [
           "notification"
         ],
-        "summary": "Mark notification threads as read",
+        "summary": "Mark notification threads as read, pinned or unread",
         "operationId": "notifyReadList",
         "parameters": [
           {
             "description": "Describes the last point that notifications were checked. Anything updated since this time will not be updated.",
             "name": "last_read_at",
             "in": "query"
+          },
+          {
+            "type": "string",
+            "description": "If true, mark all notifications on this repo. Default value is false",
+            "name": "all",
+            "in": "query"
+          },
+          {
+            "type": "array",
+            "items": {
+              "type": "string"
+            },
+            "collectionFormat": "multi",
+            "description": "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread.",
+            "name": "status-types",
+            "in": "query"
+          },
+          {
+            "type": "string",
+            "description": "Status to mark notifications as, Defaults to read.",
+            "name": "to-status",
+            "in": "query"
           }
         ],
         "responses": {
             "name": "id",
             "in": "path",
             "required": true
+          },
+          {
+            "type": "string",
+            "default": "read",
+            "description": "Status to mark notifications as",
+            "name": "to-status",
+            "in": "query"
           }
         ],
         "responses": {
             "name": "all",
             "in": "query"
           },
+          {
+            "type": "array",
+            "items": {
+              "type": "string"
+            },
+            "collectionFormat": "multi",
+            "description": "Show notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread \u0026 pinned",
+            "name": "status-types",
+            "in": "query"
+          },
           {
             "type": "string",
             "format": "date-time",
         "tags": [
           "notification"
         ],
-        "summary": "Mark notification threads as read on a specific repo",
+        "summary": "Mark notification threads as read, pinned or unread on a specific repo",
         "operationId": "notifyReadRepoList",
         "parameters": [
           {
             "in": "path",
             "required": true
           },
+          {
+            "type": "string",
+            "description": "If true, mark all notifications on this repo. Default value is false",
+            "name": "all",
+            "in": "query"
+          },
+          {
+            "type": "array",
+            "items": {
+              "type": "string"
+            },
+            "collectionFormat": "multi",
+            "description": "Mark notifications with the provided status types. Options are: unread, read and/or pinned. Defaults to unread.",
+            "name": "status-types",
+            "in": "query"
+          },
+          {
+            "type": "string",
+            "description": "Status to mark notifications as. Defaults to read.",
+            "name": "to-status",
+            "in": "query"
+          },
           {
             "type": "string",
             "format": "date-time",