Skip to content

Commit

Permalink
SAK-47234 Conversations: allow muting of emails (for batch jobs) (sak…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfish authored Apr 29, 2022
1 parent ef97426 commit f913504
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public interface ConversationsService {
boolean currentUserCanViewTopic(ConversationsTopic topic);
List<TopicTransferBean> getTopicsForSite(String siteId) throws ConversationsPermissionsException;
Optional<String> getTopicPortalUrl(String topicId);
TopicTransferBean saveTopic(TopicTransferBean topicBean) throws ConversationsPermissionsException;
TopicTransferBean saveTopic(TopicTransferBean topicBean, boolean sendMessage) throws ConversationsPermissionsException;
void deleteTopic(String topicId) throws ConversationsPermissionsException;
void pinTopic(String topicId, boolean pinned) throws ConversationsPermissionsException;
TopicTransferBean lockTopic(String topicId, boolean locked, boolean needsModerator) throws ConversationsPermissionsException;
Expand All @@ -72,7 +72,7 @@ public interface ConversationsService {
Map<Reaction, Integer> saveTopicReactions(String topicId, Map<Reaction, Boolean> reactions) throws ConversationsPermissionsException;

Optional<PostTransferBean> getPost(String postId) throws ConversationsPermissionsException;
PostTransferBean savePost(PostTransferBean postBean) throws ConversationsPermissionsException;
PostTransferBean savePost(PostTransferBean postBean, boolean sendMessage) throws ConversationsPermissionsException;
Optional<String> getPostPortalUrl(String topicId, String postId);
int getNumberOfThreadPages(String siteId, String topicId) throws ConversationsPermissionsException;
Collection<PostTransferBean> getPostsByTopicId(String siteId, String topicId, Integer page, PostSort sort, String requestedPostId) throws ConversationsPermissionsException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public Optional<String> getCommentPortalUrl(String commentId) {
}

@Transactional
public TopicTransferBean saveTopic(final TopicTransferBean topicBean) throws ConversationsPermissionsException {
public TopicTransferBean saveTopic(final TopicTransferBean topicBean, boolean sendMessage) throws ConversationsPermissionsException {

String currentUserId = getCheckedCurrentUserId();

Expand Down Expand Up @@ -477,24 +477,26 @@ public TopicTransferBean saveTopic(final TopicTransferBean topicBean) throws Con
ConversationsEvents event = isNew ? ConversationsEvents.TOPIC_CREATED : ConversationsEvents.TOPIC_UPDATED;
eventTrackingService.post(eventTrackingService.newEvent(event.label, decoratedBean.reference, decoratedBean.siteId, true, NotificationService.NOTI_OPTIONAL));

try {
Site site = siteService.getSite(decoratedBean.siteId);
Set<User> users = new HashSet<>(userDirectoryService.getUsers(site.getUsers()));

Map<String, Object> replacements = new HashMap<>();
replacements.put("siteTitle", site.getTitle());
replacements.put("topicTitle", decoratedBean.title);
replacements.put("topicUrl", decoratedBean.portalUrl);
replacements.put("bundle", new ResourceLoader("conversations_notifications"));

userMessagingService.message(users,
Message.builder()
.siteId(decoratedBean.siteId)
.tool(TOOL_ID)
.type(topic.getType() == TopicType.QUESTION ? "newquestion" : "newdiscussion").build(),
Arrays.asList(new MessageMedium[] {MessageMedium.EMAIL}), replacements, NotificationService.NOTI_OPTIONAL);
} catch (IdUnusedException iue) {
log.error("No group for site reference {}", siteRef);
if (sendMessage) {
try {
Site site = siteService.getSite(decoratedBean.siteId);
Set<User> users = new HashSet<>(userDirectoryService.getUsers(site.getUsers()));

Map<String, Object> replacements = new HashMap<>();
replacements.put("siteTitle", site.getTitle());
replacements.put("topicTitle", decoratedBean.title);
replacements.put("topicUrl", decoratedBean.portalUrl);
replacements.put("bundle", new ResourceLoader("conversations_notifications"));

userMessagingService.message(users,
Message.builder()
.siteId(decoratedBean.siteId)
.tool(TOOL_ID)
.type(topic.getType() == TopicType.QUESTION ? "newquestion" : "newdiscussion").build(),
Arrays.asList(new MessageMedium[] {MessageMedium.EMAIL}), replacements, NotificationService.NOTI_OPTIONAL);
} catch (IdUnusedException iue) {
log.error("No group for site reference {}", siteRef);
}
}
});

Expand Down Expand Up @@ -700,7 +702,7 @@ public Optional<PostTransferBean> getPost(String postId) throws ConversationsPer
}

@Transactional
public PostTransferBean savePost(PostTransferBean postBean) throws ConversationsPermissionsException {
public PostTransferBean savePost(PostTransferBean postBean, boolean sendMessage) throws ConversationsPermissionsException {

String currentUserId = getCheckedCurrentUserId();

Expand Down Expand Up @@ -795,34 +797,36 @@ public PostTransferBean savePost(PostTransferBean postBean) throws Conversations
ConversationsEvents event = isNew ? ConversationsEvents.POST_CREATED : ConversationsEvents.POST_UPDATED;
eventTrackingService.post(eventTrackingService.newEvent(event.label, decoratedBean.reference, postBean.siteId, true, NotificationService.NOTI_OPTIONAL));

try {
Site site = siteService.getSite(decoratedBean.siteId);

Map<String, Object> replacements = new HashMap<>();
replacements.put("siteTitle", site.getTitle());
replacements.put("topicTitle", topic.getTitle());
replacements.put("postUrl", decoratedBean.portalUrl);
replacements.put("creatorDisplayName", decoratedBean.creatorDisplayName);
replacements.put("bundle", new ResourceLoader("conversations_notifications"));

if (topic.getType() == TopicType.QUESTION && decoratedBean.isInstructor) {
Set<User> siteUsers = new HashSet<>(userDirectoryService.getUsers(site.getUsers()));
String topicCreator = topic.getMetadata().getCreator();
Set<User> questionCreator = Collections.singleton(userDirectoryService.getUser(topicCreator));
siteUsers.removeAll(questionCreator);
sendMessage(siteUsers, decoratedBean.siteId, replacements, "instructoranswer");

// Send a specific message to the question poster
sendMessage(questionCreator, decoratedBean.siteId, replacements, "instructorreply");
} else if (topic.getType() == TopicType.DISCUSSION && optParent.isPresent()) {
String parentCreator = optParent.get().getMetadata().getCreator();
if (!parentCreator.equals(currentUserId)) {
Set<User> users = Collections.singleton(userDirectoryService.getUser(parentCreator));
sendMessage(users, decoratedBean.siteId, replacements, "reply");
if (sendMessage) {
try {
Site site = siteService.getSite(decoratedBean.siteId);

Map<String, Object> replacements = new HashMap<>();
replacements.put("siteTitle", site.getTitle());
replacements.put("topicTitle", topic.getTitle());
replacements.put("postUrl", decoratedBean.portalUrl);
replacements.put("creatorDisplayName", decoratedBean.creatorDisplayName);
replacements.put("bundle", new ResourceLoader("conversations_notifications"));

if (topic.getType() == TopicType.QUESTION && decoratedBean.isInstructor) {
Set<User> siteUsers = new HashSet<>(userDirectoryService.getUsers(site.getUsers()));
String topicCreator = topic.getMetadata().getCreator();
Set<User> questionCreator = Collections.singleton(userDirectoryService.getUser(topicCreator));
siteUsers.removeAll(questionCreator);
sendMessage(siteUsers, decoratedBean.siteId, replacements, "instructoranswer");

// Send a specific message to the question poster
sendMessage(questionCreator, decoratedBean.siteId, replacements, "instructorreply");
} else if (topic.getType() == TopicType.DISCUSSION && optParent.isPresent()) {
String parentCreator = optParent.get().getMetadata().getCreator();
if (!parentCreator.equals(currentUserId)) {
Set<User> users = Collections.singleton(userDirectoryService.getUser(parentCreator));
sendMessage(users, decoratedBean.siteId, replacements, "reply");
}
}
} catch (Exception e) {
log.error("Failed to send notifications: {}", e.toString());
}
} catch (Exception e) {
log.error("Failed to send notifications", e);
}
});

Expand Down
Loading

0 comments on commit f913504

Please sign in to comment.