|
| 1 | +package com.eternalcode.discordapp.review.database; |
| 2 | + |
| 3 | +import com.eternalcode.discordapp.database.DataAccessException; |
| 4 | +import com.eternalcode.discordapp.database.DatabaseManager; |
| 5 | +import com.eternalcode.discordapp.database.repository.AbstractRepository; |
| 6 | +import com.eternalcode.discordapp.review.GitHubPullRequest; |
| 7 | +import com.j256.ormlite.table.TableUtils; |
| 8 | +import io.sentry.Sentry; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.time.Duration; |
| 11 | +import java.time.Instant; |
| 12 | +import java.util.concurrent.CompletableFuture; |
| 13 | + |
| 14 | +public class GitHubReviewMentionRepositoryImpl extends AbstractRepository<GitHubReviewMentionWrapper, String> |
| 15 | + implements GitHubReviewMentionRepository { |
| 16 | + |
| 17 | + private static final Duration MENTION_INTERVAL = Duration.ofHours(12); |
| 18 | + |
| 19 | + public GitHubReviewMentionRepositoryImpl(DatabaseManager databaseManager) { |
| 20 | + super(databaseManager, GitHubReviewMentionWrapper.class); |
| 21 | + } |
| 22 | + |
| 23 | + public static GitHubReviewMentionRepository create(DatabaseManager databaseManager) { |
| 24 | + try { |
| 25 | + TableUtils.createTableIfNotExists(databaseManager.getConnectionSource(), GitHubReviewMentionWrapper.class); |
| 26 | + } |
| 27 | + catch (SQLException sqlException) { |
| 28 | + Sentry.captureException(sqlException); |
| 29 | + throw new DataAccessException("Failed to create table", sqlException); |
| 30 | + } |
| 31 | + |
| 32 | + return new GitHubReviewMentionRepositoryImpl(databaseManager); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public CompletableFuture<Void> markReviewerAsMentioned(GitHubPullRequest pullRequest, long userId) { |
| 37 | + return CompletableFuture.runAsync(() -> { |
| 38 | + GitHubReviewMentionWrapper mention = |
| 39 | + GitHubReviewMentionWrapper.create(pullRequest.toUrl(), userId, Instant.now()); |
| 40 | + this.save(mention); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public CompletableFuture<Boolean> isMentioned(GitHubPullRequest pullRequest, long userId) { |
| 46 | + return this.select(pullRequest.toUrl()).thenApply(mentionOptional -> { |
| 47 | + if (mentionOptional.isEmpty()) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + GitHubReviewMentionWrapper mention = mentionOptional.get(); |
| 52 | + Instant lastMention = mention.getLastMention(); |
| 53 | + Instant nextMention = lastMention.plus(MENTION_INTERVAL); |
| 54 | + |
| 55 | + return nextMention.isAfter(Instant.now()); |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
0 commit comments