feat: allow broadcast notifications
Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
@@ -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',
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user