mattermost-plugin-cleanup-c.../server/cleanup.go
Felipe M. 2ecbd2442d
Some checks failed
ci / test (push) Failing after 1m5s
ci / lint (push) Failing after 1m6s
ci / build (push) Failing after 1m6s
release / release (push) Failing after 50s
Add Mattermost plugin for system-admin channel cleanup.
Provide a channel header menu action and /cleanup-channel slash command so system administrators can remove all messages from a channel, with admin checks enforced on both the web app and server. Configure CI and release workflows for the Forgejo runner and signed plugin bundles.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-25 18:46:39 +02:00

47 lines
1.1 KiB
Go

package main
import (
"errors"
"github.com/mattermost/mattermost/server/public/model"
pkgerrors "github.com/pkg/errors"
)
var errPermissionDenied = errors.New("permission denied: system admin required")
const postsPerPage = 200
func (p *Plugin) isSystemAdmin(userID string) bool {
return p.client.User.HasPermissionTo(userID, model.PermissionManageSystem)
}
func (p *Plugin) cleanupChannel(userID, channelID string) (int, error) {
if !p.isSystemAdmin(userID) {
return 0, errPermissionDenied
}
if _, err := p.client.Channel.Get(channelID); err != nil {
return 0, pkgerrors.Wrap(err, "failed to get channel")
}
deleted := 0
for {
postList, err := p.client.Post.GetPostsForChannel(channelID, 0, postsPerPage)
if err != nil {
return deleted, pkgerrors.Wrap(err, "failed to get channel posts")
}
if len(postList.Order) == 0 {
break
}
for _, postID := range postList.Order {
if err := p.client.Post.DeletePost(postID); err != nil {
p.client.Log.Warn("Failed to delete post", "post_id", postID, "error", err)
continue
}
deleted++
}
}
return deleted, nil
}