From 49047238b7a5cbc4e535bd65a6ab93183c34f2eb Mon Sep 17 00:00:00 2001 From: Guillaume Vagner <guillaume.vagner@supelec.fr> Date: Mon, 4 Mar 2019 13:05:25 +0100 Subject: [PATCH] saving usernames --- mysql.js | 58 ++++++++++++++++++++++++++++++++++++++++++++--------- requests.js | 11 +++++++++- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/mysql.js b/mysql.js index e1b3dd3..f1a1341 100644 --- a/mysql.js +++ b/mysql.js @@ -36,11 +36,15 @@ Promise.all([ expiration TEXT, schedule TEXT )`), - query(`CREATE TABLE IF NOT EXISTS groups( chatId BIGINT NOT NULL, grp INT, primary key (chatId, grp) +)`), + query(`CREATE TABLE IF NOT EXISTS users( + userid INT PRIMARY KEY NOT NULL, + name TEXT, + username TEXT )`) ]).then(_ => { @@ -71,14 +75,14 @@ function deleteChanByChatId(chatId) { function modifyChan(data) { return query(` - UPDATE channel - SET username = "${data.username}", - state = "${data.state}", - token = "${data.token}", - refresh = "${data.refresh}", - expiration = "${data.expiration}", - schedule = "${data.schedule}" - WHERE chatId = ${data.chatId} + UPDATE channel + SET username = "${data.username}", + state = "${data.state}", + token = "${data.token}", + refresh = "${data.refresh}", + expiration = "${data.expiration}", + schedule = "${data.schedule}" + WHERE chatId = ${data.chatId} `).then(_ => { return getChanByChatId(data.chatId) }) @@ -127,5 +131,39 @@ function getSchedules() { `) } +function addUser(data) { + return query(` + SELECT * + FROM users + WHERE userid=${data.userid} + `).then(rep => { + if (rep.length === 0) { + return query(` + INSERT INTO users + VALUES(${data.userid}, "${data.name}", "${data.username}") + `) + } else { + return query(` + UPDATE users + SET username="${data.username}" + WHERE userid=${data.userid} + `) + } + }) +} + -module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, removeGroup, removeAllGroups, getCompos, getSchedules }; \ No newline at end of file +module.exports = { + query, + getChanByChatId, + createChan, + deleteChanByChatId, + modifyChan, + getChanByState, + addGroup, + removeGroup, + removeAllGroups, + getCompos, + getSchedules, + addUser +}; \ No newline at end of file diff --git a/requests.js b/requests.js index ad07208..ac91896 100644 --- a/requests.js +++ b/requests.js @@ -4,7 +4,7 @@ const rp = require('request-promise'); // Modules propres -var { modifyChan, getChanByState } = require('./mysql'); +var { modifyChan, getChanByState, addUser } = require('./mysql'); // Configurations const config = require('./config'); @@ -119,6 +119,15 @@ function getFirstToken(code, state) { chan.refresh = rep.refresh_token; chan.expiration = rep.expires_at; chan.state = ''; + if (chan.chatId > 0) { + getMe(chan).then(me => { + addUser({ + userid: me.id, + name: `${me.firstName} ${me.lastName}`, + username: chan.username + }) + }) + } return modifyChan(chan) }) } -- GitLab