mattermost-plugin-cleanup-c.../server/autocleanup/job.go
Felipe M. 85e2a38f17
Some checks failed
ci / test (push) Successful in 3m7s
ci / lint (push) Failing after 2m50s
ci / build (push) Successful in 4m21s
release / release (push) Successful in 5m43s
Add automatic channel cleanup with unified modal UI.
Per-channel retention config, daily scheduled cleanup, slash command and REST API support, plus a single Channel cleanup menu that opens a plugin modal for configure, run-now, and disable actions.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-28 18:35:24 +02:00

59 lines
1.3 KiB
Go

package autocleanup
import (
"time"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/pluginapi/cluster"
"github.com/pkg/errors"
)
const dailyInterval = 24 * time.Hour
type ConfigStore interface {
List() ([]Config, error)
}
// CleanupFunc deletes messages older than offsetDays from channelID.
type CleanupFunc func(channelID string, offsetDays int) (int, error)
// Runner executes automatic cleanup for configured channels.
type Runner struct {
store ConfigStore
cleanup CleanupFunc
}
func NewRunner(store ConfigStore, cleanup CleanupFunc) *Runner {
return &Runner{
store: store,
cleanup: cleanup,
}
}
func (r *Runner) Run() {
configs, err := r.store.List()
if err != nil {
return
}
for _, config := range configs {
if _, err := r.cleanup(config.ChannelID, config.OffsetDays); err != nil {
continue
}
}
}
// ScheduleDaily registers a cluster-safe job that runs automatic cleanup once per day.
func ScheduleDaily(api plugin.API, runner *Runner) (*cluster.Job, error) {
job, err := cluster.Schedule(
api,
autoCleanupJobKey,
cluster.MakeWaitForRoundedInterval(dailyInterval),
runner.Run,
)
if err != nil {
return nil, errors.Wrap(err, "failed to schedule auto cleanup job")
}
return job, nil
}