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

@ -125,7 +125,7 @@ All listeners must implement this interface:
```golang ```golang
// Listener represents something that listens to events // Listener represents something that listens to events
type Listener interface { type Listener interface {
Handle(payload message.Payload) error Handle(msg *message.Message) error
Name() string Name() string
} }
``` ```

View file

@ -22,6 +22,7 @@ Each notification has to implement this interface:
type Notification interface { type Notification interface {
ToMail() *Mail ToMail() *Mail
ToDB() interface{} ToDB() interface{}
Name() string
} }
``` ```
@ -59,7 +60,9 @@ If not provided, the `from` field of the mail contains the value configured in [
### Database notifications ### Database notifications
All data returned from the `ToDB()` method is serialized to json and saved into the database, along with the id of the notifiable and a time stamp. All data returned from the `ToDB()` method is serialized to json and saved into the database, along with the id of the
notifiable, the name of the notification and a time stamp.
If you don't use the database notification, the `Name()` function can return an empty string.
## Creating a new notification ## Creating a new notification

View file

@ -828,9 +828,9 @@ func (s *` + name + `) Name() string {
} }
// Handle is executed when the event ` + name + ` listens on is fired // Handle is executed when the event ` + name + ` listens on is fired
func (s *` + name + `) Handle(payload message.Payload) (err error) { func (s *` + name + `) Handle(msg *message.Message) (err error) {
event := &` + event + `{} event := &` + event + `{}
err = json.Unmarshal(payload, event) err = json.Unmarshal(msg.Payload, event)
if err != nil { if err != nil {
return err return err
} }
@ -900,6 +900,8 @@ func (Dev) MakeNotification(name, module string) error {
name += "Notification" name += "Notification"
} }
notficationName := strings.ReplaceAll(strcase.ToDelimited(name, '.'), ".notification", "")
newNotificationCode := ` newNotificationCode := `
// ` + name + ` represents a ` + name + ` notification // ` + name + ` represents a ` + name + ` notification
type ` + name + ` struct { type ` + name + ` struct {
@ -918,6 +920,12 @@ func (n *` + name + `) ToMail() *notifications.Mail {
func (n *` + name + `) ToDB() interface{} { func (n *` + name + `) ToDB() interface{} {
return nil return nil
} }
// Name returns the name of the notification
func (n *` + name + `) Name() string {
return "` + notficationName + `"
}
` `
filename := "./pkg/" + module + "/notifications.go" filename := "./pkg/" + module + "/notifications.go"
if err := appendToFile(filename, newNotificationCode); err != nil { if err := appendToFile(filename, newNotificationCode); err != nil {

View file

@ -71,9 +71,7 @@ func InitEvents() (err error) {
for topic, funcs := range listeners { for topic, funcs := range listeners {
for _, handler := range funcs { for _, handler := range funcs {
router.AddNoPublisherHandler(topic+"."+handler.Name(), topic, pubsub, func(msg *message.Message) error { router.AddNoPublisherHandler(topic+"."+handler.Name(), topic, pubsub, handler.Handle)
return handler.Handle(msg.Payload)
})
} }
} }

View file

@ -20,7 +20,7 @@ import "github.com/ThreeDotsLabs/watermill/message"
// Listener represents something that listens to events // Listener represents something that listens to events
type Listener interface { type Listener interface {
Handle(payload message.Payload) error Handle(msg *message.Message) error
Name() string Name() string
} }

View file

@ -0,0 +1,45 @@
// 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 migration
import (
"time"
"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"
)
type notifications20210220222121 struct {
ReadAt time.Time `xorm:"datetime null" json:"read_at"`
}
func (notifications20210220222121) TableName() string {
return "notifications"
}
func init() {
migrations = append(migrations, &xormigrate.Migration{
ID: "20210220222121",
Description: "Add a read_at column to notifications",
Migrate: func(tx *xorm.Engine) error {
return tx.Sync2(notifications20210220222121{})
},
Rollback: func(tx *xorm.Engine) error {
return nil
},
})
}

View file

@ -0,0 +1,63 @@
// 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 migration
import (
"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"
)
type notifications20210221111953 struct {
Name string `xorm:"varchar(250) index null" json:"name"`
}
func (notifications20210221111953) TableName() string {
return "notifications"
}
type notifications20210221111954 struct {
Name string `xorm:"varchar(250) index not null" json:"name"`
}
func (notifications20210221111954) TableName() string {
return "notifications"
}
func init() {
migrations = append(migrations, &xormigrate.Migration{
ID: "20210221111953",
Description: "Add name property to database notifications",
Migrate: func(tx *xorm.Engine) error {
err := tx.Sync2(notifications20210221111953{})
if err != nil {
return err
}
_, err = tx.
Cols("name").
Update(&notifications20210221111953{})
if err != nil {
return err
}
return tx.Sync2(notifications20210221111954{})
},
Rollback: func(tx *xorm.Engine) error {
return nil
},
})
}

View file

@ -59,7 +59,7 @@ func (s *IncreaseTaskCounter) Name() string {
} }
// Hanlde is executed when the event IncreaseTaskCounter listens on is fired // 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) 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 // 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) 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 // 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{} event := &TaskCommentCreatedEvent{}
err = json.Unmarshal(payload, event) err = json.Unmarshal(msg.Payload, event)
if err != nil { if err != nil {
return err return err
} }
@ -133,9 +133,9 @@ func (s *SendTaskAssignedNotification) Name() string {
} }
// Handle is executed when the event SendTaskAssignedNotification listens on is fired // 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{} event := &TaskAssigneeCreatedEvent{}
err = json.Unmarshal(payload, event) err = json.Unmarshal(msg.Payload, event)
if err != nil { if err != nil {
return err return err
} }
@ -179,9 +179,9 @@ func (s *SendTaskDeletedNotification) Name() string {
} }
// Handle is executed when the event SendTaskDeletedNotification listens on is fired // 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{} event := &TaskDeletedEvent{}
err = json.Unmarshal(payload, event) err = json.Unmarshal(msg.Payload, event)
if err != nil { if err != nil {
return err return err
} }
@ -223,9 +223,9 @@ func (s *SubscribeAssigneeToTask) Name() string {
} }
// Handle is executed when the event SubscribeAssigneeToTask listens on is fired // 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{} event := &TaskAssigneeCreatedEvent{}
err = json.Unmarshal(payload, event) err = json.Unmarshal(msg.Payload, event)
if err != nil { if err != nil {
return err return err
} }
@ -257,7 +257,7 @@ func (s *IncreaseListCounter) Name() string {
return "list.counter.increase" 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) return keyvalue.IncrBy(metrics.ListCountKey, 1)
} }
@ -268,7 +268,7 @@ func (s *DecreaseListCounter) Name() string {
return "list.counter.decrease" 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) 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 // 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{} event := &ListCreatedEvent{}
err = json.Unmarshal(payload, event) err = json.Unmarshal(msg.Payload, event)
if err != nil { if err != nil {
return err return err
} }
@ -330,7 +330,7 @@ func (s *IncreaseNamespaceCounter) Name() string {
} }
// Hanlde is executed when the event IncreaseNamespaceCounter listens on is fired // 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) 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 // 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) 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 // 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) 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 // 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) 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 // 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{} event := &TeamMemberAddedEvent{}
err = json.Unmarshal(payload, event) err = json.Unmarshal(msg.Payload, event)
if err != nil { if err != nil {
return err return err
} }

