Skip to content
Snippets Groups Projects
Commit 49047238 authored by Guillaume Vagner's avatar Guillaume Vagner
Browse files

saving usernames

parent de3cc1d7
Branches
No related tags found
No related merge requests found
...@@ -36,11 +36,15 @@ Promise.all([ ...@@ -36,11 +36,15 @@ Promise.all([
expiration TEXT, expiration TEXT,
schedule TEXT schedule TEXT
)`), )`),
query(`CREATE TABLE IF NOT EXISTS groups( query(`CREATE TABLE IF NOT EXISTS groups(
chatId BIGINT NOT NULL, chatId BIGINT NOT NULL,
grp INT, grp INT,
primary key (chatId, grp) primary key (chatId, grp)
)`),
query(`CREATE TABLE IF NOT EXISTS users(
userid INT PRIMARY KEY NOT NULL,
name TEXT,
username TEXT
)`) )`)
]).then(_ => { ]).then(_ => {
...@@ -127,5 +131,39 @@ function getSchedules() { ...@@ -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 }; module.exports = {
\ No newline at end of file query,
getChanByChatId,
createChan,
deleteChanByChatId,
modifyChan,
getChanByState,
addGroup,
removeGroup,
removeAllGroups,
getCompos,
getSchedules,
addUser
};
\ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
const rp = require('request-promise'); const rp = require('request-promise');
// Modules propres // Modules propres
var { modifyChan, getChanByState } = require('./mysql'); var { modifyChan, getChanByState, addUser } = require('./mysql');
// Configurations // Configurations
const config = require('./config'); const config = require('./config');
...@@ -119,6 +119,15 @@ function getFirstToken(code, state) { ...@@ -119,6 +119,15 @@ function getFirstToken(code, state) {
chan.refresh = rep.refresh_token; chan.refresh = rep.refresh_token;
chan.expiration = rep.expires_at; chan.expiration = rep.expires_at;
chan.state = ''; 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) return modifyChan(chan)
}) })
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment