From 29a68cfe582a247e6bc275d7c6ae6d009f32c21c Mon Sep 17 00:00:00 2001
From: Guillaume Vagner <guillaume.vagner@supelec.fr>
Date: Tue, 5 Mar 2019 00:45:39 +0100
Subject: [PATCH] more user interaction

---
 requests.js |  4 +++-
 schedule.js | 18 +++++++++++++-----
 telegram.js | 49 ++++++++++++++++++++++++++++++++++---------------
 website.js  |  2 +-
 4 files changed, 51 insertions(+), 22 deletions(-)

diff --git a/requests.js b/requests.js
index a605fa6..020819b 100644
--- a/requests.js
+++ b/requests.js
@@ -28,12 +28,14 @@ async function sendRequest(req, chan) {
 
 // Récupération de tous les personnes et leurs assos ayant leur anniversaire
 function getBirthdays(chan) {
-    const req = 'query getUsersBirthday {users: usersBirthday {    ...userData}}fragment userData on User {firstName  lastName  roles {sector {composition {id}}}}'
+    const req = 'query getUsersBirthday {users: usersBirthday {    ...userData}}fragment userData on User {firstName lastName login promotion roles {sector {composition {id}}}}'
     return sendRequest(req, chan).then(body => {
         const users = [];
         body.data.users.forEach(user => {
             use = {};
             use.name = `${user.firstName} ${user.lastName}`;
+            use.login = user.login;
+            use.promo = user.promotion;
             use.asso = [];
             user.roles.forEach(role => {
                 use.asso.push(role.sector.composition.id);
diff --git a/schedule.js b/schedule.js
index 2dff4ca..6a445db 100644
--- a/schedule.js
+++ b/schedule.js
@@ -26,11 +26,19 @@ function addSchedule(chan, time, bot) {
                 // récupère que les personnes du jour qui font partie des groupes ciblés
                 const newUsers = users.filter(user => user.asso.some(asso => groups.indexOf(asso) !== -1));
                 if (newUsers.length === 0) return
-                var msg = '**Joyeux anniversaire** à :\n'
-                newUsers.forEach(user => {
-                    msg += `${user.name}\n`
-                });
-                return bot.sendMessage(chan.chatId, msg, { parse_mode: 'Markdown' });
+                if (chan.chatId > 0) { // chan privé
+                    var msg = 'N\'oubliez pas les <b>anniversaires</b> de :\n'
+                } else { // chan à plusieurs
+                    var msg = '<b>Joyeux anniversaire</b> à :\n'
+                }
+                users.forEach((user, index, array) => {
+                    if (index !== array.length - 1) {
+                        msg += `├ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n`
+                    } else {
+                        msg += `└ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n`
+                    }
+                })
+                return bot.sendMessage(chan.chatId, msg, { parse_mode: 'HTML' });
             })
         })
     })
diff --git a/telegram.js b/telegram.js
index 39f9de1..56edf90 100644
--- a/telegram.js
+++ b/telegram.js
@@ -100,11 +100,20 @@ bot.onText(/\/allbirthdays/, msg => {
     getChanByChatId(chatId).then(chan => {
         return getBirthdays(chan)
     }).then(users => {
-        var msg = '**Joyeux anniversaire** à :\n'
-        users.forEach(user => {
-            msg += `${user.name}\n`
-        });
-        bot.sendMessage(chatId, msg, { parse_mode: 'Markdown' });
+        if (chatId > 0) { // chan privé
+            var msg = 'N\'oubliez pas les <b>anniversaires</b> de :\n'
+        } else { // chan à plusieurs
+            var msg = '<b>Joyeux anniversaire</b> à :\n'
+        }
+        users.sort((use1, use2) => use1.promo - use2.promo);
+        users.forEach((user, index, array) => {
+            if (index !== array.length - 1) {
+                msg += `├ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n`
+            } else {
+                msg += `└ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n`
+            }
+        })
+        return bot.sendMessage(chatId, msg, { parse_mode: 'HTML' });
     })
 });
 
@@ -115,17 +124,26 @@ bot.onText(/\/birthdays/, msg => {
     getChanByChatId(chatId).then(chan => {
         return Promise.all([
             getBirthdays(chan),
-            getCompos(chatId),
+            getCompos(chatId)
         ])
     }).then(([users, groups]) => {
         // récupère que les personnes du jour qui font partie des groupes ciblés
         const newUsers = users.filter(user => user.asso.some(asso => groups.indexOf(asso) !== -1));
         if (newUsers.length === 0) return bot.sendMessage(chatId, 'Pas d\'anniversaire à souhaiter aujourd\'hui.')
-        var msg = '**Joyeux anniversaire** à :\n'
-        newUsers.forEach(user => {
-            msg += `${user.name}\n`
-        });
-        return bot.sendMessage(chatId, msg, { parse_mode: 'Markdown' });
+        if (chatId > 0) { // chan privé
+            var msg = 'N\'oubliez pas les <b>anniversaires</b> de :\n'
+        } else { // chan à plusieurs
+            var msg = '<b>Joyeux anniversaire</b> à :\n'
+        }
+        newUsers.sort((use1, use2) => use1.promo - use2.promo);
+        newUsers.forEach((user, index, array) => {
+            if (index !== array.length - 1) {
+                msg += `├ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n`
+            } else {
+                msg += `└ <a href = "https://linkcs.fr/user/${user.login}">${user.name}</a>\n`
+            }
+        })
+        return bot.sendMessage(chatId, msg, { parse_mode: 'HTML' });
     })
 });
 
@@ -137,7 +155,9 @@ bot.onText(new RegExp(`/search (.+)|/search@${config.telegram.name} (.+)`), (msg
         return searchGroups(chan, research)
     }).then(groups => {
         if (groups.length == 0) return bot.sendMessage(chatId, 'Pas de groupe à ce nom...');
-        var resp = 'Voici les différentes associations. Faites /add XXXX pour ajouter les anniversaires de ses membres. ';
+        const aleatgroup = groups[Math.floor(Math.random() * groups.length)];
+        const aleatcompo = aleatgroup.compositions[Math.floor(Math.random() * aleatgroup.compositions.length)];
+        var resp = `Voici les différentes associations et leurs compositions. Pour ajouter les membres d'une composition à votre liste d'anniversaire, faites par exemple /add ${aleatcompo.id} pour ajouter les membres de la composition ${aleatcompo.label} de l'asso ${aleatgroup.name}. `;
         // LinkCS limite à 20 résultats
         if (groups.length == 20) resp += 'La recherche est limitée à 20 choix.'
         resp += '\n\n'
@@ -238,11 +258,10 @@ bot.onText(/\/info/, msg => {
         if (chan.token.length === 0) return bot.sendMessage(chatId, `Une demande de connexion a été faite par @${chan.username} mais n'a toujours pas été acceptée.\n${config.website.protocol}://${config.website.hostname}/?state=${chan.state}`);
         return Promise.all([
             getMe(chan),
-            getCompos(chan.chatId),
             getCompos(chan.chatId).then(groups => {
                 return Promise.all(groups.map(group => getCompoGroupById(chan, group)))
             })
-        ]).then(([me, groups, cgs]) => {
+        ]).then(([me, cgs]) => {
             groups = {}
             cgs.forEach(cg => {
                 if (!groups[cg.group.id]) {
@@ -264,7 +283,7 @@ bot.onText(/\/info/, msg => {
             }
 
             // affiche la liste des associations à souhait
-            if (groups.length === 0) {
+            if (cgs.length === 0) {
                 msg += 'La liste des compositions est vide.\n';
                 msg += 'Vous pouvez en chercher via /search sonNom, puis faire un /add sonID\n'
             } else {
diff --git a/website.js b/website.js
index 375ccd4..a0455ee 100644
--- a/website.js
+++ b/website.js
@@ -48,7 +48,7 @@ app.get('/auth', function (req, res) {
     if (!req.query.code || !req.query.state) return res.sendFile(`${__dirname}/auth.html`)
 
 return getFirstToken(req.query.code, req.query.state).then(chan => {
-        bot.sendMessage(chan.chatId, `@${chan.username} s'est connecté à OAuth2, shall we begin?`)
+        bot.sendMessage(chan.chatId, `@${chan.username} s'est connecté à OAuth2, shall we begin?\nFaites /search XXX pour chercher une composition sur linkCS et ajouter ses membres à votre liste d'anniversaire.`)
         res.redirect(301, `${config.website.protocol}://${config.website.hostname}/auth`)
     })
 })
-- 
GitLab