feat: support to filter notification recipients after resolving
Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'@backstage/plugin-notifications-backend': patch
|
||||
'@backstage/plugin-notifications-node': patch
|
||||
---
|
||||
|
||||
Support for filtering entities from notification recipients after resolving them from the recipients
|
||||
@@ -94,6 +94,7 @@ export async function createRouter(
|
||||
|
||||
const getUsersForEntityRef = async (
|
||||
entityRef: string | string[] | null,
|
||||
excludeEntityRefs: string | string[],
|
||||
): Promise<string[]> => {
|
||||
const { token } = await auth.getPluginRequestToken({
|
||||
onBehalfOf: await auth.getOwnServiceCredentials(),
|
||||
@@ -114,13 +115,23 @@ export async function createRouter(
|
||||
},
|
||||
{ token },
|
||||
);
|
||||
|
||||
const excluded = Array.isArray(excludeEntityRefs)
|
||||
? excludeEntityRefs
|
||||
: [excludeEntityRefs];
|
||||
|
||||
const mapEntity = async (entity: Entity | undefined): Promise<string[]> => {
|
||||
if (!entity) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const currentEntityRef = stringifyEntityRef(entity);
|
||||
if (excluded.includes(currentEntityRef)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (isUserEntity(entity)) {
|
||||
return [stringifyEntityRef(entity)];
|
||||
return [currentEntityRef];
|
||||
} else if (isGroupEntity(entity) && entity.relations) {
|
||||
const users = entity.relations
|
||||
.filter(relation => relation.type === RELATION_HAS_MEMBER)
|
||||
@@ -162,6 +173,7 @@ export async function createRouter(
|
||||
const u = await mapEntity(entity);
|
||||
users.push(...u);
|
||||
}
|
||||
|
||||
return users;
|
||||
};
|
||||
|
||||
@@ -482,7 +494,10 @@ export async function createRouter(
|
||||
const entityRef = recipients.entityRef;
|
||||
|
||||
try {
|
||||
users = await getUsersForEntityRef(entityRef);
|
||||
users = await getUsersForEntityRef(
|
||||
entityRef,
|
||||
recipients.excludeEntityRef ?? [],
|
||||
);
|
||||
} catch (e) {
|
||||
logger.error(`Failed to resolve notification receivers: ${e}`);
|
||||
throw new InputError('Failed to resolve notification receivers', e);
|
||||
|
||||
@@ -41,6 +41,7 @@ export type NotificationRecipients =
|
||||
| {
|
||||
type: 'entity';
|
||||
entityRef: string | string[];
|
||||
excludeEntityRef?: string | string[];
|
||||
}
|
||||
| {
|
||||
type: 'broadcast';
|
||||
|
||||
@@ -28,7 +28,16 @@ export type NotificationServiceOptions = {
|
||||
export type NotificationRecipients =
|
||||
| {
|
||||
type: 'entity';
|
||||
/**
|
||||
* Entity references to send the notifications to
|
||||
*/
|
||||
entityRef: string | string[];
|
||||
/**
|
||||
* Optional entity reference(s) to filter out of the resolved recipients.
|
||||
* Usually the currently logged-in user for preventing sending notification
|
||||
* of user action to him/herself.
|
||||
*/
|
||||
excludeEntityRef?: string | string[];
|
||||
}
|
||||
| { type: 'broadcast' };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user