-
Notifications
You must be signed in to change notification settings - Fork 2
/
tellschn_classes.js
194 lines (175 loc) · 6.68 KB
/
tellschn_classes.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
const fs = require("fs");
const nodemailer = require("nodemailer");
const util = require("util");
const Lynx = require("lynx");
const request = require("request");
const git = require('git-last-commit');
class Tellschn {
constructor() {
const mysql = require("mysql");
this.appconf = JSON.parse(fs.readFileSync("app-config.json", "utf8"));
this.accessConf = JSON.parse(fs.readFileSync("access-config.json", "utf8"))
this.sqlConnection = mysql.createConnection(this.accessConf.mysql);
git.getLastCommit((err, commit) => {
if (err) throw err
this.appconf.lastCommit = commit;
})
}
sqlQuery(query, data) {
return new Promise((resolve, reject) => {
let sqlQueryObejct = this.sqlConnection.query(query, data, function (error, result) {
if (error) {
reject(error);
return
} else {
resolve(result);
}
})
if (this.appconf.debug) {
util.log(sqlQueryObejct.sql);
}
})
}
uuid() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
dbg(logging) {
if (this.appconf.debug) {
util.log(logging);
}
}
async sleep(time) {
setTimeout(() => {
Promise.resolve()
}, time)
}
}
class tellschnTemplate extends Tellschn {
constructor(lang = "de") {
super();
this.mustache = require("mustache");
this.translation = JSON.parse(fs.readFileSync("translations/translation_" + lang + ".json", "utf8"));
this.static_prop = {
translation: this.translation,
appconf: this.appconf,
accessconf: this.accessConf
}
}
getTextModule(moduleName, additionalMoustacheContent = {}) {
var combined_prop = { ...this.static_prop, ...additionalMoustacheContent };
return this.mustache.render(this.translation[moduleName], combined_prop);
}
renderFullTranslation(additionalMoustacheContent = {}) {
let buffer = {};
for (var i in this.translation) {
//check wether the string actually contains a variable, if not just copy it
if (this.translation[i].indexOf("{{") != -1) {
buffer[i] = this.getTextModule([i], additionalMoustacheContent);
} else {
buffer[i] = this.translation[i];
}
}
return buffer;
}
exportText_modules(data = {}) {
return { ...this.static_prop, ...{ "translation": this.renderFullTranslation(data) } };
}
}
class tellschnMailer extends tellschnTemplate {
constructor(lang) {
super(lang);
this.mailer = nodemailer.createTransport(this.accessConf.mail);
this.metrics = new tellschnMetrics();
}
async sendMail(to, subject, text) {
let info = await this.mailer.sendMail({
"from": this.accessConf.mail.sender,
"to": to,
"subject": subject,
"text": text
});
this.metrics.increment("emails-sent");
return info;
}
async sendValidationMail(address, userpayload) {
let validation_token = this.uuid();
await this.sqlQuery("INSERT INTO user_notification_services (twitter_id, address, validation_token, recipient_name, platform) VALUES (?)", [[
userpayload.twitter_id,
address,
validation_token,
address,
"email"
]])
this.sendMail(address,
this.getTextModule("mail_subject_register", { "userpayload": userpayload }),
this.getTextModule("mail_text_register", { "userpayload": userpayload, "email_token": validation_token }),
);
}
async sendRegistrationNotificationSuccessMail(address, userpayload) {
this.sendMail(address,
this.getTextModule("mail_subject_registration_success", { "userpayload": userpayload }),
this.getTextModule("mail_text_registration_success", { "userpayload": userpayload }),
);
}
}
class tellschnMetrics extends Tellschn {
constructor(service_name = "tellschn") {
super();
this.service_name = service_name;
if (this.accessConf.metrics.enabled) {
this.metrics = new Lynx(this.accessConf.metrics.statsd_host, this.accessConf.metrics.statsd_port);
}
}
increment(value) {
if (this.accessConf.metrics.enabled) {
this.metrics.increment(this.service_name + '.' + value);
}
return;
}
webHit(endpoint) {
this.increment("tellschnweb_" + endpoint.replace("/", "-"));
}
}
class tellschnMedia extends Tellschn {
constructor() {
super();
}
async addMediaFromTempDir(filename) {
return new Promise((resolve, reject) => {
var kue = require('kue')
, queue = kue.createQueue();
var processUploadJob = queue.create("process_uploaded_file", {
"tempFileLocation": filename,
"title": "Process Uploaded File " + filename
}).save();
processUploadJob.on('complete', function (result) {
console.log('Job completed with data ', result);
resolve(result);
return result;
}).on('failed', function (errorMessage) {
console.log('Job failed', errorMessage);
reject(new Error("ERR_CONVERTING_VIDEO"));
}).on('progress', function (progress, data) {
console.log('\r job #' + processUploadJob.id + ' ' + progress + '% complete with data ', data);
});
})
}
async downloadMediaToDatabase(url) {
return new Promise((resolve, reject) => {
let urlSplit = url.split("/");
let random = Math.floor(Math.random() * 1000);
let filename = 'tmp/' + random + "-" + urlSplit[urlSplit.length - 1]
request.get(url, { "encoding": null }, async function (err, response) {
if (err) reject(err);
fs.writeFileSync(filename, Buffer.from(response.body), "utf8");
let UUID = await (new tellschnMedia).addMediaFromTempDir(filename);
resolve(UUID);
})
});
}
}
module.exports = { Tellschn, tellschnTemplate, tellschnMailer, tellschnMetrics, tellschnMedia }