Skip to content

Commit

Permalink
Merge pull request #6917 from TheThingsNetwork/feature/add-notificati…
Browse files Browse the repository at this point in the history
…on-event

Add notification events
  • Loading branch information
adriansmares authored Feb 7, 2024
2 parents f8d7665 + 2deea91 commit 630b4f8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
18 changes: 18 additions & 0 deletions config/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -11438,6 +11438,24 @@
"file": "user_registry.go"
}
},
"event:user.notification.create": {
"translations": {
"en": "create notification"
},
"description": {
"package": "pkg/identityserver",
"file": "notification_registry.go"
}
},
"event:user.notification.update_status": {
"translations": {
"en": "update notification status"
},
"description": {
"package": "pkg/identityserver",
"file": "notification_registry.go"
}
},
"event:user.purge": {
"translations": {
"en": "purge user"
Expand Down
25 changes: 25 additions & 0 deletions pkg/identityserver/notification_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,31 @@ import (
"go.thethings.network/lorawan-stack/v3/pkg/auth/rights"
"go.thethings.network/lorawan-stack/v3/pkg/email"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/events"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/store"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/unique"
"google.golang.org/protobuf/types/known/emptypb"
)

var (
evtNotificationCreate = events.Define(
"user.notification.create", "create notification",
events.WithVisibility(ttnpb.Right_RIGHT_USER_NOTIFICATIONS_READ),
events.WithDataType(&ttnpb.Notification{}),
events.WithAuthFromContext(),
events.WithClientInfoFromContext(),
)
evtNotificationUpdateStatus = events.Define(
"user.notification.update_status", "update notification status",
events.WithVisibility(ttnpb.Right_RIGHT_USER_NOTIFICATIONS_READ),
events.WithDataType(&ttnpb.UpdateNotificationStatusRequest{}),
events.WithAuthFromContext(),
events.WithClientInfoFromContext(),
)
)

func receiversContains(receivers []ttnpb.NotificationReceiver, search ttnpb.NotificationReceiver) bool {
for _, receiver := range receivers {
if receiver == search {
Expand Down Expand Up @@ -259,6 +277,12 @@ func (is *IdentityServer) createNotification(ctx context.Context, req *ttnpb.Cre
}
}

evs := make([]events.Event, 0, len(receiverUserIDs))
for _, ids := range receiverUserIDs {
evs = append(evs, evtNotificationCreate.NewWithIdentifiersAndData(ctx, ids, notification))
}
events.Publish(evs...)

return &ttnpb.CreateNotificationResponse{
Id: notification.Id,
}, nil
Expand Down Expand Up @@ -341,6 +365,7 @@ func (is *IdentityServer) updateNotificationStatus(ctx context.Context, req *ttn
if err != nil {
return nil, err
}
events.Publish(evtNotificationUpdateStatus.NewWithIdentifiersAndData(ctx, req.ReceiverIds, req))
return ttnpb.Empty, nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/webui/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,8 @@
"event:user.api-key.update": "ユーザAPIキーを更新",
"event:user.create": "ユーザ作成",
"event:user.delete": "ユーザ削除",
"event:user.notification.create": "",
"event:user.notification.update_status": "",
"event:user.purge": "ユーザ消去",
"event:user.restore": "ユーザーの復元",
"event:user.update": "ユーザ更新",
Expand Down

0 comments on commit 630b4f8

Please sign in to comment.