From c172469d601220c3a3b3c35e0de3409b7e23e78c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 29 Mar 2024 21:50:19 +0300 Subject: Disable caching in development mode --- .../java/com/juick/service/MessagesService.java | 6 +++- .../com/juick/service/MessagesServiceImpl.java | 39 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) (limited to 'src/main/java/com/juick/service') diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index 6f262dd7..f2321b2a 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2023, Juick + * Copyright (C) 2008-2024, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -165,4 +165,8 @@ public interface MessagesService { @CacheEvict(value = { "discover", "discussions", "messages", "replies" }, allEntries = true) void setFriendsOnly(Integer mid, Boolean friendsOnly); + + Optional renderPlaintext(String body, String messageUrl); + + Optional renderHtml(String body, String messageUrl, Message msg, String hash); } diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index 91a257ef..940cdaba 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2021, Juick + * Copyright (C) 2008-2024, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -23,6 +23,8 @@ import com.juick.www.WebApp; import com.juick.util.MessageUtils; import com.juick.util.TagUtils; +import io.pebbletemplates.pebble.PebbleEngine; +import io.pebbletemplates.pebble.template.PebbleTemplate; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -43,6 +45,9 @@ import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import javax.inject.Inject; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; import java.net.URI; import java.sql.*; import java.time.Instant; @@ -71,6 +76,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ private String baseImagesUrl; @Inject private User archiveUser; + @Inject + private PebbleEngine pebbleEngine; private class MessageMapper implements RowMapper { @Override @@ -1208,4 +1215,34 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ int privacy = friendsOnly ? -1 : 1; jdbcTemplate.update("UPDATE messages SET privacy=? WHERE message_id=?", privacy, mid); } + + public Optional renderPlaintext(String body, String messageUrl) { + PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/plaintext"); + Map context = new HashMap<>(); + context.put("messageBody", body); + context.put("messageUrl", messageUrl); + try { + Writer writer = new StringWriter(); + noteTemplate.evaluate(writer, context); + return Optional.of(writer.toString()); + } catch (IOException e) { + return Optional.empty(); + } + } + + public Optional renderHtml(String body, String messageUrl, Message msg, String hash) { + PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/html"); + Map context = new HashMap<>(); + context.put("messageBody", body); + context.put("messageUrl", messageUrl); + context.put("msg", msg); + context.put("hash", hash); + try { + Writer writer = new StringWriter(); + noteTemplate.evaluate(writer, context); + return Optional.of(writer.toString()); + } catch (IOException e) { + return Optional.empty(); + } + } } -- cgit v1.2.3