-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 39b39c9
Showing
9 changed files
with
850 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2020 Dexter Chua | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
PLUGIN_ID=srcf.redact | ||
PLUGIN_VERSION=1.0.0 | ||
|
||
CWD := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) | ||
|
||
export GO111MODULE=on | ||
|
||
.PHONY: build | ||
build: | ||
rm -rf dist/ | ||
|
||
for os in linux darwin windows; do \ | ||
export TARGET=$(CWD)/dist/$$os/$(PLUGIN_ID); \ | ||
mkdir -p $$TARGET; \ | ||
cd $(CWD)/src && env GOOS=linux GOARCH=amd64 go build -o $$TARGET/plugin-$$os-amd64; cd ..; \ | ||
cp plugin.json $$TARGET; \ | ||
tar -C $$TARGET/.. -cvzf $(CWD)/dist/$(PLUGIN_ID)-$(PLUGIN_VERSION)-$$os-amd64.tar.gz $(PLUGIN_ID); \ | ||
done | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# srcf.redact | ||
|
||
This Mattermost plugin lets team administrators delete old messages from their | ||
channels based on their age. It provides two commands: | ||
|
||
* `/channel_id` returns the channel id | ||
* `/delete_posts` this is the command that actually deletes the messages. | ||
|
||
The delete command is currently only able to delete messages older than a | ||
specific number of days. Pull requests to enhance its functionality (e.g. to | ||
allow messages sent between two dates, or other duration formats) are welcome. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module github.com/mattermost/mattermost-plugin-demo | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/mattermost/mattermost-plugin-api v0.0.9 | ||
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20200313113657-e2883bfe5f37 | ||
github.com/mholt/archiver/v3 v3.3.0 | ||
github.com/pkg/errors v0.9.1 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"id": "srcf.redact", | ||
"name": "Message Redaction Plugin", | ||
"description": "This plugin allows team administrators to delete old messages", | ||
"version": "1.0.0", | ||
"min_server_version": "5.6.0", | ||
"server": { | ||
"executables": { | ||
"linux-amd64": "plugin-linux-amd64", | ||
"darwin-amd64": "plugin-darwin-amd64", | ||
"windows-amd64": "plugin-windows-amd64" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package main | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/mattermost/mattermost-server/v5/model" | ||
"github.com/mattermost/mattermost-server/v5/plugin" | ||
) | ||
|
||
const PostPerPage = 100 | ||
|
||
type Plugin struct { | ||
plugin.MattermostPlugin | ||
} | ||
|
||
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) { | ||
command_args := strings.Fields(args.Command) | ||
trigger := strings.TrimPrefix(command_args[0], "/") | ||
|
||
switch trigger { | ||
case "channel_id": | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: "Channel id: " + args.ChannelId, | ||
}, nil | ||
case "delete_posts": | ||
if !p.API.HasPermissionToChannel(args.UserId, args.ChannelId, model.PERMISSION_DELETE_OTHERS_POSTS) { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: "Permission denied", | ||
}, nil | ||
} | ||
|
||
if command_args[1] != args.ChannelId { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: "First argument must be channel id. First argument is " + command_args[1], | ||
}, nil | ||
} | ||
n, err := strconv.ParseFloat(command_args[2], 64) | ||
if err != nil { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: "Second arguments must be a number", | ||
}, nil | ||
} | ||
|
||
cutoff := time.Now().Unix()*1000 - int64(n*24*60*60*1000) | ||
|
||
deleted := 0 | ||
// Mattermost returns page in reverse chronological order. We first | ||
// find the first page with an old enough post. | ||
page := 0 | ||
for { | ||
posts, err := p.API.GetPostsForChannel(args.ChannelId, page+1, PostPerPage) | ||
if err != nil { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: "Failed to retrieve posts", | ||
}, nil | ||
} | ||
if len(posts.Order) == 0 { | ||
break | ||
} | ||
if posts.Posts[posts.Order[len(posts.Order)-1]].CreateAt < cutoff { | ||
break | ||
} | ||
page += 1 | ||
} | ||
|
||
// Now page is the first page with a post we have to delete. First | ||
// delete all posts in subsequent pages | ||
for { | ||
posts, err := p.API.GetPostsForChannel(args.ChannelId, page+1, PostPerPage) | ||
if err != nil { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: "Failed to retrieve posts", | ||
}, nil | ||
} | ||
if len(posts.Order) == 0 { | ||
break | ||
} | ||
for _, pid := range posts.Order { | ||
if e := p.API.DeletePost(pid); e != nil { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: "Error deleting post " + pid, | ||
}, nil | ||
} | ||
deleted += 1 | ||
} | ||
} | ||
|
||
// Now delete the rest in the final page | ||
posts, err := p.API.GetPostsForChannel(args.ChannelId, page, PostPerPage) | ||
for i := len(posts.Order) - 1; i >= 0; i-- { | ||
pid := posts.Order[i] | ||
if posts.Posts[pid].CreateAt < cutoff { | ||
if e := p.API.DeletePost(pid); e != nil { | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: "Error deleting post " + pid, | ||
}, nil | ||
} | ||
deleted += 1 | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: strconv.Itoa(deleted) + " posts older than " + command_args[2] + " days deleted", | ||
}, nil | ||
} | ||
return &model.CommandResponse{ | ||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, | ||
Text: "Unknown command: " + args.Command, | ||
}, nil | ||
} | ||
|
||
func (p *Plugin) OnActivate() error { | ||
if err := p.API.RegisterCommand(&model.Command{ | ||
Trigger: "channel_id", | ||
AutoComplete: true, | ||
AutoCompleteDesc: "Displays channel id", | ||
}); err != nil { | ||
return err | ||
} | ||
if err := p.API.RegisterCommand(&model.Command{ | ||
Trigger: "delete_posts", | ||
AutoComplete: true, | ||
AutoCompleteDesc: "Usage: /delete_posts <channel_id> <n>; This command deletes all posts more than n days old in this channel. The first argument is the channel_id obtained from /channel_id.", | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
plugin.ClientMain(&Plugin{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mattermost/mattermost-server/v5/model" | ||
) | ||
|
||
var manifest *model.Manifest | ||
|
||
const manifestStr = ` | ||
{ | ||
"id": "srcf.redact", | ||
"name": "Message Redaction Plugin", | ||
"description": "This plugin allows team administrators to delete old messages", | ||
"version": "1.0.0", | ||
"min_server_version": "5.6.0", | ||
"server": { | ||
"executables": { | ||
"linux-amd64": "plugin-linux-amd64", | ||
"darwin-amd64": "plugin-darwin-amd64", | ||
"windows-amd64": "plugin-windows-amd64" | ||
}, | ||
"executable": "" | ||
} | ||
} | ||
` | ||
|
||
func init() { | ||
manifest = model.ManifestFromJson(strings.NewReader(manifestStr)) | ||
} |