Фикс подписки пользователя на уведомления.
This commit is contained in:
2024-10-06 17:46:14 +04:00
parent 2efceeaec4
commit ac95dad30e
4 changed files with 17 additions and 11 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "schedule-parser-next", "name": "schedule-parser-next",
"version": "1.3.1", "version": "1.3.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "schedule-parser-next", "name": "schedule-parser-next",
"version": "1.3.1", "version": "1.3.2",
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {
"@nestjs/cache-manager": "^2.2.2", "@nestjs/cache-manager": "^2.2.2",

View File

@@ -1,6 +1,6 @@
{ {
"name": "schedule-parser-next", "name": "schedule-parser-next",
"version": "1.3.1", "version": "1.3.2",
"description": "", "description": "",
"author": "N08I40K", "author": "N08I40K",
"private": true, "private": true,

View File

@@ -20,7 +20,8 @@ import { isSemVer } from "class-validator";
@Controller("api/v1/fcm") @Controller("api/v1/fcm")
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
export class FirebaseAdminController { export class FirebaseAdminController {
private readonly defaultTopics = new Set(["schedule-update", "app-update"]); private readonly oldTopics = new Set(["app-update", "schedule-update"]);
private readonly defaultTopics = new Set(["common"]);
constructor(private readonly firebaseAdminService: FirebaseAdminService) {} constructor(private readonly firebaseAdminService: FirebaseAdminService) {}
@@ -37,10 +38,11 @@ export class FirebaseAdminController {
await this.firebaseAdminService.updateToken(user, token) await this.firebaseAdminService.updateToken(user, token)
).userDto; ).userDto;
await this.firebaseAdminService.subscribe( await this.firebaseAdminService
updatedUser, .subscribe(updatedUser, this.defaultTopics, true)
this.defaultTopics, .then((userDto) =>
); this.firebaseAdminService.unsubscribe(userDto, this.oldTopics),
);
} }
@Post("update-callback/:version") @Post("update-callback/:version")

View File

@@ -81,12 +81,16 @@ export class FirebaseAdminService implements OnModuleInit {
}); });
} }
async subscribe(user: UserDto, topics: Set<string>): Promise<UserDto> { async subscribe(
user: UserDto,
topics: Set<string>,
force: boolean = false,
): Promise<UserDto> {
const fcm = user.fcm; const fcm = user.fcm;
const currentTopics = new Set(fcm.topics); const currentTopics = new Set(fcm.topics);
for (const topic of topics) { for (const topic of topics) {
if (fcm.topics.includes(topic)) continue; if (fcm.topics.includes(topic) && !force) continue;
await this.messaging.subscribeToTopic(fcm.token, topic); await this.messaging.subscribeToTopic(fcm.token, topic);
currentTopics.add(topic); currentTopics.add(topic);
@@ -106,7 +110,7 @@ export class FirebaseAdminService implements OnModuleInit {
version: string, version: string,
topics: Set<string>, topics: Set<string>,
): Promise<void> { ): Promise<void> {
await this.subscribe(userDto, topics).then(async (userDto) => { await this.subscribe(userDto, topics, true).then(async (userDto) => {
await this.usersService.update({ await this.usersService.update({
where: { id: userDto.id }, where: { id: userDto.id },
data: { version: version }, data: { version: version },