taskcafe/internal/graph/resolver.go

71 lines
2.0 KiB
Go
Raw Normal View History

//go:generate sh ../scripts/genSchema.sh
2020-04-10 04:40:22 +02:00
//go:generate go run github.com/99designs/gqlgen
2020-04-10 04:40:22 +02:00
package graph
import (
2021-11-18 00:11:28 +01:00
"context"
"encoding/json"
2020-04-10 04:40:22 +02:00
2021-11-18 00:11:28 +01:00
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
"github.com/jordanknott/taskcafe/internal/config"
2020-08-07 03:50:35 +02:00
"github.com/jordanknott/taskcafe/internal/db"
2021-11-18 00:11:28 +01:00
"github.com/jordanknott/taskcafe/internal/jobs"
"github.com/jordanknott/taskcafe/internal/utils"
log "github.com/sirupsen/logrus"
2020-04-10 04:40:22 +02:00
)
// Resolver handles resolving GraphQL queries & mutations
2020-04-10 04:40:22 +02:00
type Resolver struct {
Repository db.Repository
AppConfig config.AppConfig
2021-11-18 00:11:28 +01:00
Notifications *NotificationObservers
Job jobs.JobQueue
Redis *redis.Client
}
func (r Resolver) SubscribeRedis() {
ctx := context.Background()
go func() {
subscriber := r.Redis.Subscribe(ctx, "notification-created")
log.Info("Stream starting...")
for {
msg, err := subscriber.ReceiveMessage(ctx)
if err != nil {
log.WithError(err).Error("while receiving message")
panic(err)
}
var message utils.NotificationCreatedMessage
if err := json.Unmarshal([]byte(msg.Payload), &message); err != nil {
log.WithError(err).Error("while unmarshalling notifiction created message")
panic(err)
}
log.WithField("notID", message.NotifiedID).Info("received notification message")
notified, err := r.Repository.GetNotifiedByIDNoExtra(ctx, uuid.MustParse(message.NotifiedID))
if err != nil {
log.WithError(err).Error("while getting notified by id")
panic(err)
}
notification, err := r.Repository.GetNotificationByID(ctx, uuid.MustParse(message.NotificationID))
if err != nil {
log.WithError(err).Error("while getting notified by id")
panic(err)
}
for _, observers := range r.Notifications.Subscribers {
for _, ochan := range observers {
ochan <- &Notified{
ID: notified.NotifiedID,
Read: notified.Read,
ReadAt: &notified.ReadAt.Time,
Notification: &notification,
}
}
}
}
}()
2020-04-10 04:40:22 +02:00
}