2017-11-20 19:40:09 +01:00
|
|
|
import $ from 'cafy';
|
2018-11-02 05:47:44 +01:00
|
|
|
import define from '../../define';
|
2018-12-19 20:08:13 +01:00
|
|
|
import fetchMeta from '../../../../misc/fetch-meta';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { genId } from '../../../../misc/gen-id';
|
|
|
|
import { SwSubscriptions } from '../../../../models';
|
2017-11-20 19:40:09 +01:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['account'],
|
|
|
|
|
2018-11-02 04:49:08 +01:00
|
|
|
requireCredential: true,
|
2018-07-16 21:36:44 +02:00
|
|
|
|
2018-11-02 04:49:08 +01:00
|
|
|
params: {
|
|
|
|
endpoint: {
|
|
|
|
validator: $.str
|
|
|
|
},
|
|
|
|
|
|
|
|
auth: {
|
|
|
|
validator: $.str
|
|
|
|
},
|
2017-11-20 19:40:09 +01:00
|
|
|
|
2018-11-02 04:49:08 +01:00
|
|
|
publickey: {
|
|
|
|
validator: $.str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-11-20 19:40:09 +01:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps, user) => {
|
2017-11-20 19:40:09 +01:00
|
|
|
// if already subscribed
|
2019-04-07 14:50:36 +02:00
|
|
|
const exist = await SwSubscriptions.findOne({
|
|
|
|
userId: user.id,
|
2018-11-02 04:49:08 +01:00
|
|
|
endpoint: ps.endpoint,
|
|
|
|
auth: ps.auth,
|
|
|
|
publickey: ps.publickey,
|
2017-11-20 19:40:09 +01:00
|
|
|
});
|
|
|
|
|
2018-12-19 20:08:13 +01:00
|
|
|
const instance = await fetchMeta();
|
|
|
|
|
2018-09-01 01:13:18 +02:00
|
|
|
if (exist != null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
return {
|
2018-09-01 01:13:18 +02:00
|
|
|
state: 'already-subscribed',
|
2018-12-19 20:08:13 +01:00
|
|
|
key: instance.swPublicKey
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|
2017-11-20 19:40:09 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
await SwSubscriptions.save({
|
|
|
|
id: genId(),
|
|
|
|
userId: user.id,
|
2018-11-02 04:49:08 +01:00
|
|
|
endpoint: ps.endpoint,
|
|
|
|
auth: ps.auth,
|
|
|
|
publickey: ps.publickey
|
2017-11-20 19:40:09 +01:00
|
|
|
});
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
return {
|
2018-09-01 01:13:18 +02:00
|
|
|
state: 'subscribed',
|
2018-12-19 20:08:13 +01:00
|
|
|
key: instance.swPublicKey
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|
|
|
|
});
|