Skip to content

Commit

Permalink
Rename notification subscribe endpoint and implement unsubscribe endp…
Browse files Browse the repository at this point in the history
…oint
  • Loading branch information
TomRomeo committed Jan 20, 2024
1 parent 2955433 commit aa4c303
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 44 deletions.
2 changes: 1 addition & 1 deletion backend/api-documentation/user/delete user.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: delete user
type: http
seq: 7
seq: 8
}

delete {
Expand Down
2 changes: 1 addition & 1 deletion backend/api-documentation/user/modify user.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: modify user
type: http
seq: 6
seq: 7
}

put {
Expand Down
23 changes: 23 additions & 0 deletions backend/api-documentation/user/subscribe notification.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
meta {
name: subscribe notification
type: http
seq: 6
}

post {
url: {{baseUrl}}/api/user/notifications
body: json
auth: bearer
}

auth:bearer {
token: {{accessToken}}
}

body:json {
{
"endpoint": "",
"auth": "",
"p256dh": ""
}
}
15 changes: 15 additions & 0 deletions backend/api-documentation/user/unsubscribe notifications.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
meta {
name: unsubscribe notifications
type: http
seq: 9
}

delete {
url: {{baseUrl}}/api/user/notifications/{{subscriptionID}}
body: none
auth: bearer
}

auth:bearer {
token: {{accessToken}}
}
18 changes: 16 additions & 2 deletions backend/services/frontend/handler/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ func (e *Frontend) SubscribeNotificationsHandler(c *fiber.Ctx) error {
if err := c.BodyParser(subscription); err != nil {
return err
}
rspSubscribeNotifications, err := e.notificationsService.Enroll(c.Context(), &pbNotifications.PushSubscriptionRequest{
rspSubscribeNotifications, err := e.notificationsService.Subscribe(c.Context(), &pbNotifications.PushSubscriptionRequest{
UserID: userID,
Subscription: &pbNotifications.PushSubscription{
Endpoint: subscription.Endpoint,
Expand All @@ -991,7 +991,21 @@ func (e *Frontend) SubscribeNotificationsHandler(c *fiber.Ctx) error {
if err != nil {
return err
}
if !rspSubscribeNotifications.Success {
return c.SendString(rspSubscribeNotifications.SubscriptionID)
}

func (e *Frontend) UnsubscribeNotificationsHandler(c *fiber.Ctx) error {
userID := helper.GetUserIDFromContext(c)
rspUnsubscribeNotifications, err := e.notificationsService.Unsubscribe(c.Context(), &pbNotifications.PushSubscriptionRequest{
UserID: userID,
Subscription: &pbNotifications.PushSubscription{
SubscriptionID: c.Params("subscriptionID"),
},
})
if err != nil {
return err
}
if !rspUnsubscribeNotifications.Success {
return helper.NewMicroNotSuccessfulResponseErr(helper.FrontendServiceID)
}
return c.SendStatus(200)
Expand Down
3 changes: 2 additions & 1 deletion backend/services/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ func main() {
app.Post("/api/decks/:deckID/push", svc.SrsPushHandler)
app.Get("/api/decks/:deckID/dueCards", svc.SrsDeckDueHandler)

app.Post("/api/user/notifications/subscribe", svc.SubscribeNotificationsHandler)
app.Post("/api/user/notifications", svc.SubscribeNotificationsHandler)
app.Delete("/api/user/notifications/:subscriptionID", svc.UnsubscribeNotificationsHandler)

// Register the handler with the micro framework
// if err := micro.RegisterHandler(srv.Server(), grpcHandler); err != nil {
Expand Down
21 changes: 20 additions & 1 deletion backend/services/notifications/handler/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package handler
import (
"context"
"encoding/json"
"errors"
"os"

"github.com/SherClockHolmes/webpush-go"
"github.com/kioku-project/kioku/pkg/helper"
"github.com/kioku-project/kioku/pkg/model"
pbCommon "github.com/kioku-project/kioku/pkg/proto"
pbSrs "github.com/kioku-project/kioku/services/srs/proto"
Expand All @@ -24,7 +26,7 @@ func New(s store.NotificationsStore, cds pbSrs.SrsService) *Notifications {
return &Notifications{store: s, srsService: cds}
}

func (e *Notifications) Enroll(ctx context.Context, req *pb.PushSubscriptionRequest, rsp *pbCommon.Success) error {
func (e *Notifications) Subscribe(ctx context.Context, req *pb.PushSubscriptionRequest, rsp *pb.PushSubscription) error {
logger.Infof("Received Notifications.Enroll request: %v", req)
subscription := &model.PushSubscription{
UserID: req.UserID,
Expand Down Expand Up @@ -73,6 +75,23 @@ func (e *Notifications) Enroll(ctx context.Context, req *pb.PushSubscriptionRequ
return err
}
defer resp.Body.Close()
rsp.SubscriptionID = subscription.ID
return nil
}

func (e *Notifications) Unsubscribe(ctx context.Context, req *pb.PushSubscriptionRequest, rsp *pbCommon.Success) error {
logger.Infof("Received Notifications.Unenroll request: %v", req)
subscription, err := e.store.FindPushSubscriptionByID(ctx, req.Subscription.SubscriptionID)
if err != nil {
return err
}
if subscription.UserID != req.UserID {
return helper.NewMicroNotAuthorizedErr(helper.NotificationsServiceID)
}

if err := e.store.DeletePushSubscription(ctx, subscription); err != nil {
return err
}
rsp.Success = true
return nil
}
66 changes: 42 additions & 24 deletions backend/services/notifications/proto/notifications.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 24 additions & 7 deletions backend/services/notifications/proto/notifications.pb.micro.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions backend/services/notifications/proto/notifications.proto
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
syntax = "proto3";

import "pkg/proto/common.proto";

package notifications;

option go_package ="github.com/kioku-project/kioku/services/notifications/proto;notifications";

service Notifications {
rpc Enroll(PushSubscriptionRequest) returns (common.Success) {}
rpc Subscribe(PushSubscriptionRequest) returns (PushSubscription) {}
rpc Unsubscribe(PushSubscriptionRequest) returns (common.Success) {}
}

message PushSubscriptionRequest {
Expand All @@ -15,7 +17,8 @@ message PushSubscriptionRequest {
}

message PushSubscription {
string endpoint = 1;
string auth = 2;
string p256dh = 3;
string subscriptionID = 1;
string endpoint = 2;
string auth = 3;
string p256dh = 4;
}
11 changes: 11 additions & 0 deletions backend/store/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,14 @@ func (s *NotificationStoreImpl) CreatePushSubscription(ctx context.Context, newS
func (s *NotificationStoreImpl) FindAllPushSubscriptions(ctx context.Context) (subscriptions []*model.PushSubscription, err error) {
return subscriptions, s.db.WithContext(ctx).Find(&subscriptions).Error
}

func (s *NotificationStoreImpl) FindPushSubscriptionByID(ctx context.Context, subscriptionID string) (subscription *model.PushSubscription, err error) {
if err = s.db.WithContext(ctx).Where(&model.PushSubscription{ID: subscriptionID}).First(&subscription).Error; errors.Is(err, gorm.ErrRecordNotFound) {
err = helper.ErrStoreNoEntryWithID
}
return
}

func (s *NotificationStoreImpl) DeletePushSubscription(ctx context.Context, subscription *model.PushSubscription) error {
return s.db.WithContext(ctx).Delete(subscription).Error
}
7 changes: 4 additions & 3 deletions backend/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ type SrsStore interface {
}

type NotificationsStore interface {
FindAllPushSubscriptions(ctx context.Context) ([]*model.PushSubscription, error)
CreatePushSubscription(ctx context.Context, newSubscription *model.PushSubscription) error
FindAllPushSubscriptions(ctx context.Context) ([]*model.PushSubscription, error)
CreatePushSubscription(ctx context.Context, newSubscription *model.PushSubscription) error
DeletePushSubscription(ctx context.Context, subscription *model.PushSubscription) error
FindPushSubscriptionByID(ctx context.Context, subscriptionID string) (*model.PushSubscription, error)
}

0 comments on commit aa4c303

Please sign in to comment.