Skip to content
Snippets Groups Projects
Commit f284e8e0 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

- Add a debug mode for email subscription

- Fix events sent many times - fix #16
parent 556acf46
No related branches found
No related tags found
No related merge requests found
...@@ -91,6 +91,15 @@ public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> { ...@@ -91,6 +91,15 @@ public class PluginSettings extends AbstractLifecycleComponent<PluginSettings> {
return settings.getAsBoolean("duniter.subscription.email.atStartup", false); return settings.getAsBoolean("duniter.subscription.email.atStartup", false);
} }
/**
* Should email subscription execute as DEBUG mode ?
* @return
*/
public boolean isEmailSubscriptionsDebug() {
return settings.getAsBoolean("duniter.subscription.email.debug", false);
}
/** /**
* Day of the week to trigger weekly email subscription (default: 2 = monday) * Day of the week to trigger weekly email subscription (default: 2 = monday)
* @return * @return
......
...@@ -152,8 +152,8 @@ public class SubscriptionService extends AbstractService { ...@@ -152,8 +152,8 @@ public class SubscriptionService extends AbstractService {
logger.warn(I18n.t("duniter4j.es.subscription.email.start", pluginSettings.getEmailSubscriptionsExecuteHour(), dayOfWeek)); logger.warn(I18n.t("duniter4j.es.subscription.email.start", pluginSettings.getEmailSubscriptionsExecuteHour(), dayOfWeek));
} }
// Execution at startup // Execution at startup (or DEBUG mode)
if (pluginSettings.isEmailSubscriptionsExecuteAtStartup()) { if (pluginSettings.isEmailSubscriptionsExecuteAtStartup() || pluginSettings.isEmailSubscriptionsDebug()) {
threadPool.schedule( threadPool.schedule(
() -> executeEmailSubscriptions(EmailSubscription.Frequency.daily), () -> executeEmailSubscriptions(EmailSubscription.Frequency.daily),
new TimeValue(20, TimeUnit.SECONDS) /* after 20s */ new TimeValue(20, TimeUnit.SECONDS) /* after 20s */
...@@ -213,7 +213,7 @@ public class SubscriptionService extends AbstractService { ...@@ -213,7 +213,7 @@ public class SubscriptionService extends AbstractService {
from += size; from += size;
} }
logger.info(String.format("Executing %s email subscription... [OK] (%s executions in %s ms)", logger.info(String.format("Executing %s email subscription... [OK] emails sent [%s] (in %s ms)",
frequency.name(), executionCount, System.currentTimeMillis()-now)); frequency.name(), executionCount, System.currentTimeMillis()-now));
} }
...@@ -259,7 +259,24 @@ public class SubscriptionService extends AbstractService { ...@@ -259,7 +259,24 @@ public class SubscriptionService extends AbstractService {
final Map<String, String> profileTitles) { final Map<String, String> profileTitles) {
Preconditions.checkNotNull(subscription); Preconditions.checkNotNull(subscription);
logger.info(String.format("Processing email subscription [%s]", subscription.getId())); boolean debug = pluginSettings.isEmailSubscriptionsDebug();
if (subscription.getContent() != null && subscription.getContent().getEmail() != null) {
if (debug) {
logger.info(String.format("Processing email subscription to [%s - %s] on account [%s]",
senderName,
subscription.getContent().getEmail(),
ModelUtils.minifyPubkey(subscription.getIssuer())));
}
else {
logger.info(String.format("Processing email subscription [%s] on account [%s]",
subscription.getId(),
ModelUtils.minifyPubkey(subscription.getIssuer())));
}
}
else {
logger.warn(String.format("Processing email subscription [%s] - no email found in subscription content: skipping", subscription.getId()));
return null;
}
SubscriptionExecution lastExecution = subscriptionExecutionDao.getLastExecution(subscription); SubscriptionExecution lastExecution = subscriptionExecutionDao.getLastExecution(subscription);
Long lastExecutionTime; Long lastExecutionTime;
...@@ -319,12 +336,21 @@ public class SubscriptionService extends AbstractService { ...@@ -319,12 +336,21 @@ public class SubscriptionService extends AbstractService {
pluginSettings.getCesiumUrl()) pluginSettings.getCesiumUrl())
.render(issuerLocale); .render(issuerLocale);
// Schedule email sending final String object = emailSubjectPrefix + I18n.t("duniter4j.es.subscription.email.subject", userEvents.size());
threadPool.schedule(() -> mailService.sendHtmlEmailWithText( if (pluginSettings.isEmailSubscriptionsDebug()) {
emailSubjectPrefix + I18n.t("duniter4j.es.subscription.email.subject", userEvents.size()), logger.info(String.format("---- Email to send (debug mode) ------\nTo:%s\nObject: %s\nText content:\n%s",
text, subscription.getContent().getEmail(),
"<body>" + html + "</body>", object,
subscription.getContent().getEmail())); text));
}
else {
// Schedule email sending
threadPool.schedule(() -> mailService.sendHtmlEmailWithText(
object,
text,
"<body>" + html + "</body>",
subscription.getContent().getEmail()));
}
// Compute last time (should be the first one, as events are sorted in DESC order) // Compute last time (should be the first one, as events are sorted in DESC order)
Long lastEventTime = userEvents.get(0).getTime(); Long lastEventTime = userEvents.get(0).getTime();
...@@ -352,6 +378,9 @@ public class SubscriptionService extends AbstractService { ...@@ -352,6 +378,9 @@ public class SubscriptionService extends AbstractService {
issuerProfilNames.get(subscription.getIssuer()) : issuerProfilNames.get(subscription.getIssuer()) :
ModelUtils.minifyPubkey(subscription.getIssuer()); ModelUtils.minifyPubkey(subscription.getIssuer());
// Remove comma (to avoid to be used as many args in the i18n_args template)
issuerName = issuerName.replaceAll("[, ]+", " ");
senderName = StringUtils.isNotBlank(senderName) ? senderName.replaceAll("[, ]+", " ") : senderName;
try { try {
// Compute body // Compute body
...@@ -393,11 +422,10 @@ public class SubscriptionService extends AbstractService { ...@@ -393,11 +422,10 @@ public class SubscriptionService extends AbstractService {
execution.setSignature(cryptoService.sign(json, pluginSettings.getNodeKeypair().getSecKey())); execution.setSignature(cryptoService.sign(json, pluginSettings.getNodeKeypair().getSecKey()));
if (execution.getId() == null) { if (execution.getId() == null) {
subscriptionExecutionDao.create(toJson(execution), false/*not wait*/);
//subscriptionExecutionDao.create(json, false/*not wait*/);
} }
else { else {
//subscriptionExecutionDao.update(execution.getId(), json, false/*not wait*/); subscriptionExecutionDao.update(execution.getId(), toJson(execution), false/*not wait*/);
} }
return execution; return execution;
} }
......
text_event_item(e) ::= << text_event_item(e) ::= <<
- [$e.time; format="short"$] $e.description$ - [$e.time; format="short"$] $e.description$
>> >>
\ No newline at end of file
...@@ -73,7 +73,7 @@ public class SynchroService extends AbstractSynchroService { ...@@ -73,7 +73,7 @@ public class SynchroService extends AbstractSynchroService {
importInvitationChanges(result, peer, sinceTime); importInvitationChanges(result, peer, sinceTime);
long duration = System.currentTimeMillis() - time; long duration = System.currentTimeMillis() - time;
logger.info(String.format("[%s] Synchronizing user data since %s [OK] %s (ins %s ms)", peer.toString(), sinceTime, result.toString(), duration)); logger.info(String.format("[%s] Synchronizing user data since %s [OK] %s (in %s ms)", peer.toString(), sinceTime, result.toString(), duration));
} }
protected void importHistoryChanges(SynchroResult result, Peer peer, long sinceTime) { protected void importHistoryChanges(SynchroResult result, Peer peer, long sinceTime) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment