From 50b47b707ccc4f0d7ed3887f08f0a88a39686f29 Mon Sep 17 00:00:00 2001 From: Sascha Szott Date: Tue, 14 Nov 2023 20:36:52 +0100 Subject: [PATCH] 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 --- .../subscriptions/ContentGenerator.java | 34 +++++++++++-------- dspace/config/emails/subscriptions_content | 16 +++++---- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/subscriptions/ContentGenerator.java b/dspace-api/src/main/java/org/dspace/subscriptions/ContentGenerator.java index a913f2504a5..c3035614343 100644 --- a/dspace-api/src/main/java/org/dspace/subscriptions/ContentGenerator.java +++ b/dspace-api/src/main/java/org/dspace/subscriptions/ContentGenerator.java @@ -56,8 +56,16 @@ public void notifyForSubscriptions(Context context, EPerson ePerson, Locale supportedLocale = I18nUtil.getEPersonLocale(ePerson); Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "subscriptions_content")); 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(); } } catch (Exception e) { @@ -67,21 +75,19 @@ public void notifyForSubscriptions(Context context, EPerson ePerson, } private String generateBodyMail(Context context, List indexableObjects) { + if (indexableObjects == null || indexableObjects.isEmpty()) { + return EMPTY; + } try { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write("\n".getBytes(UTF_8)); - if (indexableObjects.size() > 0) { - for (IndexableObject indexableObject : indexableObjects) { - out.write("\n".getBytes(UTF_8)); - Item item = (Item) indexableObject.getIndexedObject(); - String entityType = itemService.getEntityTypeLabel(item); - Optional.ofNullable(entityType2Disseminator.get(entityType)) - .orElseGet(() -> entityType2Disseminator.get("Item")) - .disseminate(context, item, out); - } - return out.toString(); - } else { - out.write("No items".getBytes(UTF_8)); + for (IndexableObject indexableObject : indexableObjects) { + out.write("\n".getBytes(UTF_8)); + Item item = (Item) indexableObject.getIndexedObject(); + String entityType = itemService.getEntityTypeLabel(item); + Optional.ofNullable(entityType2Disseminator.get(entityType)) + .orElseGet(() -> entityType2Disseminator.get("Item")) + .disseminate(context, item, out); } return out.toString(); } catch (Exception e) { diff --git a/dspace/config/emails/subscriptions_content b/dspace/config/emails/subscriptions_content index a330c595371..9b8c91e559d 100644 --- a/dspace/config/emails/subscriptions_content +++ b/dspace/config/emails/subscriptions_content @@ -2,15 +2,17 @@ ## ## Parameters: {0} Collections 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. -Communities ------------ +#if( not( "$params[0]" == "" )) +Community Subscriptions: +------------------------ List of changed items : ${params[0]} -Collections ------------ +#end +#if( not( "$params[1]" == "" )) +Collection Subscriptions: +------------------------- List of changed items : ${params[1]} - +#end