From cc2d581eb0524604b6dcf0523e9ca96e0b8a6ce3 Mon Sep 17 00:00:00 2001 From: Mohit Yadav <105265192+mohityadav766@users.noreply.github.com> Date: Wed, 12 Jun 2024 17:40:35 +0530 Subject: [PATCH] Fix owner notification (#16629) * - Fix Task notification not getting sent to owners * - Fix Task notification not getting sent to owners --- .../service/util/SubscriptionUtil.java | 114 +++++++++++------- 1 file changed, 71 insertions(+), 43 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java index 7ca7e830c44..3bad33e1e21 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java @@ -154,39 +154,54 @@ public class SubscriptionUtil { } private static Set getTaskAssignees( - SubscriptionDestination.SubscriptionType type, ChangeEvent event) { + SubscriptionDestination.SubscriptionCategory category, + SubscriptionDestination.SubscriptionType type, + ChangeEvent event) { Thread thread = AlertsRuleEvaluator.getThread(event); - List assignees = thread.getTask().getAssignees(); Set receiversList = new HashSet<>(); Map teams = new HashMap<>(); Map users = new HashMap<>(); Team tempTeamVar = null; User tempUserVar = null; - if (!nullOrEmpty(assignees)) { - for (EntityReference reference : assignees) { - if (Entity.USER.equals(reference.getType())) { - tempUserVar = Entity.getEntity(USER, reference.getId(), "profile", Include.NON_DELETED); - users.put(tempUserVar.getId(), tempUserVar); - } else if (TEAM.equals(reference.getType())) { - tempTeamVar = Entity.getEntity(TEAM, reference.getId(), "profile", Include.NON_DELETED); - teams.put(tempTeamVar.getId(), tempTeamVar); + + if (category.equals(SubscriptionDestination.SubscriptionCategory.ASSIGNEES)) { + List assignees = thread.getTask().getAssignees(); + if (!nullOrEmpty(assignees)) { + for (EntityReference reference : assignees) { + if (Entity.USER.equals(reference.getType())) { + tempUserVar = Entity.getEntity(USER, reference.getId(), "profile", Include.NON_DELETED); + users.put(tempUserVar.getId(), tempUserVar); + } else if (TEAM.equals(reference.getType())) { + tempTeamVar = Entity.getEntity(TEAM, reference.getId(), "profile", Include.NON_DELETED); + teams.put(tempTeamVar.getId(), tempTeamVar); + } + } + } + + for (Post post : thread.getPosts()) { + tempUserVar = Entity.getEntityByName(USER, post.getFrom(), "profile", Include.NON_DELETED); + users.put(tempUserVar.getId(), tempUserVar); + List mentions = MessageParser.getEntityLinks(post.getMessage()); + for (MessageParser.EntityLink link : mentions) { + if (USER.equals(link.getEntityType())) { + tempUserVar = Entity.getEntity(link, "profile", Include.NON_DELETED); + users.put(tempUserVar.getId(), tempUserVar); + } else if (TEAM.equals(link.getEntityType())) { + tempTeamVar = Entity.getEntity(link, "profile", Include.NON_DELETED); + teams.put(tempTeamVar.getId(), tempTeamVar); + } } } } - for (Post post : thread.getPosts()) { - tempUserVar = Entity.getEntityByName(USER, post.getFrom(), "profile", Include.NON_DELETED); - users.put(tempUserVar.getId(), tempUserVar); - List mentions = MessageParser.getEntityLinks(post.getMessage()); - for (MessageParser.EntityLink link : mentions) { - if (USER.equals(link.getEntityType())) { - tempUserVar = Entity.getEntity(link, "profile", Include.NON_DELETED); - users.put(tempUserVar.getId(), tempUserVar); - } else if (TEAM.equals(link.getEntityType())) { - tempTeamVar = Entity.getEntity(link, "profile", Include.NON_DELETED); - teams.put(tempTeamVar.getId(), tempTeamVar); - } + if (category.equals(SubscriptionDestination.SubscriptionCategory.OWNERS)) { + try { + tempUserVar = + Entity.getEntityByName(USER, thread.getCreatedBy(), "profile", Include.NON_DELETED); + users.put(tempUserVar.getId(), tempUserVar); + } catch (Exception ex) { + LOG.warn("Thread created by unknown user: {}", thread.getCreatedBy()); } } @@ -200,7 +215,9 @@ public class SubscriptionUtil { } public static Set handleConversationNotification( - SubscriptionDestination.SubscriptionType type, ChangeEvent event) { + SubscriptionDestination.SubscriptionCategory category, + SubscriptionDestination.SubscriptionType type, + ChangeEvent event) { Thread thread = AlertsRuleEvaluator.getThread(event); Set receiversList = new HashSet<>(); Map teams = new HashMap<>(); @@ -208,33 +225,43 @@ public class SubscriptionUtil { Team tempTeamVar = null; User tempUserVar = null; - tempUserVar = - Entity.getEntityByName(USER, thread.getCreatedBy(), "profile", Include.NON_DELETED); - users.put(tempUserVar.getId(), tempUserVar); - List mentions = MessageParser.getEntityLinks(thread.getMessage()); - for (MessageParser.EntityLink link : mentions) { - if (USER.equals(link.getEntityType())) { - tempUserVar = Entity.getEntity(link, "profile", Include.NON_DELETED); - users.put(tempUserVar.getId(), tempUserVar); - } else if (TEAM.equals(link.getEntityType())) { - tempTeamVar = Entity.getEntity(link, "", Include.NON_DELETED); - teams.put(tempTeamVar.getId(), tempTeamVar); - } - } - for (Post post : thread.getPosts()) { - tempUserVar = Entity.getEntityByName(USER, post.getFrom(), "profile", Include.NON_DELETED); - users.put(tempUserVar.getId(), tempUserVar); - mentions = MessageParser.getEntityLinks(post.getMessage()); + if (category.equals(SubscriptionDestination.SubscriptionCategory.MENTIONS)) { + List mentions = MessageParser.getEntityLinks(thread.getMessage()); for (MessageParser.EntityLink link : mentions) { if (USER.equals(link.getEntityType())) { tempUserVar = Entity.getEntity(link, "profile", Include.NON_DELETED); users.put(tempUserVar.getId(), tempUserVar); } else if (TEAM.equals(link.getEntityType())) { - tempTeamVar = Entity.getEntity(link, "profile", Include.NON_DELETED); + tempTeamVar = Entity.getEntity(link, "", Include.NON_DELETED); teams.put(tempTeamVar.getId(), tempTeamVar); } } + + for (Post post : thread.getPosts()) { + tempUserVar = Entity.getEntityByName(USER, post.getFrom(), "profile", Include.NON_DELETED); + users.put(tempUserVar.getId(), tempUserVar); + mentions = MessageParser.getEntityLinks(post.getMessage()); + for (MessageParser.EntityLink link : mentions) { + if (USER.equals(link.getEntityType())) { + tempUserVar = Entity.getEntity(link, "profile", Include.NON_DELETED); + users.put(tempUserVar.getId(), tempUserVar); + } else if (TEAM.equals(link.getEntityType())) { + tempTeamVar = Entity.getEntity(link, "profile", Include.NON_DELETED); + teams.put(tempTeamVar.getId(), tempTeamVar); + } + } + } + } + + if (category.equals(SubscriptionDestination.SubscriptionCategory.OWNERS)) { + try { + tempUserVar = + Entity.getEntityByName(USER, thread.getCreatedBy(), "profile", Include.NON_DELETED); + users.put(tempUserVar.getId(), tempUserVar); + } catch (Exception ex) { + LOG.warn("Thread created by unknown user: {}", thread.getCreatedBy()); + } } // Users @@ -340,8 +367,9 @@ public class SubscriptionUtil { if (event.getEntityType().equals(THREAD)) { Thread thread = AlertsRuleEvaluator.getThread(event); switch (thread.getType()) { - case Task -> receiverUrls.addAll(getTaskAssignees(type, event)); - case Conversation -> receiverUrls.addAll(handleConversationNotification(type, event)); + case Task -> receiverUrls.addAll(getTaskAssignees(category, type, event)); + case Conversation -> receiverUrls.addAll( + handleConversationNotification(category, type, event)); // TODO: For Announcement, Immediate Consumer needs to be Notified (find information from // Lineage) }