-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (147 loc) · 4.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const {promiseError} = require('@kwsites/promise-result');
const debug = require('debug');
const {get, wrap} = require('lodash');
module.exports = {
extend: 'apostrophe-widgets',
defer: true,
label: 'Instagram Widget',
verbose: false,
galleryUserName: '',
addFields: [
{
type: 'string',
name: 'title',
label: 'Title'
},
{
type: 'select',
name: 'style',
label: 'Widget Type',
choices: [
{
label: 'Latest',
value: 'latest',
showFields: [
'username', 'columns', 'limit',
],
},
{
label: 'Single',
value: 'single',
showFields: [
'url',
],
},
],
},
{
type: 'string',
name: 'username',
label: 'User name'
},
{
type: 'integer',
name: 'columns',
label: 'Grid Column Count',
def: 3
},
{
type: 'integer',
name: 'limit',
label: 'Max image display',
def: -1
},
{
type: 'string',
name: 'url',
label: 'Instagram Post URL',
required: true,
},
],
beforeConstruct (self, options) {
self.log = debug(options.logName || self.__meta.name);
if (options.verbose) {
debug.enable(`${ self.__meta.name }:*`);
}
},
afterConstruct (self) {
self.enableCache();
require('./lib/instagram-oembetter')(self, self.apos.oembed.oembetter);
},
construct (self, options) {
self.isWorkingOffline = () => get(self, 'apos.options.locals.offline') === true;
self.getAuthConfig = (req) => self.getOption(req, 'auth');
require('./lib/instagram-profile-cache')(self, options);
require('./lib/instagram-anon-profile-loader')(self, options);
require('./lib/instagram-auth-profile-loader')(self, options);
require('./lib/latest-gallery-widget')(self, options);
require('./lib/single-embed-widget')(self, options);
self.pushAssets = wrap(self.pushAssets, (superFn) => {
self.pushAsset('stylesheet', 'always', {when: 'always', data: true});
superFn();
});
self.load = wrap(self.load, load);
self.sanitize = wrap(self.sanitize, sanitize);
function sanitize (superFn, req, input, callback) {
const log = self.log.extend('sanitize');
superFn(req, input, async (err, data) => {
if (err) {
log('apostrophe-widgets error: "%s"', err);
return callback(err, data);
}
if (data.style !== 'single') {
log('early return for data.style="%s"', data.style);
return callback(err, data);
}
if (!/^https?:\/\/(www\.)?instagram\.com\/p\//.test(data.url)) {
log('url error, must be instagram.com/p, got "%s"', data.url);
return callback(new Error('URL must be an instagram image post URL'));
}
try {
data._embed = await oEmbed(req, data.url);
log('successfully loaded "%s"', data.url);
callback(null, data);
} catch (e) {
log('error: "%s"', e);
callback(e);
}
});
}
const widgetReqCache = new WeakMap();
self.widgetReq = (widget, req) => {
if (arguments.length === 2) {
widgetReqCache.set(widget, req);
}
return widgetReqCache.get(widget);
};
async function load (superFn, req, widgets, callback) {
self.log(`load widgets(%s)`, widgets.length);
const loaders = widgets.map(async (widget) => {
self.widgetReq(widget, req);
if (widget.style === 'single') {
self.log(`load single widget, %O`, widget);
return await self.loadSingleEmbedWidget(req, widget);
}
let username;
if ((username = widget.username)) {
self.log(`load gallery widget for %s, set in widget configuration`, username);
}
else if ((username = self.getOption(req, 'galleryUserName'))) {
self.log(`load gallery widget for %s, read from 'getOption("galleryUserName")`, username);
}
else {
self.log(`load gallery widget unable to find username`);
return;
}
Object.assign(widget, await self.latestGalleryWidget(req, username));
});
const err = await promiseError(Promise.all(loaders));
if (err) {
self.log('load:error %o', err);
return callback(err);
}
self.log('load:complete %O', widgets);
superFn(req, widgets, callback);
}
}
};