Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.Message.Attachment;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.javadiscord.javabot.data.config.BotConfig;
Expand All @@ -25,6 +26,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -108,7 +110,7 @@ public void cache(Message message) {
public void sendUpdatedMessageToLog(Message updated, CachedMessage before) {
MessageCacheConfig config = botConfig.get(updated.getGuild()).getMessageCacheConfig();
if (config.getMessageCacheLogChannel() == null) return;
if (updated.getContentRaw().trim().equals(before.getMessageContent())) return;
if (updated.getContentRaw().trim().equals(before.getMessageContent()) && updated.getAttachments().size() == before.getAttachments().size()) return;
MessageCreateAction action = config.getMessageCacheLogChannel()
.sendMessageEmbeds(buildMessageEditEmbed(updated.getGuild(), updated.getAuthor(), updated.getChannel(), before, updated))
.setActionRow(Button.link(updated.getJumpUrl(), "Jump to Message"));
Expand Down Expand Up @@ -148,26 +150,49 @@ private EmbedBuilder buildMessageCacheEmbed(MessageChannel channel, User author,
}

private MessageEmbed buildMessageEditEmbed(Guild guild, User author, MessageChannel channel, CachedMessage before, Message after) {
return buildMessageCacheEmbed(channel, author, before)
EmbedBuilder eb = buildMessageCacheEmbed(channel, author, before)
.setTitle("Message Edited")
.setColor(Responses.Type.WARN.getColor())
.addField("Before", before.getMessageContent().substring(0, Math.min(
before.getMessageContent().length(),
MessageEmbed.VALUE_MAX_LENGTH)), false)
.addField("After", after.getContentRaw().substring(0, Math.min(
after.getContentRaw().length(),
MessageEmbed.VALUE_MAX_LENGTH)), false)
MessageEmbed.VALUE_MAX_LENGTH)), false);
if(before.getAttachments().size() != after.getAttachments().size()) {
eb.addField("Deleted Attachments",
before
.getAttachments()
.stream()
.filter(attachment -> after//not present in 'after'
.getAttachments()
.stream()
.map(Attachment::getProxyUrl)
.noneMatch(attachment::equals))
.collect(Collectors.joining("\n")),
false);
}
return eb
.build();
}

private MessageEmbed buildMessageDeleteEmbed(Guild guild, User author, MessageChannel channel, CachedMessage message) {
return buildMessageCacheEmbed(channel, author, message)
EmbedBuilder eb = buildMessageCacheEmbed(channel, author, message)
.setTitle("Message Deleted")
.setColor(Responses.Type.ERROR.getColor())
.addField("Message Content",
message.getMessageContent().substring(0, Math.min(
message.getMessageContent().length(),
MessageEmbed.VALUE_MAX_LENGTH)), false)
MessageEmbed.VALUE_MAX_LENGTH)), false);
if (!message.getAttachments().isEmpty()) {
eb.addField("Attachments",
message
.getAttachments()
.stream()
.collect(Collectors.joining("\n")),
false);
}
return eb
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
* Dao class that represents the QOTW_POINTS SQL Table.
Expand All @@ -20,20 +23,6 @@
public class MessageCacheRepository {
private final JdbcTemplate jdbcTemplate;

/**
* Inserts a new {@link CachedMessage} object.
*
* @param message The Message to insert.
* @return Whether there were rows affected by this process.
* @throws SQLException If an error occurs.
*/
public boolean insert(CachedMessage message) throws DataAccessException {
int rows = jdbcTemplate.update(
"INSERT INTO message_cache (message_id, author_id, message_content) VALUES (?, ?, ?)",
message.getMessageId(), message.getAuthorId(), message.getMessageContent());
return rows > 0;
}

/**
* Inserts a {@link List} of {@link CachedMessage} objects.
*
Expand All @@ -57,31 +46,30 @@ public int getBatchSize() {
return messages.size();
}
});
}
List<Map.Entry<CachedMessage, Integer>> attachments=new ArrayList<>();
for (CachedMessage msg : messages) {
for (int i = 0; i < msg.getAttachments().size(); i++) {
attachments.add(Map.entry(msg, i));
}
}
jdbcTemplate.batchUpdate("MERGE INTO message_cache_attachments (message_id, attachment_index, link) VALUES (?, ?, ?)",
new BatchPreparedStatementSetter() {

/**
* Edit an existing {@link CachedMessage} object.
*
* @param message The new Message object.
* @return Whether there were rows affected by this process.
* @throws SQLException If an error occurs.
*/
public boolean update(@NotNull CachedMessage message) throws DataAccessException {
int rows = jdbcTemplate.update("UPDATE message_cache SET message_content = ? WHERE message_id = ?",
message.getMessageContent(), message.getMessageId());
return rows > 0;
}
@Override
public void setValues(PreparedStatement stmt, int i) throws SQLException {
Entry<CachedMessage, Integer> entry = attachments.get(i);
CachedMessage msg = entry.getKey();
Integer attachmentIndex = entry.getValue();
stmt.setLong(1, msg.getMessageId());
stmt.setInt(2, attachmentIndex);
stmt.setString(3, msg.getAttachments().get(attachmentIndex));
}

/**
* Deletes a single {@link CachedMessage} object from the Message Cache.
*
* @param messageId The message's id.
* @return Whether there were rows affected by this process.
* @throws SQLException If an error occurs.
*/
public boolean delete(long messageId) throws DataAccessException {
int rows = jdbcTemplate.update("DELETE FROM message_cache WHERE message_id = ?", messageId);
return rows > 0;
@Override
public int getBatchSize() {
return attachments.size();
}
});
}

