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>
59 lines
1.3 KiB
Go
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
|
|
}
|