aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2024-03-29 21:50:19 +0300
committerGravatar Vitaly Takmazov2024-03-29 22:08:34 +0300
commitc172469d601220c3a3b3c35e0de3409b7e23e78c (patch)
tree3a2142fc1916ff467d3466acef9c45d2ff4566c5 /src/main/java/com/juick/service
parentc1ce093dacb09f164ae31c2bc01a04ce5cac3184 (diff)
Disable caching in development mode
Diffstat (limited to 'src/main/java/com/juick/service')
-rw-r--r--src/main/java/com/juick/service/MessagesService.java6
-rw-r--r--src/main/java/com/juick/service/MessagesServiceImpl.java39
2 files changed, 43 insertions, 2 deletions
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<String> renderPlaintext(String body, String messageUrl);
+
+ Optional<String> 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<Message> {
@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<String> renderPlaintext(String body, String messageUrl) {
+ PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/plaintext");
+ Map<String, Object> 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<String> renderHtml(String body, String messageUrl, Message msg, String hash) {
+ PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/html");
+ Map<String, Object> 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();
+ }
+ }
}