/
ucommon.js
393 lines (364 loc) · 12.4 KB
/
ucommon.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
'use strict';
const path = require('path');
const web = require("request");
const weburl = require("url");
const webreq = require("request-promise");
const Iconv = require('iconv-lite');
const jschardet = require('jschardet');
const mimelib = require("mimelib-noiconv");
const sanhtml = require("sanitize-html");
var cls_mstdn = require("./cls_mstdn");
var res_langs = require("./res_langs");
const sysconst = require("./sysconst");
const {
JSDOM
} = require("jsdom");
const fs = require("fs");
//const Mastodon = require("mastodon-api")
//import Mastodon from '../node_modules/mastodon-api/lib/mastodon';
const CON_ACCEPT_HOSTS = [
"localhost",
"gpdontest.azurewebsites.net",
"gplusdon.azurewebsites.net",
"gplusdon.net"
];
const INSTANCE_TOKEN = sysconst.server_token();
//lumsis@mastodon.cloud
const __user_dirname = __dirname.replace("apps", "");
const appEffectiveName = "G+Don";
const avalable_languages = [
{
code: "ja", name: "日本語"
},
{
code: "en", name: "English",
},
{
code: "eo", name: "Esperanto",
},
{
code: "fr", name: "Français",
},
{
code: "de", name: "Deutsche",
}
];
function judgeLanguage(request) {
var lan = request.acceptsLanguages();
if (request.query) {
if ("hl" in request.query) {
lan = [request.query.hl];
}
}
if ("_gp_lang" in request.cookies) {
lan = [request.cookies["_gp_lang"]];
}
return lan;
}
/**
*
* @param {Request} request request object
* @param {String} locales locale string
* @return {JSON} locale data
*/
function load_translation(request, locales) {
var ret = "";
var tmp = ["ja"]; //default languages
if (locales) tmp = locales;
var ishit = "";
ret = res_langs.loadData(tmp[0]);
/*
for (var i = 0; i < tmp.length; i++) {
var lo = tmp[i];
var lofile = path.join(__user_dirname, `/static/strings/${lo}.json`);
if (fs.existsSync(lofile)) {
ishit = lo;
ret = fs.readFileSync(lofile, "utf-8");
break;
}
}
*/
return ret;
}
async function loadWebsiteOGP(request, info, url) {
var ret = "";
//console.log("param url=", url);
var def = new Promise((resolve, reject) => {
var ishit = false;
for (var i = 0; i < CON_ACCEPT_HOSTS.length; i++) {
if (request.headers.referer.indexOf(CON_ACCEPT_HOSTS[i]) > -1) {
ishit = true;
break;
}
}
//console.log("ishit=",ishit);
if (!ishit) reject("");
var tmpurl = weburl.parse(url);
if (CON_ACCEPT_HOSTS.indexOf(tmpurl.hostname) > -1) {
//---when in this app site
var arr = tmpurl.pathname.split("/");
var instance = arr[2];
var id = arr[3];
if (arr[1] == "users") {
var api = cls_mstdn.loadAPImaster();
cls_mstdn.getUser(api, instance, id)
.then(result => {
//console.log(result);
//---set up og:
var oginfo = { //---default value
title: appEffectiveName,
type: "website",
description: "",
url: "https://gplusdon.net",
image: "https://gplusdon.net/static/images/gp_og_image.png",
site_name: appEffectiveName
};
if ((result) && ("id" in result)) {
var name = (result.display_name.trim() == "" ? result.username : result.display_name);
oginfo.title = ucommon._T(info.realtrans, "lab_profile", [`${name}@${result.instance}`]);
oginfo.description = result.text;
oginfo.type = "profile";
oginfo.url = `https://${request.hostname}/users/${instance}/${id}`;
oginfo.image = result.avatar;
}
var HTML_FOR_OGP = `
<meta property="og:title" content="${oginfo.title}" />
<meta property="og:type" content="${oginfo.type}" />
<meta property="og:description" content="${oginfo.description}" />
<meta property="og:url" content="${oginfo.url}" />
<meta property="og:image" content="${oginfo.image}"/>
<meta property="og:site_name" content="${oginfo.site_name}" />
`;
resolve(HTML_FOR_OGP);
});
}
} else {
web({ url: url, method: "GET", encoding: null },(error, response, body) => {
//console.log("web.get=", error, response, body);
if (error) {
reject({
cd: -1,
err: "noget"
});
} else {
if (response.statusCode === 200) {
var encoding = null;
//var iconv = new Iconv('EUC-JP', 'UTF-8//TRANSLIT//IGNORE');
if (response.headers["content-type"]) {
encoding = mimelib.parseHeaderLine(response.headers['content-type']).charset;
}
if (!encoding) {
encoding = jschardet.detect(body).encoding;
}
if (encoding && Iconv.encodingExists(encoding)) {
body = Iconv.decode(body, encoding);
var dom = new JSDOM(body);
var info = dom.window.document.head;
var ht = (info.innerHTML);
resolve({
raw: body,
html: ht
});
} else {
reject({
cd: -1,
err: "nofinish"
});
}
} else {
reject({
cd: -1,
err: "nofinish"
});
}
}
});
}
});
return def;
}
/*
*
* @param {Rquest} request
* @param {JSON} param
* lat : 9.99,
* lng : 9.99,
* dist : 9
* apitype : 9 (0 - placeinfo, 1 - localsearch)
*/
async function getGeolocation(request, param) {
var getLatLngRange = function (lat, lng, dist) {
let lat_len = 30.9;
let lng_len = 20.5;
//---km -> m
var rad = dist * 1000;
var lat_scope = (rad / lat_len) / 3600;
var lng_scope = (rad / lng_len) / 3600;
var ret = {
ne: {
lat: lat + lat_scope,
lng: lng - lng_scope
},
nw: {
lat: lat + lat_scope,
lng: lng + lng_scope
},
se: {
lat: lat - lat_scope,
lng: lng - lng_scope
},
sw: {
lat: lat - lat_scope,
lng: lng + lng_scope
}
};
return ret;
};
var def = new Promise((resolve, reject) => {
var ishit = false;
//console.log("refer=",request.headers.referer);
for (var i = 0; i < CON_ACCEPT_HOSTS.length; i++) {
if (request.headers.referer.indexOf(CON_ACCEPT_HOSTS[i]) > -1) {
ishit = true;
break;
}
}
var endpoint = "";
var yolpurl = "";
if (param.apitype == "0") {
var coord = getLatLngRange(param.lat, param.lng, parseFloat(param.dist));
var precision = 25;
var zoom = 13;
endpoint = "https://map.yahooapis.jp/spatial/V1/shapeSearch";
yolpurl = `${endpoint}?appid=${sysconst.yh_id}&coordinates=${coord.ne.lng},${coord.ne.lat} ${coord.nw.lng},${coord.nw.lat} ${coord.sw.lng},${coord.sw.lat} ${coord.se.lng},${coord.se.lat}&output=json&precision=${precision}&z=${zoom}`;
}else{
endpoint = "https://map.yahooapis.jp/search/local/V1/localSearch";
yolpurl = `${endpoint}?appid=${sysconst.yh_id}&lat=${param.lat}&lon=${param.lng}&dist=${param.dist}&output=json`;
}
console.log(yolpurl);
//console.log("ishit=",ishit);
if (!ishit) reject("");
web(yolpurl, (error, response, body) => {
//console.log(body);
resolve(body);
});
});
return def;
}
async function getDirectoryMastodon(request, param) {
var def = new Promise((resolve, reject) => {
var ishit = false;
for (var i = 0; i < CON_ACCEPT_HOSTS.length; i++) {
if (request.headers.referer.indexOf(CON_ACCEPT_HOSTS[i]) > -1) {
ishit = true;
break;
}
}
//console.log("ishit=",ishit);
if (!ishit) reject("");
web(url, (error, response, body) => {
//console.log("web.get=", error, response, body);
const dom = new JSDOM(body);
var info = dom.window.document.head;
resolve(sanhtml(info.innerHTML));
});
});
return def;
}
var ucommon = {
swjs : "pwabuilder-sw.js",
sysinfo: {
name: sysconst.package_info.name,
fullname: appEffectiveName,
author: sysconst.package_info.author.name,
advisor: [],
version: sysconst.package_info.version,
hostname : "",
oginfo: { //---default value
title: appEffectiveName,
type: "website",
description: "",
url: "https://gplusdon.net",
image: "https://gplusdon.net/static/images/gp_og_image.png",
site_name: appEffectiveName
},
yh_id: sysconst.yh_id,
mab_id: sysconst.mab_id,
VAPID: sysconst.vap_id(),
gdaky: sysconst.gdrive.web.api_key,
gdid: sysconst.gdrive.web.client_id,
gdaky_pic: sysconst.gdrive.web.picker_api_key,
color: {
bg_first: "white",
bg_second: "grey lighten-3",
fw: "black--text"
}
},
load_translation: load_translation,
/**
*
* @param {Request} request request object
* @return {Object} neccesary data
*/
analyze_locale: function (request) {
var lan = judgeLanguage(request);
var trans = ucommon.load_translation(request, lan);
//var js = trans;
ucommon.sysinfo.oginfo = { //---default value
title: appEffectiveName,
type: "website",
description: "",
url: "https://gplusdon.net",
image: "https://gplusdon.net/static/images/gp_og_image.png",
site_name: appEffectiveName
};
ucommon.sysinfo.oginfo.description = trans.appDescription;
ucommon.sysinfo.hostname = request.hostname;
return {
lang: lan[0],
avalable_strings: res_langs.support,
trans: JSON.stringify(trans),
realtrans : trans,
sysinfo: ucommon.sysinfo
};
},
_T : function () {
var js = arguments[0];
var res = arguments[1];
var params = [];
var retstr = "";
if (arguments.length > 2) {
params = arguments[2];
}
if (js != null) {
if (res in js) {
retstr = js[res];
}else{
retstr = res;
}
}else{
retstr = res;
}
//---replace parameters.
var pcnt = params.length;
if (pcnt > 9) pcnt = 9;
for (var i = 0; i < pcnt; i++) {
var repstr = "%"+(i+1);
retstr = retstr.replace(repstr,params[i]);
}
return retstr;
},
load_instance_search_token: function (request) {
var ret = "";
for (var i = 0; i < CON_ACCEPT_HOSTS.length; i++) {
if (request.headers.referer.indexOf(CON_ACCEPT_HOSTS[i]) > -1) {
ret = INSTANCE_TOKEN;
break;
}
}
return ret;
},
load_website_ogp: loadWebsiteOGP,
get_geolocation : getGeolocation
};
module.exports = ucommon;