Commit 49047238 authored by Guillaume Vagner's avatar Guillaume Vagner
Browse files

saving usernames

parent de3cc1d7
......@@ -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
......@@ -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)
})
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment