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.
master
Hugo Thunnissen 4 years ago
parent 96e843dd96
commit 7cbb90809a

@ -8,34 +8,21 @@ import (
"github.com/hugot/whapp-deltachat/core" "github.com/hugot/whapp-deltachat/core"
) )
func NewWhappBridge(bridgeContext *core.BridgeContext) *WhappBridge { func NewTextMessageProxy(bridgeContext *core.BridgeContext) *TextMessageProxy {
return &WhappBridge{ return &TextMessageProxy{
bridgeCtx: bridgeContext, bridgeCtx: bridgeContext,
} }
} }
type WhappBridge struct { type TextMessageProxy struct {
bridgeCtx *core.BridgeContext bridgeCtx *core.BridgeContext
} }
func (b *WhappBridge) Accepts(c *deltachat.Chat, m *deltachat.Message) bool { func (b *TextMessageProxy) Accepts(c *deltachat.Chat, m *deltachat.Message) bool {
chatID := c.GetID() return b.Accepts(c, m)
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 *WhappBridge) Execute( func (b *TextMessageProxy) Execute(
c *deltachat.Context, c *deltachat.Context,
chat *deltachat.Chat, chat *deltachat.Chat,
m *deltachat.Message, m *deltachat.Message,

@ -22,7 +22,7 @@ func (b *Bridge) Init(config *core.Config) error {
whappHandler, whappHandler,
[]deltabot.Command{ []deltabot.Command{
&botcommands.Echo{}, &botcommands.Echo{},
botcommands.NewWhappBridge(ctx), botcommands.NewTextMessageProxy(ctx),
}, },
) )

@ -109,3 +109,21 @@ func (b *BridgeContext) SendLog(logString string) {
b.logger.Println(logString) b.logger.Println(logString)
b.DCContext.SendTextMessage(b.DCUserChatID, 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()
}

@ -121,8 +121,6 @@ func AddUserAsVerifiedContact(
if err != nil { if err != nil {
logger.Println(err) logger.Println(err)
// Something weird is going on here, deltachat is not passing expected
// values. Make the join process fail.
confirmChan <- false confirmChan <- false
return return
} }

Loading…
Cancel
Save