backend-manager 3.2.170 → 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.
Files changed (88) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/dist/cli/cli.js +1534 -0
  4. package/dist/manager/functions/core/actions/api/admin/backup.js +338 -0
  5. package/dist/manager/functions/core/actions/api/admin/create-post.js +388 -0
  6. package/dist/manager/functions/core/actions/api/admin/cron.js +37 -0
  7. package/dist/manager/functions/core/actions/api/admin/database-read.js +35 -0
  8. package/dist/manager/functions/core/actions/api/admin/database-write.js +39 -0
  9. package/dist/manager/functions/core/actions/api/admin/edit-post.js +158 -0
  10. package/dist/manager/functions/core/actions/api/admin/firestore-query.js +165 -0
  11. package/dist/manager/functions/core/actions/api/admin/firestore-read.js +38 -0
  12. package/dist/manager/functions/core/actions/api/admin/firestore-write.js +54 -0
  13. package/dist/manager/functions/core/actions/api/admin/get-stats.js +269 -0
  14. package/dist/manager/functions/core/actions/api/admin/payment-processor.js +57 -0
  15. package/dist/manager/functions/core/actions/api/admin/run-hook.js +95 -0
  16. package/dist/manager/functions/core/actions/api/admin/send-notification.js +197 -0
  17. package/dist/manager/functions/core/actions/api/admin/sync-users.js +125 -0
  18. package/dist/manager/functions/core/actions/api/admin/templates/post.html +16 -0
  19. package/dist/manager/functions/core/actions/api/firebase/get-providers.js +102 -0
  20. package/dist/manager/functions/core/actions/api/general/emails/general:download-app-link.js +21 -0
  21. package/dist/manager/functions/core/actions/api/general/fetch-post.js +99 -0
  22. package/dist/manager/functions/core/actions/api/general/generate-uuid.js +41 -0
  23. package/dist/manager/functions/core/actions/api/general/send-email.js +112 -0
  24. package/dist/manager/functions/core/actions/api/handler/create-post.js +146 -0
  25. package/dist/manager/functions/core/actions/api/special/setup-electron-manager-client.js +103 -0
  26. package/dist/manager/functions/core/actions/api/template.js +33 -0
  27. package/dist/manager/functions/core/actions/api/test/authenticate.js +22 -0
  28. package/dist/manager/functions/core/actions/api/test/create-test-accounts.js +27 -0
  29. package/dist/manager/functions/core/actions/api/test/lab.js +55 -0
  30. package/dist/manager/functions/core/actions/api/test/redirect.js +26 -0
  31. package/dist/manager/functions/core/actions/api/test/webhook.js +30 -0
  32. package/dist/manager/functions/core/actions/api/user/create-custom-token.js +32 -0
  33. package/dist/manager/functions/core/actions/api/user/delete.js +68 -0
  34. package/dist/manager/functions/core/actions/api/user/get-active-sessions.js +45 -0
  35. package/dist/manager/functions/core/actions/api/user/get-subscription-info.js +49 -0
  36. package/dist/manager/functions/core/actions/api/user/oauth2/discord.js +114 -0
  37. package/dist/manager/functions/core/actions/api/user/oauth2/google.js +99 -0
  38. package/dist/manager/functions/core/actions/api/user/oauth2.js +476 -0
  39. package/dist/manager/functions/core/actions/api/user/regenerate-api-keys.js +54 -0
  40. package/dist/manager/functions/core/actions/api/user/resolve.js +32 -0
  41. package/dist/manager/functions/core/actions/api/user/sign-out-all-sessions.js +118 -0
  42. package/dist/manager/functions/core/actions/api/user/sign-up copy.js +544 -0
  43. package/dist/manager/functions/core/actions/api/user/sign-up.js +99 -0
  44. package/dist/manager/functions/core/actions/api/user/submit-feedback.js +96 -0
  45. package/dist/manager/functions/core/actions/api/user/validate-settings.js +86 -0
  46. package/dist/manager/functions/core/actions/api.js +354 -0
  47. package/dist/manager/functions/core/actions/create-post-handler.js +184 -0
  48. package/dist/manager/functions/core/actions/generate-uuid.js +62 -0
  49. package/dist/manager/functions/core/actions/sign-up-handler.js +205 -0
  50. package/dist/manager/functions/core/admin/create-post.js +206 -0
  51. package/dist/manager/functions/core/admin/firestore-write.js +72 -0
  52. package/dist/manager/functions/core/admin/get-stats.js +218 -0
  53. package/dist/manager/functions/core/admin/query.js +198 -0
  54. package/dist/manager/functions/core/admin/send-notification.js +206 -0
  55. package/dist/manager/functions/core/cron/daily/ghostii-auto-publisher.js +377 -0
  56. package/dist/manager/functions/core/cron/daily/reset-usage.js +197 -0
  57. package/dist/manager/functions/core/cron/daily.js +114 -0
  58. package/dist/manager/functions/core/events/auth/before-create.js +124 -0
  59. package/dist/manager/functions/core/events/auth/before-signin.js +62 -0
  60. package/dist/manager/functions/core/events/auth/on-create copy.js +121 -0
  61. package/dist/manager/functions/core/events/auth/on-create.js +564 -0
  62. package/dist/manager/functions/core/events/auth/on-delete.js +72 -0
  63. package/dist/manager/functions/core/events/firestore/on-subscription.js +107 -0
  64. package/dist/manager/functions/test/authenticate.js +38 -0
  65. package/dist/manager/functions/test/create-test-accounts.js +144 -0
  66. package/dist/manager/functions/test/webhook.js +37 -0
  67. package/dist/manager/functions/wrappers/mailchimp/addToList.js +25 -0
  68. package/dist/manager/helpers/analytics copy.js +217 -0
  69. package/dist/manager/helpers/analytics.js +467 -0
  70. package/dist/manager/helpers/api-manager.js +324 -0
  71. package/dist/manager/helpers/assistant.js +1043 -0
  72. package/dist/manager/helpers/metadata.js +32 -0
  73. package/dist/manager/helpers/middleware.js +154 -0
  74. package/dist/manager/helpers/roles.js +69 -0
  75. package/dist/manager/helpers/settings.js +158 -0
  76. package/dist/manager/helpers/subscription-resolver-new.js +828 -0
  77. package/dist/manager/helpers/subscription-resolver.js +842 -0
  78. package/dist/manager/helpers/usage.js +381 -0
  79. package/dist/manager/helpers/user.js +198 -0
  80. package/dist/manager/helpers/utilities.js +292 -0
  81. package/dist/manager/index.js +1076 -0
  82. package/dist/manager/libraries/openai.js +460 -0
  83. package/dist/manager/routes/restart/index.js +52 -0
  84. package/dist/manager/routes/test/index.js +43 -0
  85. package/dist/manager/schemas/restart.js +13 -0
  86. package/dist/manager/schemas/test.js +13 -0
  87. package/dist/require.js +3 -0
  88. package/package.json +18 -8
