Skip to content
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

Feat/add push notifications #452

Merged
merged 13 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ module.exports = {
'@typescript-eslint/ban-ts-comment': 0,
'@typescript-eslint/no-unsafe-member-access': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/no-unsafe-call': 0,
'@typescript-eslint/no-unsafe-return': 0,
'@typescript-eslint/no-unused-expressions': [2, { allowShortCircuit: true }],
'@typescript-eslint/no-floating-promises': [2, { ignoreIIFE: true, ignoreVoid: true }],
'@typescript-eslint/naming-convention': [
Expand Down
5 changes: 5 additions & 0 deletions packages/api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ [email protected];[email protected]
RATE_LIMIT_MIN_TIME=60
RATE_LIMIT_MAX_CONCURRENT=10

# Push notifications
# For more details check: https://github.com/web-push-libs/web-push#using-vapid-key-for-applicationserverkey
PUSH_NOTIFICATION_PUBLIC_VAPID_KEY=
PUSH_NOTIFICATION_PRIVATE_VAPID_KEY=

# Database configuration
DB_DATABASE=smart-gate-db
DB_DATABASE_TEST=smart-gate-db-test
Expand Down
4 changes: 3 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/nodemailer": "^6.4.2",
"@types/socket.io": "^3.0.2",
"@types/uuid": "^8.3.0",
"@types/web-push": "^3.3.2",
Jozwiaczek marked this conversation as resolved.
Show resolved Hide resolved
"bcrypt": "^5.0.1",
"cache-manager": "^3.4.4",
"class-transformer": "^0.4.0",
Expand All @@ -63,7 +64,8 @@
"tsconfig-paths": "^3.9.0",
"typeorm": "^0.2.34",
"typescript": "^4.3.2",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"web-push": "^3.4.5"
},
"devDependencies": {
"@nestjs/testing": "^7.6.17",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const bootstrap = async (): Promise<INestApplication> => {
const config = app.get(Config);

app.enableCors({
origin: config.clientUrl,
origin: config.clientUrl.split(';'),
Jozwiaczek marked this conversation as resolved.
Show resolved Hide resolved
credentials: true,
});
app.use(cookieParser(config.cookie.secret));
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/modules/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DatabaseModule } from './database/database.module';
import { InvitationsModule } from './invitations/invitations.module';
import { MailerModule } from './mailer/mailer.module';
import { PasswordResetModule } from './password-reset/password-reset.module';
import { PushNotificationsModule } from './push-notifications/push-notifications.module';
import { RepositoryModule } from './repository/repository.module';
import { SentryModule } from './sentry/sentry.module';
import { TicketModule } from './ticket/ticket.module';
Expand Down Expand Up @@ -35,6 +36,7 @@ import { WebsocketModule } from './websocket/websocket.module';
RepositoryModule,
WebsocketModule,
TicketModule,
PushNotificationsModule,
],
})
export class AppModule {}
4 changes: 4 additions & 0 deletions packages/api/src/modules/config/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class ConfigLoader {
firstName: this.envConfigService.get('TEST_USER_FIRSTNAME', isTest),
lastName: this.envConfigService.get('TEST_USER_LASTNAME', isTest),
},
pushNotifications: {
publicVapidKey: this.envConfigService.get('PUSH_NOTIFICATION_PUBLIC_VAPID_KEY', isProd),
privateVapidKey: this.envConfigService.get('PUSH_NOTIFICATION_PRIVATE_VAPID_KEY', isProd),
},
rateLimiter: {
minTime: this.envConfigService.get('RATE_LIMIT_MIN_TIME', isProd, Number),
maxConcurrent: this.envConfigService.get('RATE_LIMIT_MAX_CONCURRENT', isProd, Number),
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/modules/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export class Config {
lastName: string | undefined;
};

pushNotifications: {
publicVapidKey?: string;
privateVapidKey?: string;
};

database: {
database: string;
databaseTest: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/modules/database/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { InvitationEntity } from './invitation.entity';
import { PushNotificationEntity } from './pushNotification.entity';
import { RefreshTokenEntity } from './refreshToken.entity';
import { UserEntity } from './user.entity';

export default [UserEntity, RefreshTokenEntity, InvitationEntity];
export default [UserEntity, RefreshTokenEntity, InvitationEntity, PushNotificationEntity];
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Column, Entity, ManyToOne } from 'typeorm';

import { BaseEntity } from './base.entity';
// eslint-disable-next-line import/no-cycle
import { UserEntity } from './user.entity';

@Entity('push_notifications')
export class PushNotificationEntity extends BaseEntity {
@Column({
type: 'varchar',
unique: true,
})
public endpoint: string;

@Column({
type: 'varchar',
})
public p256dh: string;

@Column({
type: 'varchar',
})
public auth: string;

@ManyToOne(() => UserEntity, (user) => user.pushNotifications, { onDelete: 'CASCADE' })
public user: Promise<UserEntity>;
Jozwiaczek marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 7 additions & 0 deletions packages/api/src/modules/database/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { BaseEntity } from './base.entity';
// eslint-disable-next-line import/no-cycle
import { InvitationEntity } from './invitation.entity';
// eslint-disable-next-line import/no-cycle
import { PushNotificationEntity } from './pushNotification.entity';
// eslint-disable-next-line import/no-cycle
import { RefreshTokenEntity } from './refreshToken.entity';

@Entity('users')
Expand Down Expand Up @@ -46,4 +48,9 @@ export class UserEntity extends BaseEntity {

@OneToMany(() => InvitationEntity, (invitation) => invitation.updatedBy, { onDelete: 'CASCADE' })
public updatedInvitations: Array<InvitationEntity>;

@OneToMany(() => PushNotificationEntity, (pushNotification) => pushNotification.user, {
onDelete: 'CASCADE',
})
public pushNotifications: Array<InvitationEntity>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class PushNotificationEntity1626479469506 implements MigrationInterface {
name = 'PushNotificationEntity1626479469506';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "push_notifications" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "endpoint" character varying NOT NULL, "p256dh" character varying NOT NULL, "auth" character varying NOT NULL, "userId" uuid, CONSTRAINT "UQ_0fbc76039fde71a789ccbfbf081" UNIQUE ("endpoint"), CONSTRAINT "PK_99bba16844a5a39fd0d23fb8835" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`ALTER TABLE "push_notifications" ADD CONSTRAINT "FK_a4cb30fb825189ba472f54b163e" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "push_notifications" DROP CONSTRAINT "FK_a4cb30fb825189ba472f54b163e"`,
);
await queryRunner.query(`DROP TABLE "push_notifications"`);
}
}
2 changes: 1 addition & 1 deletion packages/api/src/modules/mailer/mailer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class MailerService {
private readonly config: Config,
) {}

async sendEmail(options: Mail.Options): Promise<void> {
private async sendEmail(options: Mail.Options): Promise<void> {
const transporterBaseConfig = await this.mailerConfigService.getTransporterConfig();
const transporter = nodemailer.createTransport(transporterBaseConfig);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IsArray, IsOptional, IsString, ValidateNested } from 'class-validator';

import { Role } from '../../../enums/role.enum';

export class SendPushNotificationDto {
@IsString()
title: string;

@IsString()
body: string;

@IsOptional()
@ValidateNested()
options?: PushNotificationOptions;

@IsOptional()
@IsArray()
roles?: [Role];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ValidateNested } from 'class-validator';
import { PushSubscription } from 'web-push';

import { UserEntity } from '../../database/entities/user.entity';

export class SubscribePushNotificationDto {
@ValidateNested()
subscription: PushSubscription;

@ValidateNested()
userPromise: Promise<UserEntity>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Body, Controller, Post } from '@nestjs/common';
import { PushSubscription } from 'web-push';

import { TokenPayload } from '../../interfaces/token-types';
import { AuthService } from '../auth/auth.service';
import { Auth } from '../auth/decorators/auth.decorator';
import { CookiePayload } from '../auth/decorators/cookiePayload.decorator';
import { PushNotificationsService } from './push-notifications.service';

@Auth()
@Controller('push-notifications')
export class PushNotificationsController {
constructor(
private readonly pushNotificationsService: PushNotificationsService,
private readonly authService: AuthService,
) {}

@Post()
async subscribe(
@CookiePayload() { sub }: TokenPayload,
@Body() subscription: PushSubscription,
): Promise<void> {
const userPromise = this.authService.getUser(sub);
await this.pushNotificationsService.subscribe({ subscription, userPromise });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from '@nestjs/common';

import { AuthModule } from '../auth/auth.module';
import { TokenModule } from '../auth/token/token.module';
import { ConfigModule } from '../config/config.module';
import { RepositoryModule } from '../repository/repository.module';
import { PushNotificationsController } from './push-notifications.controller';
import { PushNotificationsService } from './push-notifications.service';

@Module({
imports: [ConfigModule, RepositoryModule, AuthModule, TokenModule],
providers: [PushNotificationsService],
controllers: [PushNotificationsController],
exports: [PushNotificationsService],
})
export class PushNotificationsModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Injectable, Logger } from '@nestjs/common';
import webPush, { PushSubscription } from 'web-push';

import { Role } from '../../enums/role.enum';
import { Config } from '../config/config';
import { PushNotificationRepository } from '../repository/push-notification.repository';
import { SendPushNotificationDto } from './dto/send-push-notification.dto';
import { SubscribePushNotificationDto } from './dto/subscribe-push-notification.dto';

@Injectable()
export class PushNotificationsService {
private readonly logger: Logger = new Logger(PushNotificationsService.name);

constructor(
private readonly config: Config,
private readonly pushNotificationRepository: PushNotificationRepository,
) {
const { pushNotifications, mailer } = config;
const { publicVapidKey, privateVapidKey } = pushNotifications;
const { replyTo } = mailer;

if (publicVapidKey && privateVapidKey) {
webPush.setVapidDetails(`mailto:${replyTo}`, publicVapidKey, privateVapidKey);
}
}

async subscribe({ subscription, userPromise }: SubscribePushNotificationDto): Promise<void> {
this.logger.log('New subscriber');
await this.pushNotificationRepository.create({
user: userPromise,
Jozwiaczek marked this conversation as resolved.
Show resolved Hide resolved
endpoint: subscription.endpoint,
p256dh: subscription.keys.p256dh,
auth: subscription.keys.auth,
});
}

private async getSubscriptions(roles?: [Role] | undefined) {
if (!roles) {
return this.pushNotificationRepository.find();
}

return this.pushNotificationRepository.findByRoles(roles);
}

async send({ title, body, options, roles }: SendPushNotificationDto): Promise<void> {
this.logger.log('Sending...');

const subscriptions = await this.getSubscriptions(roles);

if (!subscriptions.length) {
this.logger.log('No subscriptions');
return;
}

const payload: PushNotificationPayload = {
title,
options: {
body,
...options,
},
};

const sendNotificationsPromises = subscriptions.map(({ endpoint, p256dh, auth }) => {
const sub: PushSubscription = { endpoint, keys: { p256dh, auth } };
return webPush.sendNotification(sub, JSON.stringify(payload));
});

try {
await Promise.all(sendNotificationsPromises);

const sendTotal = sendNotificationsPromises.length;
this.logger.log(`Sent ${sendTotal} ${sendTotal > 1 ? 'notifications' : 'notification'}`);
} catch (error) {
this.logger.error(error);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
interface PushNotificationOptions {
dir?: PushNotificationDirection;
lang?: string;
body?: string;
tag?: string;
image?: string;
icon?: string;
badge?: string;
sound?: string;
vibrate?: number | number[];
timestamp?: number;
renotify?: boolean;
silent?: boolean;
requireInteraction?: boolean;
actions?: PushNotificationAction[];
}

interface PushNotificationPayload {
title: string;
options?: PushNotificationOptions;
}

type PushNotificationDirection = 'auto' | 'ltr' | 'rtl';

interface PushNotificationAction {
action: string;
title: string;
icon?: string;
}
3 changes: 1 addition & 2 deletions packages/api/src/modules/repository/base.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ export const BaseRepository = <T extends BaseEntity>(
try {
return await this.repository.save(dataToCreate);
} catch (err) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Cannot create entity with data ${dataToCreate}`);
throw new Error(`Cannot create entity with data ${JSON.stringify(dataToCreate)}`);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { In } from 'typeorm';

import { Role } from '../../enums/role.enum';
import { PushNotificationEntity } from '../database/entities/pushNotification.entity';
import { BaseRepository } from './base.repository';

export class PushNotificationRepository extends BaseRepository(PushNotificationEntity) {
async findByRoles(roles: Role[]): Promise<Array<PushNotificationEntity>> {
return this.find({
relations: ['user'],
where: {
user: {
roles: In(roles),
},
},
});
}
}
Loading