hebdobot/src/org/april/hebdobot/bot/hooks/FinishReviewHook.java

188 lines
7.8 KiB
Java
Raw Normal View History

/**
* Copyright (C) 2018-2019 Christian Pierre MOMON <cmomon@april.org>
*
* This file is part of (April) Hebdobot.
*
* Hebdobot 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.
*
* Hebdobot 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 Hebdobot. 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;
2018-09-25 02:37:30 +02:00
import org.april.hebdobot.bot.review.ReviewReporter;
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;
/**
* 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)
{
2018-09-25 02:37:30 +02:00
bot.sendMessage("Participation nulle détectée. La revue est ignorée.");
bot.setReview(null);
}
else
{
2018-09-25 02:37:30 +02:00
//
bot.getReview().endReview();
// Load and update review statistics.
2018-09-25 02:37:30 +02:00
ReviewDatas datas;
try
{
File reviewDataFile = new File(bot.getReviewDirectory(), "reviewstats.csv");
2018-09-25 02:37:30 +02:00
datas = ReviewDatasFile.load(reviewDataFile);
datas.clean();
ReviewData currentReviewData = new ReviewData(LocalDateTime.now(), bot.getReview().getParticipants().size(),
bot.getReview().getDurationInMinutes());
datas.add(currentReviewData);
if (bot.getReview().getParticipants().size() > 1)
{
ReviewDatasFile.append(reviewDataFile, currentReviewData);
}
}
catch (Exception exception)
{
logger.warn("Exception during statistics work.", exception);
2018-09-25 02:37:30 +02:00
datas = null;
}
2018-09-25 02:37:30 +02:00
// Conclusion message in channel.
String date = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
String textReview = ReviewReporter.report(datas, bot.getReview());
bot.sendMessage("% " + ReviewStatsReporter.reportReviewCount(datas));
//
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.
2018-09-25 02:37:30 +02:00
bot.sendMessage("% " + ReviewStatsReporter.reportNewMaxUserCount(datas, bot.getReview().getParticipants().size()));
bot.sendMessage(bot.getReview().getOwner(), ReviewStatsReporter.reportUserCount(datas, bot.getReview().getParticipants().size()));
bot.sendMessage(bot.getReview().getOwner(), ReviewStatsReporter.reportDuration(datas, bot.getReview().getDurationInMinutes()));
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;
}
}