88import java .util .List ;
99import java .util .Optional ;
1010
11+ /**
12+ * DAO for interacting with the collection of stored {@link Mute} objects.
13+ */
1114@ RequiredArgsConstructor
1215public class MuteRepository {
1316 private final Connection con ;
1417
18+ /**
19+ * Inserts a new mute into the database. Note that this ignores the mute's
20+ * {@link Mute#isDiscarded()}, {@link Mute#getId()}, and {@link Mute#getCreatedAt()}.
21+ * @param mute The mute to save.
22+ * @return The mute that was saved.
23+ * @throws SQLException If an error occurs.
24+ */
1525 public Mute insert (Mute mute ) throws SQLException {
1626 try (var s = con .prepareStatement (
1727 "INSERT INTO mute (user_id, muted_by, reason, ends_at) VALUES (?, ?, ?, ?)" ,
@@ -29,8 +39,17 @@ public Mute insert(Mute mute) throws SQLException {
2939 }
3040 }
3141
42+ /**
43+ * Gets the list of active mutes for a user, or those which are not
44+ * discarded, and whose ending date is some time in the future.
45+ * @param userId The id of the user to get active mutes for.
46+ * @return A list of mutes.
47+ * @throws SQLException If an error occurs.
48+ */
3249 public List <Mute > getActiveMutes (long userId ) throws SQLException {
33- try (var s = con .prepareStatement ("SELECT * FROM mute WHERE user_id = ? AND discarded = FALSE AND ends_at > CURRENT_TIMESTAMP(0)" )) {
50+ try (var s = con .prepareStatement ("""
51+ SELECT * FROM mute
52+ WHERE user_id = ? AND discarded = FALSE AND ends_at > CURRENT_TIMESTAMP(0)""" )) {
3453 s .setLong (1 , userId );
3554 var rs = s .executeQuery ();
3655 List <Mute > mutes = new ArrayList <>();
@@ -41,8 +60,33 @@ public List<Mute> getActiveMutes(long userId) throws SQLException {
4160 }
4261 }
4362
44- public List <Mute > getActiveMutes () throws SQLException {
45- try (var s = con .prepareStatement ("SELECT * FROM mute WHERE discarded = FALSE AND ends_at > CURRENT_TIMESTAMP(0)" )) {
63+ /**
64+ * Determines if a user has at least one active mute.
65+ * @param userId The user to check.
66+ * @return True if there is at least one active mute for the user.
67+ * @throws SQLException If an error occurs.
68+ */
69+ public boolean hasActiveMutes (long userId ) throws SQLException {
70+ try (var s = con .prepareStatement ("""
71+ SELECT COUNT(id)
72+ FROM mute
73+ WHERE user_id = ? AND discarded = FALSE AND ends_at > CURRENT_TIMESTAMP(0)""" )) {
74+ s .setLong (1 , userId );
75+ var rs = s .executeQuery ();
76+ return rs .next () && rs .getLong (1 ) > 0 ;
77+ }
78+ }
79+
80+ /**
81+ * Gets a list of expired mutes, which are those that are not yet discarded,
82+ * but whose ending time is in the past.
83+ * @return The list of mutes.
84+ * @throws SQLException If an error occurs.
85+ */
86+ public List <Mute > getExpiredMutes () throws SQLException {
87+ try (var s = con .prepareStatement ("""
88+ SELECT * FROM mute
89+ WHERE discarded = FALSE AND ends_at < CURRENT_TIMESTAMP(0)""" )) {
4690 var rs = s .executeQuery ();
4791 List <Mute > mutes = new ArrayList <>();
4892 while (rs .next ()) {
@@ -52,6 +96,12 @@ public List<Mute> getActiveMutes() throws SQLException {
5296 }
5397 }
5498
99+ /**
100+ * Finds a mute by its id.
101+ * @param id The id of the mute to fetch.
102+ * @return An optional that contains the mute, if it was found.
103+ * @throws SQLException If an error occurs.
104+ */
55105 public Optional <Mute > findById (long id ) throws SQLException {
56106 try (var s = con .prepareStatement ("SELECT * FROM mute WHERE id = ?" )) {
57107 s .setLong (1 , id );
@@ -61,15 +111,28 @@ public Optional<Mute> findById(long id) throws SQLException {
61111 return Optional .empty ();
62112 }
63113
114+ /**
115+ * Discards a mute.
116+ * @param mute The mute to discard.
117+ * @throws SQLException If an error occurs.
118+ */
64119 public void discard (Mute mute ) throws SQLException {
65120 try (var s = con .prepareStatement ("UPDATE mute SET discarded = TRUE WHERE id = ?" )) {
66121 s .setLong (1 , mute .getId ());
67122 s .executeUpdate ();
68123 }
69124 }
70125
126+ /**
127+ * Discards all currently active mutes for a given user.
128+ * @param userId The id of the user whose active mutes to discard.
129+ * @throws SQLException If an error occurs.
130+ */
71131 public void discardAllActive (long userId ) throws SQLException {
72- try (var s = con .prepareStatement ("UPDATE mute SET discarded = TRUE WHERE user_id = ? AND discarded = FALSE AND ends_at > CURRENT_TIMESTAMP(0)" )) {
132+ try (var s = con .prepareStatement ("""
133+ UPDATE mute
134+ SET discarded = TRUE
135+ WHERE user_id = ? AND discarded = FALSE AND ends_at > CURRENT_TIMESTAMP(0)""" )) {
73136 s .setLong (1 , userId );
74137 s .executeUpdate ();
75138 }
0 commit comments