278 lines
11 KiB
Go
278 lines
11 KiB
Go
package v1
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/adapter/primary/http/api"
|
|
"lincodex/backend/internal/adapter/primary/http/problem"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
"lincodex/backend/internal/usecase/notification"
|
|
)
|
|
|
|
const defaultNotificationLimit = 20
|
|
|
|
// NotificationOperations lists and marks the inbox of the authenticated
|
|
// account. The candidate cabinet and the cabinet of one organization share
|
|
// the operations; the request names which one.
|
|
type NotificationOperations interface {
|
|
List(ctx context.Context, request notification.InboxListRequest) (notification.InboxPage, error)
|
|
MarkRead(ctx context.Context, request notification.InboxMarkReadRequest) (domain.NotificationInboxEntry, error)
|
|
}
|
|
|
|
// NotificationHandler binds the inbox use cases to the v1 API.
|
|
type NotificationHandler struct {
|
|
operations NotificationOperations
|
|
}
|
|
|
|
func NewNotificationHandler(operations NotificationOperations) *NotificationHandler {
|
|
if operations == nil {
|
|
panic("v1.NewNotificationHandler - operations must not be nil")
|
|
}
|
|
return &NotificationHandler{operations: operations}
|
|
}
|
|
|
|
func (handler *Handler) ListMyNotifications(ctx context.Context, params api.ListMyNotificationsParams) (api.ListMyNotificationsRes, error) {
|
|
if handler.notifications == nil {
|
|
return handler.UnimplementedHandler.ListMyNotifications(ctx, params)
|
|
}
|
|
return handler.notifications.ListMyNotifications(ctx, params)
|
|
}
|
|
|
|
func (handler *Handler) MarkMyNotificationRead(ctx context.Context, params api.MarkMyNotificationReadParams) (api.MarkMyNotificationReadRes, error) {
|
|
if handler.notifications == nil {
|
|
return handler.UnimplementedHandler.MarkMyNotificationRead(ctx, params)
|
|
}
|
|
return handler.notifications.MarkMyNotificationRead(ctx, params)
|
|
}
|
|
|
|
func (handler *Handler) ListOrganizationNotifications(
|
|
ctx context.Context,
|
|
params api.ListOrganizationNotificationsParams,
|
|
) (api.ListOrganizationNotificationsRes, error) {
|
|
if handler.notifications == nil {
|
|
return handler.UnimplementedHandler.ListOrganizationNotifications(ctx, params)
|
|
}
|
|
return handler.notifications.ListOrganizationNotifications(ctx, params)
|
|
}
|
|
|
|
func (handler *Handler) MarkOrganizationNotificationRead(
|
|
ctx context.Context,
|
|
params api.MarkOrganizationNotificationReadParams,
|
|
) (api.MarkOrganizationNotificationReadRes, error) {
|
|
if handler.notifications == nil {
|
|
return handler.UnimplementedHandler.MarkOrganizationNotificationRead(ctx, params)
|
|
}
|
|
return handler.notifications.MarkOrganizationNotificationRead(ctx, params)
|
|
}
|
|
|
|
func (handler *NotificationHandler) ListMyNotifications(ctx context.Context, params api.ListMyNotificationsParams) (api.ListMyNotificationsRes, error) {
|
|
principal, ok := PrincipalFromContext(ctx)
|
|
if !ok {
|
|
return (*api.ListMyNotificationsUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
|
|
}
|
|
list, pointer := handler.list(ctx, principal.UserID, nil, params.Limit, params.Cursor)
|
|
if pointer != "" {
|
|
return (*api.ListMyNotificationsBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationQuery, "/"+pointer).Response), nil
|
|
}
|
|
page, err := list()
|
|
switch {
|
|
case err == nil:
|
|
return page, nil
|
|
case errors.Is(err, notification.ErrInboxForbidden):
|
|
return (*api.ListMyNotificationsForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (handler *NotificationHandler) ListOrganizationNotifications(
|
|
ctx context.Context,
|
|
params api.ListOrganizationNotificationsParams,
|
|
) (api.ListOrganizationNotificationsRes, error) {
|
|
principal, ok := PrincipalFromContext(ctx)
|
|
if !ok {
|
|
return (*api.ListOrganizationNotificationsUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
|
|
}
|
|
organizationID := params.OrgID
|
|
list, pointer := handler.list(ctx, principal.UserID, &organizationID, params.Limit, params.Cursor)
|
|
if pointer != "" {
|
|
return (*api.ListOrganizationNotificationsBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationQuery, "/"+pointer).Response), nil
|
|
}
|
|
page, err := list()
|
|
switch {
|
|
case err == nil:
|
|
return page, nil
|
|
case errors.Is(err, notification.ErrInboxEmailNotVerified):
|
|
return (*api.ListOrganizationNotificationsForbidden)(&problem.New(ctx, api.ProblemCodeEmailNotVerified).Response), nil
|
|
case errors.Is(err, notification.ErrInboxForbidden):
|
|
return (*api.ListOrganizationNotificationsForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
|
|
case errors.Is(err, outbound.ErrOrganizationNotFound):
|
|
return (*api.ListOrganizationNotificationsNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (handler *NotificationHandler) MarkMyNotificationRead(
|
|
ctx context.Context,
|
|
params api.MarkMyNotificationReadParams,
|
|
) (api.MarkMyNotificationReadRes, error) {
|
|
principal, ok := PrincipalFromContext(ctx)
|
|
if !ok {
|
|
return (*api.MarkMyNotificationReadUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
|
|
}
|
|
receipt, err := handler.mark(ctx, principal.UserID, nil, params.NotificationID)
|
|
switch {
|
|
case err == nil:
|
|
return receipt, nil
|
|
case errors.Is(err, notification.ErrInboxForbidden):
|
|
return (*api.MarkMyNotificationReadForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
|
|
case errors.Is(err, outbound.ErrNotificationInboxEntryNotFound):
|
|
return (*api.MarkMyNotificationReadNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (handler *NotificationHandler) MarkOrganizationNotificationRead(
|
|
ctx context.Context,
|
|
params api.MarkOrganizationNotificationReadParams,
|
|
) (api.MarkOrganizationNotificationReadRes, error) {
|
|
principal, ok := PrincipalFromContext(ctx)
|
|
if !ok {
|
|
return (*api.MarkOrganizationNotificationReadUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil
|
|
}
|
|
organizationID := params.OrgID
|
|
receipt, err := handler.mark(ctx, principal.UserID, &organizationID, params.NotificationID)
|
|
switch {
|
|
case err == nil:
|
|
return receipt, nil
|
|
case errors.Is(err, notification.ErrInboxEmailNotVerified):
|
|
return (*api.MarkOrganizationNotificationReadForbidden)(&problem.New(ctx, api.ProblemCodeEmailNotVerified).Response), nil
|
|
case errors.Is(err, notification.ErrInboxForbidden):
|
|
return (*api.MarkOrganizationNotificationReadForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil
|
|
case errors.Is(err, outbound.ErrOrganizationNotFound), errors.Is(err, outbound.ErrNotificationInboxEntryNotFound):
|
|
// A missing membership, an unknown organization, and an item that is
|
|
// not the caller's in this organization are one answer.
|
|
return (*api.MarkOrganizationNotificationReadNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// list validates the page parameters and returns the deferred read, so the
|
|
// two list operations share the mapping of a page to the public type. A
|
|
// non-empty pointer names the invalid query parameter.
|
|
func (handler *NotificationHandler) list(
|
|
ctx context.Context,
|
|
recipientID uuid.UUID,
|
|
organizationID *uuid.UUID,
|
|
limitParam api.OptInt32,
|
|
cursorParam api.OptString,
|
|
) (func() (*api.NotificationListHeaders, error), string) {
|
|
limit, set := limitParam.Get()
|
|
if !set {
|
|
limit = defaultNotificationLimit
|
|
}
|
|
if limit < 1 || limit > outbound.MaxNotificationInboxPageSize {
|
|
return nil, "limit"
|
|
}
|
|
request := notification.InboxListRequest{RecipientUserID: recipientID, OrganizationID: organizationID, Limit: limit}
|
|
if cursor, set := cursorParam.Get(); set {
|
|
position, err := decodeNotificationCursor(cursor, recipientID, organizationID)
|
|
if err != nil {
|
|
return nil, "cursor"
|
|
}
|
|
request.After = &position
|
|
}
|
|
return func() (*api.NotificationListHeaders, error) {
|
|
const location = "NotificationHandler.list"
|
|
page, err := handler.operations.List(ctx, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]api.Notification, 0, len(page.Items))
|
|
for _, item := range page.Items {
|
|
mapped, err := notificationResponse(item)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s - map item: %w", location, err)
|
|
}
|
|
items = append(items, mapped)
|
|
}
|
|
next, err := privacyNextCursor(page.HasMore, len(page.Items), func() (string, error) {
|
|
last := outbound.NotificationInboxPositionOf(page.Items[len(page.Items)-1].Entry)
|
|
return encodeNotificationCursor(recipientID, organizationID, last)
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s - encode cursor: %w", location, err)
|
|
}
|
|
return &api.NotificationListHeaders{
|
|
CacheControl: api.CacheControlNoStoreNoStore,
|
|
Response: api.NotificationList{Items: items, Page: api.PageInfo{HasMore: page.HasMore, NextCursor: next}},
|
|
}, nil
|
|
}, ""
|
|
}
|
|
|
|
func (handler *NotificationHandler) mark(
|
|
ctx context.Context,
|
|
recipientID uuid.UUID,
|
|
organizationID *uuid.UUID,
|
|
eventID uuid.UUID,
|
|
) (*api.NotificationReadReceiptHeaders, error) {
|
|
entry, err := handler.operations.MarkRead(ctx, notification.InboxMarkReadRequest{
|
|
RecipientUserID: recipientID, OrganizationID: organizationID, EventID: eventID,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
readAt, isRead := entry.ReadAt()
|
|
if !isRead {
|
|
return nil, errors.New("NotificationHandler.mark - a marked item has no read time")
|
|
}
|
|
return &api.NotificationReadReceiptHeaders{
|
|
CacheControl: api.CacheControlNoStoreNoStore,
|
|
Response: api.NotificationReadReceipt{ID: entry.EventID(), ReadAt: api.Timestamp(readAt)},
|
|
}, nil
|
|
}
|
|
|
|
func notificationResponse(item notification.InboxItem) (api.Notification, error) {
|
|
kind, ok := notificationKind(item.Entry.EventType())
|
|
if !ok {
|
|
return api.Notification{}, errors.New("v1.notificationResponse - the stored event type has no public kind")
|
|
}
|
|
response := api.Notification{
|
|
ID: item.Entry.EventID(), Kind: kind, OrganizationID: item.Entry.OrganizationID(),
|
|
DeliveredAt: api.Timestamp(item.Entry.OccurredAt()),
|
|
}
|
|
response.ReadAt.SetToNull()
|
|
if readAt, isRead := item.Entry.ReadAt(); isRead {
|
|
response.ReadAt.SetTo(api.Timestamp(readAt))
|
|
}
|
|
response.Content.SetToNull()
|
|
if item.Content != nil {
|
|
content := api.NotificationContent{Title: item.Content.Title(), Summary: item.Content.Summary()}
|
|
content.LinkPath.SetToNull()
|
|
if path := item.Content.LinkPath(); path != "" {
|
|
content.LinkPath.SetTo(path)
|
|
}
|
|
response.Content.SetTo(content)
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func notificationKind(eventType domain.NotificationInboxEventType) (api.NotificationKind, bool) {
|
|
switch eventType {
|
|
case domain.NotificationInboxInvitationDelivered:
|
|
return api.NotificationKindInvitationDelivered, true
|
|
case domain.NotificationInboxSyntheticDelivered:
|
|
return api.NotificationKindSynthetic, true
|
|
default:
|
|
return "", false
|
|
}
|
|
}
|