feat: allow broadcast notifications

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2024-01-25 10:12:31 +02:00
parent 6a79f9f1ee
commit a3d42153d1
6 changed files with 45 additions and 13 deletions
@@ -23,7 +23,7 @@ export default async function createPlugin(
): Promise<Router> {
setInterval(() => {
env.notificationService.send({
entityRef: 'user:default/guest',
receivers: { type: 'broadcast' },
title: 'Test',
description: 'Test',
link: '/catalog',
+2 -2
View File
@@ -4,7 +4,7 @@ Welcome to the notifications backend plugin!
## Getting started
First you have to install `@backstage/plugin-notifications-node` and `@backstage/plugin-signals-node`
First you have to install `@backstage/plugin-notifications-node` and `@backstage/plugin-signals-node`
packages.
Then create a new file to `packages/backend/src/plugins/notifications.ts`:
@@ -23,7 +23,7 @@ export default async function createPlugin(
tokenManager: env.tokenManager,
database: env.database,
discovery: env.discovery,
signalService: env.signalService
signalService: env.signalService,
});
}
```
@@ -89,13 +89,24 @@ export async function createRouter(
};
const getUsersForEntityRef = async (
entityRef: string | string[],
entityRef: string | string[] | null,
): Promise<string[]> => {
const refs = Array.isArray(entityRef) ? entityRef : [entityRef];
const { token } = await tokenManager.getToken();
// Broadcast
if (entityRef === null) {
const users = await catalogClient.getEntities({
filter: { kind: 'User' },
fields: ['kind', 'metadata.name', 'metadata.namespace'],
});
return users.items.map(stringifyEntityRef);
}
const refs = Array.isArray(entityRef) ? entityRef : [entityRef];
const entities = await catalogClient.getEntitiesByRefs(
{
entityRefs: refs,
fields: ['kind', 'metadata.name', 'metadata.namespace'],
},
{ token },
);
@@ -206,7 +217,7 @@ export async function createRouter(
});
router.post('/notifications', async (req, res) => {
const { entityRef, title, description, link } = req.body;
const { receivers, title, description, link } = req.body;
const notifications = [];
let users = [];
@@ -218,6 +229,11 @@ export async function createRouter(
return;
}
let entityRef = null;
if (receivers.entityRef && receivers.type === 'entity') {
entityRef = receivers.entityRef;
}
try {
users = await getUsersForEntityRef(entityRef);
} catch (e) {
@@ -254,7 +270,7 @@ export async function createRouter(
if (signalService) {
await signalService.publish({
recipients: users,
recipients: entityRef === null ? null : users,
message: { action: 'refresh' },
channel: 'notifications',
});
+14 -3
View File
@@ -22,18 +22,29 @@ export class DefaultNotificationService implements NotificationService {
send(options: NotificationSendOptions): Promise<Notification_2[]>;
}
// @public (undocumented)
export type NotificationReceivers =
| {
type: 'entity';
entityRef: string | string[];
}
| {
type: 'broadcast';
};
// @public (undocumented)
export type NotificationSendOptions = {
entityRef: string | string[];
receivers: NotificationReceivers;
title: string;
description: string;
link: string;
};
// @public (undocumented)
export type NotificationService = {
export interface NotificationService {
// (undocumented)
send(options: NotificationSendOptions): Promise<Notification_2[]>;
};
}
// @public (undocumented)
export const notificationService: ServiceRef<NotificationService, 'plugin'>;
@@ -27,9 +27,14 @@ export type NotificationServiceOptions = {
signalService: SignalService;
};
/** @public */
export type NotificationReceivers =
| { type: 'entity'; entityRef: string | string[] }
| { type: 'broadcast' };
/** @public */
export type NotificationSendOptions = {
entityRef: string | string[];
receivers: NotificationReceivers;
title: string;
description: string;
link: string;
@@ -18,6 +18,6 @@ import { NotificationSendOptions } from './DefaultNotificationService';
import { Notification } from '@backstage/plugin-notifications-common';
/** @public */
export type NotificationService = {
export interface NotificationService {
send(options: NotificationSendOptions): Promise<Notification[]>;
};
}