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

198 lines
8.1 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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;
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.privatebin.PrivatebinClient;
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().isEnded())
{
bot.sendMessage("La revue est déjà finie.");
}
else if (bot.getReview().getParticipants().size() == 0)
{
bot.sendMessage("Participation nulle détectée. La revue est ignorée.");
bot.setReview(null);
}
else
{
// End the review.
bot.getReview().endReview();
bot.sendMessage("%% +%01d:%02d", bot.getTimekeeper().format());
// Load and update review statistics.
ReviewDatas datas;
try
{
File reviewDataFile = new File(bot.getReviewDirectory(), "reviewstats.csv");
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);
datas = null;
}
// Generate the review text.
String date = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
String reviewText = ReviewReporter.report(datas, bot.getReview());
// Pastebin the review text.
String pastebinUrl;
if (bot.getPrivatebinSettings().isValid())
{
logger.info("Pastebin the review.");
try
{
PrivatebinClient pastebinClient = new PrivatebinClient(bot.getPrivatebinSettings());
pastebinUrl = pastebinClient.paste(reviewText);
}
catch (final Exception exception)
{
logger.error("Error during Pastebin submit.", exception);
pastebinUrl = "erreur";
}
}
else
{
pastebinUrl = "aucun";
}
// Save the review text.
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, reviewText, StandardCharsets.UTF_8);
logger.info("File review saved in: [{}]", reviewFile.getAbsolutePath());
}
catch (final Exception exception)
{
logger.error("Error during file writing.", exception);
}
// Send conclusion message in channel.
bot.sendMessage("% " + ReviewStatsReporter.reportCurrentReviewCount(datas));
bot.sendMessage("% Compte-rendu de la revue : " + pastebinUrl);
bot.sendMessage("% Durée de la revue : " + bot.getReview().getDurationInMinutes() + " minutes");
bot.sendMessage("% Nombre de personnes participantes : " + bot.getReview().getParticipants().size());
bot.sendMessage("% " + ReviewStatsReporter.reportCheckUserCountRecord(datas));
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;
}
}