@@ -0,0 +1,32 @@
1
+ const moment = require('moment');
2
+ const uuidv4 = require('uuid').v4;
3
+
4
+ function Metadata(Manager, document) {
5
+ const self = this;
6
+
7
+ self.Manager = Manager;
8
+
9
+ self.document = document || {};
10
+
11
+ return self;
12
+ }
13
+
14
+ Metadata.prototype.set = function (metadata) {
15
+ const self = this;
16
+
17
+ const now = moment();
18
+
19
+ self.document = self.document || {};
20
+ self.document.metadata = self.document.metadata || {};
21
+
22
+ self.document.metadata.updated = self.document.metadata.updated || {};
23
+ self.document.metadata.updated.timestamp = now.toISOString();
24
+ self.document.metadata.updated.timestampUNIX = now.unix();
25
+ self.document.metadata.tag = metadata.tag || uuidv4();
26
+
27
+ self.Manager.assistant.log(`Metadata: #${self.document.metadata.tag}`);
28
+
29
+ return self.document.metadata;
30
+ };
31
+
32
+ module.exports = Metadata;
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Middleware
3
+ * Used to handle middleware for the assistant
4
+ */
5
+
6
+ const path = require('path');
7
+ const powertools = require('node-powertools');
8
+ const { merge } = require('lodash');
9
+
10
+ function Middleware(m, req, res) {
11
+ const self = this;
12
+
13
+ self.Manager = m;
14
+ self.req = req;
15
+ self.res = res;
16
+ }
17
+
18
+ Middleware.prototype.run = function (libPath, options) {
19
+ const self = this;
20
+
21
+ // Set shortcuts
22
+ const Manager = self.Manager;
23
+ const req = self.req;
24
+ const res = self.res;
25
+ const { cors } = Manager.libraries;
26
+
27
+ return cors(req, res, async () => {
28
+ const assistant = Manager.Assistant({req: req, res: res});
29
+
30
+ const data = assistant.request.data;
31
+ const geolocation = assistant.request.geolocation;
32
+ const client = assistant.request.client;
33
+
34
+ // Set options
35
+ options = options || {};
36
+ options.authenticate = typeof options.authenticate === 'boolean' ? options.authenticate : true;
37
+ options.setupAnalytics = typeof options.setupAnalytics === 'boolean' ? options.setupAnalytics : true;
38
+ options.setupUsage = typeof options.setupUsage === 'boolean' ? options.setupUsage : true;
39
+ options.setupSettings = typeof options.setupSettings === 'undefined' ? true : options.setupSettings;
40
+ options.cleanSettings = typeof options.cleanSettings === 'undefined' ? true : options.cleanSettings;
41
+ options.includeNonSchemaSettings = typeof options.includeNonSchemaSettings === 'undefined' ? false : options.includeNonSchemaSettings;
42
+ options.schema = typeof options.schema === 'undefined' ? undefined : options.schema;
43
+
44
+ // Set base path
45
+ options.routesDir = typeof options.routesDir === 'undefined' ? `${Manager.cwd}/routes` : options.routesDir;
46
+ options.schemasDir = typeof options.schemasDir === 'undefined' ? `${Manager.cwd}/schemas` : options.schemasDir;
47
+
48
+ // Log
49
+ assistant.log(`Middleware.process(): Request (${geolocation.ip} @ ${geolocation.country}, ${geolocation.region}, ${geolocation.city})`, JSON.stringify(data));
50
+
51
+ // Set paths
52
+ const routesDir = path.resolve(options.routesDir, libPath.replace('.js', ''));
53
+ const schemasDir = path.resolve(options.schemasDir);
54
+
55
+ // Wakeup trigger (quit immediately if wakeup is true to avoid cold start on a future request)
56
+ if (data.wakeup) {
57
+ assistant.log(`Middleware.process(): Wakeup activated at ${new Date().toISOString()}`);
58
+
59
+ return assistant.respond({wakeup: true});
60
+ }
61
+
62
+ // Load library
63
+ let library;
64
+ try {
65
+ libPath = path.resolve(routesDir, `index.js`);
66
+ library = new (require(libPath))();
67
+ } catch (e) {
68
+ return assistant.respond(new Error(`Unable to load library @ (${libPath}): ${e.message}`), {code: 500, sentry: true});
69
+ }
70
+
71
+ // Setup user
72
+ if (!options.setupUsage && options.authenticate) {
73
+ await assistant.authenticate();
74
+ }
75
+
76
+ // Setup usage
77
+ if (options.setupUsage) {
78
+ // assistant.usage = await Manager.Usage().init(assistant, {log: assistant.isProduction()});
79
+ assistant.usage = await Manager.Usage().init(assistant, {log: false});
80
+ }
81
+
82
+ // Log working user
83
+ const workingUser = assistant.getUser();
84
+ assistant.log(`Middleware.process(): User (${workingUser.auth.uid}, ${workingUser.auth.email}, ${workingUser.plan.id}=${workingUser.plan.status}):`, JSON.stringify(workingUser));
85
+
86
+ // Setup analytics
87
+ if (options.setupAnalytics) {
88
+ const uuid = assistant?.usage?.user?.auth?.uid
89
+ || assistant.request.user.auth.uid
90
+ || assistant.request.geolocation.ip
91
+
92
+ assistant.analytics = Manager.Analytics({
93
+ assistant: assistant,
94
+ uuid: uuid,
95
+ });
96
+ }
97
+
98
+ // Resolve settings
99
+ if (options.setupSettings) {
100
+ // assistant.log(`Middleware.process(): Resolving settings with schema ${options.schema}...`);
101
+
102
+ // Resolve settings
103
+ try {
104
+ // assistant.settings = Manager.Settings().resolve(assistant, options.schema, data);
105
+ assistant.settings = Manager.Settings().resolve(assistant, undefined, data, {dir: schemasDir, schema: options.schema});
106
+ } catch (e) {
107
+ return assistant.respond(new Error(`Unable to resolve schema ${options.schema}: ${e.message}`), {code: 500, sentry: true});
108
+ }
109
+
110
+ // Merge settings with data
111
+ if (options.includeNonSchemaSettings) {
112
+ assistant.settings = merge(data, assistant.settings)
113
+ }
114
+
115
+ // Clean settings by looping through and trimming all strings
116
+ if (options.cleanSettings) {
117
+ clean(assistant.settings);
118
+ }
119
+
120
+ // Log
121
+ assistant.log(`Middleware.process(): Resolved settings with schema ${options.schema}`, JSON.stringify(assistant.settings));
122
+ } else {
123
+ assistant.settings = data;
124
+ }
125
+
126
+ // Process
127
+ try {
128
+ // Set properties
129
+ library.Manager = Manager;
130
+ library.assistant = assistant;
131
+
132
+ // Run library
133
+ library.main(assistant, req, res)
134
+ .catch(e => {
135
+ return assistant.respond(e, {code: e.code, sentry: true});
136
+ });
137
+ } catch (e) {
138
+ return assistant.respond(e, {code: e.code, sentry: true});
139
+ }
140
+ });
141
+ };
142
+
143
+ function clean(obj) {
144
+ for (let key in obj) {
145
+ if (typeof obj[key] === 'object') {
146
+ clean(obj[key]);
147
+ } else if (typeof obj[key] === 'string') {
148
+ obj[key] = obj[key]
149
+ .trim();
150
+ }
151
+ }
152
+ }
153
+
154
+ module.exports = Middleware;
@@ -0,0 +1,69 @@
1
+ function Roles(Manager) {
2
+ const self = this;
3
+
4
+ self.Manager = Manager;
5
+ }
6
+
7
+ Roles.prototype.list = function () {
8
+ const self = this;
9
+
10
+ return [
11
+ // Staff
12
+ {
13
+ id: 'staff',
14
+ name: 'Staff',
15
+ regex: /(^staff$)/ig,
16
+ },
17
+ {
18
+ id: 'admin',
19
+ name: 'Administrator',
20
+ regex: /(^administrator$|^admin$)/ig,
21
+ },
22
+ {
23
+ id: 'moderator',
24
+ name: 'Moderator',
25
+ regex: /(^moderator$|^mod$)/ig,
26
+ },
27
+ {
28
+ id: 'moderatorJr',
29
+ name: 'Jr. Moderator',
30
+ regex: /(^moderatorjr$|^modjr$)/ig,
31
+ },
32
+ {
33
+ id: 'blogger',
34
+ name: 'Blogger',
35
+ regex: /(^blogger$|^blog$)/ig,
36
+ },
37
+ {
38
+ id: 'developer',
39
+ name: 'Developer',
40
+ regex: /(^developer$|^dev$)/ig,
41
+ },
42
+
43
+ // Features
44
+ {
45
+ id: 'betaTester',
46
+ name: 'Beta Tester',
47
+ regex: /(^betatester$|^beta$)/ig,
48
+ },
49
+ {
50
+ id: 'serverBooster',
51
+ name: 'Server Booster',
52
+ regex: /(^serverbooster$|^booster$)/ig,
53
+ },
54
+
55
+ // Vanity
56
+ {
57
+ id: 'og',
58
+ name: 'OG',
59
+ regex: /(^og$)/ig,
60
+ },
61
+ {
62
+ id: 'vip',
63
+ name: 'VIP',
64
+ regex: /(^vip)/ig,
65
+ },
66
+ ]
67
+ };
68
+
69
+ module.exports = Roles;
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Settings
3
+ *
4
+ */
5
+ // const jetpack = require('fs-jetpack');
6
+ const path = require('path');
7
+ const powertools = require('node-powertools');
8
+ const _ = require('lodash');
9
+ const moment = require('moment');
10
+
11
+ function Settings(m) {
12
+ const self = this;
13
+
14
+ self.Manager = m;
15
+
16
+ self.settings = null;
17
+ }
18
+
19
+ Settings.prototype.resolve = function (assistant, schema, settings, options) {
20
+ const self = this;
21
+ const Manager = self.Manager;
22
+
23
+ // Set settings
24
+ schema = schema || undefined;
25
+ settings = settings || {};
26
+
27
+ // Set options
28
+ options = options || {};
29
+ options.dir = typeof options.dir === 'undefined' ? `${Manager.cwd}/schemas` : options.dir;
30
+ options.schema = typeof options.schema === 'undefined' ? undefined : options.schema;
31
+
32
+ // Load schema if not provided and schema is defined in options
33
+ // console.log('----schema:', schema);
34
+ // console.log('----settings:', settings);
35
+ // console.log('----options.dir:', options.dir);
36
+ // console.log('----options.schema:', options.schema);
37
+ if (
38
+ typeof schema === 'undefined'
39
+ && typeof options.schema !== 'undefined'
40
+ ) {
41
+ const schemaPath = path.resolve(options.dir, `${options.schema.replace('.js', '')}.js`);
42
+
43
+ schema = loadSchema(assistant, schemaPath, settings);
44
+ }
45
+
46
+ // Resolve settings
47
+ self.settings = powertools.defaults(settings, schema);
48
+
49
+ // If schema is not an object, throw an error
50
+ if (!schema || typeof schema !== 'object') {
51
+ throw assistant.errorify(`Invalid schema provided`, {code: 400});
52
+ }
53
+ // console.log('---schema', schema);
54
+ // console.log('---self.settings', self.settings);
55
+
56
+ // Iterate each key and check for some things
57
+ processSchema(schema, (path, schemaNode) => {
58
+ const originalValue = _.get(settings, path);
59
+ const resolvedValue = _.get(self.settings, path);
60
+ let replaceValue = undefined;
61
+
62
+ // assistant.log('Found:', path, schemaNode);
63
+ // assistant.log('originalValue:', originalValue);
64
+ // assistant.log('resolvedValue:', resolvedValue);
65
+
66
+ // Check if this node is marked as required
67
+ let isRequired = false;
68
+ if (typeof schemaNode.required === 'function') {
69
+ isRequired = schemaNode.required(assistant);
70
+ } else if (typeof schemaNode.required === 'boolean') {
71
+ isRequired = schemaNode.required;
72
+ }
73
+
74
+ // assistant.log('isRequired:', isRequired);
75
+
76
+ // If the key is required and the original value is undefined, throw an error
77
+ if (isRequired && typeof originalValue === 'undefined') {
78
+ throw assistant.errorify(`Required key {${path}} is missing in settings`, {code: 400});
79
+ }
80
+
81
+ // Clean
82
+ if (schemaNode.clean) {
83
+ if (schemaNode.clean instanceof RegExp) {
84
+ replaceValue = resolvedValue.replace(schemaNode.clean, '');
85
+ } else if (typeof schemaNode.clean === 'function') {
86
+ replaceValue = schemaNode.clean(resolvedValue);
87
+ }
88
+ }
89
+
90
+ // assistant.log('replaceValue:', replaceValue);
91
+
92
+ // Replace
93
+ if (typeof replaceValue !== 'undefined' && replaceValue !== resolvedValue) {
94
+ assistant.warn(`Replacing ${path}: originalValue=${originalValue}, resolvedValue=${resolvedValue}, replaceValue=${replaceValue}`);
95
+ _.set(self.settings, path, replaceValue);
96
+ }
97
+ });
98
+
99
+ // Resolve
100
+ return self.settings;
101
+ };
102
+
103
+ Settings.prototype.constant = function (name, options) {
104
+ const self = this;
105
+ const Manager = self.Manager;
106
+
107
+ options = options || {};
108
+ options.date = typeof options.date === 'undefined' ? moment() : moment(options.date);
109
+
110
+ if (name === 'timestamp') {
111
+ return {
112
+ types: ['string'],
113
+ value: undefined,
114
+ default: options.date.toISOString(),
115
+ }
116
+ } else if (name === 'timestampUNIX') {
117
+ return {
118
+ types: ['number'],
119
+ value: undefined,
120
+ default: options.date.unix(),
121
+ }
122
+ } else if (name === 'timestampFULL') {
123
+ return {
124
+ timestamp: self.constant('timestamp', options),
125
+ timestampUNIX: self.constant('timestampUNIX', options),
126
+ }
127
+ }
128
+ };
129
+
130
+ function processSchema(schema, fn, path) {
131
+ path = path || '';
132
+
133
+ // Base case: Check if the current level has 'types' and 'default', indicating metadata
134
+ if (schema.hasOwnProperty('types') && schema.hasOwnProperty('default')) {
135
+ // Call the processing function with the current path and schema as arguments
136
+ fn(path, schema);
137
+ return;
138
+ }
139
+
140
+ // Recursive case: Iterate through nested keys if we're not at a metadata node
141
+ Object.keys(schema).forEach(key => {
142
+ const nextPath = path ? `${path}.${key}` : key;
143
+ processSchema(schema[key], fn, nextPath);
144
+ });
145
+ }
146
+
147
+ function loadSchema(assistant, schema, settings) {
148
+ const planId = assistant.request.user.plan.id;
149
+
150
+ const lib = require(schema)(assistant);
151
+ const def = lib.defaults;
152
+ const plan = lib[planId];
153
+
154
+ // Merge
155
+ return _.merge({}, def, plan);
156
+ }
157
+
158
+ module.exports = Settings;