/**
Expand All @@ -91,7 +79,17 @@ public boolean delete(long messageId) throws DataAccessException {
* @throws SQLException If anything goes wrong.
*/
public List<CachedMessage> getAll() throws DataAccessException {
return jdbcTemplate.query("SELECT * FROM message_cache",(RowMapper<CachedMessage>) (rs, rowNum) -> this.read(rs));
List<CachedMessage> messagesWithLink = jdbcTemplate.query(
"SELECT * FROM message_cache LEFT JOIN message_cache_attachments ON message_cache.message_id = message_cache_attachments.message_id",
(rs, rowNum) -> this.read(rs));
Map<Long, CachedMessage> messages=new LinkedHashMap<>();
for (CachedMessage msg : messagesWithLink) {
CachedMessage previous = messages.putIfAbsent(msg.getMessageId(), msg);
if(previous!=null) {
previous.getAttachments().addAll(msg.getAttachments());
}
}
return new ArrayList<>(messages.values());
}

/**
Expand All @@ -103,14 +101,22 @@ public List<CachedMessage> getAll() throws DataAccessException {
*/
public boolean delete(int amount) throws DataAccessException {
int rows = jdbcTemplate.update("DELETE FROM message_cache LIMIT ?", amount);
return rows > 0;
if(rows > 0){
jdbcTemplate.update("DELETE FROM message_cache_attachments WHERE message_id NOT IN (SELECT message_id FROM message_cache)");
return true;
}
return false;
}

private CachedMessage read(ResultSet rs) throws SQLException {
CachedMessage cachedMessage = new CachedMessage();
cachedMessage.setMessageId(rs.getLong("message_id"));
cachedMessage.setMessageId(rs.getLong("message_cache.message_id"));
cachedMessage.setAuthorId(rs.getLong("author_id"));
cachedMessage.setMessageContent(rs.getString("message_content"));
String attachment = rs.getString("link");
if(attachment!=null) {
cachedMessage.getAttachments().add(attachment);
}
return cachedMessage;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package net.javadiscord.javabot.data.h2db.message_cache.model;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Message.Attachment;

/**
* Represents a cached Message.
Expand All @@ -11,6 +15,7 @@ public class CachedMessage {
private long messageId;
private long authorId;
private String messageContent;
private List<String> attachments=new ArrayList<>();

/**
* Converts a {@link Message} object to a {@link CachedMessage}.
Expand All @@ -23,6 +28,11 @@ public static CachedMessage of(Message message) {
cachedMessage.setMessageId(message.getIdLong());
cachedMessage.setAuthorId(message.getAuthor().getIdLong());
cachedMessage.setMessageContent(message.getContentRaw().trim());
cachedMessage.attachments = message
.getAttachments()
.stream()
.map(Attachment::getProxyUrl)
.toList();
return cachedMessage;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE message_cache_attachments (
message_id BIGINT NOT NULL,
attachment_index INT NOT NULL,
link VARCHAR(255),
PRIMARY KEY(message_id, attachment_index)
)
6 changes: 6 additions & 0 deletions src/main/resources/database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ CREATE TABLE message_cache
author_id BIGINT NOT NULL,
message_content VARCHAR(4000) NOT NULL
);
CREATE TABLE message_cache_attachments (
message_id BIGINT NOT NULL,
attachment_index INT NOT NULL,
link VARCHAR(255),
PRIMARY KEY(message_id, attachment_index)
)

// User Preferences
CREATE TABLE user_preferences
Expand Down