Make command system work better in private chat portals

This commit is contained in:
Tulir Asokan 2020-06-25 22:42:52 +03:00
parent 09a08a9ef4
commit 67864d8475

View file

@ -54,6 +54,7 @@ func NewCommandHandler(bridge *Bridge) *CommandHandler {
type CommandEvent struct { type CommandEvent struct {
Bot *appservice.IntentAPI Bot *appservice.IntentAPI
Bridge *Bridge Bridge *Bridge
Portal *Portal
Handler *CommandHandler Handler *CommandHandler
RoomID id.RoomID RoomID id.RoomID
User *User User *User
@ -65,7 +66,11 @@ type CommandEvent struct {
func (ce *CommandEvent) Reply(msg string, args ...interface{}) { func (ce *CommandEvent) Reply(msg string, args ...interface{}) {
content := format.RenderMarkdown(fmt.Sprintf(msg, args...), true, false) content := format.RenderMarkdown(fmt.Sprintf(msg, args...), true, false)
content.MsgType = event.MsgNotice content.MsgType = event.MsgNotice
_, err := ce.Bot.SendMessageEvent(ce.RoomID, event.EventMessage, content) intent := ce.Bot
if ce.Portal != nil && ce.Portal.IsPrivateChat() {
intent = ce.Portal.MainIntent()
}
_, err := intent.SendMessageEvent(ce.RoomID, event.EventMessage, content)
if err != nil { if err != nil {
ce.Handler.log.Warnfln("Failed to reply to command from %s: %v", ce.User.MXID, err) ce.Handler.log.Warnfln("Failed to reply to command from %s: %v", ce.User.MXID, err)
} }
@ -77,6 +82,7 @@ func (handler *CommandHandler) Handle(roomID id.RoomID, user *User, message stri
ce := &CommandEvent{ ce := &CommandEvent{
Bot: handler.bridge.Bot, Bot: handler.bridge.Bot,
Bridge: handler.bridge, Bridge: handler.bridge,
Portal: handler.bridge.GetPortalByMXID(roomID),
Handler: handler, Handler: handler,
RoomID: roomID, RoomID: roomID,
User: user, User: user,
@ -189,16 +195,15 @@ func (handler *CommandHandler) CommandVersion(ce *CommandEvent) {
const cmdInviteLinkHelp = `invite-link - Get an invite link to the current group chat.` const cmdInviteLinkHelp = `invite-link - Get an invite link to the current group chat.`
func (handler *CommandHandler) CommandInviteLink(ce *CommandEvent) { func (handler *CommandHandler) CommandInviteLink(ce *CommandEvent) {
portal := ce.Bridge.GetPortalByMXID(ce.RoomID) if ce.Portal == nil {
if portal == nil {
ce.Reply("Not a portal room") ce.Reply("Not a portal room")
return return
} else if portal.IsPrivateChat() { } else if ce.Portal.IsPrivateChat() {
ce.Reply("Can't get invite link to private chat") ce.Reply("Can't get invite link to private chat")
return return
} }
link, err := ce.User.Conn.GroupInviteLink(portal.Key.JID) link, err := ce.User.Conn.GroupInviteLink(ce.Portal.Key.JID)
if err != nil { if err != nil {
ce.Reply("Failed to get invite link: %v", err) ce.Reply("Failed to get invite link: %v", err)
return return
@ -209,8 +214,7 @@ func (handler *CommandHandler) CommandInviteLink(ce *CommandEvent) {
const cmdSetPowerLevelHelp = `set-pl [user ID] <power level> - Change the power level in a portal room. Only for bridge admins.` const cmdSetPowerLevelHelp = `set-pl [user ID] <power level> - Change the power level in a portal room. Only for bridge admins.`
func (handler *CommandHandler) CommandSetPowerLevel(ce *CommandEvent) { func (handler *CommandHandler) CommandSetPowerLevel(ce *CommandEvent) {
portal := ce.Bridge.GetPortalByMXID(ce.RoomID) if ce.Portal == nil {
if portal == nil {
ce.Reply("Not a portal room") ce.Reply("Not a portal room")
return return
} }
@ -240,7 +244,7 @@ func (handler *CommandHandler) CommandSetPowerLevel(ce *CommandEvent) {
ce.Reply("**Usage:** `set-pl [user] <level>`") ce.Reply("**Usage:** `set-pl [user] <level>`")
return return
} }
intent := portal.MainIntent() intent := ce.Portal.MainIntent()
_, err = intent.SetPowerLevel(ce.RoomID, userID, level) _, err = intent.SetPowerLevel(ce.RoomID, userID, level)
if err != nil { if err != nil {
ce.Reply("Failed to set power levels: %v", err) ce.Reply("Failed to set power levels: %v", err)
@ -506,23 +510,22 @@ func (handler *CommandHandler) CommandSync(ce *CommandEvent) {
const cmdDeletePortalHelp = `delete-portal - Delete the current portal. If the portal is used by other people, this is limited to bridge admins.` const cmdDeletePortalHelp = `delete-portal - Delete the current portal. If the portal is used by other people, this is limited to bridge admins.`
func (handler *CommandHandler) CommandDeletePortal(ce *CommandEvent) { func (handler *CommandHandler) CommandDeletePortal(ce *CommandEvent) {
portal := ce.Bridge.GetPortalByMXID(ce.RoomID) if ce.Portal == nil {
if portal == nil {
ce.Reply("You must be in a portal room to use that command") ce.Reply("You must be in a portal room to use that command")
return return
} }
if !ce.User.Admin { if !ce.User.Admin {
users := portal.GetUserIDs() users := ce.Portal.GetUserIDs()
if len(users) > 1 || (len(users) == 1 && users[0] != ce.User.MXID) { if len(users) > 1 || (len(users) == 1 && users[0] != ce.User.MXID) {
ce.Reply("Only bridge admins can delete portals with other Matrix users") ce.Reply("Only bridge admins can delete portals with other Matrix users")
return return
} }
} }
portal.log.Infoln(ce.User.MXID, "requested deletion of portal.") ce.Portal.log.Infoln(ce.User.MXID, "requested deletion of portal.")
portal.Delete() ce.Portal.Delete()
portal.Cleanup(false) ce.Portal.Cleanup(false)
} }
const cmdDeleteAllPortalsHelp = `delete-all-portals - Delete all your portals that aren't used by any other user.'` const cmdDeleteAllPortalsHelp = `delete-all-portals - Delete all your portals that aren't used by any other user.'`