package autocleanup import ( "github.com/mattermost/mattermost/server/public/pluginapi" "github.com/pkg/errors" ) const listKeysPageSize = 1000 // Store persists per-channel automatic cleanup configuration. type Store struct { client *pluginapi.Client } func NewStore(client *pluginapi.Client) *Store { return &Store{client: client} } func (s *Store) Get(channelID string) (*Config, error) { var config Config if err := s.client.KV.Get(kvKey(channelID), &config); err != nil { return nil, errors.Wrap(err, "failed to get auto cleanup config") } if config.OffsetDays <= 0 { return nil, nil } config.ChannelID = channelID return &config, nil } func (s *Store) Set(channelID string, offsetDays int) error { if offsetDays <= 0 { return errors.New("offset_days must be greater than zero") } config := Config{ ChannelID: channelID, OffsetDays: offsetDays, } _, err := s.client.KV.Set(kvKey(channelID), config) return errors.Wrap(err, "failed to set auto cleanup config") } func (s *Store) Delete(channelID string) error { if err := s.client.KV.Delete(kvKey(channelID)); err != nil { return errors.Wrap(err, "failed to delete auto cleanup config") } return nil } func (s *Store) List() ([]Config, error) { configs := make([]Config, 0) for page := 0; ; page++ { keys, err := s.client.KV.ListKeys(page, listKeysPageSize, pluginapi.WithPrefix(kvKeyPrefix)) if err != nil { return nil, errors.Wrap(err, "failed to list auto cleanup configs") } if len(keys) == 0 { break } for _, key := range keys { channelID := key[len(kvKeyPrefix):] config, err := s.Get(channelID) if err != nil { return nil, err } if config != nil { configs = append(configs, *config) } } if len(keys) < listKeysPageSize { break } } return configs, nil }