View file

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

View file

@ -0,0 +1,90 @@
// 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 notifications
import (
"time"
"xorm.io/xorm"
)
// DatabaseNotification represents a notification that was saved to the database
type DatabaseNotification struct {
// The unique, numeric id of this notification.
ID int64 `xorm:"bigint autoincr not null unique pk" json:"id" param:"notificationid"`
// The ID of the notifiable this notification is associated with.
NotifiableID int64 `xorm:"bigint not null" json:"-"`
// The actual content of the notification.
Notification interface{} `xorm:"json not null" json:"notification"`
// The name of the notification
Name string `xorm:"varchar(250) index not null" json:"name"`
// When this notification is marked as read, this will be updated with the current timestamp.
ReadAt time.Time `xorm:"datetime null" json:"read_at"`
// A timestamp when this notification was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
}
// TableName resolves to a better table name for notifications
func (d *DatabaseNotification) TableName() string {
return "notifications"
}
// GetNotificationsForUser returns all notifications for a user. It is possible to limit the amount of notifications
// to return with the limit and start parameters.
// We're not passing a user object in directly because every other package imports this one so we'd get import cycles.
func GetNotificationsForUser(s *xorm.Session, notifiableID int64, limit, start int) (notifications []*DatabaseNotification, resultCount int, total int64, err error) {
err = s.
Where("notifiable_id = ?", notifiableID).
Limit(limit, start).
OrderBy("id DESC").
Find(&notifications)
if err != nil {
return nil, 0, 0, err
}
total, err = s.
Where("notifiable_id = ?", notifiableID).
Count(&DatabaseNotification{})
return notifications, len(notifications), total, err
}
// CanMarkNotificationAsRead checks if a user can mark a notification as read.
func CanMarkNotificationAsRead(s *xorm.Session, notification *DatabaseNotification, notifiableID int64) (can bool, err error) {
can, err = s.
Where("notifiable_id = ? AND id = ?", notifiableID, notification.ID).
NoAutoCondition().
Get(notification)
return
}
// MarkNotificationAsRead marks a notification as read. It should be called only after CanMarkNotificationAsRead has
// been called.
func MarkNotificationAsRead(s *xorm.Session, notification *DatabaseNotification, read bool) (err error) {
notification.ReadAt = time.Time{}
if read {
notification.ReadAt = time.Now()
}
_, err = s.
Where("id = ?", notification.ID).
Cols("read_at").
Update(notification)
return
}

