mirror of
https://github.com/mattermost/mattermost.git
synced 2026-05-28 04:35:04 -04:00
Some checks failed
API / build (push) Waiting to run
Server CI / Compute Go Version (push) Waiting to run
Server CI / Check mocks (push) Blocked by required conditions
Server CI / Check go mod tidy (push) Blocked by required conditions
Server CI / check-style (push) Blocked by required conditions
Server CI / Check serialization methods for hot structs (push) Blocked by required conditions
Server CI / Vet API (push) Blocked by required conditions
Server CI / Check migration files (push) Blocked by required conditions
Server CI / Generate email templates (push) Blocked by required conditions
Server CI / Check store layers (push) Blocked by required conditions
Server CI / Check mmctl docs (push) Blocked by required conditions
Server CI / Postgres with binary parameters (push) Blocked by required conditions
Server CI / Postgres (push) Blocked by required conditions
Server CI / Postgres (FIPS) (push) Blocked by required conditions
Server CI / Generate Test Coverage (push) Blocked by required conditions
Server CI / Run mmctl tests (push) Blocked by required conditions
Server CI / Run mmctl tests (FIPS) (push) Blocked by required conditions
Server CI / Build mattermost server app (push) Blocked by required conditions
Web App CI / check-lint (push) Waiting to run
Web App CI / check-i18n (push) Blocked by required conditions
Web App CI / check-types (push) Blocked by required conditions
Web App CI / test (platform) (push) Blocked by required conditions
Web App CI / test (mattermost-redux) (push) Blocked by required conditions
Web App CI / test (channels shard 1/4) (push) Blocked by required conditions
Web App CI / test (channels shard 2/4) (push) Blocked by required conditions
Web App CI / test (channels shard 3/4) (push) Blocked by required conditions
Web App CI / test (channels shard 4/4) (push) Blocked by required conditions
Web App CI / upload-coverage (push) Blocked by required conditions
Web App CI / build (push) Blocked by required conditions
Opensearch Docker Image / build-image (push) Has been cancelled
* Draft changes for BoR post soft-deletion * Handled the case for author's BoR post read receipt * lint fix * Updated text * Updated tests * review fixes * review fixes * Paginated and batched temperory post deletion * Updated test * unmocked store * logged instead of erroring out * i18n fix * review fixes
80 lines
2.5 KiB
Go
80 lines
2.5 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package delete_expired_posts
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
"github.com/mattermost/mattermost/server/public/shared/request"
|
|
"github.com/mattermost/mattermost/server/v8/channels/jobs"
|
|
"github.com/mattermost/mattermost/server/v8/channels/store"
|
|
)
|
|
|
|
const (
|
|
expiredPostsBatchSize = 100
|
|
expiredPostsJobBatchWaitTime = 100 * time.Millisecond
|
|
)
|
|
|
|
type AppIface interface {
|
|
DeletePost(rctx request.CTX, postID, deleteByID string) (*model.Post, *model.AppError)
|
|
PermanentDeletePostDataRetainStub(rctx request.CTX, post *model.Post, deleteByID string) *model.AppError
|
|
GetSinglePost(rctx request.CTX, postID string, includeDeleted bool) (*model.Post, *model.AppError)
|
|
GetPostsByIds(postIDs []string) ([]*model.Post, int64, *model.AppError)
|
|
}
|
|
|
|
func MakeWorker(jobServer *jobs.JobServer, store store.Store, app AppIface) *jobs.SimpleWorker {
|
|
const workerName = "DeleteExpiredPosts"
|
|
|
|
isEnabled := func(cfg *model.Config) bool {
|
|
return model.SafeDereference(cfg.ServiceSettings.EnableBurnOnRead)
|
|
}
|
|
execute := func(logger mlog.LoggerIFace, job *model.Job) error {
|
|
if job.Data == nil {
|
|
job.Data = make(model.StringMap)
|
|
}
|
|
|
|
deletedPostIDs := make([]string, 0)
|
|
lastPostId := ""
|
|
for {
|
|
time.Sleep(expiredPostsJobBatchWaitTime)
|
|
postIDs, err := store.TemporaryPost().GetExpiredPosts(request.EmptyContext(logger), lastPostId, expiredPostsBatchSize)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(postIDs) == 0 {
|
|
break
|
|
}
|
|
|
|
lastPostId = postIDs[len(postIDs)-1]
|
|
|
|
expiredPosts, _, appErr := app.GetPostsByIds(postIDs)
|
|
if appErr != nil {
|
|
logger.Error("Failed to get expired posts by IDs", mlog.Err(appErr))
|
|
return appErr
|
|
}
|
|
|
|
for _, post := range expiredPosts {
|
|
appErr = app.PermanentDeletePostDataRetainStub(request.EmptyContext(logger), post, "")
|
|
if appErr != nil {
|
|
logger.Error("Failed to delete expired post", mlog.Err(appErr), mlog.String("post_id", post.Id))
|
|
continue
|
|
}
|
|
deletedPostIDs = append(deletedPostIDs, post.Id)
|
|
}
|
|
}
|
|
|
|
deletedPostIDsJSON, err := json.Marshal(deletedPostIDs)
|
|
if err != nil {
|
|
logger.Error("Failed to marshal deleted post IDs", mlog.Err(err))
|
|
return err
|
|
}
|
|
job.Data["deleted_post_ids"] = string(deletedPostIDsJSON)
|
|
return nil
|
|
}
|
|
return jobs.NewSimpleWorker(workerName, jobServer, execute, isEnabled)
|
|
}
|