216 lines
8.9 KiB
Java
216 lines
8.9 KiB
Java
/**
|
||
* Copyright (C) 2018 Christian Pierre MOMON <cmomon@april.org>
|
||
*
|
||
* This file is part of (April) Ememem.
|
||
*
|
||
* Ememem is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU Affero General Public License as published by
|
||
* the Free Software Foundation, either version 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* Ememem is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU Affero General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU Affero General Public License
|
||
* along with Ememem. If not, see <http://www.gnu.org/licenses/>
|
||
*/
|
||
package org.april.hebdobot.bot.hooks;
|
||
|
||
import java.io.File;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.time.LocalDate;
|
||
import java.time.LocalDateTime;
|
||
import java.time.LocalTime;
|
||
import java.time.format.DateTimeFormatter;
|
||
|
||
import org.apache.commons.io.FileUtils;
|
||
import org.apache.commons.lang3.StringUtils;
|
||
import org.april.hebdobot.bot.Hebdobot;
|
||
import org.april.hebdobot.bot.stats.ReviewData;
|
||
import org.april.hebdobot.bot.stats.ReviewDatas;
|
||
import org.april.hebdobot.bot.stats.ReviewDatasFile;
|
||
import org.april.hebdobot.bot.stats.ReviewStatsReporter;
|
||
import org.april.hebdobot.pastebin.PastebinClient;
|
||
import org.april.hebdobot.pastebin.Private;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
|
||
import fr.devinsy.strings.StringList;
|
||
|
||
/**
|
||
* The Class FinishReviewHook.
|
||
*/
|
||
public class FinishReviewHook extends Hook
|
||
{
|
||
private static final Logger logger = LoggerFactory.getLogger(FinishReviewHook.class);
|
||
|
||
public static String DEFAULT_SUFFIX = "-log-irc-revue-hebdomadaire.txt";
|
||
|
||
/* (non-Javadoc)
|
||
* @see org.april.hebdobot.bot.hooks.Hook#attemptProcess(org.april.hebdobot.bot.Hebdobot, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
|
||
*/
|
||
@Override
|
||
public boolean attemptProcess(final Hebdobot bot, final String channel, final String sender, final String login, final String hostname,
|
||
final String message)
|
||
{
|
||
boolean result;
|
||
|
||
if (StringUtils.equalsAnyIgnoreCase(message, "!fin"))
|
||
{
|
||
logger.info("!fin caught.");
|
||
|
||
// End.
|
||
if (bot.getReview() == null)
|
||
{
|
||
bot.sendMessage(sender + ", pas de revue en cours.");
|
||
}
|
||
else if (!bot.getReview().isOwner(sender))
|
||
{
|
||
bot.sendMessage(sender + ", vous n'êtes pas le conducteur de la réunion");
|
||
}
|
||
else if (bot.getReview().getParticipants().size() == 0)
|
||
{
|
||
bot.sendMessage("Participation nulle détecté. La revue est ignorée.");
|
||
bot.setReview(null);
|
||
}
|
||
else
|
||
{
|
||
String date = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
|
||
String textReview = bot.getReview().toString();
|
||
|
||
// Display statistics. This feature has to not break
|
||
// Hebdobot.
|
||
|
||
String newMaxUserCountReport;
|
||
String userCountReport;
|
||
String durationReport;
|
||
try
|
||
{
|
||
File reviewDataFile = new File(bot.getReviewDirectory(), "reviewstats.csv");
|
||
if (reviewDataFile.exists())
|
||
{
|
||
ReviewDatas datas = ReviewDatasFile.load(reviewDataFile);
|
||
datas.clean();
|
||
|
||
newMaxUserCountReport = ReviewStatsReporter.reportNewMaxUserCount(datas, bot.getReview().getParticipants().size());
|
||
ReviewData currentReview = new ReviewData(LocalDateTime.now(), bot.getReview().getParticipants().size(),
|
||
(int) bot.getReview().getDurationInMinutes());
|
||
datas.add(currentReview);
|
||
userCountReport = ReviewStatsReporter.reportUserCount(datas, currentReview.getUserCount());
|
||
durationReport = ReviewStatsReporter.reportDuration(datas, currentReview.getDuration());
|
||
|
||
if (bot.getReview().getParticipants().size() > 1)
|
||
{
|
||
ReviewDatasFile.append(reviewDataFile, currentReview);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
logger.warn("Statistic file not found [{}]", reviewDataFile.getAbsolutePath());
|
||
bot.sendMessage("% Fichier de statistiques absent.");
|
||
newMaxUserCountReport = null;
|
||
userCountReport = null;
|
||
durationReport = null;
|
||
}
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
logger.warn("Exception during statistics work.", exception);
|
||
newMaxUserCountReport = null;
|
||
userCountReport = null;
|
||
durationReport = null;
|
||
}
|
||
|
||
textReview = new StringList(textReview).appendln(newMaxUserCountReport, "\n", userCountReport, "\n", durationReport).toString();
|
||
|
||
//
|
||
if (bot.getPastebinSettings().isValid())
|
||
{
|
||
logger.info("Pastebin the review.");
|
||
try
|
||
{
|
||
PastebinClient pastebinClient = new PastebinClient(bot.getPastebinSettings().getApiKey());
|
||
|
||
String pastebinUrl = pastebinClient.paste(textReview, "Revue APRIL " + date, Private.UNLISTED);
|
||
|
||
bot.sendMessage("% Compte-rendu de la revue : " + pastebinUrl);
|
||
}
|
||
catch (final Exception exception)
|
||
{
|
||
logger.error("Error during Pastebin submit.", exception);
|
||
}
|
||
}
|
||
|
||
//
|
||
logger.info("Write review file.");
|
||
File reviewFile = new File(bot.getReviewDirectory(), date + StringUtils.defaultString(bot.getReviewFileSuffix(), DEFAULT_SUFFIX));
|
||
if (reviewFile.exists())
|
||
{
|
||
LocalTime now = LocalTime.now();
|
||
String newSuffix = String.format("-%02dh%02d.txt", now.getHour(), now.getMinute());
|
||
reviewFile = new File(reviewFile.getAbsolutePath().replace(".txt", newSuffix));
|
||
}
|
||
try
|
||
{
|
||
if (!bot.getReviewDirectory().exists())
|
||
{
|
||
logger.info("Create review directory: " + bot.getReviewDirectory().getAbsolutePath());
|
||
bot.getReviewDirectory().mkdirs();
|
||
}
|
||
FileUtils.writeStringToFile(reviewFile, textReview, StandardCharsets.UTF_8);
|
||
logger.info("File review saved in: [{}]", reviewFile.getAbsolutePath());
|
||
}
|
||
catch (final Exception exception)
|
||
{
|
||
logger.error("Error during file writing", exception);
|
||
}
|
||
|
||
bot.sendMessage("% Durée de la revue : " + bot.getReview().getDurationInMinutes() + " minutes");
|
||
bot.sendMessage("% Nombre de personnes participantes : " + bot.getReview().getParticipants().size());
|
||
|
||
// Display statistics.
|
||
if (newMaxUserCountReport != null)
|
||
{
|
||
bot.sendMessage("% " + newMaxUserCountReport);
|
||
}
|
||
|
||
if (userCountReport != null)
|
||
{
|
||
bot.sendMessage(bot.getReview().getOwner(), userCountReport);
|
||
}
|
||
|
||
if (durationReport != null)
|
||
{
|
||
bot.sendMessage(bot.getReview().getOwner(), durationReport);
|
||
}
|
||
|
||
String participants = StringUtils.join(bot.getReview().getParticipants(), " ");
|
||
bot.sendMessage("% " + participants + ", pensez à noter votre bénévalo : http://www.april.org/my?action=benevalo");
|
||
|
||
String reminderMessage = String.format(
|
||
"%% %s, ne pas oublier d'ajouter le compte-rendu de la revue sur https://agir.april.org/issues/135 en utilisant comme nom de fichier %s",
|
||
bot.getReview().getOwner(), reviewFile.getName());
|
||
logger.info("reminderMessage=[{}]", reminderMessage);
|
||
bot.sendMessage(reminderMessage);
|
||
|
||
// Finalize review.
|
||
bot.sendMessage("% Fin de la revue hebdomadaire");
|
||
bot.sendMessage(bot.getReview().getOwner(), "Revue finie.");
|
||
|
||
bot.setReview(null);
|
||
}
|
||
|
||
result = true;
|
||
}
|
||
else
|
||
{
|
||
result = false;
|
||
}
|
||
|
||
//
|
||
return result;
|
||
}
|
||
}
|