ContactMessage.java

  1. package no.nav.data.team.contact.domain;

  2. import lombok.Value;
  3. import no.nav.data.team.contact.domain.ContactMessage.Paragraph.VarselUrl;
  4. import no.nav.data.team.integration.slack.dto.SlackDtos.PostMessageRequest.Block;
  5. import org.springframework.util.Assert;

  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;

  9. import static no.nav.data.common.utils.StreamUtils.convert;
  10. import static no.nav.data.team.contact.domain.ContactMessage.Paragraph.VarselUrl.url;

  11. @Value
  12. public class ContactMessage {

  13.     String title;
  14.     String sourceName;
  15.     List<Paragraph> paragraphs = new ArrayList<>();

  16.     public ContactMessage spacing() {
  17.         return paragraph(" ");
  18.     }

  19.     public ContactMessage paragraph(String val) {
  20.         Assert.isTrue(val != null && val.length() > 0, "cannot be empty");
  21.         paragraphs.add(new Paragraph(val, sourceName, List.of()));
  22.         return this;
  23.     }

  24.     public ContactMessage paragraph(String val, VarselUrl... urls) {
  25.         paragraphs.add(new Paragraph(val, sourceName, Arrays.asList(urls)));
  26.         return this;
  27.     }

  28.     public ContactMessage footer(String baseUrl) {
  29.         return spacing()
  30.                 .paragraph("%s - mvh %s", url(baseUrl, "Teamkatalog"), url("slack://channel?team=T5LNAMWNA&id=CG2S8D25D", "teamkatalogen"));
  31.     }

  32.     @Value
  33.     public static class Paragraph {

  34.         String val;
  35.         String sourceName;
  36.         List<VarselUrl> urls;

  37.         private String toSlack() {
  38.             var urlsFormatted = convert(urls, u -> "<%s%s|%s>".formatted(u.url, source(u), u.name));
  39.             return val.formatted(urlsFormatted.toArray());
  40.         }

  41.         private String toHtml() {
  42.             var urlsFormatted = convert(urls, u -> "<a href=\"%s%s\">%s</a>".formatted(u.url, source(u), u.name));
  43.             return val.formatted(urlsFormatted.toArray());
  44.         }

  45.         private String source(VarselUrl u) {
  46.             return (u.url.contains("?") ? "&" : "?") + "source=" + sourceName;
  47.         }

  48.         @Value
  49.         public static class VarselUrl {

  50.             String url;
  51.             String name;

  52.             public static VarselUrl url(String url, String name) {
  53.                 return new VarselUrl(url, name);
  54.             }
  55.         }

  56.     }

  57.     public List<Block> toSlack() {
  58.         var blocks = new ArrayList<Block>();
  59.         blocks.add(Block.header(title));

  60.         paragraphs.forEach(p -> blocks.add(Block.text(p.toSlack())));

  61.         return blocks;
  62.     }

  63.     public String toHtml() {
  64.         return "<h1>%s</h1>".formatted(title) +
  65.                 String.join("\n", convert(paragraphs, p -> "<p>%s</p>".formatted(p.toHtml())));
  66.     }

  67. }