backend-manager 3.2.169 → 3.2.171
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/LICENSE +1 -1
- package/README.md +1 -1
- package/dist/cli/cli.js +1534 -0
- package/dist/manager/functions/core/actions/api/admin/backup.js +338 -0
- package/dist/manager/functions/core/actions/api/admin/create-post.js +388 -0
- package/dist/manager/functions/core/actions/api/admin/cron.js +37 -0
- package/dist/manager/functions/core/actions/api/admin/database-read.js +35 -0
- package/dist/manager/functions/core/actions/api/admin/database-write.js +39 -0
- package/dist/manager/functions/core/actions/api/admin/edit-post.js +158 -0
- package/dist/manager/functions/core/actions/api/admin/firestore-query.js +165 -0
- package/dist/manager/functions/core/actions/api/admin/firestore-read.js +38 -0
- package/dist/manager/functions/core/actions/api/admin/firestore-write.js +54 -0
- package/dist/manager/functions/core/actions/api/admin/get-stats.js +269 -0
- package/dist/manager/functions/core/actions/api/admin/payment-processor.js +57 -0
- package/dist/manager/functions/core/actions/api/admin/run-hook.js +95 -0
- package/dist/manager/functions/core/actions/api/admin/send-notification.js +197 -0
- package/dist/manager/functions/core/actions/api/admin/sync-users.js +125 -0
- package/dist/manager/functions/core/actions/api/admin/templates/post.html +16 -0
- package/dist/manager/functions/core/actions/api/firebase/get-providers.js +102 -0
- package/dist/manager/functions/core/actions/api/general/emails/general:download-app-link.js +21 -0
- package/dist/manager/functions/core/actions/api/general/fetch-post.js +99 -0
- package/dist/manager/functions/core/actions/api/general/generate-uuid.js +41 -0
- package/dist/manager/functions/core/actions/api/general/send-email.js +112 -0
- package/dist/manager/functions/core/actions/api/handler/create-post.js +146 -0
- package/dist/manager/functions/core/actions/api/special/setup-electron-manager-client.js +103 -0
- package/dist/manager/functions/core/actions/api/template.js +33 -0
- package/dist/manager/functions/core/actions/api/test/authenticate.js +22 -0
- package/dist/manager/functions/core/actions/api/test/create-test-accounts.js +27 -0
- package/dist/manager/functions/core/actions/api/test/lab.js +55 -0
- package/dist/manager/functions/core/actions/api/test/redirect.js +26 -0
- package/dist/manager/functions/core/actions/api/test/webhook.js +30 -0
- package/dist/manager/functions/core/actions/api/user/create-custom-token.js +32 -0
- package/dist/manager/functions/core/actions/api/user/delete.js +68 -0
- package/dist/manager/functions/core/actions/api/user/get-active-sessions.js +45 -0
- package/dist/manager/functions/core/actions/api/user/get-subscription-info.js +49 -0
- package/dist/manager/functions/core/actions/api/user/oauth2/discord.js +114 -0
- package/dist/manager/functions/core/actions/api/user/oauth2/google.js +99 -0
- package/dist/manager/functions/core/actions/api/user/oauth2.js +476 -0
- package/dist/manager/functions/core/actions/api/user/regenerate-api-keys.js +54 -0
- package/dist/manager/functions/core/actions/api/user/resolve.js +32 -0
- package/dist/manager/functions/core/actions/api/user/sign-out-all-sessions.js +118 -0
- package/dist/manager/functions/core/actions/api/user/sign-up copy.js +544 -0
- package/dist/manager/functions/core/actions/api/user/sign-up.js +99 -0
- package/dist/manager/functions/core/actions/api/user/submit-feedback.js +96 -0
- package/dist/manager/functions/core/actions/api/user/validate-settings.js +86 -0
- package/dist/manager/functions/core/actions/api.js +354 -0
- package/dist/manager/functions/core/actions/create-post-handler.js +184 -0
- package/dist/manager/functions/core/actions/generate-uuid.js +62 -0
- package/dist/manager/functions/core/actions/sign-up-handler.js +205 -0
- package/dist/manager/functions/core/admin/create-post.js +206 -0
- package/dist/manager/functions/core/admin/firestore-write.js +72 -0
- package/dist/manager/functions/core/admin/get-stats.js +218 -0
- package/dist/manager/functions/core/admin/query.js +198 -0
- package/dist/manager/functions/core/admin/send-notification.js +206 -0
- package/dist/manager/functions/core/cron/daily/ghostii-auto-publisher.js +377 -0
- package/dist/manager/functions/core/cron/daily/reset-usage.js +197 -0
- package/dist/manager/functions/core/cron/daily.js +114 -0
- package/dist/manager/functions/core/events/auth/before-create.js +124 -0
- package/dist/manager/functions/core/events/auth/before-signin.js +62 -0
- package/dist/manager/functions/core/events/auth/on-create copy.js +121 -0
- package/dist/manager/functions/core/events/auth/on-create.js +564 -0
- package/dist/manager/functions/core/events/auth/on-delete.js +72 -0
- package/dist/manager/functions/core/events/firestore/on-subscription.js +107 -0
- package/dist/manager/functions/test/authenticate.js +38 -0
- package/dist/manager/functions/test/create-test-accounts.js +144 -0
- package/dist/manager/functions/test/webhook.js +37 -0
- package/dist/manager/functions/wrappers/mailchimp/addToList.js +25 -0
- package/dist/manager/helpers/analytics copy.js +217 -0
- package/dist/manager/helpers/analytics.js +467 -0
- package/dist/manager/helpers/api-manager.js +324 -0
- package/dist/manager/helpers/assistant.js +1043 -0
- package/dist/manager/helpers/metadata.js +32 -0
- package/dist/manager/helpers/middleware.js +154 -0
- package/dist/manager/helpers/roles.js +69 -0
- package/dist/manager/helpers/settings.js +158 -0
- package/dist/manager/helpers/subscription-resolver-new.js +828 -0
- package/dist/manager/helpers/subscription-resolver.js +842 -0
- package/dist/manager/helpers/usage.js +381 -0
- package/dist/manager/helpers/user.js +198 -0
- package/dist/manager/helpers/utilities.js +292 -0
- package/dist/manager/index.js +1076 -0
- package/dist/manager/libraries/openai.js +460 -0
- package/dist/manager/routes/restart/index.js +52 -0
- package/dist/manager/routes/test/index.js +43 -0
- package/dist/manager/schemas/restart.js +13 -0
- package/dist/manager/schemas/test.js +13 -0
- package/dist/require.js +3 -0
- package/package.json +19 -9
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
let nanoId;
|
|
2
|
+
let _;
|
|
3
|
+
|
|
4
|
+
function Utilities(Manager) {
|
|
5
|
+
const self = this;
|
|
6
|
+
|
|
7
|
+
_ = require('lodash');
|
|
8
|
+
|
|
9
|
+
self.cache = {};
|
|
10
|
+
|
|
11
|
+
self.Manager = Manager;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
Utilities.prototype.iterateCollection = function (callback, options) {
|
|
15
|
+
const self = this;
|
|
16
|
+
const Manager = self.Manager;
|
|
17
|
+
const admin = Manager.libraries.admin;
|
|
18
|
+
|
|
19
|
+
return new Promise(function(resolve, reject) {
|
|
20
|
+
let batch = -1;
|
|
21
|
+
let collectionCount = 0;
|
|
22
|
+
|
|
23
|
+
// Set defaults
|
|
24
|
+
options = options || {};
|
|
25
|
+
options.collection = options.collection || '';
|
|
26
|
+
options.batchSize = options.batchSize || 1000;
|
|
27
|
+
options.where = options.where || [];
|
|
28
|
+
options.orderBy = options.orderBy || null;
|
|
29
|
+
options.startAt = options.startAt || null;
|
|
30
|
+
options.startAfter = options.startAfter || null;
|
|
31
|
+
options.prefetchCursor = typeof options.prefetchCursor === 'undefined'
|
|
32
|
+
? (!!options.startAt || !!options.startAfter) && !options.orderBy
|
|
33
|
+
: options.prefetchCursor;
|
|
34
|
+
options.log = options.log;
|
|
35
|
+
|
|
36
|
+
// List all documents in a collection
|
|
37
|
+
async function listAllDocuments(nextPageToken) {
|
|
38
|
+
let query = admin.firestore().collection(options.collection);
|
|
39
|
+
|
|
40
|
+
// Insert where clauses
|
|
41
|
+
options.where
|
|
42
|
+
.forEach(clause => {
|
|
43
|
+
query = query.where(clause.field, clause.operator, clause.value);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Insert orderBy
|
|
47
|
+
if (options.orderBy) {
|
|
48
|
+
query = query.orderBy(options.orderBy);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Process the first batch differently
|
|
52
|
+
if (batch === -1) {
|
|
53
|
+
let prefetchedCursor = null;
|
|
54
|
+
|
|
55
|
+
// Prefetch the cursor
|
|
56
|
+
if (options.prefetchCursor) {
|
|
57
|
+
prefetchedCursor = await admin.firestore().doc(`${options.collection}/${options.startAt || options.startAfter}`)
|
|
58
|
+
.get()
|
|
59
|
+
.catch(e => e);
|
|
60
|
+
|
|
61
|
+
if (prefetchedCursor instanceof Error) {
|
|
62
|
+
return reject(prefetchedCursor);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Insert startAt or startAfter
|
|
67
|
+
if (options.startAt) {
|
|
68
|
+
query = query.startAt(prefetchedCursor || options.startAt);
|
|
69
|
+
} else if (options.startAfter) {
|
|
70
|
+
query = query.startAfter(prefetchedCursor || options.startAfter);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Calculate count
|
|
74
|
+
const collectionCountResult = await query.count().get()
|
|
75
|
+
.then((r) => r.data().count)
|
|
76
|
+
.catch((e) => e);
|
|
77
|
+
|
|
78
|
+
// Check for errors
|
|
79
|
+
if (collectionCountResult instanceof Error) {
|
|
80
|
+
return reject(collectionCountResult);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Set collection count
|
|
84
|
+
collectionCount = collectionCountResult;
|
|
85
|
+
|
|
86
|
+
// Log
|
|
87
|
+
if (options.log) {
|
|
88
|
+
console.log('Total count:', collectionCount);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Start at next page
|
|
93
|
+
if (nextPageToken) {
|
|
94
|
+
query = query.startAfter(nextPageToken);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// batchSize
|
|
98
|
+
query = query.limit(options.batchSize);
|
|
99
|
+
|
|
100
|
+
// Get
|
|
101
|
+
query.get()
|
|
102
|
+
.then(async (snap) => {
|
|
103
|
+
const lastVisible = snap.docs[snap.docs.length - 1];
|
|
104
|
+
|
|
105
|
+
batch++;
|
|
106
|
+
|
|
107
|
+
if (snap.docs.length === 0) {
|
|
108
|
+
return resolve();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (options.log) {
|
|
112
|
+
console.log('Processing batch:', batch);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
callback(
|
|
116
|
+
{
|
|
117
|
+
snap: snap,
|
|
118
|
+
docs: snap.docs.map(x => x),
|
|
119
|
+
},
|
|
120
|
+
batch,
|
|
121
|
+
collectionCount,
|
|
122
|
+
)
|
|
123
|
+
.then((r) => {
|
|
124
|
+
// Construct a new query starting at this document
|
|
125
|
+
if (lastVisible) {
|
|
126
|
+
listAllDocuments(lastVisible)
|
|
127
|
+
} else {
|
|
128
|
+
return resolve();
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
.catch((e) => {
|
|
132
|
+
console.error('Callback failed', e);
|
|
133
|
+
return reject(e);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
})
|
|
137
|
+
.catch((e) => {
|
|
138
|
+
console.error('Query failed', e);
|
|
139
|
+
return reject(e);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Run
|
|
144
|
+
listAllDocuments();
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
Utilities.prototype.iterateUsers = function (callback, options) {
|
|
149
|
+
const self = this;
|
|
150
|
+
const Manager = self.Manager;
|
|
151
|
+
const admin = Manager.libraries.admin;
|
|
152
|
+
|
|
153
|
+
return new Promise(function(resolve, reject) {
|
|
154
|
+
let batch = -1;
|
|
155
|
+
|
|
156
|
+
options = options || {};
|
|
157
|
+
options.batchSize = options.batchSize || 1000;
|
|
158
|
+
options.log = options.log;
|
|
159
|
+
options.pageToken = options.pageToken;
|
|
160
|
+
|
|
161
|
+
function listAllUsers(nextPageToken) {
|
|
162
|
+
// List batch of users, 1000 at a time.
|
|
163
|
+
admin.auth()
|
|
164
|
+
.listUsers(options.batchSize, nextPageToken)
|
|
165
|
+
.then(async (listUsersResult) => {
|
|
166
|
+
|
|
167
|
+
batch++;
|
|
168
|
+
|
|
169
|
+
if (listUsersResult.users.length === 0) {
|
|
170
|
+
return resolve();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (options.log) {
|
|
174
|
+
console.log('Processing batch:', batch);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
callback({
|
|
178
|
+
snap: listUsersResult,
|
|
179
|
+
users: listUsersResult.users,
|
|
180
|
+
pageToken: listUsersResult.pageToken,
|
|
181
|
+
}, batch)
|
|
182
|
+
.then(r => {
|
|
183
|
+
if (listUsersResult.pageToken) {
|
|
184
|
+
listAllUsers(listUsersResult.pageToken);
|
|
185
|
+
} else {
|
|
186
|
+
return resolve();
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
.catch((e) => {
|
|
190
|
+
console.error('Callback failed', e);
|
|
191
|
+
return reject(e)
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
})
|
|
195
|
+
.catch((e) => {
|
|
196
|
+
console.error('Query failed', e);
|
|
197
|
+
return reject(e)
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
listAllUsers(options.pageToken);
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
Utilities.prototype.randomId = function (options) {
|
|
206
|
+
const self = this;
|
|
207
|
+
|
|
208
|
+
options = options || {};
|
|
209
|
+
options.size = options.size || 14;
|
|
210
|
+
|
|
211
|
+
if (!nanoId) {
|
|
212
|
+
nanoId = require('nanoid');
|
|
213
|
+
|
|
214
|
+
nanoId = nanoId.customAlphabet(
|
|
215
|
+
nanoId.urlAlphabet.replace(/_|-/g, ''),
|
|
216
|
+
options.size,
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return nanoId();
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
Utilities.prototype.get = function (docPath, options) {
|
|
224
|
+
const self = this;
|
|
225
|
+
|
|
226
|
+
return new Promise(function(resolve, reject) {
|
|
227
|
+
const Manager = self.Manager;
|
|
228
|
+
const { admin } = Manager.libraries;
|
|
229
|
+
|
|
230
|
+
// Set defaults
|
|
231
|
+
options = options || {};
|
|
232
|
+
options.maxAge = options.maxAge || (1000 * 60 * 5); // 5 minutes
|
|
233
|
+
options.readTime = typeof options.readTime === 'undefined' ? null : options.readTime;
|
|
234
|
+
options.log = typeof options.log === 'undefined' ? false : options.log;
|
|
235
|
+
options.format = typeof options.format === 'undefined' ? 'raw' : options.format;
|
|
236
|
+
|
|
237
|
+
// Check cache
|
|
238
|
+
const item = _.get(self.cache, docPath, null)
|
|
239
|
+
const age = item ? Date.now() - item.time : null;
|
|
240
|
+
|
|
241
|
+
// Format
|
|
242
|
+
function _format(doc) {
|
|
243
|
+
if (options.format === 'raw') {
|
|
244
|
+
return doc;
|
|
245
|
+
} else if (options.format === 'data') {
|
|
246
|
+
return doc.data();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Log
|
|
251
|
+
if (options.readTime) {
|
|
252
|
+
const { Timestamp } = require('firebase-admin/firestore')
|
|
253
|
+
const time = Math.round(new Date(options.readTime).getTime() / 1000 / 60);
|
|
254
|
+
const timeLog = new Date(time * 1000 * 60);
|
|
255
|
+
|
|
256
|
+
// Log
|
|
257
|
+
if (options.log) {
|
|
258
|
+
console.log('Read time:', timeLog);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Loop docs
|
|
262
|
+
admin.firestore().runTransaction(
|
|
263
|
+
updateFunction => updateFunction.get(admin.firestore().doc(docPath)),
|
|
264
|
+
{readOnly: true, readTime: new Timestamp(time * 60, 0)}
|
|
265
|
+
)
|
|
266
|
+
.then((snap) => resolve(_format(snap)))
|
|
267
|
+
.catch((e) => reject(e));
|
|
268
|
+
} else if (item && age && age < options.maxAge) {
|
|
269
|
+
return resolve(_format(item.doc));
|
|
270
|
+
} else {
|
|
271
|
+
admin.firestore().doc(docPath)
|
|
272
|
+
.get()
|
|
273
|
+
.then(async (doc) => {
|
|
274
|
+
const data = doc.data();
|
|
275
|
+
|
|
276
|
+
// Set cache
|
|
277
|
+
if (data) {
|
|
278
|
+
_.set(self.cache, docPath, {
|
|
279
|
+
doc: doc,
|
|
280
|
+
time: Date.now(),
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Return
|
|
285
|
+
return resolve(_format(doc));
|
|
286
|
+
})
|
|
287
|
+
.catch((e) => reject(e));
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
module.exports = Utilities;
|