Мда... Хуже не придумаешь.
This commit is contained in:
2024-10-10 23:52:51 +04:00
parent 64168dc517
commit a4d7bb9cdc
5 changed files with 30 additions and 17 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "schedule-parser-next", "name": "schedule-parser-next",
"version": "1.3.4", "version": "1.3.5",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "schedule-parser-next", "name": "schedule-parser-next",
"version": "1.3.4", "version": "1.3.5",
"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.4", "version": "1.3.5",
"description": "", "description": "",
"author": "N08I40K", "author": "N08I40K",
"private": true, "private": true,

View File

@@ -21,7 +21,6 @@ import { isSemVer } from "class-validator";
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
export class FirebaseAdminController { export class FirebaseAdminController {
private readonly oldTopics = new Set(["app-update", "schedule-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) {}
@@ -39,7 +38,7 @@ export class FirebaseAdminController {
).userDto; ).userDto;
await this.firebaseAdminService await this.firebaseAdminService
.subscribe(updatedUser, this.defaultTopics, true) .subscribe(updatedUser, new Set(), true)
.then((userDto) => .then((userDto) =>
this.firebaseAdminService.unsubscribe(userDto, this.oldTopics), this.firebaseAdminService.unsubscribe(userDto, this.oldTopics),
); );
@@ -58,11 +57,7 @@ export class FirebaseAdminController {
); );
} }
await this.firebaseAdminService.updateApp( await this.firebaseAdminService.updateApp(userDto, version);
userDto,
version,
this.defaultTopics,
);
} }
@Post("post-update") @Post("post-update")
@@ -70,6 +65,9 @@ export class FirebaseAdminController {
@ResultDto(null) @ResultDto(null)
async postUpdate(@Body() postUpdateDto: FcmPostUpdateDto): Promise<void> { async postUpdate(@Body() postUpdateDto: FcmPostUpdateDto): Promise<void> {
await this.firebaseAdminService.sendByTopic("common", { await this.firebaseAdminService.sendByTopic("common", {
android: {
priority: "high",
},
data: { data: {
type: "app-update", type: "app-update",
version: postUpdateDto.version, version: postUpdateDto.version,

View File

@@ -23,6 +23,8 @@ export class FirebaseAdminService implements OnModuleInit {
private app: App; private app: App;
private messaging: Messaging; private messaging: Messaging;
private readonly defaultTopics = new Set(["common"]);
onModuleInit() { onModuleInit() {
this.app = initializeApp({ this.app = initializeApp({
credential: credential.cert(firebaseConstants.serviceAccountPath), credential: credential.cert(firebaseConstants.serviceAccountPath),
@@ -86,10 +88,12 @@ export class FirebaseAdminService implements OnModuleInit {
topics: Set<string>, topics: Set<string>,
force: boolean = false, force: boolean = false,
): Promise<UserDto> { ): Promise<UserDto> {
const newTopics = new Set([...this.defaultTopics, ...topics]);
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 newTopics) {
if (fcm.topics.includes(topic) && !force) continue; if (fcm.topics.includes(topic) && !force) continue;
await this.messaging.subscribeToTopic(fcm.token, topic); await this.messaging.subscribeToTopic(fcm.token, topic);
@@ -105,12 +109,8 @@ export class FirebaseAdminService implements OnModuleInit {
}); });
} }
async updateApp( async updateApp(userDto: UserDto, version: string): Promise<void> {
userDto: UserDto, await this.subscribe(userDto, new Set(), true).then(async (userDto) => {
version: string,
topics: Set<string>,
): Promise<void> {
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 },

View File

@@ -48,6 +48,21 @@ export class ScheduleService {
this.scheduleReplacerService, this.scheduleReplacerService,
); );
} }
setInterval(async () => {
const now = new Date();
if (now.getHours() != 7 || now.getMinutes() != 30) return;
await this.firebaseAdminService.sendByTopic("common", {
android: {
priority: "high",
ttl: 60 * 60 * 1000,
},
data: {
type: "lessons-start",
},
});
}, 60000);
} }
getCacheStatus(): CacheStatusDto { getCacheStatus(): CacheStatusDto {