From 95ec7be10b0cf1dc49c85fd3fd8df191087b73a1 Mon Sep 17 00:00:00 2001 From: JFronny Date: Sat, 22 Oct 2022 23:55:55 +0200 Subject: [PATCH] More fixes, should work now --- build.gradle.kts | 1 + .../jfronny/gitea/helpdesk/UpdateTask.java | 30 +++++++++++-------- .../gitea/helpdesk/db/DBInterface.java | 24 +++++++-------- .../gitea/helpdesk/mail/MailInterface.java | 2 +- .../gitea/helpdesk/mail/WrappedMessage.java | 4 +++ src/main/resources/mail/create.html | 3 +- src/main/resources/mail/issue_deleted.html | 3 +- 7 files changed, 39 insertions(+), 28 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index d5eeb4b..afe755a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -25,6 +25,7 @@ dependencies { implementation("com.kohlschutter.junixsocket:junixsocket-core:2.6.0") implementation("org.jsoup:jsoup:1.15.3") implementation("net.freeutils:jlhttp:2.6") + implementation("org.commonmark:commonmark:0.20.0") compileOnly("org.jetbrains:annotations:23.0.0") } diff --git a/src/main/java/io/gitlab/jfronny/gitea/helpdesk/UpdateTask.java b/src/main/java/io/gitlab/jfronny/gitea/helpdesk/UpdateTask.java index becd4e5..424f813 100644 --- a/src/main/java/io/gitlab/jfronny/gitea/helpdesk/UpdateTask.java +++ b/src/main/java/io/gitlab/jfronny/gitea/helpdesk/UpdateTask.java @@ -8,11 +8,15 @@ import io.gitlab.jfronny.gitea.helpdesk.gitea.*; import io.gitlab.jfronny.gitea.helpdesk.mail.*; import io.gitlab.jfronny.gitea.helpdesk.web.WebInterface; import jakarta.mail.MessagingException; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; import java.io.FileNotFoundException; import java.io.IOException; public record UpdateTask(DBInterface db, MailInterface mail, GiteaInterface gitea, WebInterface web) implements Runnable { + private static final Parser MARKDOWN_PARSER = Parser.builder().build(); + private static final HtmlRenderer MARKDOWN_RENDER = HtmlRenderer.builder().build(); private static final String TEMPLATE = Main.getResource("/mail/template.html"); private static final String MAIL_ERROR = Main.getResource("/mail/error.html"); private static final String MAIL_UNEXPECTED = Main.getResource("/mail/unexpected.html"); @@ -42,7 +46,6 @@ public record UpdateTask(DBInterface db, MailInterface mail, GiteaInterface gite ThrowingBiConsumer reply = (content, subject) -> { String[] previousMessages = subscription.referenceChain().split(" "); String previousMessageId = previousMessages[previousMessages.length - 1]; - //TODO test mail.reply(addressParts[0] + "+reply+" + subscription.id(), subscription.email(), content, subject, previousMessageId, subscription.referenceChain(), null); }; GiteaIssue issue; @@ -53,13 +56,13 @@ public record UpdateTask(DBInterface db, MailInterface mail, GiteaInterface gite db.removeSubscription(subscription.id()); return; } - if (issue.state.equals("closed")) { //TODO test + if (issue.state.equals("closed")) { reply.accept(template(MAIL_ISSUE_CLOSED), "Issue closed"); db.removeSubscription(subscription.id()); } for (GiteaIssueComment comment : gitea.getComments(subscription.repoOwner(), subscription.repo(), subscription.issue())) { if (comment.id > subscription.issueComment()) { - reply.accept(comment.body, issue.title); + reply.accept(MARKDOWN_RENDER.render(MARKDOWN_PARSER.parse(comment.body)), issue.title); db.updateSubscriptionIssueComment(subscription.id(), comment.id); } } @@ -79,19 +82,20 @@ public record UpdateTask(DBInterface db, MailInterface mail, GiteaInterface gite String repo = args[2]; checkArgs(owner, repo); GiteaIssue issue = gitea.createIssue(owner, repo, message.getSubject(), formatBody(message)); - String id = db.addSubscription(message.getSender(), owner, repo, issue.id, 0, message.getSender(), true); - String unsubscribeUrl = web.getAddress() + "/unsubscribe?id=" + id; //TODO test - message.reply(addressParts[0] + "+reply+" + owner + "+" + repo + "+" + issue.id + '@' + addressParts[1], template(MAIL_CREATE.formatted(issue.url, unsubscribeUrl))); + String id = db.addSubscription(message.getSender(), owner, repo, issue.id, 0, message.getMessageID(), true); + String unsubscribeUrl = web.getAddress() + "/unsubscribe?id=" + id; + String closeUrl = web.getAddress() + "/close?id=" + id; + message.reply(addressParts[0] + "+reply+" + id + '@' + addressParts[1], template(MAIL_CREATE.formatted(issue.url, unsubscribeUrl, closeUrl))); } case "reply" -> { if (args.length != 2) throw new UnexpectedMailException("Reply classifier only allows one parameter"); Subscription sub = db.getSubscription(args[1]).orElseThrow(() -> new UnexpectedMailException("Reply classifier does not represent an active issue")); GiteaIssueComment commentId = gitea.addComment(sub.repoOwner(), sub.repo(), sub.issue(), formatBody(message)); db.updateSubscriptionIssueComment(sub.id(), commentId.id); - db.updateSubscriptionReferenceChain(sub.id(), sub.referenceChain() + " " + message.getSender()); + db.updateSubscriptionReferenceChain(sub.id(), sub.referenceChain() + " " + message.getMessageID()); } case "comment" -> { - if (args.length == 4) throw new UnexpectedMailException("Comment classifier requires four parameters"); + if (args.length != 4) throw new UnexpectedMailException("Comment classifier requires four parameters"); String owner = args[1]; String repo = args[2]; long issueId = Long.parseLong(args[3]); @@ -102,13 +106,13 @@ public record UpdateTask(DBInterface db, MailInterface mail, GiteaInterface gite } catch (FileNotFoundException fe) { throw new UnexpectedMailException("This issue does not exist"); } - gitea.addComment(owner, repo, issueId, formatBody(message)); + long issueComment = gitea.addComment(owner, repo, issueId, formatBody(message)).id; if (issue.state.equals("closed")) { - message.reply(addressParts[0] + "+reply+" + owner + "+" + repo + "+" + issue.id + '@' + addressParts[1], template(MAIL_COMMENT_CLOSED.formatted(issue.url))); + message.reply(mail.getAddress(), template(MAIL_COMMENT_CLOSED.formatted(issue.url))); } else { - String id = db.addSubscription(message.getSender(), owner, repo, issue.id, 0, message.getSender(), false); - String unsubscribeUrl = web.getAddress() + "/unsubscribe?id=" + id; //TODO test - message.reply(addressParts[0] + "+reply+" + owner + "+" + repo + "+" + issue.id + '@' + addressParts[1], template(MAIL_COMMENT.formatted(issue.url, unsubscribeUrl))); + String id = db.addSubscription(message.getSender(), owner, repo, issue.id, issueComment, message.getMessageID(), false); + String unsubscribeUrl = web.getAddress() + "/unsubscribe?id=" + id; + message.reply(addressParts[0] + "+reply+" + id + '@' + addressParts[1], template(MAIL_COMMENT.formatted(issue.url, unsubscribeUrl))); } } default -> throw new UnexpectedMailException("Did not expect classifier " + args[0]); diff --git a/src/main/java/io/gitlab/jfronny/gitea/helpdesk/db/DBInterface.java b/src/main/java/io/gitlab/jfronny/gitea/helpdesk/db/DBInterface.java index 605b038..a1b3284 100644 --- a/src/main/java/io/gitlab/jfronny/gitea/helpdesk/db/DBInterface.java +++ b/src/main/java/io/gitlab/jfronny/gitea/helpdesk/db/DBInterface.java @@ -21,22 +21,22 @@ public class DBInterface implements AutoCloseable { try (Connection cx = ds.getConnection()) { try (Statement st = cx.createStatement()) { st.executeUpdate(""" - CREATE EXTENSION pgcrypto; - CREATE OR REPLACE FUNCTION helpdesk_generate_uid(size INT) RETURNS TEXT AS $$ - DECLARE - characters TEXT := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - bytes BYTEA := gen_random_bytes(size); - l INT := length(characters); - i INT := 0; - output TEXT := ''; + create extension if not exists pgcrypto; + create or replace function helpdesk_generate_uid(size int) returns text as $$ + declare + characters text := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + bytes bytea := gen_random_bytes(size); + l int := length(characters); + i int := 0; + output text := ''; BEGIN - WHILE i < size LOOP + while i < size loop output := output || substr(characters, get_byte(bytes, i) % l + 1, 1); i := i + 1; - END LOOP; - RETURN output; + end loop; + return output; END; - $$ LANGUAGE plpgsql VOLATILE; + $$ language plpgsql volatile; create table if not exists helpdesk_subscriptions( id text primary key default helpdesk_generate_uid(20), email text, diff --git a/src/main/java/io/gitlab/jfronny/gitea/helpdesk/mail/MailInterface.java b/src/main/java/io/gitlab/jfronny/gitea/helpdesk/mail/MailInterface.java index 4eb6bdf..16e4b51 100644 --- a/src/main/java/io/gitlab/jfronny/gitea/helpdesk/mail/MailInterface.java +++ b/src/main/java/io/gitlab/jfronny/gitea/helpdesk/mail/MailInterface.java @@ -66,7 +66,7 @@ public class MailInterface implements AutoCloseable { MimeMessage reply = new MimeMessage(smtp); reply.setFrom(from); reply.addRecipients(Message.RecipientType.TO, to); - reply.setSubject(subject.startsWith("Re: ") ? subject : "Re: " + subject); + reply.setSubject(subject == null ? "Reply" : subject.startsWith("Re: ") ? subject : "Re: " + subject); final MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent(Jsoup.parse(content).text(), "text/plain"); diff --git a/src/main/java/io/gitlab/jfronny/gitea/helpdesk/mail/WrappedMessage.java b/src/main/java/io/gitlab/jfronny/gitea/helpdesk/mail/WrappedMessage.java index 765b320..b150ab2 100644 --- a/src/main/java/io/gitlab/jfronny/gitea/helpdesk/mail/WrappedMessage.java +++ b/src/main/java/io/gitlab/jfronny/gitea/helpdesk/mail/WrappedMessage.java @@ -91,6 +91,10 @@ public class WrappedMessage { return "Sender Suppressed"; } + public String getMessageID() throws MessagingException { + return message.getMessageID(); + } + public void reply(String from, String content) throws MessagingException { mail.reply( from, diff --git a/src/main/resources/mail/create.html b/src/main/resources/mail/create.html index 65f152c..a266103 100644 --- a/src/main/resources/mail/create.html +++ b/src/main/resources/mail/create.html @@ -1,4 +1,5 @@

Successfully added issue

You can find your new issue here

If you wish to unsubscribe from new comments, you may do so here

-

Please be aware that you will not be sent these a second time!

\ No newline at end of file +

Please be aware that you will not be sent these a second time!

+

If this was a mistake or you resolved the issue yourself, you can close it here

\ No newline at end of file diff --git a/src/main/resources/mail/issue_deleted.html b/src/main/resources/mail/issue_deleted.html index 5af0123..0c1320f 100644 --- a/src/main/resources/mail/issue_deleted.html +++ b/src/main/resources/mail/issue_deleted.html @@ -1,2 +1,3 @@

This issue was moved or deleted

-

This issue was moved or deleted and is no longer accessible

\ No newline at end of file +

This issue was moved or deleted and is no longer accessible

+

Your subscription has therefore been removed

\ No newline at end of file