Skip to content

Commit

Permalink
[Port dspace-7_x] subscription email: do not send email if nothing ha…
Browse files Browse the repository at this point in the history
…s changed (DSpace#9204)

* improved subscriptions email template

(cherry picked from commit 6e7b327)

* do not send emails without content

(cherry picked from commit 926b242)

* fixed coding style violations

(cherry picked from commit fdacec0)

* removed unnecessary isEmpty check as suggested by reviewer

(cherry picked from commit 30a837f)

* moved null check on indexableObjects in generateBodyMail

(cherry picked from commit b43c340)

* fixed unhandled IOException

(cherry picked from commit 9b3f7b6)

* fixed typo in bodyCommunities

(cherry picked from commit ac3d02e)

* do not use != to compare strings

(cherry picked from commit e460183)

* fixed improper handling of empty list

(cherry picked from commit ac72aae)

---------

Co-authored-by: Sascha Szott <[email protected]>
  • Loading branch information
dspace-bot and saschaszott authored Nov 14, 2023
1 parent a3501c6 commit 64d30e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -67,21 +75,19 @@ public void notifyForSubscriptions(Context context, EPerson ePerson,
}

private String generateBodyMail(Context context, List<IndexableObject> 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) {
Expand Down
16 changes: 9 additions & 7 deletions dspace/config/emails/subscriptions_content
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 64d30e6

Please sign in to comment.