Skip to content

Commit 2885adc

Browse files
committed
Translate channel referencs to channel names and escape the kudos content for json when sending it to the poster.
1 parent 6ada313 commit 2885adc

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

server/src/main/java/com/objectcomputing/checkins/notifications/social_media/SlackSearch.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ public String findChannelId(String channelName) {
5858
return null;
5959
}
6060

61+
public String findChannelName(String channelId) {
62+
String token = configuration.getApplication().getSlack().getBotToken();
63+
if (token != null) {
64+
try {
65+
MethodsClient client = Slack.getInstance().methods(token);
66+
ConversationsListResponse response = client.conversationsList(
67+
ConversationsListRequest.builder().build()
68+
);
69+
70+
if (response.isOk()) {
71+
for (Conversation conversation: response.getChannels()) {
72+
if (conversation.getId().equals(channelId)) {
73+
return conversation.getName();
74+
}
75+
}
76+
}
77+
} catch(IOException e) {
78+
LOG.error("SlackSearch.findChannelName: " + e.toString());
79+
} catch(SlackApiException e) {
80+
LOG.error("SlackSearch.findChannelName: " + e.toString());
81+
}
82+
}
83+
return null;
84+
}
85+
6186
public String findUserId(String userEmail) {
6287
String token = configuration.getApplication().getSlack().getBotToken();
6388
if (token != null) {

server/src/main/java/com/objectcomputing/checkins/services/slack/kudos/SlackKudosCreator.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private MemberProfile lookupUser(String userId) {
9292
}
9393

9494
private String processText(String text, List<UUID> recipients) {
95+
// First, process user references.
9596
StringBuffer buffer = new StringBuffer(text.length());
9697
Pattern userRef = Pattern.compile("<@([^>]+)>");
9798
Matcher action = userRef.matcher(StringEscapeUtils.unescapeHtml4(text));
@@ -107,6 +108,25 @@ private String processText(String text, List<UUID> recipients) {
107108
MemberProfileUtils.getFullName(profile)));
108109
}
109110
action.appendTail(buffer);
111+
text = buffer.toString();
112+
113+
// Next, translate channel references to channel names.
114+
Pattern channelRef = Pattern.compile("<#([^>]+)\\|>");
115+
buffer = new StringBuffer(text.length());
116+
action = channelRef.matcher(text);
117+
while (action.find()) {
118+
// Get the name of the channel.
119+
String channelId = action.group(1);
120+
String name = slackSearch.findChannelName(channelId);
121+
if (name == null) {
122+
name = "unknown_channel";
123+
}
124+
name = "#" + name;
125+
126+
// Replace the channel reference with the channel name.
127+
action.appendReplacement(buffer, Matcher.quoteReplacement(name));
128+
}
129+
action.appendTail(buffer);
110130
return buffer.toString();
111131
}
112132

@@ -137,7 +157,7 @@ private String getSlackBlocks(String kudosUUID, String contents) {
137157
try {
138158
return String.format(IOUtils.readText(
139159
new BufferedReader(kudosSlackBlocks.asReader())),
140-
kudosUUID, contents);
160+
kudosUUID, StringEscapeUtils.escapeJson(contents));
141161
} catch(Exception ex) {
142162
LOG.error("SlackKudosCreator.getSlackBlocks: " + ex.toString());
143163
return "";

0 commit comments

Comments
 (0)