2022-02-27 11:07:39 +09:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
|
import { SwSubscriptions } from '@/models/index.js';
|
2022-06-14 18:01:23 +09:00
|
|
|
import define from '../../define.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-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' },
|
|
|
|
|
},
|
|
|
|
|
required: ['endpoint', 'auth', 'publickey'],
|
|
|
|
|
} as const;
|
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 14:05:32 +09:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2017-11-21 03:40:09 +09:00
|
|
|
// if already subscribed
|
2022-03-26 15:34:00 +09:00
|
|
|
const exist = await SwSubscriptions.findOneBy({
|
2019-04-07 21:50:36 +09:00
|
|
|
userId: user.id,
|
2018-11-02 12:49:08 +09:00
|
|
|
endpoint: ps.endpoint,
|
|
|
|
|
auth: ps.auth,
|
|
|
|
|
publickey: ps.publickey,
|
2017-11-21 03:40:09 +09:00
|
|
|
});
|
|
|
|
|
|
2019-04-24 08:11:19 +09:00
|
|
|
const instance = await fetchMeta(true);
|
2018-12-20 04:08:13 +09:00
|
|
|
|
2018-09-01 08:13:18 +09:00
|
|
|
if (exist != null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
return {
|
2022-02-19 23:21:28 +09:00
|
|
|
state: 'already-subscribed' as const,
|
2021-12-09 23:58:30 +09:00
|
|
|
key: instance.swPublicKey,
|
2019-02-22 11:46:58 +09:00
|
|
|
};
|
2017-11-21 03:40:09 +09:00
|
|
|
}
|
|
|
|
|
|
2021-03-21 21:27:09 +09:00
|
|
|
await SwSubscriptions.insert({
|
2019-04-07 21:50:36 +09:00
|
|
|
id: genId(),
|
2019-04-15 16:37:54 +09:00
|
|
|
createdAt: new Date(),
|
2019-04-07 21:50:36 +09:00
|
|
|
userId: user.id,
|
2018-11-02 12:49:08 +09:00
|
|
|
endpoint: ps.endpoint,
|
|
|
|
|
auth: ps.auth,
|
2021-12-09 23:58:30 +09:00
|
|
|
publickey: ps.publickey,
|
2017-11-21 03:40:09 +09:00
|
|
|
});
|
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
return {
|
2022-02-19 23:21:28 +09:00
|
|
|
state: 'subscribed' as const,
|
2021-12-09 23:58:30 +09:00
|
|
|
key: instance.swPublicKey,
|
2019-02-22 11:46:58 +09:00
|
|
|
};
|
|
|
|
|
});
|