Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.slack.api.methods.response.conversations.ConversationsListResponse;
import com.slack.api.methods.request.users.UsersLookupByEmailRequest;
import com.slack.api.methods.response.users.UsersLookupByEmailResponse;
import com.slack.api.methods.request.users.UsersInfoRequest;
import com.slack.api.methods.response.users.UsersInfoResponse;

import jakarta.inject.Singleton;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -76,5 +78,26 @@ public String findUserId(String userEmail) {
}
return null;
}

public String findUserEmail(String userId) {
String token = configuration.getApplication().getNotifications().getSlack().getBotToken();
if (token != null) {
try {
MethodsClient client = Slack.getInstance().methods(token);
UsersInfoResponse response = client.usersInfo(
UsersInfoRequest.builder().user(userId).build()
);

if (response.isOk()) {
return response.getUser().getProfile().getEmail();
}
} catch(IOException e) {
LOG.error("SlackSearch.findUserEmail: " + e.toString());
} catch(SlackApiException e) {
LOG.error("SlackSearch.findUserEmail: " + e.toString());
}
}
return null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,18 @@ public class PulseResponseController {
private final MemberProfileServices memberProfileServices;
private final SlackSignatureVerifier slackSignatureVerifier;
private final PulseSlackCommand pulseSlackCommand;
private final SlackPulseResponseConverter slackPulseResponseConverter;

public PulseResponseController(PulseResponseService pulseResponseServices,
MemberProfileServices memberProfileServices,
SlackSignatureVerifier slackSignatureVerifier,
PulseSlackCommand pulseSlackCommand) {
PulseSlackCommand pulseSlackCommand,
SlackPulseResponseConverter slackPulseResponseConverter) {
this.pulseResponseServices = pulseResponseServices;
this.memberProfileServices = memberProfileServices;
this.slackSignatureVerifier = slackSignatureVerifier;
this.pulseSlackCommand = pulseSlackCommand;
this.slackPulseResponseConverter = slackPulseResponseConverter;
}

/**
Expand Down Expand Up @@ -169,7 +172,7 @@ public HttpResponse<PulseResponse> externalPulseResponse(
final String key = "payload";
if (body.containsKey(key)) {
PulseResponseCreateDTO pulseResponseDTO =
SlackPulseResponseConverter.get(memberProfileServices,
slackPulseResponseConverter.get(memberProfileServices,
(String)body.get(key));

// DEBUG Only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.objectcomputing.checkins.exceptions.BadArgException;
import com.objectcomputing.checkins.services.memberprofile.MemberProfile;
import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices;
import com.objectcomputing.checkins.notifications.social_media.SlackSearch;

import jakarta.inject.Singleton;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -15,10 +18,17 @@
import java.util.UUID;
import java.time.LocalDate;

@Singleton
public class SlackPulseResponseConverter {
private static final Logger LOG = LoggerFactory.getLogger(SlackPulseResponseConverter.class);

public static PulseResponseCreateDTO get(
private final SlackSearch slackSearch;

public SlackPulseResponseConverter(SlackSearch slackSearch) {
this.slackSearch = slackSearch;
}

public PulseResponseCreateDTO get(
MemberProfileServices memberProfileServices, String body) {
try {
// Get the map of values from the string body
Expand Down Expand Up @@ -59,8 +69,8 @@ public static PulseResponseCreateDTO get(
}
}

private static String getMappedValue(Map<String, Object> map,
String key, boolean required) {
private String getMappedValue(Map<String, Object> map,
String key, boolean required) {
final String valueKey = "value";
if (map.containsKey(key)) {
final Map<String, Object> other = (Map<String, Object>)map.get(key);
Expand All @@ -78,14 +88,16 @@ private static String getMappedValue(Map<String, Object> map,
}
}

private static UUID lookupUser(MemberProfileServices memberProfileServices,
Map<String, Object> map) {
private UUID lookupUser(MemberProfileServices memberProfileServices,
Map<String, Object> map) {
// Get the user's profile map.
Map<String, Object> user = (Map<String, Object>)map.get("user");
Map<String, Object> profile = (Map<String, Object>)user.get("profile");

// Lookup the user based on the email address.
String email = (String)profile.get("email");
String email = slackSearch.findUserEmail((String)user.get("id"));
if (email == null) {
throw new BadArgException("Unable to find the user email address");
}
MemberProfile member = memberProfileServices.findByWorkEmail(email);
return member.getId();
}
Expand Down
Loading