Skip to content
Snippets Groups Projects
Unverified Commit 982ef6f4 authored by Diego Sampaio's avatar Diego Sampaio Committed by GitHub
Browse files

feat: send banner direct to users (#29788)

parent a59841ac
No related branches found
No related tags found
No related merge requests found
---
'@rocket.chat/ddp-client': minor
'@rocket.chat/core-services': minor
'@rocket.chat/meteor': minor
---
Add new event to notify users directly about new banners
...@@ -11,6 +11,7 @@ export const useRemoteBanners = () => { ...@@ -11,6 +11,7 @@ export const useRemoteBanners = () => {
const serverContext = useContext(ServerContext); const serverContext = useContext(ServerContext);
const getBanners = useEndpoint('GET', '/v1/banners'); const getBanners = useEndpoint('GET', '/v1/banners');
const subscribeToNotifyLoggedIn = useStream('notify-logged'); const subscribeToNotifyLoggedIn = useStream('notify-logged');
const subscribeToNotifyUser = useStream('notify-user');
useEffect(() => { useEffect(() => {
if (!uid) { if (!uid) {
...@@ -63,11 +64,17 @@ export const useRemoteBanners = () => { ...@@ -63,11 +64,17 @@ export const useRemoteBanners = () => {
}); });
}); });
const unsubscribeBanners = subscribeToNotifyUser(`${uid}/banners`, async (banner) => {
banners.open(banner.view);
});
return () => { return () => {
controller.abort(); controller.abort();
unsubscribeFromBannerChanged(); unsubscribeFromBannerChanged();
unsubscribeBanners();
banners.clear(); banners.clear();
}; };
}, [getBanners, serverContext, subscribeToNotifyLoggedIn, uid]); }, [getBanners, serverContext, subscribeToNotifyLoggedIn, uid, subscribeToNotifyUser]);
}; };
...@@ -337,13 +337,19 @@ export class ListenersModule { ...@@ -337,13 +337,19 @@ export class ListenersModule {
}); });
}); });
service.onEvent('banner.user', (userId, banner): void => {
notifications.notifyUserInThisInstance(userId, 'banners', banner);
});
service.onEvent('banner.new', (bannerId): void => { service.onEvent('banner.new', (bannerId): void => {
notifications.notifyLoggedInThisInstance('new-banner', { bannerId }); // deprecated notifications.notifyLoggedInThisInstance('new-banner', { bannerId }); // deprecated
notifications.notifyLoggedInThisInstance('banner-changed', { bannerId }); notifications.notifyLoggedInThisInstance('banner-changed', { bannerId });
}); });
service.onEvent('banner.disabled', (bannerId): void => { service.onEvent('banner.disabled', (bannerId): void => {
notifications.notifyLoggedInThisInstance('banner-changed', { bannerId }); notifications.notifyLoggedInThisInstance('banner-changed', { bannerId });
}); });
service.onEvent('banner.enabled', (bannerId): void => { service.onEvent('banner.enabled', (bannerId): void => {
notifications.notifyLoggedInThisInstance('banner-changed', { bannerId }); notifications.notifyLoggedInThisInstance('banner-changed', { bannerId });
}); });
......
...@@ -42,7 +42,7 @@ export class BannerService extends ServiceClassInternal implements IBannerServic ...@@ -42,7 +42,7 @@ export class BannerService extends ServiceClassInternal implements IBannerServic
throw new Error('error-creating-banner'); throw new Error('error-creating-banner');
} }
void api.broadcast('banner.new', banner._id); void this.sendToUsers(banner);
return banner; return banner;
} }
...@@ -123,9 +123,38 @@ export class BannerService extends ServiceClassInternal implements IBannerServic ...@@ -123,9 +123,38 @@ export class BannerService extends ServiceClassInternal implements IBannerServic
const { _id, ...banner } = result; const { _id, ...banner } = result;
await Banners.updateOne({ _id }, { $set: { ...banner, ...doc, active: true } }); // reenable the banner const newBanner = { ...banner, ...doc, active: true };
await Banners.updateOne({ _id }, { $set: newBanner }); // reenable the banner
void this.sendToUsers({ _id, ...newBanner });
return true;
}
async sendToUsers(banner: IBanner): Promise<boolean> {
if (!banner.active) {
return false;
}
// no roles set, so it should be sent to all users
if (!banner.roles?.length) {
void api.broadcast('banner.enabled', banner._id);
return true;
}
const total = await Users.countActiveUsersInRoles(banner.roles);
// if more than 100 users should receive the banner, send it to all users
if (total > 100) {
void api.broadcast('banner.enabled', banner._id);
return true;
}
await Users.findActiveUsersInRoles(banner.roles, { projection: { _id: 1 } }).forEach((user) => {
void api.broadcast('banner.user', user._id, banner);
});
void api.broadcast('banner.enabled', bannerId);
return true; return true;
} }
} }
...@@ -21,6 +21,7 @@ import type { ...@@ -21,6 +21,7 @@ import type {
ICalendarNotification, ICalendarNotification,
IUserStatus, IUserStatus,
ILivechatInquiryRecord, ILivechatInquiryRecord,
IBanner,
} from '@rocket.chat/core-typings'; } from '@rocket.chat/core-typings';
type ClientAction = 'inserted' | 'updated' | 'removed' | 'changed'; type ClientAction = 'inserted' | 'updated' | 'removed' | 'changed';
...@@ -165,6 +166,7 @@ export interface StreamerEvents { ...@@ -165,6 +166,7 @@ export interface StreamerEvents {
]; ];
}, },
{ key: `${string}/calendar`; args: [ICalendarNotification] }, { key: `${string}/calendar`; args: [ICalendarNotification] },
{ key: `${string}/banners`; args: [IBanner] },
]; ];
'importers': [ 'importers': [
......
...@@ -30,6 +30,7 @@ import type { ...@@ -30,6 +30,7 @@ import type {
ICalendarNotification, ICalendarNotification,
AtLeast, AtLeast,
ILivechatInquiryRecord, ILivechatInquiryRecord,
IBanner,
} from '@rocket.chat/core-typings'; } from '@rocket.chat/core-typings';
import type { AutoUpdateRecord } from './types/IMeteor'; import type { AutoUpdateRecord } from './types/IMeteor';
...@@ -48,6 +49,7 @@ export type EventSignatures = { ...@@ -48,6 +49,7 @@ export type EventSignatures = {
'banner.new'(bannerId: string): void; 'banner.new'(bannerId: string): void;
'banner.enabled'(bannerId: string): void; 'banner.enabled'(bannerId: string): void;
'banner.disabled'(bannerId: string): void; 'banner.disabled'(bannerId: string): void;
'banner.user'(userId: string, banner: IBanner): void;
'emoji.deleteCustom'(emoji: IEmoji): void; 'emoji.deleteCustom'(emoji: IEmoji): void;
'emoji.updateCustom'(emoji: IEmoji): void; 'emoji.updateCustom'(emoji: IEmoji): void;
'license.module'(data: { module: string; valid: boolean }): void; 'license.module'(data: { module: string; valid: boolean }): void;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment