Skip to content

[backend] now also send stream message in digest notifications (#8685) #11004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: release/current
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
type: 'digest'
target: NotificationUser
playbook_source?: string
data: Array<{ notification_id: string, instance: StixObject, type: string, message: string }>
data: Array<{ notification_id: string, instance: StixObject, type: string, message: string, origin?: Partial<UserOrigin>, streamMessage?: string }>
}

export const isLiveKnowledge = (n: ResolvedTrigger): n is ResolvedLive => {
Expand Down Expand Up @@ -598,6 +598,8 @@
type: userTarget?.type ?? type,
instance: n.data,
message: await generateNotificationMessageForInstance(context, user, n.data),
origin: n.origin,
streamMessage: n.streamMessage

Check warning on line 602 in opencti-platform/opencti-graphql/src/manager/notificationManager.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/manager/notificationManager.ts#L601-L602

Added lines #L601 - L602 were not covered by tests
});
});
const data = await Promise.all(dataPromises);
Expand Down
38 changes: 26 additions & 12 deletions opencti-platform/opencti-graphql/src/manager/publisherManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import { ENTITY_TYPE_SETTINGS, ENTITY_TYPE_USER } from '../schema/internalObject';
import type { SseEvent, StreamNotifEvent } from '../types/event';
import type { BasicStoreSettings } from '../types/settings';
import type { AuthContext, AuthUser } from '../types/user';
import type { AuthContext, AuthUser, UserOrigin } from '../types/user';
import { executionContext, SYSTEM_USER } from '../utils/access';
import { now } from '../utils/format';
import type { NotificationData } from '../utils/publisher-mock';
Expand Down Expand Up @@ -175,31 +175,45 @@
}
};

const createFullNotificationMessage = (notificationMessage: string, usersMap: Map<string, AuthUser>, streamMessage?: string, origin?: Partial<UserOrigin>) => {
let fullMessage = notificationMessage;
if (origin && streamMessage) {
const { user_id } = origin;
const streamUser = usersMap.get(user_id ?? '');
if (streamUser) {
const streamBuiltMessage = `\`${streamUser.name}\` ${streamMessage}`;
if (streamBuiltMessage !== notificationMessage) {
fullMessage = `${notificationMessage} - ${streamBuiltMessage}`;
}
}
}

Check warning on line 189 in opencti-platform/opencti-graphql/src/manager/publisherManager.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/manager/publisherManager.ts#L179-L189

Added lines #L179 - L189 were not covered by tests

return fullMessage;
};

Check warning on line 192 in opencti-platform/opencti-graphql/src/manager/publisherManager.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/manager/publisherManager.ts#L191-L192

Added lines #L191 - L192 were not covered by tests

const processLiveNotificationEvent = async (
context: AuthContext,
notificationMap: Map<string, BasicStoreEntityTrigger>,
event: KnowledgeNotificationEvent | ActivityNotificationEvent | ActionNotificationEvent
) => {
const { targets, data: instance, origin: { user_id } } = event;
const streamUser = (await getEntitiesMapFromCache(context, SYSTEM_USER, ENTITY_TYPE_USER)).get(user_id as string) as AuthUser;
const { targets, data: instance, origin } = event;
const usersMap = await getEntitiesMapFromCache<AuthUser>(context, SYSTEM_USER, ENTITY_TYPE_USER);
const { streamMessage } = event as KnowledgeNotificationEvent;

Check warning on line 201 in opencti-platform/opencti-graphql/src/manager/publisherManager.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/manager/publisherManager.ts#L199-L201

Added lines #L199 - L201 were not covered by tests
for (let index = 0; index < targets.length; index += 1) {
const { user, type, message } = targets[index];
let notificationMessage = message;
if (streamUser && (event as KnowledgeNotificationEvent).streamMessage) {
const { streamMessage } = event as KnowledgeNotificationEvent;
const streamBuiltMessage = `\`${streamUser.name}\` ${streamMessage}`;
if (streamBuiltMessage !== notificationMessage) {
notificationMessage = `${message} - ${streamBuiltMessage}`;
}
}
const notificationMessage = createFullNotificationMessage(message, usersMap, streamMessage, origin);

Check warning on line 204 in opencti-platform/opencti-graphql/src/manager/publisherManager.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/manager/publisherManager.ts#L204

Added line #L204 was not covered by tests
const data = [{ notification_id: event.notification_id, instance, type, message: notificationMessage }];
await processNotificationEvent(context, notificationMap, event.notification_id, user, data);
}
};

const processDigestNotificationEvent = async (context: AuthContext, notificationMap: Map<string, BasicStoreEntityTrigger>, event: DigestEvent) => {
const { target: user, data } = event;
await processNotificationEvent(context, notificationMap, event.notification_id, user, data);
const usersMap = await getEntitiesMapFromCache<AuthUser>(context, SYSTEM_USER, ENTITY_TYPE_USER);
const dataWithFullMessage = data.map((d) => {
return { ...d, message: createFullNotificationMessage(d.message, usersMap, d.streamMessage, d.origin) };
});
await processNotificationEvent(context, notificationMap, event.notification_id, user, dataWithFullMessage);

Check warning on line 216 in opencti-platform/opencti-graphql/src/manager/publisherManager.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/manager/publisherManager.ts#L212-L216

Added lines #L212 - L216 were not covered by tests
};

const publisherStreamHandler = async (streamEvents: Array<SseEvent<StreamNotifEvent>>) => {
Expand Down