Skip to content

Commit

Permalink
GH-159 Store mentioned members in database instead of cache. (#159)
Browse files Browse the repository at this point in the history
* Store mentioned members in database instead of cache.

* Add null check for guild and member in leaderboard
  • Loading branch information
vLuckyyy authored Jun 29, 2024
1 parent 0c3172d commit de65d66
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 94 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/eternalcode/discordapp/DiscordApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import com.eternalcode.discordapp.review.GitHubReviewService;
import com.eternalcode.discordapp.review.GitHubReviewTask;
import com.eternalcode.discordapp.review.command.GitHubReviewCommand;
import com.eternalcode.discordapp.review.database.GitHubReviewMentionRepository;
import com.eternalcode.discordapp.review.database.GitHubReviewMentionRepositoryImpl;
import com.eternalcode.discordapp.user.UserRepositoryImpl;
import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
Expand Down Expand Up @@ -67,6 +69,7 @@ public class DiscordApp {

private static ExperienceService experienceService;
private static LevelService levelService;
private static GitHubReviewService gitHubReviewService;

public static void main(String... args) throws InterruptedException {
ObserverRegistry observerRegistry = new ObserverRegistry();
Expand Down Expand Up @@ -96,9 +99,12 @@ public static void main(String... args) throws InterruptedException {
DatabaseManager databaseManager = new DatabaseManager(databaseConfig, new File("database"));
databaseManager.connect();
UserRepositoryImpl.create(databaseManager);
GitHubReviewMentionRepository gitHubReviewMentionRepository =
GitHubReviewMentionRepositoryImpl.create(databaseManager);

experienceService = new ExperienceService(databaseManager, observerRegistry);
levelService = new LevelService(databaseManager);
gitHubReviewService = new GitHubReviewService(config, configManager, gitHubReviewMentionRepository);
}
catch (SQLException exception) {
Sentry.captureException(exception);
Expand All @@ -112,8 +118,6 @@ public static void main(String... args) throws InterruptedException {
FilterService filterService = new FilterService()
.registerFilter(new RenovateForcedPushFilter());

GitHubReviewService gitHubReviewService = new GitHubReviewService(config, configManager);

CommandClient commandClient = new CommandClientBuilder()
.setOwnerId(config.topOwnerId)
.setActivity(Activity.playing("IntelliJ IDEA"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.eternalcode.discordapp.leveling.Level;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
Expand Down Expand Up @@ -70,7 +72,19 @@ private void updateLeaderboard(ButtonInteractionEvent event, int currentPage) {

for (Level level : top) {
int userLevel = level.getCurrentLevel();
String effectiveName = event.getGuild().getMemberById(level.getId()).getEffectiveName();
Guild guild = event.getGuild();

if (guild == null) {
continue;
}

Member member = guild.getMemberById(level.getId());

if (member == null) {
continue;
}

String effectiveName = member.getEffectiveName();

leaderboardContent.append(this.leaderboardService.formatLeaderboardEntry(index, effectiveName, userLevel)).append("\n");
index++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.entities.emoji.Emoji;

Expand Down Expand Up @@ -45,7 +47,19 @@ public void execute(SlashCommandEvent event) {

for (Level level : top) {
int userLevel = level.getCurrentLevel();
String effectiveName = event.getGuild().getMemberById(level.getId()).getEffectiveName();
Guild guild = event.getGuild();

if (guild == null) {
continue;
}

Member member = guild.getMemberById(level.getId());

if (member == null) {
continue;
}

String effectiveName = member.getEffectiveName();

leaderboardContent.append(this.leaderboardService.formatLeaderboardEntry(index, effectiveName, userLevel)).append("\n");
index++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GitHubPullRequest {
private final String repository;
private final int number;

private GitHubPullRequest(String owner, String repository, int number) {
public GitHubPullRequest(String owner, String repository, int number) {
this.owner = owner;
this.repository = repository;
this.number = number;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.eternalcode.discordapp.config.AppConfig;
import com.eternalcode.discordapp.config.ConfigManager;
import com.eternalcode.discordapp.review.database.GitHubReviewMentionRepository;
import io.sentry.Sentry;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
Expand All @@ -21,16 +23,20 @@ public class GitHubReviewService {
private final static Logger LOGGER = Logger.getLogger(GitHubReviewService.class.getName());

private static final String DM_REVIEW_MESSAGE = "You have been assigned as a reviewer for this pull request: %s";
private static final String SERVER_REVIEW_MESSAGE =
"%s, you have been assigned as a reviewer for this pull request: %s";
private static final String SERVER_REVIEW_MESSAGE = "%s, you have been assigned as a reviewer for this pull request: %s";

private final AppConfig appConfig;
private final ConfigManager configManager;
private final GitHubReviewMentionRepository mentionRepository = new GitHubReviewMentionRepositoryImpl();
private final GitHubReviewMentionRepository mentionRepository;

public GitHubReviewService(AppConfig appConfig, ConfigManager configManager) {
public GitHubReviewService(
AppConfig appConfig,
ConfigManager configManager,
GitHubReviewMentionRepository mentionRepository
) {
this.appConfig = appConfig;
this.configManager = configManager;
this.mentionRepository = mentionRepository;
}

public String createReview(Guild guild, String url, JDA jda) {
Expand Down Expand Up @@ -94,6 +100,7 @@ public void mentionReviewers(JDA jda, GitHubPullRequest pullRequest, long forumI
}

StringBuilder reviewersMention = new StringBuilder();
CompletableFuture<Void> mentionFuture = CompletableFuture.completedFuture(null);

for (String reviewer : assignedReviewers) {
GitHubReviewUser gitHubReviewUser = this.getReviewUserByUsername(reviewer);
Expand All @@ -104,44 +111,52 @@ public void mentionReviewers(JDA jda, GitHubPullRequest pullRequest, long forumI

Long discordId = gitHubReviewUser.getDiscordId();

if (discordId != null && !this.mentionRepository.isMentioned(pullRequest, discordId)) {
User user = jda.getUserById(discordId);
GitHubReviewNotificationType notificationType = gitHubReviewUser.getNotificationType();

if (user == null) {
return;
}

String message = String.format(DM_REVIEW_MESSAGE, pullRequest.toUrl());

if (notificationType.isDmNotify()) {
try {
LOGGER.info("Sending message to: " + user.getName());
user.openPrivateChannel().queue(
privateChannel -> privateChannel.sendMessage(message).queue(),
throwable -> LOGGER.warning("Cannot send message to: " + user.getName()));
if (discordId != null) {
mentionFuture = mentionFuture.thenComposeAsync(v ->
this.mentionRepository.isMentioned(pullRequest, discordId)
).thenAcceptAsync(isMentioned -> {
if (!isMentioned) {
User user = jda.getUserById(discordId);
GitHubReviewNotificationType notificationType = gitHubReviewUser.getNotificationType();

if (user == null) {
return;
}

String message = String.format(DM_REVIEW_MESSAGE, pullRequest.toUrl());

if (notificationType.isDmNotify()) {
try {
LOGGER.info("Sending message to: " + user.getName());
user.openPrivateChannel().queue(
privateChannel -> privateChannel.sendMessage(message).queue(),
throwable -> LOGGER.warning("Cannot send message to: " + user.getName()));
}
catch (Exception exception) {
Sentry.captureException(exception);
LOGGER.warning("Cannot send message to: " + user.getName());
}
}
if (notificationType.isServerNotify()) {
reviewersMention.append(user.getAsMention()).append(" ");
}

this.mentionRepository.markReviewerAsMentioned(pullRequest, discordId);
}
catch (Exception exception) {
Sentry.captureException(exception);
LOGGER.warning("Cannot send message to: " + user.getName());
}
}
if (notificationType.isServerNotify()) {
reviewersMention.append(user.getAsMention()).append(" ");
}

this.mentionRepository.markReviewerAsMentioned(pullRequest, discordId);
});
}
}

if (!reviewersMention.isEmpty()) {
String message = String.format(SERVER_REVIEW_MESSAGE, reviewersMention, pullRequest.toUrl());
ThreadChannel threadChannel = jda.getThreadChannelById(forumId);
mentionFuture.thenRunAsync(() -> {
if (!reviewersMention.isEmpty()) {
String message = String.format(SERVER_REVIEW_MESSAGE, reviewersMention, pullRequest.toUrl());
ThreadChannel threadChannel = jda.getThreadChannelById(forumId);

if (threadChannel != null) {
threadChannel.sendMessage(message).queue();
if (threadChannel != null) {
threadChannel.sendMessage(message).queue();
}
}
}
});
}

public void mentionReviewersOnAllReviewChannels(JDA jda) {
Expand Down Expand Up @@ -204,17 +219,17 @@ public void archiveMergedPullRequest(JDA jda) {

if (GitHubReviewUtil.isPullRequestMerged(pullRequest, this.appConfig.githubToken)) {
threadChannel.getManager()
.setAppliedTags(ForumTagSnowflake.fromId(reviewSystem.mergedTagId))
.setLocked(true)
.setArchived(true)
.setAppliedTags(ForumTagSnowflake.fromId(reviewSystem.mergedTagId))
.queue();
}

if (GitHubReviewUtil.isPullRequestClosed(pullRequest, this.appConfig.githubToken)) {
threadChannel.getManager()
.setAppliedTags(ForumTagSnowflake.fromId(reviewSystem.closedTagId))
.setLocked(true)
.setArchived(true)
.setAppliedTags(ForumTagSnowflake.fromId(reviewSystem.closedTagId))
.queue();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eternalcode.discordapp.review;

import io.sentry.Sentry;
import net.dv8tion.jda.api.JDA;

import java.util.TimerTask;
Expand All @@ -16,7 +17,14 @@ public GitHubReviewTask(GitHubReviewService gitHubReviewService, JDA jda) {

@Override
public void run() {
this.gitHubReviewService.archiveMergedPullRequest(this.jda);
this.gitHubReviewService.mentionReviewersOnAllReviewChannels(this.jda);
try {
this.gitHubReviewService.archiveMergedPullRequest(this.jda);
this.gitHubReviewService.mentionReviewersOnAllReviewChannels(this.jda);
System.out.println("GitHub review task executed");
}
catch (Exception exception) {
Sentry.captureException(exception);
exception.printStackTrace();
}
}
}

0 comments on commit de65d66

Please sign in to comment.