mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
49672a1e4d
no-issue
95 lines
2.5 KiB
JavaScript
95 lines
2.5 KiB
JavaScript
const {URL} = require('url');
|
|
const settingsCache = require('../settings/cache');
|
|
const urlUtils = require('../../lib/url-utils');
|
|
const MembersApi = require('@tryghost/members-api');
|
|
const common = require('../../lib/common');
|
|
const mail = require('../mail');
|
|
const models = require('../../models');
|
|
|
|
function createMember({email}) {
|
|
return models.Member.add({
|
|
email
|
|
}).then((member) => {
|
|
return member.toJSON();
|
|
});
|
|
}
|
|
|
|
function getMember(data, options = {}) {
|
|
if (!data.email && !data.id) {
|
|
return Promise.resolve(null);
|
|
}
|
|
return models.Member.findOne(data, options).then((model) => {
|
|
if (!model) {
|
|
return null;
|
|
}
|
|
return model.toJSON(options);
|
|
});
|
|
}
|
|
|
|
function deleteMember(options) {
|
|
options = options || {};
|
|
return models.Member.destroy(options).catch(models.Member.NotFoundError, () => {
|
|
throw new common.errors.NotFoundError({
|
|
message: common.i18n.t('errors.api.resource.resourceNotFound', {
|
|
resource: 'Member'
|
|
})
|
|
});
|
|
});
|
|
}
|
|
|
|
function listMembers(options) {
|
|
return models.Member.findPage(options).then((models) => {
|
|
return {
|
|
members: models.data.map(model => model.toJSON(options)),
|
|
meta: models.meta
|
|
};
|
|
});
|
|
}
|
|
|
|
const getApiUrl = ({version, type}) => {
|
|
const {href} = new URL(
|
|
urlUtils.getApiPath({version, type}),
|
|
urlUtils.urlFor('admin', true)
|
|
);
|
|
return href;
|
|
};
|
|
|
|
const siteUrl = urlUtils.getSiteUrl();
|
|
const membersApiUrl = getApiUrl({version: 'v2', type: 'members'});
|
|
|
|
const ghostMailer = new mail.GhostMailer();
|
|
|
|
module.exports = createApiInstance;
|
|
|
|
function createApiInstance() {
|
|
const membersApiInstance = MembersApi({
|
|
tokenConfig: {
|
|
issuer: membersApiUrl,
|
|
publicKey: settingsCache.get('members_public_key'),
|
|
privateKey: settingsCache.get('members_private_key')
|
|
},
|
|
auth: {
|
|
getSigninURL(token) {
|
|
const signinURL = new URL(siteUrl);
|
|
signinURL.searchParams.set('token', token);
|
|
return signinURL.href;
|
|
}
|
|
},
|
|
mail: {
|
|
transporter: {
|
|
sendMail(message) {
|
|
return ghostMailer.send(Object.assign({subject: 'Signin'}, message));
|
|
}
|
|
}
|
|
},
|
|
createMember,
|
|
getMember,
|
|
deleteMember,
|
|
listMembers
|
|
});
|
|
|
|
membersApiInstance.setLogger(common.logging);
|
|
|
|
return membersApiInstance;
|
|
}
|