2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { SwSubscriptionsRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2017-11-21 03:40:09 +09:00
|
|
|
|
2018-07-17 04:36:44 +09:00
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['account'],
|
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2018-07-17 04:36:44 +09:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Register to receive push notifications.',
|
|
|
|
|
|
2021-03-06 22:34:11 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 22:34:11 +09:00
|
|
|
properties: {
|
|
|
|
|
state: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'string',
|
2022-02-19 23:21:28 +09:00
|
|
|
optional: true, nullable: false,
|
2021-12-09 23:58:30 +09:00
|
|
|
enum: ['already-subscribed', 'subscribed'],
|
2021-03-06 22:34:11 +09:00
|
|
|
},
|
|
|
|
|
key: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'string',
|
2022-02-19 23:21:28 +09:00
|
|
|
optional: false, nullable: true,
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-12-18 01:59:59 +09:00
|
|
|
userId: {
|
|
|
|
|
type: 'string',
|
|
|
|
|
optional: false, nullable: false,
|
|
|
|
|
},
|
|
|
|
|
endpoint: {
|
|
|
|
|
type: 'string',
|
|
|
|
|
optional: false, nullable: false,
|
|
|
|
|
},
|
|
|
|
|
sendReadMessage: {
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
optional: false, nullable: false,
|
|
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2017-11-21 03:40:09 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
endpoint: { type: 'string' },
|
|
|
|
|
auth: { type: 'string' },
|
|
|
|
|
publickey: { type: 'string' },
|
2022-12-18 01:59:59 +09:00
|
|
|
sendReadMessage: { type: 'boolean', default: false },
|
2022-02-19 14:05:32 +09:00
|
|
|
},
|
|
|
|
|
required: ['endpoint', 'auth', 'publickey'],
|
|
|
|
|
} as const;
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
2023-08-17 21:20:58 +09:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.swSubscriptionsRepository)
|
|
|
|
|
private swSubscriptionsRepository: SwSubscriptionsRepository,
|
2017-11-21 03:40:09 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
private idService: IdService,
|
|
|
|
|
private metaService: MetaService,
|
|
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
|
// if already subscribed
|
|
|
|
|
const exist = await this.swSubscriptionsRepository.findOneBy({
|
|
|
|
|
userId: me.id,
|
|
|
|
|
endpoint: ps.endpoint,
|
|
|
|
|
auth: ps.auth,
|
|
|
|
|
publickey: ps.publickey,
|
|
|
|
|
});
|
2018-12-20 04:08:13 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const instance = await this.metaService.fetch(true);
|
|
|
|
|
|
|
|
|
|
if (exist != null) {
|
|
|
|
|
return {
|
|
|
|
|
state: 'already-subscribed' as const,
|
|
|
|
|
key: instance.swPublicKey,
|
2022-12-18 01:59:59 +09:00
|
|
|
userId: me.id,
|
|
|
|
|
endpoint: exist.endpoint,
|
|
|
|
|
sendReadMessage: exist.sendReadMessage,
|
2022-09-18 03:27:08 +09:00
|
|
|
};
|
|
|
|
|
}
|
2017-11-21 03:40:09 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
await this.swSubscriptionsRepository.insert({
|
|
|
|
|
id: this.idService.genId(),
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
userId: me.id,
|
|
|
|
|
endpoint: ps.endpoint,
|
|
|
|
|
auth: ps.auth,
|
|
|
|
|
publickey: ps.publickey,
|
2022-12-18 01:59:59 +09:00
|
|
|
sendReadMessage: ps.sendReadMessage,
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
2017-11-21 03:40:09 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
return {
|
|
|
|
|
state: 'subscribed' as const,
|
|
|
|
|
key: instance.swPublicKey,
|
2022-12-18 01:59:59 +09:00
|
|
|
userId: me.id,
|
|
|
|
|
endpoint: ps.endpoint,
|
|
|
|
|
sendReadMessage: ps.sendReadMessage,
|
2022-09-18 03:27:08 +09:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|