Мда... Хуже не придумаешь.
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

View File

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

View File

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

View File

@@ -48,6 +48,21 @@ export class ScheduleService {
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 {