From 6e507f7a1514593e2dc543809c5bd447f4f4c7ce Mon Sep 17 00:00:00 2001 From: Niko Diamadis Date: Fri, 20 Dec 2024 20:29:32 -0500 Subject: [PATCH] fix: improve auto-joining rooms --- main.go | 44 +++++++++++++++++++++++++------------------- util/logs.go | 4 ++++ util/notices.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 util/notices.go diff --git a/main.go b/main.go index a7a69b1..807cd8c 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,6 @@ import ( ) var config Config -var client *mautrix.Client var database *sql.DB func main() { @@ -36,7 +35,9 @@ func main() { syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) { onMessage(client, ctx, evt) }) - syncer.OnEventType(event.StateMember, onRoomInvite) + syncer.OnEventType(event.StateMember, func(ctx context.Context, evt *event.Event) { + onRoomInvite(client, ctx, evt) + }) syncCtx, cancelSync := context.WithCancel(context.Background()) _, err := client.Login(syncCtx, &mautrix.ReqLogin{ @@ -156,7 +157,7 @@ func onManagementMessage(client *mautrix.Client, ctx context.Context, evt *event "Configured MIME types to block:\n%s", strings.Join(list, "\n"), ) - _, err = client.SendNotice(ctx, evt.RoomID, message) + _, err = client.SendNotice(ctx, config.mngtRoomId, message) if err != nil { return } @@ -273,12 +274,12 @@ func redactMessage(client *mautrix.Client, ctx context.Context, evt *event.Event } func getRedactNotice(reason string, evt *event.Event) string { - roomId := evt.RoomID.String() - userId := evt.Sender.String() + roomId := evt.RoomID + userId := evt.Sender template := "Message redacted - %s;
" + - "User %s in room %s :
" + + "User %s in room %s :
" + "
%s
" - return fmt.Sprintf(template, reason, userId, userId, roomId, roomId, evt.Content.AsMessage().Body) + return fmt.Sprintf(template, reason, util.GetUserHtmlUrl(userId), util.GetRoomHtmlUrl(roomId), evt.Content.AsMessage().Body) } func getRawRedactNotice(reason string, evt *event.Event) string { @@ -288,22 +289,27 @@ func getRawRedactNotice(reason string, evt *event.Event) string { return fmt.Sprintf(template, reason, userId, roomId, evt.Content.AsMessage().Body) } -func onRoomInvite(ctx context.Context, evt *event.Event) { +func onRoomInvite(client *mautrix.Client, ctx context.Context, evt *event.Event) { if evt.GetStateKey() == client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite { _, err := client.JoinRoomByID(ctx, evt.RoomID) if err == nil { - util.Printf("Joined room after invite: %s", evt.RoomID.String()) - //rl.SetPrompt(fmt.Sprintf("%s> ", lastRoomID)) - //log.Info(). - // Str("room_id", evt.RoomID.String()). - // Str("inviter", evt.Sender.String()). - // Msg("Joined room after invite") + rawMessage := fmt.Sprintf("Joined room after invite: %s", evt.RoomID.String()) + util.Print(rawMessage) + message := fmt.Sprintf( + "Guardian Note 🛡️:
"+ + "Joined room after invite: %s", + util.GetRoomHtmlUrl(evt.RoomID), + ) + util.SendHtmlNotice(client, ctx, config.mngtRoomId, rawMessage, message) } else { - util.Printf("Failed to join room after invite: %s", evt.RoomID.String()) - //log.Error().Err(err). - // Str("room_id", evt.RoomID.String()). - // Str("inviter", evt.Sender.String()). - // Msg("Failed to join room after invite") + rawMessage := fmt.Sprintf("Failed to join room after invite: %s", evt.RoomID.String()) + util.Print(rawMessage) + message := fmt.Sprintf( + "Guardian Note 🛡️:
"+ + "Failed to join room after invite: %s", + util.GetRoomHtmlUrl(evt.RoomID), + ) + util.SendHtmlNotice(client, ctx, config.mngtRoomId, rawMessage, message) } } } diff --git a/util/logs.go b/util/logs.go index 457ecfd..5846849 100644 --- a/util/logs.go +++ b/util/logs.go @@ -2,6 +2,10 @@ package util import "fmt" +func Print(message string) { + fmt.Print(message + "\n") +} + func Printf(message string, a ...interface{}) { fmt.Printf(message+"\n", a...) } diff --git a/util/notices.go b/util/notices.go new file mode 100644 index 0000000..3246050 --- /dev/null +++ b/util/notices.go @@ -0,0 +1,42 @@ +package util + +import ( + "context" + "fmt" + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +func getRoomUrl(roomId id.RoomID) string { + return fmt.Sprintf("https://matrix.to/#/%s", roomId) +} + +func getUserUrl(userId id.UserID) string { + return fmt.Sprintf("https://matrix.to/#/%s", userId) +} + +func getHtmlUrl(url string, text string) string { + return fmt.Sprintf("%s", url, text) +} + +func GetRoomHtmlUrl(roomId id.RoomID) string { + return getHtmlUrl(getRoomUrl(roomId), roomId.String()) +} + +func GetUserHtmlUrl(userId id.UserID) string { + return getHtmlUrl(getUserUrl(userId), userId.String()) +} + +func SendHtmlNotice(client *mautrix.Client, ctx context.Context, mngtRoomId id.RoomID, rawMessage string, message string) { + contentJson := &event.MessageEventContent{ + MsgType: event.MsgNotice, + Format: event.FormatHTML, + Body: rawMessage, + FormattedBody: message, + } + _, err := client.SendMessageEvent(ctx, mngtRoomId, event.EventMessage, contentJson) + if err != nil { + return + } +}