feat: add notification UI
showPopup was also refactored to be better
This commit is contained in:
53
internal/notification/logger.go
Normal file
53
internal/notification/logger.go
Normal file
@ -0,0 +1,53 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// MachineryLogger is a customer logger for machinery worker
|
||||
type MachineryLogger struct{}
|
||||
|
||||
// Print sends to logrus.Info
|
||||
func (m *MachineryLogger) Print(args ...interface{}) {
|
||||
log.Info(args...)
|
||||
}
|
||||
|
||||
// Printf sends to logrus.Infof
|
||||
func (m *MachineryLogger) Printf(format string, args ...interface{}) {
|
||||
log.Infof(format, args...)
|
||||
}
|
||||
|
||||
// Println sends to logrus.Info
|
||||
func (m *MachineryLogger) Println(args ...interface{}) {
|
||||
log.Info(args...)
|
||||
}
|
||||
|
||||
// Fatal sends to logrus.Fatal
|
||||
func (m *MachineryLogger) Fatal(args ...interface{}) {
|
||||
log.Fatal(args...)
|
||||
}
|
||||
|
||||
// Fatalf sends to logrus.Fatalf
|
||||
func (m *MachineryLogger) Fatalf(format string, args ...interface{}) {
|
||||
log.Fatalf(format, args...)
|
||||
}
|
||||
|
||||
// Fatalln sends to logrus.Fatal
|
||||
func (m *MachineryLogger) Fatalln(args ...interface{}) {
|
||||
log.Fatal(args...)
|
||||
}
|
||||
|
||||
// Panic sends to logrus.Panic
|
||||
func (m *MachineryLogger) Panic(args ...interface{}) {
|
||||
log.Panic(args...)
|
||||
}
|
||||
|
||||
// Panicf sends to logrus.Panic
|
||||
func (m *MachineryLogger) Panicf(format string, args ...interface{}) {
|
||||
log.Panic(args...)
|
||||
}
|
||||
|
||||
// Panicln sends to logrus.Panic
|
||||
func (m *MachineryLogger) Panicln(args ...interface{}) {
|
||||
log.Panic(args...)
|
||||
}
|
78
internal/notification/notification.go
Normal file
78
internal/notification/notification.go
Normal file
@ -0,0 +1,78 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/RichardKnop/machinery/v1"
|
||||
"github.com/RichardKnop/machinery/v1/tasks"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jordanknott/taskcafe/internal/db"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func RegisterTasks(server *machinery.Server, repo db.Repository) {
|
||||
tasks := NotificationTasks{repo}
|
||||
server.RegisterTasks(map[string]interface{}{
|
||||
"taskMemberWasAdded": tasks.TaskMemberWasAdded,
|
||||
})
|
||||
}
|
||||
|
||||
type NotificationTasks struct {
|
||||
Repository db.Repository
|
||||
}
|
||||
|
||||
func (m *NotificationTasks) TaskMemberWasAdded(taskID, notifierID, notifiedID string) (bool, error) {
|
||||
tid := uuid.MustParse(taskID)
|
||||
notifier := uuid.MustParse(notifierID)
|
||||
notified := uuid.MustParse(notifiedID)
|
||||
if notifier == notified {
|
||||
return true, nil
|
||||
}
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
notificationObject, err := m.Repository.CreateNotificationObject(ctx, db.CreateNotificationObjectParams{EntityType: 1, EntityID: tid, ActionType: 1, ActorID: notifier, CreatedOn: now})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
notification, err := m.Repository.CreateNotification(ctx, db.CreateNotificationParams{NotificationObjectID: notificationObject.NotificationObjectID, NotifierID: notified})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
log.WithFields(log.Fields{"notificationID": notification.NotificationID}).Info("created new notification")
|
||||
return true, nil
|
||||
}
|
||||
|
||||
type NotificationQueue struct {
|
||||
Server *machinery.Server
|
||||
}
|
||||
|
||||
func (n *NotificationQueue) TaskMemberWasAdded(taskID, notifier, notified uuid.UUID) error {
|
||||
task := tasks.Signature{
|
||||
Name: "taskMemberWasAdded",
|
||||
Args: []tasks.Arg{
|
||||
{
|
||||
Name: "taskID",
|
||||
Type: "string",
|
||||
Value: taskID.String(),
|
||||
},
|
||||
{
|
||||
Name: "notifierID",
|
||||
Type: "string",
|
||||
Value: notifier.String(),
|
||||
},
|
||||
{
|
||||
Name: "notifiedID",
|
||||
Type: "string",
|
||||
Value: notified.String(),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err := n.Server.SendTask(&task)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user