Skip to content

feat(api): enable web push notifications #902

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

Merged
merged 1 commit into from
Jul 20, 2024
Merged
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 @@ -18,14 +18,35 @@ export class NotificationWebPushGateway {
);
}

public async sendWebPush(profileId: number, notification: INotification) {
private async sendWebPushToProfile(
profileId: number,
notification: INotification,
): Promise<boolean> {
const pushSubscription =
this.notificationSubscriptionService.getSubscription(profileId);

if (!pushSubscription) {
return;
return false;
}

await sendNotification(pushSubscription, JSON.stringify(notification));
const result = await sendNotification(
pushSubscription,
JSON.stringify(notification),
);

return result.statusCode < 400;
}

public async sendWebPush(
profileIds: number[],
notification: INotification,
): Promise<Record<number, boolean>> {
const result: Record<number, boolean> = {};

for (const profileId of profileIds) {
await this.sendWebPushToProfile(profileId, notification);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Profile } from "src/core/profiles";
import { getRequiredStringConfig } from "src/utils/config.helper";

import { NotificationGateway } from "../../client/gateways/notification.gateway";
import { NotificationWebPushGateway } from "../../client/gateways/notification.web-push.gateway";
import { Notification } from "../../infrastructure/entities/notification.entity";
import { INotification } from "../../types";

Expand All @@ -24,6 +25,7 @@ export class NotificationService {
@InjectRepository(Notification)
private readonly notifications: Repository<Notification>,
private readonly notificationGateway: NotificationGateway,
private readonly notificationWebPushGateway: NotificationWebPushGateway,
@InjectRepository(OrganizationMembership)
private readonly organizationMemberships: Repository<OrganizationMembership>,
@InjectRepository(Profile)
Expand Down Expand Up @@ -61,6 +63,10 @@ export class NotificationService {
});
await this.notifications.save(newNotifications);

// TODO: Do not send web push if web socket succeded
const profileIds = memberships.map((membership) => membership.profile.id);
await this.notificationWebPushGateway.sendWebPush(profileIds, notification);

const userIds = memberships.map((membership) => membership.profile.userId);
await this.notifyUsers(userIds, notification);
}
Expand All @@ -72,6 +78,9 @@ export class NotificationService {
where: { id: In(profileIds) },
});

// TODO: Do not send web push if web socket succeded
await this.notificationWebPushGateway.sendWebPush(profileIds, notification);

const ids = profiles.map((profile) => profile.userId);

await this.notifyUsers(ids, notification);
Expand Down
Loading