Skip to content

Commit

Permalink
feat: check if the report entry was muted
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Oct 19, 2024
1 parent 0b42f53 commit 8225f42
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ type Database interface {
UpsertMute(mute *types.Mute) error
DeleteMute(mute *types.Mute) (bool, error)
GetAllMutes() ([]*types.Mute, error)
IsMuted(chain, proposalID string) (bool, error)
}
22 changes: 22 additions & 0 deletions pkg/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,25 @@ func (d *SqliteDatabase) DeleteMute(mute *types.Mute) (bool, error) {

return rowsAffected > 0, nil
}

func (d *SqliteDatabase) IsMuted(chain, proposalID string) (bool, error) {
row := d.client.QueryRow(
"SELECT COUNT(*) FROM mutes WHERE ((chain IS NULL AND proposal_id IS NULL) OR (chain = $1 AND proposal_id IS NULL) OR (chain IS NULL AND proposal_id = $2) OR (chain = $1 AND proposal_id = $2)) AND expires >= datetime('now')",
chain,
proposalID,
)

if err := row.Err(); err != nil {
d.logger.Error().Err(err).Msg("Could not check if entry was muted")
return false, err
}

count := 0

if err := row.Scan(&count); err != nil {
d.logger.Error().Err(err).Msg("Could not scan to check entry was muted")
return false, err
}

return count > 0, nil
}
4 changes: 4 additions & 0 deletions pkg/database/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,7 @@ func (d *StubDatabase) GetAllMutes() ([]*types.Mute, error) {
func (d *StubDatabase) DeleteMute(mute *types.Mute) (bool, error) {
return true, nil
}

func (d *StubDatabase) IsMuted(chain, proposalID string) (bool, error) {
return false, nil
}
11 changes: 5 additions & 6 deletions pkg/mutes/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ func NewMutesManager(logger *zerolog.Logger, database databasePkg.Database) *Man
}
}

func (m *Manager) IsEntryMuted(reportEntry entry.ReportEntry) bool {
func (m *Manager) IsEntryMuted(reportEntry entry.ReportEntry) (bool, error) {
entryConverted, ok := reportEntry.(entry.ReportEntryNotError)
if !ok {
return false
return false, nil
}

_ = entryConverted.GetChain()
_ = entryConverted.GetProposal()
chain := entryConverted.GetChain()
proposal := entryConverted.GetProposal()

return false
// return m.Mutes.IsMuted(chain.Name, proposal.ID)
return m.Database.IsMuted(chain.Name, proposal.ID)
}

func (m *Manager) GetAllMutes() ([]*types.Mute, error) {
Expand Down
6 changes: 5 additions & 1 deletion pkg/report/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ func (d *Dispatcher) SendReport(report reportersPkg.Report, ctx context.Context)
Msg("Sending report...")

for _, reportEntry := range report.Entries {
if d.MutesManager.IsEntryMuted(reportEntry) {
if isMuted, muteErr := d.MutesManager.IsEntryMuted(reportEntry); muteErr != nil {
d.Logger.Warn().
Err(muteErr).
Msg("Error checking whether the proposal was muted.")
} else if isMuted {
d.Logger.Debug().
Str("entry", reportEntry.Name()).
Msg("Notifications are muted, not sending.")
Expand Down

0 comments on commit 8225f42

Please sign in to comment.