From 7cbb90809a0f2ec03462bb8fdff96ce25a6432eb Mon Sep 17 00:00:00 2001 From: Hugo Thunnissen Date: Mon, 17 Feb 2020 08:38:48 +0100 Subject: [PATCH] Add general "Accepts" method to BridgeContext and s/WhappBridge/TextMessageProxy/g We can use this general "Accepts" method when we add more commands that call it and add some extra criteria like message type using DC_MSG*. This will help with the implementation of commands that proxy other message types like images and files. --- ...{whapp-bridge.go => text_message_proxy.go} | 25 +++++-------------- bridge/bridge.go | 2 +- core/bridge_context.go | 18 +++++++++++++ core/deltachat.go | 2 -- 4 files changed, 25 insertions(+), 22 deletions(-) rename botcommands/{whapp-bridge.go => text_message_proxy.go} (59%) diff --git a/botcommands/whapp-bridge.go b/botcommands/text_message_proxy.go similarity index 59% rename from botcommands/whapp-bridge.go rename to botcommands/text_message_proxy.go index 59359e0..b0b3884 100644 --- a/botcommands/whapp-bridge.go +++ b/botcommands/text_message_proxy.go @@ -8,34 +8,21 @@ import ( "github.com/hugot/whapp-deltachat/core" ) -func NewWhappBridge(bridgeContext *core.BridgeContext) *WhappBridge { - return &WhappBridge{ +func NewTextMessageProxy(bridgeContext *core.BridgeContext) *TextMessageProxy { + return &TextMessageProxy{ bridgeCtx: bridgeContext, } } -type WhappBridge struct { +type TextMessageProxy struct { bridgeCtx *core.BridgeContext } -func (b *WhappBridge) Accepts(c *deltachat.Chat, m *deltachat.Message) bool { - chatID := c.GetID() - - chatJID, err := b.bridgeCtx.DB.GetWhappJIDForDCID(chatID) - - if err != nil { - // The database is failing, very much an edge case. - b.bridgeCtx.SendLog(err.Error()) - - return false - } - - // Only forward messages for known groups, - // Don't forward info messages like "group name changed" etc. - return chatJID != nil && !m.IsInfo() +func (b *TextMessageProxy) Accepts(c *deltachat.Chat, m *deltachat.Message) bool { + return b.Accepts(c, m) } -func (b *WhappBridge) Execute( +func (b *TextMessageProxy) Execute( c *deltachat.Context, chat *deltachat.Chat, m *deltachat.Message, diff --git a/bridge/bridge.go b/bridge/bridge.go index b725f43..9aa0563 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -22,7 +22,7 @@ func (b *Bridge) Init(config *core.Config) error { whappHandler, []deltabot.Command{ &botcommands.Echo{}, - botcommands.NewWhappBridge(ctx), + botcommands.NewTextMessageProxy(ctx), }, ) diff --git a/core/bridge_context.go b/core/bridge_context.go index d5b68b6..02983f8 100644 --- a/core/bridge_context.go +++ b/core/bridge_context.go @@ -109,3 +109,21 @@ func (b *BridgeContext) SendLog(logString string) { b.logger.Println(logString) b.DCContext.SendTextMessage(b.DCUserChatID, logString) } + +// Returns true when a DC message is eligible to be bridged. +func (b *BridgeContext) Accepts(c *deltachat.Chat, m *deltachat.Message) bool { + chatID := c.GetID() + + chatJID, err := b.DB.GetWhappJIDForDCID(chatID) + + if err != nil { + // The database is failing, very much an edge case. + b.SendLog(err.Error()) + + return false + } + + // Only forward messages for known groups, + // Don't forward info messages like "group name changed" etc. + return chatJID != nil && !m.IsInfo() +} diff --git a/core/deltachat.go b/core/deltachat.go index 7aeb722..c540777 100644 --- a/core/deltachat.go +++ b/core/deltachat.go @@ -121,8 +121,6 @@ func AddUserAsVerifiedContact( if err != nil { logger.Println(err) - // Something weird is going on here, deltachat is not passing expected - // values. Make the join process fail. confirmChan <- false return }