All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.blazar.resources.UserFeedbackResource Maven / Gradle / Ivy

The newest version!
package com.hubspot.blazar.resources;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import com.hubspot.blazar.externalservice.slack.SlackAttachment;
import com.hubspot.blazar.externalservice.slack.SlackAttachmentField;
import com.hubspot.blazar.externalservice.slack.SlackMessage;
import com.hubspot.blazar.base.feedback.Feedback;
import com.hubspot.blazar.config.BlazarSlackConfiguration;
import com.hubspot.blazar.integration.slack.SlackClient;

@Path("/user-feedback")
public class UserFeedbackResource {
  private final SlackClient slackClient;
  private BlazarSlackConfiguration blazarSlackConfiguration;

  @Inject
  public UserFeedbackResource(SlackClient slackClient) {
    this.slackClient = slackClient;
    this.blazarSlackConfiguration = slackClient.getBlazarSlackConfiguration();
  }

  @POST
  public void handleFeedback(Feedback feedback) throws IOException {
    if (!blazarSlackConfiguration.getFeedbackRoom().isPresent()) {
      throw new WebApplicationException(new Throwable("To use the feedback endpoint the feedbackRoom needs to be set in BlazarSlackConfiguration"));
    }

    List fields = new ArrayList<>();
    Optional absentString = Optional.absent();
    if (feedback.getOther().isPresent()) {
      SlackAttachmentField attachedField = new SlackAttachmentField("OtherData", feedback.getOther().get(), false);
      fields.add(attachedField);
    }

    Optional title = Optional.of(String.format("New Feedback from %s", feedback.getUsername()));
    String fallback = String.format("New Feedback from %s: %s", feedback.getUsername(), feedback.getMessage());
    Optional link = Optional.of(feedback.getPage());
    Optional message = Optional.of(feedback.getMessage());

    SlackAttachment attachment = new SlackAttachment(fallback, absentString, absentString, absentString, absentString, absentString, title, link, message, fields, absentString);

    slackClient.sendMessage(new SlackMessage(absentString, absentString, absentString, Optional.of(":fire:"), blazarSlackConfiguration.getFeedbackRoom().get(), ImmutableList.of(attachment)));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy