~edwargix/axon

730f7472b30342b5ddfb0257167e3e7c60c806dc — David Florness 3 years ago baabf14
deletemymsgs
2 files changed, 73 insertions(+), 2 deletions(-)

A deletemymsgs/cmd.go
M main.go
A deletemymsgs/cmd.go => deletemymsgs/cmd.go +70 -0
@@ 0,0 1,70 @@
package deletemymsgs

import (
	"fmt"
	"os"

	"git.hnitbjorg.xyz/~edwargix/axon/shared"
	"github.com/rwxrob/cmdtab"
	"maunium.net/go/mautrix"
	"maunium.net/go/mautrix/event"
	"maunium.net/go/mautrix/id"
)

func init() {
	x := cmdtab.New("deletemymsgs")
	x.Summary = "delete all messages we've sent to a particular room (be careful!)"
	x.Usage = "<roomID>"

	x.Method = func(args []string) error {
		if len(args) != 1 {
			return x.UsageError()
		}
		return deletemymsgs(id.RoomID(args[0]))
	}
}

func deletemymsgs(roomID id.RoomID) error {
	authInfo, err := shared.GetAuthInfo()
	if err != nil {
		return err
	}

	client, err := mautrix.NewClient(authInfo.Homeserver, authInfo.UserID, authInfo.AccessToken)
	if err != nil {
		return err
	}

	syncer := client.Syncer.(*mautrix.DefaultSyncer)
	syncer.OnEvent(func (source mautrix.EventSource, evt *event.Event) {
		// skip events that we didn't send
		if evt.Sender != client.UserID {
			return
		}
		// skip events that aren't in this room
		if evt.RoomID != roomID {
			return
		}
		// only redact messages events (not state, ephemeral, etc.)
		if evt.Type.Class != event.MessageEventType {
			return
		}
		// don't redact redactions
		if evt.Type.Type == "m.room.redaction" {
			return
		}
		if evt.Unsigned.RedactedBecause != nil {
			fmt.Printf("event %s already redacted\n", evt.ID)
			return
		}
		_, err := client.RedactEvent(evt.RoomID, evt.ID)
		if err != nil {
			fmt.Fprintf(os.Stderr, "couldn't redact event %v: %s\n", evt, err)
			return
		}
		fmt.Printf("redacted event %v\n", evt)
	})

	fmt.Printf("Deleting messages from room with ID %s...\n", roomID)
	return client.Sync()
}

M main.go => main.go +3 -2
@@ 1,12 1,13 @@
package main

import (
	"github.com/rwxrob/cmdtab"
	_ "git.hnitbjorg.xyz/~edwargix/axon/autojoin"
	_ "git.hnitbjorg.xyz/~edwargix/axon/deletemymsgs"
	"github.com/rwxrob/cmdtab"
)

func main() {
	x := cmdtab.New("axon", "autojoin")
	x := cmdtab.New("axon", "autojoin", "deletemymsgs")
	x.Summary = "a set of simple Matrix utility programs for the Client-Server API"
	x.Usage = "<cmd>"
	x.Version = "v0.0.0"