subscription email: do not send email if nothing has changed (#8981)

* improved subscriptions email template

* do not send emails without content

* fixed coding style violations

* removed unnecessary isEmpty check as suggested by reviewer

* moved null check on indexableObjects in generateBodyMail

* fixed unhandled IOException

* fixed typo in bodyCommunities

* do not use != to compare strings

* fixed improper handling of empty list
This commit is contained in:
Sascha Szott
2023-11-14 20:36:52 +01:00
committed by GitHub
parent 500fbe70a3
commit 50b47b707c
2 changed files with 29 additions and 21 deletions

View File

@@ -56,8 +56,16 @@ public class ContentGenerator implements SubscriptionGenerator<IndexableObject>
Locale supportedLocale = I18nUtil.getEPersonLocale(ePerson); Locale supportedLocale = I18nUtil.getEPersonLocale(ePerson);
Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "subscriptions_content")); Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "subscriptions_content"));
email.addRecipient(ePerson.getEmail()); email.addRecipient(ePerson.getEmail());
email.addArgument(generateBodyMail(context, indexableComm));
email.addArgument(generateBodyMail(context, indexableColl)); String bodyCommunities = generateBodyMail(context, indexableComm);
String bodyCollections = generateBodyMail(context, indexableColl);
if (bodyCommunities.equals(EMPTY) && bodyCollections.equals(EMPTY)) {
log.debug("subscription(s) of eperson {} do(es) not match any new items: nothing to send" +
" - exit silently", ePerson::getID);
return;
}
email.addArgument(bodyCommunities);
email.addArgument(bodyCollections);
email.send(); email.send();
} }
} catch (Exception e) { } catch (Exception e) {
@@ -67,10 +75,12 @@ public class ContentGenerator implements SubscriptionGenerator<IndexableObject>
} }
private String generateBodyMail(Context context, List<IndexableObject> indexableObjects) { private String generateBodyMail(Context context, List<IndexableObject> indexableObjects) {
if (indexableObjects == null || indexableObjects.isEmpty()) {
return EMPTY;
}
try { try {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write("\n".getBytes(UTF_8)); out.write("\n".getBytes(UTF_8));
if (indexableObjects.size() > 0) {
for (IndexableObject indexableObject : indexableObjects) { for (IndexableObject indexableObject : indexableObjects) {
out.write("\n".getBytes(UTF_8)); out.write("\n".getBytes(UTF_8));
Item item = (Item) indexableObject.getIndexedObject(); Item item = (Item) indexableObject.getIndexedObject();
@@ -80,10 +90,6 @@ public class ContentGenerator implements SubscriptionGenerator<IndexableObject>
.disseminate(context, item, out); .disseminate(context, item, out);
} }
return out.toString(); return out.toString();
} else {
out.write("No items".getBytes(UTF_8));
}
return out.toString();
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }

View File

@@ -2,15 +2,17 @@
## ##
## Parameters: {0} Collections updates ## Parameters: {0} Collections updates
## {1} Communities updates ## {1} Communities updates
#set($subject = "${config.get('dspace.name')} Subscription") #set($subject = "${config.get('dspace.name')} Subscriptions")
This email is sent from ${config.get('dspace.name')} based on the chosen subscription preferences. This email is sent from ${config.get('dspace.name')} based on the chosen subscription preferences.
Communities #if( not( "$params[0]" == "" ))
----------- Community Subscriptions:
------------------------
List of changed items : ${params[0]} List of changed items : ${params[0]}
Collections #end
----------- #if( not( "$params[1]" == "" ))
Collection Subscriptions:
-------------------------
List of changed items : ${params[1]} List of changed items : ${params[1]}
#end