Sharkey/src/server/api/endpoints/sw/register.ts

53 lines
916 B
TypeScript
Raw Normal View History

2017-11-20 20:40:09 +02:00
import $ from 'cafy';
2018-03-29 14:32:18 +03:00
import Subscription from '../../../../models/sw-subscription';
2018-09-01 02:13:18 +03:00
import config from '../../../../config';
2018-11-02 06:47:44 +02:00
import define from '../../define';
2017-11-20 20:40:09 +02:00
2018-07-16 22:36:44 +03:00
export const meta = {
2018-11-02 05:49:08 +02:00
requireCredential: true,
2018-07-16 22:36:44 +03:00
2018-11-02 05:49:08 +02:00
params: {
endpoint: {
validator: $.str
},
auth: {
validator: $.str
},
2017-11-20 20:40:09 +02:00
2018-11-02 05:49:08 +02:00
publickey: {
validator: $.str
}
}
};
2017-11-20 20:40:09 +02:00
2018-11-02 06:47:44 +02:00
export default define(meta, (ps, user) => new Promise(async (res, rej) => {
2017-11-20 20:40:09 +02:00
// if already subscribed
const exist = await Subscription.findOne({
2018-03-29 08:48:47 +03:00
userId: user._id,
2018-11-02 05:49:08 +02:00
endpoint: ps.endpoint,
auth: ps.auth,
publickey: ps.publickey,
2018-03-29 08:48:47 +03:00
deletedAt: { $exists: false }
2017-11-20 20:40:09 +02:00
});
2018-09-01 02:13:18 +03:00
if (exist != null) {
return res({
state: 'already-subscribed',
key: config.sw.public_key
});
2017-11-20 20:40:09 +02:00
}
await Subscription.insert({
2018-03-29 08:48:47 +03:00
userId: user._id,
2018-11-02 05:49:08 +02:00
endpoint: ps.endpoint,
auth: ps.auth,
publickey: ps.publickey
2017-11-20 20:40:09 +02:00
});
2018-09-01 02:13:18 +03:00
res({
state: 'subscribed',
key: config.sw.public_key
});
2018-11-02 06:47:44 +02:00
}));