backend-manager 3.2.172 → 3.2.173
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/cli/cli.js +27 -11
- package/dist/cli/cli.js +0 -1534
- package/dist/manager/functions/core/actions/api/admin/backup.js +0 -338
- package/dist/manager/functions/core/actions/api/admin/create-post.js +0 -388
- package/dist/manager/functions/core/actions/api/admin/cron.js +0 -37
- package/dist/manager/functions/core/actions/api/admin/database-read.js +0 -35
- package/dist/manager/functions/core/actions/api/admin/database-write.js +0 -39
- package/dist/manager/functions/core/actions/api/admin/edit-post.js +0 -158
- package/dist/manager/functions/core/actions/api/admin/firestore-query.js +0 -165
- package/dist/manager/functions/core/actions/api/admin/firestore-read.js +0 -38
- package/dist/manager/functions/core/actions/api/admin/firestore-write.js +0 -54
- package/dist/manager/functions/core/actions/api/admin/get-stats.js +0 -269
- package/dist/manager/functions/core/actions/api/admin/payment-processor.js +0 -57
- package/dist/manager/functions/core/actions/api/admin/run-hook.js +0 -95
- package/dist/manager/functions/core/actions/api/admin/send-notification.js +0 -197
- package/dist/manager/functions/core/actions/api/admin/sync-users.js +0 -125
- package/dist/manager/functions/core/actions/api/admin/templates/post.html +0 -16
- package/dist/manager/functions/core/actions/api/firebase/get-providers.js +0 -102
- package/dist/manager/functions/core/actions/api/general/emails/general:download-app-link.js +0 -21
- package/dist/manager/functions/core/actions/api/general/fetch-post.js +0 -99
- package/dist/manager/functions/core/actions/api/general/generate-uuid.js +0 -41
- package/dist/manager/functions/core/actions/api/general/send-email.js +0 -112
- package/dist/manager/functions/core/actions/api/handler/create-post.js +0 -146
- package/dist/manager/functions/core/actions/api/special/setup-electron-manager-client.js +0 -103
- package/dist/manager/functions/core/actions/api/template.js +0 -33
- package/dist/manager/functions/core/actions/api/test/authenticate.js +0 -22
- package/dist/manager/functions/core/actions/api/test/create-test-accounts.js +0 -27
- package/dist/manager/functions/core/actions/api/test/lab.js +0 -55
- package/dist/manager/functions/core/actions/api/test/redirect.js +0 -26
- package/dist/manager/functions/core/actions/api/test/webhook.js +0 -30
- package/dist/manager/functions/core/actions/api/user/create-custom-token.js +0 -32
- package/dist/manager/functions/core/actions/api/user/delete.js +0 -68
- package/dist/manager/functions/core/actions/api/user/get-active-sessions.js +0 -45
- package/dist/manager/functions/core/actions/api/user/get-subscription-info.js +0 -49
- package/dist/manager/functions/core/actions/api/user/oauth2/discord.js +0 -114
- package/dist/manager/functions/core/actions/api/user/oauth2/google.js +0 -99
- package/dist/manager/functions/core/actions/api/user/oauth2.js +0 -476
- package/dist/manager/functions/core/actions/api/user/regenerate-api-keys.js +0 -54
- package/dist/manager/functions/core/actions/api/user/resolve.js +0 -32
- package/dist/manager/functions/core/actions/api/user/sign-out-all-sessions.js +0 -118
- package/dist/manager/functions/core/actions/api/user/sign-up copy.js +0 -544
- package/dist/manager/functions/core/actions/api/user/sign-up.js +0 -99
- package/dist/manager/functions/core/actions/api/user/submit-feedback.js +0 -96
- package/dist/manager/functions/core/actions/api/user/validate-settings.js +0 -86
- package/dist/manager/functions/core/actions/api.js +0 -354
- package/dist/manager/functions/core/actions/create-post-handler.js +0 -184
- package/dist/manager/functions/core/actions/generate-uuid.js +0 -62
- package/dist/manager/functions/core/actions/sign-up-handler.js +0 -205
- package/dist/manager/functions/core/admin/create-post.js +0 -206
- package/dist/manager/functions/core/admin/firestore-write.js +0 -72
- package/dist/manager/functions/core/admin/get-stats.js +0 -218
- package/dist/manager/functions/core/admin/query.js +0 -198
- package/dist/manager/functions/core/admin/send-notification.js +0 -206
- package/dist/manager/functions/core/cron/daily/ghostii-auto-publisher.js +0 -377
- package/dist/manager/functions/core/cron/daily/reset-usage.js +0 -197
- package/dist/manager/functions/core/cron/daily.js +0 -114
- package/dist/manager/functions/core/events/auth/before-create.js +0 -124
- package/dist/manager/functions/core/events/auth/before-signin.js +0 -62
- package/dist/manager/functions/core/events/auth/on-create copy.js +0 -121
- package/dist/manager/functions/core/events/auth/on-create.js +0 -564
- package/dist/manager/functions/core/events/auth/on-delete.js +0 -72
- package/dist/manager/functions/core/events/firestore/on-subscription.js +0 -107
- package/dist/manager/functions/test/authenticate.js +0 -38
- package/dist/manager/functions/test/create-test-accounts.js +0 -144
- package/dist/manager/functions/test/webhook.js +0 -37
- package/dist/manager/functions/wrappers/mailchimp/addToList.js +0 -25
- package/dist/manager/helpers/analytics copy.js +0 -217
- package/dist/manager/helpers/analytics.js +0 -467
- package/dist/manager/helpers/api-manager.js +0 -324
- package/dist/manager/helpers/assistant.js +0 -1043
- package/dist/manager/helpers/metadata.js +0 -32
- package/dist/manager/helpers/middleware.js +0 -154
- package/dist/manager/helpers/roles.js +0 -69
- package/dist/manager/helpers/settings.js +0 -158
- package/dist/manager/helpers/subscription-resolver-new.js +0 -828
- package/dist/manager/helpers/subscription-resolver.js +0 -842
- package/dist/manager/helpers/usage.js +0 -381
- package/dist/manager/helpers/user.js +0 -198
- package/dist/manager/helpers/utilities.js +0 -292
- package/dist/manager/index.js +0 -1076
- package/dist/manager/libraries/openai.js +0 -460
- package/dist/manager/routes/restart/index.js +0 -52
- package/dist/manager/routes/test/index.js +0 -43
- package/dist/manager/schemas/restart.js +0 -13
- package/dist/manager/schemas/test.js +0 -13
- package/dist/require.js +0 -3
|
@@ -1,388 +0,0 @@
|
|
|
1
|
-
const fetch = require('wonderful-fetch');
|
|
2
|
-
const moment = require('moment');
|
|
3
|
-
const jetpack = require('fs-jetpack');
|
|
4
|
-
const powertools = require('node-powertools');
|
|
5
|
-
const uuidv4 = require('uuid').v4;
|
|
6
|
-
const { get, set } = require('lodash');
|
|
7
|
-
const path = require('path');
|
|
8
|
-
const { Octokit } = require('@octokit/rest');
|
|
9
|
-
|
|
10
|
-
const POST_TEMPLATE = jetpack.read(`${__dirname}/templates/post.html`);
|
|
11
|
-
const IMAGE_TAG = `{%- include /master/helpers/blog-image.html name="{name}" alt="{alt}" -%}`;
|
|
12
|
-
const IMAGE_PATH_SRC = `assets/_src/images/blog/posts/post-{id}/`;
|
|
13
|
-
// const IMAGE_PATH_REG = `/assets/images/blog/posts/post-{id}/`;
|
|
14
|
-
const AD_TAG = `{% include /master/modules/adunits/adsense-in-article.html index="{index}" %}`;
|
|
15
|
-
|
|
16
|
-
const IMAGE_REGEX = /(?:!\[(.*?)\]\((.*?)\))/img;
|
|
17
|
-
const LINK_REGEX = /(?:\[(.*?)\]\((.*?)\))/img;
|
|
18
|
-
|
|
19
|
-
function Module() {
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
Module.prototype.main = function () {
|
|
24
|
-
const self = this;
|
|
25
|
-
const Manager = self.Manager;
|
|
26
|
-
const Api = self.Api;
|
|
27
|
-
const assistant = self.assistant;
|
|
28
|
-
const payload = self.payload;
|
|
29
|
-
|
|
30
|
-
return new Promise(async function(resolve, reject) {
|
|
31
|
-
try {
|
|
32
|
-
// Perform checks
|
|
33
|
-
if (!payload.user.roles.admin && !payload.user.roles.blogger) {
|
|
34
|
-
return reject(assistant.errorify(`Admin required.`, {code: 401}));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Log payload
|
|
38
|
-
assistant.log(`main(): payload.data`, payload.data);
|
|
39
|
-
|
|
40
|
-
// Set now
|
|
41
|
-
const now = assistant.meta.startTime.timestamp;
|
|
42
|
-
const bemRepo = assistant.parseRepo(Manager?.config?.github?.repo_website);
|
|
43
|
-
|
|
44
|
-
// Setup Octokit
|
|
45
|
-
self.octokit = new Octokit({
|
|
46
|
-
auth: Manager?.config?.github?.key,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Check for required values
|
|
50
|
-
if (!payload.data.payload.title) {
|
|
51
|
-
return reject(assistant.errorify(`Missing required parameter: title`, {code: 400}));
|
|
52
|
-
} else if (!payload.data.payload.url) {
|
|
53
|
-
return reject(assistant.errorify(`Missing required parameter: url`, {code: 400}));
|
|
54
|
-
} else if (!payload.data.payload.excerpt) {
|
|
55
|
-
return reject(assistant.errorify(`Missing required parameter: excerpt`, {code: 400}));
|
|
56
|
-
} else if (!payload.data.payload.headerImageURL) {
|
|
57
|
-
return reject(assistant.errorify(`Missing required parameter: headerImageURL`, {code: 400}));
|
|
58
|
-
} else if (!payload.data.payload.body) {
|
|
59
|
-
return reject(assistant.errorify(`Missing required parameter: body`, {code: 400}));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Fix required values
|
|
63
|
-
payload.data.payload.url = payload.data.payload.url
|
|
64
|
-
// Replace blog/
|
|
65
|
-
.replace(/blog\//ig, '')
|
|
66
|
-
// Remove leading and trailing slashes
|
|
67
|
-
.replace(/^\/|\/$/g, '')
|
|
68
|
-
// Replace anything that's not a letter or number with a hyphen
|
|
69
|
-
.replace(/[^a-zA-Z0-9]/g, '-')
|
|
70
|
-
// Remove multiple hyphens
|
|
71
|
-
.replace(/-+/g, '-')
|
|
72
|
-
// Lowercase
|
|
73
|
-
.toLowerCase();
|
|
74
|
-
|
|
75
|
-
// Fix body
|
|
76
|
-
payload.data.payload.body = payload.data.payload.body
|
|
77
|
-
// Replace heading text (# + payload.data.payload.title) (just the first instance in case it is repeated)
|
|
78
|
-
.replace(powertools.regexify(`/# ${payload.data.payload.title}/i`), '')
|
|
79
|
-
// Remove extra newlines
|
|
80
|
-
.replace(/\n\n\n+/g, '\n\n')
|
|
81
|
-
// Trim
|
|
82
|
-
.trim();
|
|
83
|
-
|
|
84
|
-
// Fix other values
|
|
85
|
-
payload.data.payload.author = payload.data.payload.author || 'alex';
|
|
86
|
-
payload.data.payload.affiliate = payload.data.payload.affiliate || '';
|
|
87
|
-
payload.data.payload.tags = payload.data.payload.tags || [];
|
|
88
|
-
payload.data.payload.categories = payload.data.payload.categories || [];
|
|
89
|
-
|
|
90
|
-
// Fix even more values
|
|
91
|
-
payload.data.payload.layout = payload.data.payload.layout || 'app/blog/post';
|
|
92
|
-
payload.data.payload.date = moment(payload.data.payload.date || now).subtract(1, 'days').format('YYYY-MM-DD');
|
|
93
|
-
payload.data.payload.id = payload.data.payload.id || Math.round(new Date(now).getTime() / 1000);
|
|
94
|
-
payload.data.payload.path = `_posts/${moment(now).format('YYYY')}/${payload.data.payload.path || 'guest'}`;
|
|
95
|
-
payload.data.payload.githubUser = payload.data.payload.githubUser || bemRepo.user;
|
|
96
|
-
payload.data.payload.githubRepo = payload.data.payload.githubRepo || bemRepo.name;
|
|
97
|
-
|
|
98
|
-
// Log
|
|
99
|
-
assistant.log(`main(): Creating post...`, payload.data.payload);
|
|
100
|
-
|
|
101
|
-
// Extract all images
|
|
102
|
-
await self.extractImages();
|
|
103
|
-
|
|
104
|
-
// Set defaults
|
|
105
|
-
const formattedContent = powertools.template(
|
|
106
|
-
POST_TEMPLATE,
|
|
107
|
-
formatClone(payload.data.payload),
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
// Insert ads
|
|
111
|
-
const articleWithAds = insertAds(formattedContent, 6, 4000);
|
|
112
|
-
|
|
113
|
-
// Log
|
|
114
|
-
assistant.log(`main(): articleWithAds`, articleWithAds);
|
|
115
|
-
|
|
116
|
-
// Upload post
|
|
117
|
-
const uploadPost = await self.uploadPost(articleWithAds);
|
|
118
|
-
|
|
119
|
-
// Log
|
|
120
|
-
assistant.log(`main(): uploadPost`, uploadPost);
|
|
121
|
-
|
|
122
|
-
// Resolve
|
|
123
|
-
return resolve({data: payload.data.payload});
|
|
124
|
-
} catch (e) {
|
|
125
|
-
return reject(e);
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
// Extract images
|
|
131
|
-
Module.prototype.extractImages = function () {
|
|
132
|
-
const self = this;
|
|
133
|
-
const Manager = self.Manager;
|
|
134
|
-
const Api = self.Api;
|
|
135
|
-
const assistant = self.assistant;
|
|
136
|
-
const payload = self.payload;
|
|
137
|
-
|
|
138
|
-
return new Promise(async function(resolve, reject) {
|
|
139
|
-
// Extract images
|
|
140
|
-
const matches = payload.data.payload.body.matchAll(IMAGE_REGEX);
|
|
141
|
-
const images = Array.from(matches).map(match => ({
|
|
142
|
-
full: match[0] || '',
|
|
143
|
-
src: match[2] || '',
|
|
144
|
-
alt: match[1] || uuidv4(),
|
|
145
|
-
replace: true,
|
|
146
|
-
}));
|
|
147
|
-
|
|
148
|
-
// Add heading image to beginning of images
|
|
149
|
-
images.unshift({
|
|
150
|
-
full: '',
|
|
151
|
-
src: payload.data.payload.headerImageURL,
|
|
152
|
-
alt: payload.data.payload.url,
|
|
153
|
-
replace: false,
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
// Log
|
|
157
|
-
assistant.log(`extractImages(): images`, images);
|
|
158
|
-
|
|
159
|
-
// Check if no images
|
|
160
|
-
if (!images) {
|
|
161
|
-
return resolve();
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Loop through images
|
|
165
|
-
for (let index = 0; index < images.length; index++) {
|
|
166
|
-
const image = images[index];
|
|
167
|
-
|
|
168
|
-
// Download image
|
|
169
|
-
const download = await self.downloadImage(image.src, image.alt).catch((e) => e);
|
|
170
|
-
|
|
171
|
-
// Log
|
|
172
|
-
assistant.log(`extractImages(): download`, download);
|
|
173
|
-
|
|
174
|
-
// Check for error
|
|
175
|
-
if (download instanceof Error) {
|
|
176
|
-
return reject(download);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// Upload image
|
|
180
|
-
const upload = await self.uploadImage(download).catch((e) => e);
|
|
181
|
-
|
|
182
|
-
// Log
|
|
183
|
-
assistant.log(`extractImages(): upload`, upload);
|
|
184
|
-
|
|
185
|
-
// Check for error
|
|
186
|
-
if (upload instanceof Error) {
|
|
187
|
-
return reject(upload);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// Create new image tag
|
|
191
|
-
const tag = powertools.template(IMAGE_TAG, {name: download.filename, alt: image.alt});
|
|
192
|
-
|
|
193
|
-
// Replace image in body
|
|
194
|
-
if (image.replace) {
|
|
195
|
-
payload.data.payload.body = payload.data.payload.body
|
|
196
|
-
.replace(image.full, tag);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// Log
|
|
200
|
-
assistant.log(`extractImages(): tag`, tag);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// Resolve
|
|
204
|
-
return resolve();
|
|
205
|
-
});
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
// Downlaod image
|
|
209
|
-
Module.prototype.downloadImage = function (src, alt) {
|
|
210
|
-
const self = this;
|
|
211
|
-
const Manager = self.Manager;
|
|
212
|
-
const Api = self.Api;
|
|
213
|
-
const assistant = self.assistant;
|
|
214
|
-
const payload = self.payload;
|
|
215
|
-
|
|
216
|
-
return new Promise(async function(resolve, reject) {
|
|
217
|
-
// Log
|
|
218
|
-
const hyphenated = hyphenate(alt);
|
|
219
|
-
|
|
220
|
-
// Log
|
|
221
|
-
assistant.log(`downloadImage(): src=${src}, alt=${alt}, hyphenated=${hyphenated}`);
|
|
222
|
-
|
|
223
|
-
// Get image
|
|
224
|
-
await fetch(src, {
|
|
225
|
-
method: 'get',
|
|
226
|
-
download: `${assistant.tmpdir}/${hyphenated}`,
|
|
227
|
-
})
|
|
228
|
-
.then((r) => {
|
|
229
|
-
r.filename = path.basename(r.path);
|
|
230
|
-
r.ext = path.extname(r.path);
|
|
231
|
-
|
|
232
|
-
// Log
|
|
233
|
-
assistant.log(`downloadImage(): Result`, r.path);
|
|
234
|
-
|
|
235
|
-
// If not .jpg, reject
|
|
236
|
-
if (r.ext !== '.jpg') {
|
|
237
|
-
return reject(assistant.errorify(`Images must be .jpg (not ${r.ext})`, {code: 400}));
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// Save image
|
|
241
|
-
return resolve(r);
|
|
242
|
-
})
|
|
243
|
-
.catch((e) => reject(e));
|
|
244
|
-
});
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
// Upload image to GitHub
|
|
248
|
-
Module.prototype.uploadImage = function (image) {
|
|
249
|
-
const self = this;
|
|
250
|
-
const Manager = self.Manager;
|
|
251
|
-
const Api = self.Api;
|
|
252
|
-
const assistant = self.assistant;
|
|
253
|
-
const payload = self.payload;
|
|
254
|
-
|
|
255
|
-
return new Promise(async function(resolve, reject) {
|
|
256
|
-
// Save variables
|
|
257
|
-
const filepath = image.path;
|
|
258
|
-
const filename = image.filename;
|
|
259
|
-
const assetsPath = powertools.template(IMAGE_PATH_SRC, payload.data.payload);
|
|
260
|
-
|
|
261
|
-
// Log
|
|
262
|
-
assistant.log(`uploadImage(): image`, image);
|
|
263
|
-
assistant.log(`uploadImage(): path`, `${assetsPath}${filename}`);
|
|
264
|
-
|
|
265
|
-
// Upload image
|
|
266
|
-
await self.octokit.rest.repos.createOrUpdateFileContents({
|
|
267
|
-
owner: payload.data.payload.githubUser,
|
|
268
|
-
repo: payload.data.payload.githubRepo,
|
|
269
|
-
path: `${assetsPath}${filename}`,
|
|
270
|
-
message: `📦 admin:create-post:upload-image ${filename}`,
|
|
271
|
-
content: jetpack.read(filepath, 'buffer').toString('base64'),
|
|
272
|
-
})
|
|
273
|
-
.then((r) => {
|
|
274
|
-
// Log
|
|
275
|
-
assistant.log(`uploadImage(): Result`, r);
|
|
276
|
-
|
|
277
|
-
// Resolve
|
|
278
|
-
return resolve(r);
|
|
279
|
-
})
|
|
280
|
-
.catch((e) => reject(e));
|
|
281
|
-
});
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
// Upload post to GitHub
|
|
285
|
-
Module.prototype.uploadPost = function (content) {
|
|
286
|
-
const self = this;
|
|
287
|
-
const Manager = self.Manager;
|
|
288
|
-
const Api = self.Api;
|
|
289
|
-
const assistant = self.assistant;
|
|
290
|
-
const payload = self.payload;
|
|
291
|
-
|
|
292
|
-
return new Promise(async function(resolve, reject) {
|
|
293
|
-
// Save variables
|
|
294
|
-
const filename = `${payload.data.payload.path}/${payload.data.payload.date}-${payload.data.payload.url}.md`;
|
|
295
|
-
const owner = payload.data.payload.githubUser;
|
|
296
|
-
const repo = payload.data.payload.githubRepo;
|
|
297
|
-
|
|
298
|
-
// Log
|
|
299
|
-
assistant.log(`uploadPost(): filename`, filename);
|
|
300
|
-
|
|
301
|
-
// Get existing post
|
|
302
|
-
const existing = await self.octokit.rest.repos.getContent({
|
|
303
|
-
owner: owner,
|
|
304
|
-
repo: repo,
|
|
305
|
-
path: filename,
|
|
306
|
-
})
|
|
307
|
-
.catch(e => e);
|
|
308
|
-
|
|
309
|
-
// Log
|
|
310
|
-
assistant.log(`uploadPost(): Existing`, existing);
|
|
311
|
-
|
|
312
|
-
// Quit if error and it's not a 404
|
|
313
|
-
if (
|
|
314
|
-
existing instanceof Error
|
|
315
|
-
&& existing?.status !== 404
|
|
316
|
-
) {
|
|
317
|
-
return reject(existing);
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
// Upload post
|
|
321
|
-
await self.octokit.rest.repos.createOrUpdateFileContents({
|
|
322
|
-
owner: owner,
|
|
323
|
-
repo: repo,
|
|
324
|
-
path: filename,
|
|
325
|
-
sha: existing?.data?.sha || undefined,
|
|
326
|
-
message: `📦 admin:create-post:upload-post ${filename}`,
|
|
327
|
-
content: Buffer.from(content).toString('base64'),
|
|
328
|
-
})
|
|
329
|
-
.then((r) => {
|
|
330
|
-
// Log
|
|
331
|
-
assistant.log(`uploadPost(): Result`, r);
|
|
332
|
-
|
|
333
|
-
// Resolve
|
|
334
|
-
return resolve(r);
|
|
335
|
-
})
|
|
336
|
-
.catch((e) => reject(e));
|
|
337
|
-
});
|
|
338
|
-
};
|
|
339
|
-
|
|
340
|
-
function formatClone(payload) {
|
|
341
|
-
powertools.getKeys(payload).forEach((item) => {
|
|
342
|
-
const value = get(payload, item);
|
|
343
|
-
const isArray = Array.isArray(value);
|
|
344
|
-
|
|
345
|
-
// If it's an array, format as JSON so it appears in frontmatter as an array
|
|
346
|
-
if (isArray) {
|
|
347
|
-
set(payload, item, JSON.stringify(value));
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
return payload;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
function hyphenate(s) {
|
|
355
|
-
return s
|
|
356
|
-
// Remove everything that is not a letter or a number
|
|
357
|
-
.replace(/[^a-zA-Z0-9]/g, '-')
|
|
358
|
-
// Replace multiple hyphens with a single hyphen
|
|
359
|
-
.replace(/-+/g, '-')
|
|
360
|
-
// Remove leading and trailing hyphens
|
|
361
|
-
.replace(/^-|-$/g, '')
|
|
362
|
-
// Lowercase
|
|
363
|
-
.toLowerCase();
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function insertAds(article, paragraphsPerAd, minCharsBetweenAds) {
|
|
367
|
-
const paragraphs = article.split('\n\n');
|
|
368
|
-
const newParagraphs = [];
|
|
369
|
-
let charCount = 0;
|
|
370
|
-
let adCount = 0;
|
|
371
|
-
|
|
372
|
-
for (let i = 0, total = paragraphs.length; i < total; i++) {
|
|
373
|
-
const collection = paragraphs[i];
|
|
374
|
-
|
|
375
|
-
charCount += collection.length;
|
|
376
|
-
|
|
377
|
-
newParagraphs.push(collection);
|
|
378
|
-
|
|
379
|
-
if (i >= paragraphsPerAd && charCount >= minCharsBetweenAds) {
|
|
380
|
-
newParagraphs.push(powertools.template(AD_TAG, {index: adCount++}));
|
|
381
|
-
charCount = 0;
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
return newParagraphs.join('\n\n');
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
module.exports = Module;
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
function Module() {
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
Module.prototype.main = function () {
|
|
6
|
-
const self = this;
|
|
7
|
-
const Manager = self.Manager;
|
|
8
|
-
const Api = self.Api;
|
|
9
|
-
const assistant = self.assistant;
|
|
10
|
-
const payload = self.payload;
|
|
11
|
-
|
|
12
|
-
return new Promise(async function(resolve, reject) {
|
|
13
|
-
|
|
14
|
-
// Check if the user is an admin
|
|
15
|
-
if (!payload.user.roles.admin && assistant.isProduction()) {
|
|
16
|
-
return reject(assistant.errorify(`Admin required.`, {code: 401}));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Check if the ID is set
|
|
20
|
-
if (!payload.data.payload.id) {
|
|
21
|
-
return reject(assistant.errorify(`Missing parameter {id}`, {code: 400}));
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Run the cron job
|
|
25
|
-
Manager._process((new (require(`../../../cron/${payload.data.payload.id}.js`))()).init(Manager, { context: {}, }))
|
|
26
|
-
.then((res) => {
|
|
27
|
-
return resolve({data: res});
|
|
28
|
-
})
|
|
29
|
-
.catch(e => {
|
|
30
|
-
return reject(assistant.errorify(e, {code: 400}));
|
|
31
|
-
})
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
module.exports = Module;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
function Module() {
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
Module.prototype.main = function () {
|
|
6
|
-
const self = this;
|
|
7
|
-
const Manager = self.Manager;
|
|
8
|
-
const Api = self.Api;
|
|
9
|
-
const assistant = self.assistant;
|
|
10
|
-
const payload = self.payload;
|
|
11
|
-
|
|
12
|
-
return new Promise(async function(resolve, reject) {
|
|
13
|
-
// Set defaults
|
|
14
|
-
payload.data.payload.path = `${payload.data.payload.path || ''}`;
|
|
15
|
-
payload.data.payload.options = payload.data.payload.options || {};
|
|
16
|
-
|
|
17
|
-
// Perform checks
|
|
18
|
-
if (!payload.user.roles.admin) {
|
|
19
|
-
return reject(assistant.errorify(`Admin required.`, {code: 401}));
|
|
20
|
-
} else if (!payload.data.payload.path) {
|
|
21
|
-
return reject(assistant.errorify(`<path> parameter required`, {code: 400}));
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Read from Firestore
|
|
25
|
-
self.libraries.admin.database().ref(payload.data.payload.path)
|
|
26
|
-
.on('value', (snapshot) => {
|
|
27
|
-
const data = snapshot.val();
|
|
28
|
-
return resolve({data: data});
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
module.exports = Module;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
function Module() {
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
Module.prototype.main = function () {
|
|
6
|
-
const self = this;
|
|
7
|
-
const Manager = self.Manager;
|
|
8
|
-
const Api = self.Api;
|
|
9
|
-
const assistant = self.assistant;
|
|
10
|
-
const payload = self.payload;
|
|
11
|
-
|
|
12
|
-
return new Promise(async function(resolve, reject) {
|
|
13
|
-
// Set defaults
|
|
14
|
-
payload.data.payload.path = `${payload.data.payload.path || ''}`;
|
|
15
|
-
payload.data.payload.document = payload.data.payload.document || {};
|
|
16
|
-
payload.data.payload.options = payload.data.payload.options || {};
|
|
17
|
-
|
|
18
|
-
// Perform checks
|
|
19
|
-
if (payload.user.roles.admin) {
|
|
20
|
-
return reject(assistant.errorify(`Admin required.`, {code: 401}));
|
|
21
|
-
} else if (!payload.data.payload.path) {
|
|
22
|
-
return reject(assistant.errorify(`<path> parameter required`, {code: 400}));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Write to Firestore
|
|
26
|
-
self.libraries.admin.database().ref(payload.data.payload.path)
|
|
27
|
-
.set(payload.data.payload.document)
|
|
28
|
-
.then(() => {
|
|
29
|
-
return resolve({data: payload.data.payload.document});
|
|
30
|
-
})
|
|
31
|
-
.catch((e) => {
|
|
32
|
-
return reject(assistant.errorify(e, {code: 500}));
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
module.exports = Module;
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
const fetch = require('wonderful-fetch');
|
|
2
|
-
const moment = require('moment');
|
|
3
|
-
const jetpack = require('fs-jetpack');
|
|
4
|
-
const powertools = require('node-powertools');
|
|
5
|
-
const uuidv4 = require('uuid').v4;
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const { Octokit } = require('@octokit/rest');
|
|
8
|
-
|
|
9
|
-
function Module() {
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
Module.prototype.main = function () {
|
|
14
|
-
const self = this;
|
|
15
|
-
const Manager = self.Manager;
|
|
16
|
-
const Api = self.Api;
|
|
17
|
-
const assistant = self.assistant;
|
|
18
|
-
const payload = self.payload;
|
|
19
|
-
|
|
20
|
-
return new Promise(async function(resolve, reject) {
|
|
21
|
-
try {
|
|
22
|
-
// Perform checks
|
|
23
|
-
if (!payload.user.roles.admin && !payload.user.roles.blogger) {
|
|
24
|
-
return reject(assistant.errorify(`Admin required.`, {code: 401}));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Log payload
|
|
28
|
-
assistant.log(`main(): payload.data`, payload.data);
|
|
29
|
-
|
|
30
|
-
// Set now
|
|
31
|
-
const now = assistant.meta.startTime.timestamp;
|
|
32
|
-
const bemRepo = assistant.parseRepo(Manager?.config?.github?.repo_website);
|
|
33
|
-
|
|
34
|
-
// Setup Octokit
|
|
35
|
-
self.octokit = new Octokit({
|
|
36
|
-
auth: Manager?.config?.github?.key,
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// Check for required values
|
|
40
|
-
if (!payload.data.payload.url) {
|
|
41
|
-
return reject(assistant.errorify(`Missing required parameter: url`, {code: 400}));
|
|
42
|
-
} else if (!payload.data.payload.body) {
|
|
43
|
-
return reject(assistant.errorify(`Missing required parameter: body`, {code: 400}));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Set defaults
|
|
47
|
-
payload.data.payload.url = payload.data.payload.url
|
|
48
|
-
// Replace blog/
|
|
49
|
-
.replace(/blog\//ig, '')
|
|
50
|
-
// Remove leading and trailing slashes
|
|
51
|
-
.replace(/^\/|\/$/g, '')
|
|
52
|
-
// Trim
|
|
53
|
-
.trim();
|
|
54
|
-
payload.data.payload.body = payload.data.payload.body
|
|
55
|
-
.replace(powertools.regexify(`/# ${payload.data.payload.title}/i`), '')
|
|
56
|
-
.replace(/\n\n\n+/g, '\n\n')
|
|
57
|
-
.trim();
|
|
58
|
-
|
|
59
|
-
// Fix even more values
|
|
60
|
-
payload.data.payload.path = `_posts/${moment(now).format('YYYY')}/${payload.data.payload.path || 'guest'}`;
|
|
61
|
-
payload.data.payload.githubUser = payload.data.payload.githubUser || bemRepo.user;
|
|
62
|
-
payload.data.payload.githubRepo = payload.data.payload.githubRepo || bemRepo.name;
|
|
63
|
-
|
|
64
|
-
// Log
|
|
65
|
-
assistant.log(`main(): Editing post...`, payload.data.payload);
|
|
66
|
-
|
|
67
|
-
// Upload post
|
|
68
|
-
const fetchedPost = await self.fetchPost(payload.data.payload.url);
|
|
69
|
-
|
|
70
|
-
// Upload post
|
|
71
|
-
const uploadPost = await self.uploadPost(fetchedPost, payload.data.payload.body);
|
|
72
|
-
|
|
73
|
-
// Log
|
|
74
|
-
assistant.log(`main(): uploadPost`, uploadPost);
|
|
75
|
-
|
|
76
|
-
// Resolve
|
|
77
|
-
return resolve({data: payload.data.payload});
|
|
78
|
-
} catch (e) {
|
|
79
|
-
return reject(e);
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
// Fetch post
|
|
85
|
-
Module.prototype.fetchPost = function (url) {
|
|
86
|
-
const self = this;
|
|
87
|
-
|
|
88
|
-
const Manager = self.Manager;
|
|
89
|
-
const Api = self.Api;
|
|
90
|
-
const assistant = self.assistant;
|
|
91
|
-
const payload = self.payload;
|
|
92
|
-
|
|
93
|
-
return new Promise(async function(resolve, reject) {
|
|
94
|
-
fetch(`${Manager.project.functionsUrl}/bm_api`, {
|
|
95
|
-
method: 'post',
|
|
96
|
-
response: 'json',
|
|
97
|
-
timeout: 190000,
|
|
98
|
-
tries: 1,
|
|
99
|
-
body: {
|
|
100
|
-
command: 'general:fetch-post',
|
|
101
|
-
payload: {
|
|
102
|
-
url: url,
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
})
|
|
106
|
-
.then((r) => {
|
|
107
|
-
assistant.log(`fetchPost(): Result`, r);
|
|
108
|
-
|
|
109
|
-
return resolve(r);
|
|
110
|
-
})
|
|
111
|
-
.catch((e) => {
|
|
112
|
-
assistant.log(`fetchPost(): Error`, e);
|
|
113
|
-
|
|
114
|
-
return reject(e);
|
|
115
|
-
});
|
|
116
|
-
})
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
Module.prototype.uploadPost = function (fetchedPost, content) {
|
|
120
|
-
const self = this;
|
|
121
|
-
const Manager = self.Manager;
|
|
122
|
-
const Api = self.Api;
|
|
123
|
-
const assistant = self.assistant;
|
|
124
|
-
const payload = self.payload;
|
|
125
|
-
|
|
126
|
-
return new Promise(async function(resolve, reject) {
|
|
127
|
-
const filename = fetchedPost.path;
|
|
128
|
-
const sha = fetchedPost.sha;
|
|
129
|
-
const frontmatter = fetchedPost.frontmatter;
|
|
130
|
-
const owner = payload.data.payload.githubUser;
|
|
131
|
-
const repo = payload.data.payload.githubRepo;
|
|
132
|
-
|
|
133
|
-
// Combine content
|
|
134
|
-
const fullContent = '---\n'
|
|
135
|
-
+ `${frontmatter}\n`
|
|
136
|
-
+ '---\n'
|
|
137
|
-
+ '\n'
|
|
138
|
-
+ content;
|
|
139
|
-
|
|
140
|
-
// Upload post
|
|
141
|
-
await self.octokit.rest.repos.createOrUpdateFileContents({
|
|
142
|
-
owner: owner,
|
|
143
|
-
repo: repo,
|
|
144
|
-
path: filename,
|
|
145
|
-
sha: sha,
|
|
146
|
-
message: `📦 admin:edit-post:upload-post ${filename}`,
|
|
147
|
-
content: Buffer.from(fullContent).toString('base64'),
|
|
148
|
-
})
|
|
149
|
-
.then((r) => {
|
|
150
|
-
assistant.log(`uploadPost(): Result`, r);
|
|
151
|
-
|
|
152
|
-
return resolve(r);
|
|
153
|
-
})
|
|
154
|
-
.catch((e) => reject(e));
|
|
155
|
-
});
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
module.exports = Module;
|