View file

@ -18,7 +18,6 @@ package notifications
import ( import (
"encoding/json" "encoding/json"
"time"
"code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/db"
) )
@ -27,6 +26,7 @@ import (
type Notification interface { type Notification interface {
ToMail() *Mail ToMail() *Mail
ToDB() interface{} ToDB() interface{}
Name() string
} }
// Notifiable is an entity which can be notified. Usually a user. // Notifiable is an entity which can be notified. Usually a user.
@ -37,25 +37,6 @@ type Notifiable interface {
RouteForDB() int64 RouteForDB() int64
} }
// DatabaseNotification represents a notification that was saved to the database
type DatabaseNotification struct {
// The unique, numeric id of this notification.
ID int64 `xorm:"bigint autoincr not null unique pk" json:"id"`
// The ID of the notifiable this notification is associated with.
NotifiableID int64 `xorm:"bigint not null" json:"-"`
// The actual content of the notification.
Notification interface{} `xorm:"json not null" json:"notification"`
// A timestamp when this notification was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
}
// TableName resolves to a better table name for notifications
func (d *DatabaseNotification) TableName() string {
return "notifications"
}
// Notify notifies a notifiable of a notification // Notify notifies a notifiable of a notification
func Notify(notifiable Notifiable, notification Notification) (err error) { func Notify(notifiable Notifiable, notification Notification) (err error) {
@ -98,6 +79,7 @@ func notifyDB(notifiable Notifiable, notification Notification) (err error) {
dbNotification := &DatabaseNotification{ dbNotification := &DatabaseNotification{
NotifiableID: notifiable.RouteForDB(), NotifiableID: notifiable.RouteForDB(),
Notification: content, Notification: content,
Name: notification.Name(),
} }
_, err = s.Insert(dbNotification) _, err = s.Insert(dbNotification)

View file

@ -44,6 +44,11 @@ func (n *testNotification) ToDB() interface{} {
return data return data
} }
// Name returns the name of the notification
func (n *testNotification) Name() string {
return "test.notification"
}
type testNotifiable struct { type testNotifiable struct {
} }

View file

@ -521,6 +521,15 @@ func registerAPIRoutes(a *echo.Group) {
a.PUT("/subscriptions/:entity/:entityID", subscriptionHandler.CreateWeb) a.PUT("/subscriptions/:entity/:entityID", subscriptionHandler.CreateWeb)
a.DELETE("/subscriptions/:entity/:entityID", subscriptionHandler.DeleteWeb) a.DELETE("/subscriptions/:entity/:entityID", subscriptionHandler.DeleteWeb)
// Notifications
notificationHandler := &handler.WebHandler{
EmptyStruct: func() handler.CObject {
return &models.DatabaseNotifications{}
},
}
a.GET("/notifications", notificationHandler.ReadAllWeb)
a.POST("/notifications/:notificationid", notificationHandler.UpdateWeb)
// Migrations // Migrations
m := a.Group("/migration") m := a.Group("/migration")

View file

@ -3901,6 +3901,118 @@ var doc = `{
} }
} }
}, },
"/notifications": {
"get": {
"security": [
{
"JWTKeyAuth": []
}
],
"description": "Returns an array with all notifications for the current user.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"subscriptions"
],
"summary": "Get all notifications for the current user",
"parameters": [
{
"type": "integer",
"description": "The page number. Used for pagination. If not provided, the first page of results is returned.",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page.",
"name": "per_page",
"in": "query"
}
],
"responses": {
"200": {
"description": "The notifications",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/notifications.DatabaseNotification"
}
}
},
"403": {
"description": "Link shares cannot have notifications.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"500": {
"description": "Internal error",
"schema": {
"$ref": "#/definitions/models.Message"
}
}
}
}
},
"/notifications/{id}": {
"post": {
"security": [
{
"JWTKeyAuth": []
}
],
"description": "Marks a notification as either read or unread. A user can only mark their own notifications as read.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"subscriptions"
],
"summary": "Mark a notification as (un-)read",
"parameters": [
{
"type": "integer",
"description": "Notification ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "The notification to mark as read.",
"schema": {
"$ref": "#/definitions/models.DatabaseNotifications"
}
},
"403": {
"description": "Link shares cannot have notifications.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"404": {
"description": "The notification does not exist.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"500": {
"description": "Internal error",
"schema": {
"$ref": "#/definitions/models.Message"
}
}
}
}
},
"/register": { "/register": {
"post": { "post": {
"description": "Creates a new user account.", "description": "Creates a new user account.",
@ -7200,6 +7312,35 @@ var doc = `{
} }
} }
}, },
"models.DatabaseNotifications": {
"type": "object",
"properties": {
"created": {
"description": "A timestamp when this notification was created. You cannot change this value.",
"type": "string"
},
"id": {
"description": "The unique, numeric id of this notification.",
"type": "integer"
},
"name": {
"description": "The name of the notification",
"type": "string"
},
"notification": {
"description": "The actual content of the notification.",
"type": "object"
},
"read": {
"description": "Whether or not to mark this notification as read or unread.\nTrue is read, false is unread.",
"type": "boolean"
},
"read_at": {
"description": "When this notification is marked as read, this will be updated with the current timestamp.",
"type": "string"
}
}
},
"models.Label": { "models.Label": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -8078,6 +8219,31 @@ var doc = `{
} }
} }
}, },
"notifications.DatabaseNotification": {
"type": "object",
"properties": {
"created": {
"description": "A timestamp when this notification was created. You cannot change this value.",
"type": "string"
},
"id": {
"description": "The unique, numeric id of this notification.",
"type": "integer"
},
"name": {
"description": "The name of the notification",
"type": "string"
},
"notification": {
"description": "The actual content of the notification.",
"type": "object"
},
"read_at": {
"description": "When this notification is marked as read, this will be updated with the current timestamp.",
"type": "string"
}
}
},
"openid.Callback": { "openid.Callback": {
"type": "object", "type": "object",
"properties": { "properties": {

View file

@ -3884,6 +3884,118 @@
} }
} }
}, },
"/notifications": {
"get": {
"security": [
{
"JWTKeyAuth": []
}
],
"description": "Returns an array with all notifications for the current user.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"subscriptions"
],
"summary": "Get all notifications for the current user",
"parameters": [
{
"type": "integer",
"description": "The page number. Used for pagination. If not provided, the first page of results is returned.",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page.",
"name": "per_page",
"in": "query"
}
],
"responses": {
"200": {
"description": "The notifications",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/notifications.DatabaseNotification"
}
}
},
"403": {
"description": "Link shares cannot have notifications.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"500": {
"description": "Internal error",
"schema": {
"$ref": "#/definitions/models.Message"
}
}
}
}
},
"/notifications/{id}": {
"post": {
"security": [
{
"JWTKeyAuth": []
}
],
"description": "Marks a notification as either read or unread. A user can only mark their own notifications as read.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"subscriptions"
],
"summary": "Mark a notification as (un-)read",
"parameters": [
{
"type": "integer",
"description": "Notification ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "The notification to mark as read.",
"schema": {
"$ref": "#/definitions/models.DatabaseNotifications"
}
},
"403": {
"description": "Link shares cannot have notifications.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"404": {
"description": "The notification does not exist.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"500": {
"description": "Internal error",
"schema": {
"$ref": "#/definitions/models.Message"
}
}
}
}
},
"/register": { "/register": {
"post": { "post": {
"description": "Creates a new user account.", "description": "Creates a new user account.",
@ -7183,6 +7295,35 @@
} }
} }
}, },
"models.DatabaseNotifications": {
"type": "object",
"properties": {
"created": {
"description": "A timestamp when this notification was created. You cannot change this value.",
"type": "string"
},
"id": {
"description": "The unique, numeric id of this notification.",
"type": "integer"
},
"name": {
"description": "The name of the notification",
"type": "string"
},
"notification": {
"description": "The actual content of the notification.",
"type": "object"
},
"read": {
"description": "Whether or not to mark this notification as read or unread.\nTrue is read, false is unread.",
"type": "boolean"
},
"read_at": {
"description": "When this notification is marked as read, this will be updated with the current timestamp.",
"type": "string"
}
}
},
"models.Label": { "models.Label": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -8061,6 +8202,31 @@
} }
} }
}, },
"notifications.DatabaseNotification": {
"type": "object",
"properties": {
"created": {
"description": "A timestamp when this notification was created. You cannot change this value.",
"type": "string"
},
"id": {
"description": "The unique, numeric id of this notification.",
"type": "integer"
},
"name": {
"description": "The name of the notification",
"type": "string"
},
"notification": {
"description": "The actual content of the notification.",
"type": "object"
},
"read_at": {
"description": "When this notification is marked as read, this will be updated with the current timestamp.",
"type": "string"
}
}
},
"openid.Callback": { "openid.Callback": {
"type": "object", "type": "object",
"properties": { "properties": {

View file

@ -229,6 +229,29 @@ definitions:
description: A timestamp when this task was last updated. You cannot change this value. description: A timestamp when this task was last updated. You cannot change this value.
type: string type: string
type: object type: object
models.DatabaseNotifications:
properties:
created:
description: A timestamp when this notification was created. You cannot change this value.
type: string
id:
description: The unique, numeric id of this notification.
type: integer
name:
description: The name of the notification
type: string
notification:
description: The actual content of the notification.
type: object
read:
description: |-
Whether or not to mark this notification as read or unread.
True is read, false is unread.
type: boolean
read_at:
description: When this notification is marked as read, this will be updated with the current timestamp.
type: string
type: object
models.Label: models.Label:
properties: properties:
created: created:
@ -884,6 +907,24 @@ definitions:
minLength: 1 minLength: 1
type: string type: string
type: object type: object
notifications.DatabaseNotification:
properties:
created:
description: A timestamp when this notification was created. You cannot change this value.
type: string
id:
description: The unique, numeric id of this notification.
type: integer
name:
description: The name of the notification
type: string
notification:
description: The actual content of the notification.
type: object
read_at:
description: When this notification is marked as read, this will be updated with the current timestamp.
type: string
type: object
openid.Callback: openid.Callback:
properties: properties:
code: code:
@ -3643,6 +3684,77 @@ paths:
summary: Update a user <-> namespace relation summary: Update a user <-> namespace relation
tags: tags:
- sharing - sharing
/notifications:
get:
consumes:
- application/json
description: Returns an array with all notifications for the current user.
parameters:
- description: The page number. Used for pagination. If not provided, the first page of results is returned.
in: query
name: page
type: integer
- description: The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page.
in: query
name: per_page
type: integer
produces:
- application/json
responses:
"200":
description: The notifications
schema:
items:
$ref: '#/definitions/notifications.DatabaseNotification'
type: array
"403":
description: Link shares cannot have notifications.
schema:
$ref: '#/definitions/web.HTTPError'
"500":
description: Internal error
schema:
$ref: '#/definitions/models.Message'
security:
- JWTKeyAuth: []
summary: Get all notifications for the current user
tags:
- subscriptions
/notifications/{id}:
post:
consumes:
- application/json
description: Marks a notification as either read or unread. A user can only mark their own notifications as read.
parameters:
- description: Notification ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: The notification to mark as read.
schema:
$ref: '#/definitions/models.DatabaseNotifications'
"403":
description: Link shares cannot have notifications.
schema:
$ref: '#/definitions/web.HTTPError'
"404":
description: The notification does not exist.
schema:
$ref: '#/definitions/web.HTTPError'
"500":
description: Internal error
schema:
$ref: '#/definitions/models.Message'
security:
- JWTKeyAuth: []
summary: Mark a notification as (un-)read
tags:
- subscriptions
/register: /register:
post: post:
consumes: consumes:

View file

@ -40,6 +40,6 @@ func (s *IncreaseUserCounter) Name() string {
} }
// Hanlde is executed when the event IncreaseUserCounter listens on is fired // Hanlde is executed when the event IncreaseUserCounter listens on is fired
func (s *IncreaseUserCounter) Handle(payload message.Payload) (err error) { func (s *IncreaseUserCounter) Handle(msg *message.Message) (err error) {
return keyvalue.IncrBy(metrics.UserCountKey, 1) return keyvalue.IncrBy(metrics.UserCountKey, 1)
} }

View file

@ -54,6 +54,11 @@ func (n *EmailConfirmNotification) ToDB() interface{} {
return nil return nil
} }
// Name returns the name of the notification
func (n *EmailConfirmNotification) Name() string {
return ""
}
// PasswordChangedNotification represents a PasswordChangedNotification notification // PasswordChangedNotification represents a PasswordChangedNotification notification
type PasswordChangedNotification struct { type PasswordChangedNotification struct {
User *User User *User
@ -73,6 +78,11 @@ func (n *PasswordChangedNotification) ToDB() interface{} {
return nil return nil
} }
// Name returns the name of the notification
func (n *PasswordChangedNotification) Name() string {
return ""
}
// ResetPasswordNotification represents a ResetPasswordNotification notification // ResetPasswordNotification represents a ResetPasswordNotification notification
type ResetPasswordNotification struct { type ResetPasswordNotification struct {
User *User User *User
@ -92,3 +102,8 @@ func (n *ResetPasswordNotification) ToMail() *notifications.Mail {
func (n *ResetPasswordNotification) ToDB() interface{} { func (n *ResetPasswordNotification) ToDB() interface{} {
return nil return nil
} }
// Name returns the name of the notification
func (n *ResetPasswordNotification) Name() string {
return ""
}