1
0

Add crud endpoints for notifications (#801)

Co-authored-by: kolaente <k@knt.li>
Reviewed-on: https://kolaente.dev/vikunja/api/pulls/801
Co-authored-by: konrad <konrad@kola-entertainments.de>
Co-committed-by: konrad <konrad@kola-entertainments.de>
This commit is contained in:
konrad
2021-02-21 14:50:34 +00:00
parent 509f23c550
commit 2178166ece
19 changed files with 842 additions and 66 deletions

View File

@ -59,7 +59,7 @@ func (s *IncreaseTaskCounter) Name() string {
}
// Hanlde is executed when the event IncreaseTaskCounter listens on is fired
func (s *IncreaseTaskCounter) Handle(payload message.Payload) (err error) {
func (s *IncreaseTaskCounter) Handle(msg *message.Message) (err error) {
return keyvalue.IncrBy(metrics.TaskCountKey, 1)
}
@ -73,7 +73,7 @@ func (s *DecreaseTaskCounter) Name() string {
}
// Hanlde is executed when the event DecreaseTaskCounter listens on is fired
func (s *DecreaseTaskCounter) Handle(payload message.Payload) (err error) {
func (s *DecreaseTaskCounter) Handle(msg *message.Message) (err error) {
return keyvalue.DecrBy(metrics.TaskCountKey, 1)
}
@ -87,9 +87,9 @@ func (s *SendTaskCommentNotification) Name() string {
}
// Handle is executed when the event SendTaskCommentNotification listens on is fired
func (s *SendTaskCommentNotification) Handle(payload message.Payload) (err error) {
func (s *SendTaskCommentNotification) Handle(msg *message.Message) (err error) {
event := &TaskCommentCreatedEvent{}
err = json.Unmarshal(payload, event)
err = json.Unmarshal(msg.Payload, event)
if err != nil {
return err
}
@ -133,9 +133,9 @@ func (s *SendTaskAssignedNotification) Name() string {
}
// Handle is executed when the event SendTaskAssignedNotification listens on is fired
func (s *SendTaskAssignedNotification) Handle(payload message.Payload) (err error) {
func (s *SendTaskAssignedNotification) Handle(msg *message.Message) (err error) {
event := &TaskAssigneeCreatedEvent{}
err = json.Unmarshal(payload, event)
err = json.Unmarshal(msg.Payload, event)
if err != nil {
return err
}
@ -179,9 +179,9 @@ func (s *SendTaskDeletedNotification) Name() string {
}
// Handle is executed when the event SendTaskDeletedNotification listens on is fired
func (s *SendTaskDeletedNotification) Handle(payload message.Payload) (err error) {
func (s *SendTaskDeletedNotification) Handle(msg *message.Message) (err error) {
event := &TaskDeletedEvent{}
err = json.Unmarshal(payload, event)
err = json.Unmarshal(msg.Payload, event)
if err != nil {
return err
}
@ -223,9 +223,9 @@ func (s *SubscribeAssigneeToTask) Name() string {
}
// Handle is executed when the event SubscribeAssigneeToTask listens on is fired
func (s *SubscribeAssigneeToTask) Handle(payload message.Payload) (err error) {
func (s *SubscribeAssigneeToTask) Handle(msg *message.Message) (err error) {
event := &TaskAssigneeCreatedEvent{}
err = json.Unmarshal(payload, event)
err = json.Unmarshal(msg.Payload, event)
if err != nil {
return err
}
@ -257,7 +257,7 @@ func (s *IncreaseListCounter) Name() string {
return "list.counter.increase"
}
func (s *IncreaseListCounter) Handle(payload message.Payload) (err error) {
func (s *IncreaseListCounter) Handle(msg *message.Message) (err error) {
return keyvalue.IncrBy(metrics.ListCountKey, 1)
}
@ -268,7 +268,7 @@ func (s *DecreaseListCounter) Name() string {
return "list.counter.decrease"
}
func (s *DecreaseListCounter) Handle(payload message.Payload) (err error) {
func (s *DecreaseListCounter) Handle(msg *message.Message) (err error) {
return keyvalue.DecrBy(metrics.ListCountKey, 1)
}
@ -282,9 +282,9 @@ func (s *SendListCreatedNotification) Name() string {
}
// Handle is executed when the event SendListCreatedNotification listens on is fired
func (s *SendListCreatedNotification) Handle(payload message.Payload) (err error) {
func (s *SendListCreatedNotification) Handle(msg *message.Message) (err error) {
event := &ListCreatedEvent{}
err = json.Unmarshal(payload, event)
err = json.Unmarshal(msg.Payload, event)
if err != nil {
return err
}
@ -330,7 +330,7 @@ func (s *IncreaseNamespaceCounter) Name() string {
}
// Hanlde is executed when the event IncreaseNamespaceCounter listens on is fired
func (s *IncreaseNamespaceCounter) Handle(payload message.Payload) (err error) {
func (s *IncreaseNamespaceCounter) Handle(msg *message.Message) (err error) {
return keyvalue.IncrBy(metrics.NamespaceCountKey, 1)
}
@ -344,7 +344,7 @@ func (s *DecreaseNamespaceCounter) Name() string {
}
// Hanlde is executed when the event DecreaseNamespaceCounter listens on is fired
func (s *DecreaseNamespaceCounter) Handle(payload message.Payload) (err error) {
func (s *DecreaseNamespaceCounter) Handle(msg *message.Message) (err error) {
return keyvalue.DecrBy(metrics.NamespaceCountKey, 1)
}
@ -361,7 +361,7 @@ func (s *IncreaseTeamCounter) Name() string {
}
// Hanlde is executed when the event IncreaseTeamCounter listens on is fired
func (s *IncreaseTeamCounter) Handle(payload message.Payload) (err error) {
func (s *IncreaseTeamCounter) Handle(msg *message.Message) (err error) {
return keyvalue.IncrBy(metrics.TeamCountKey, 1)
}
@ -375,7 +375,7 @@ func (s *DecreaseTeamCounter) Name() string {
}
// Hanlde is executed when the event DecreaseTeamCounter listens on is fired
func (s *DecreaseTeamCounter) Handle(payload message.Payload) (err error) {
func (s *DecreaseTeamCounter) Handle(msg *message.Message) (err error) {
return keyvalue.DecrBy(metrics.TeamCountKey, 1)
}
@ -389,9 +389,9 @@ func (s *SendTeamMemberAddedNotification) Name() string {
}
// Handle is executed when the event SendTeamMemberAddedNotification listens on is fired
func (s *SendTeamMemberAddedNotification) Handle(payload message.Payload) (err error) {
func (s *SendTeamMemberAddedNotification) Handle(msg *message.Message) (err error) {
event := &TeamMemberAddedEvent{}
err = json.Unmarshal(payload, event)
err = json.Unmarshal(msg.Payload, event)
if err != nil {
return err
}

View File

@ -28,8 +28,8 @@ import (
// ReminderDueNotification represents a ReminderDueNotification notification
type ReminderDueNotification struct {
User *user.User
Task *Task
User *user.User `json:"user"`
Task *Task `json:"task"`
}
// ToMail returns the mail notification for ReminderDueNotification
@ -48,11 +48,16 @@ func (n *ReminderDueNotification) ToDB() interface{} {
return nil
}
// Name returns the name of the notification
func (n *ReminderDueNotification) Name() string {
return ""
}
// TaskCommentNotification represents a TaskCommentNotification notification
type TaskCommentNotification struct {
Doer *user.User
Task *Task
Comment *TaskComment
Doer *user.User `json:"doer"`
Task *Task `json:"task"`
Comment *TaskComment `json:"comment"`
}
// ToMail returns the mail notification for TaskCommentNotification
@ -76,11 +81,16 @@ func (n *TaskCommentNotification) ToDB() interface{} {
return n
}
// Name returns the name of the notification
func (n *TaskCommentNotification) Name() string {
return "task.comment"
}
// TaskAssignedNotification represents a TaskAssignedNotification notification
type TaskAssignedNotification struct {
Doer *user.User
Task *Task
Assignee *user.User
Doer *user.User `json:"doer"`
Task *Task `json:"task"`
Assignee *user.User `json:"assignee"`
}
// ToMail returns the mail notification for TaskAssignedNotification
@ -96,10 +106,15 @@ func (n *TaskAssignedNotification) ToDB() interface{} {
return n
}
// Name returns the name of the notification
func (n *TaskAssignedNotification) Name() string {
return "task.assigned"
}
// TaskDeletedNotification represents a TaskDeletedNotification notification
type TaskDeletedNotification struct {
Doer *user.User
Task *Task
Doer *user.User `json:"doer"`
Task *Task `json:"task"`
}
// ToMail returns the mail notification for TaskDeletedNotification
@ -114,10 +129,15 @@ func (n *TaskDeletedNotification) ToDB() interface{} {
return n
}
// Name returns the name of the notification
func (n *TaskDeletedNotification) Name() string {
return "task.deleted"
}
// ListCreatedNotification represents a ListCreatedNotification notification
type ListCreatedNotification struct {
Doer *user.User
List *List
Doer *user.User `json:"doer"`
List *List `json:"list"`
}
// ToMail returns the mail notification for ListCreatedNotification
@ -130,14 +150,19 @@ func (n *ListCreatedNotification) ToMail() *notifications.Mail {
// ToDB returns the ListCreatedNotification notification in a format which can be saved in the db
func (n *ListCreatedNotification) ToDB() interface{} {
return nil
return n
}
// Name returns the name of the notification
func (n *ListCreatedNotification) Name() string {
return "list.created"
}
// TeamMemberAddedNotification represents a TeamMemberAddedNotification notification
type TeamMemberAddedNotification struct {
Member *user.User
Doer *user.User
Team *Team
Member *user.User `json:"member"`
Doer *user.User `json:"doer"`
Team *Team `json:"team"`
}
// ToMail returns the mail notification for TeamMemberAddedNotification
@ -152,5 +177,10 @@ func (n *TeamMemberAddedNotification) ToMail() *notifications.Mail {
// ToDB returns the TeamMemberAddedNotification notification in a format which can be saved in the db
func (n *TeamMemberAddedNotification) ToDB() interface{} {
return nil
return n
}
// Name returns the name of the notification
func (n *TeamMemberAddedNotification) Name() string {
return "team.member.added"
}

View File

@ -0,0 +1,84 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"code.vikunja.io/api/pkg/notifications"
"code.vikunja.io/web"
"xorm.io/xorm"
)
// DatabaseNotifications is a wrapper around the crud operations that come with a database notification.
type DatabaseNotifications struct {
notifications.DatabaseNotification
// Whether or not to mark this notification as read or unread.
// True is read, false is unread.
Read bool `xorm:"-" json:"read"`
web.CRUDable `xorm:"-" json:"-"`
web.Rights `xorm:"-" json:"-"`
}
// ReadAll returns all database notifications for a user
// @Summary Get all notifications for the current user
// @Description Returns an array with all notifications for the current user.
// @tags subscriptions
// @Accept json
// @Produce json
// @Param page query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
// @Security JWTKeyAuth
// @Success 200 {array} notifications.DatabaseNotification "The notifications"
// @Failure 403 {object} web.HTTPError "Link shares cannot have notifications."
// @Failure 500 {object} models.Message "Internal error"
// @Router /notifications [get]
func (d *DatabaseNotifications) ReadAll(s *xorm.Session, a web.Auth, search string, page int, perPage int) (ls interface{}, resultCount int, numberOfEntries int64, err error) {
if _, is := a.(*LinkSharing); is {
return nil, 0, 0, ErrGenericForbidden{}
}
limit, start := getLimitFromPageIndex(page, perPage)
return notifications.GetNotificationsForUser(s, a.GetID(), limit, start)
}
// CanUpdate checks if a user can mark a notification as read.
func (d *DatabaseNotifications) CanUpdate(s *xorm.Session, a web.Auth) (bool, error) {
if _, is := a.(*LinkSharing); is {
return false, nil
}
return notifications.CanMarkNotificationAsRead(s, &d.DatabaseNotification, a.GetID())
}
// Update marks a notification as read.
// @Summary Mark a notification as (un-)read
// @Description Marks a notification as either read or unread. A user can only mark their own notifications as read.
// @tags subscriptions
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "Notification ID"
// @Success 200 {object} models.DatabaseNotifications "The notification to mark as read."
// @Failure 403 {object} web.HTTPError "The user does not have access to that notification."
// @Failure 403 {object} web.HTTPError "Link shares cannot have notifications."
// @Failure 404 {object} web.HTTPError "The notification does not exist."
// @Failure 500 {object} models.Message "Internal error"
// @Router /notifications/{id} [post]
func (d *DatabaseNotifications) Update(s *xorm.Session, a web.Auth) (err error) {
return notifications.MarkNotificationAsRead(s, &d.DatabaseNotification, d.Read)
}