From 6025be4fb129bf6bcde5348d1a793e1df2b28498 Mon Sep 17 00:00:00 2001 From: Hugo Thunnissen Date: Wed, 8 Jan 2020 18:53:29 +0100 Subject: [PATCH] Allow user to configure whether his/her own messages should be bridged or not --- bridge_context.go | 15 +++++++++++++++ config.go | 1 + message_handlers.go | 12 ++++++------ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/bridge_context.go b/bridge_context.go index a549bd2..4e5ff09 100644 --- a/bridge_context.go +++ b/bridge_context.go @@ -59,3 +59,18 @@ func (b *BridgeContext) MessageWasSent(ID string) bool { return sent } + +func (b *BridgeContext) ShouldMessageBeSent(info whatsapp.MessageInfo) bool { + // Skip if the message has already been sent + if b.MessageWasSent(info.Id) { + return false + } + + // send if not from user + if !info.FromMe { + return true + } + + // If from user, only send when it is enabled in the config + return b.Config.App.ShowFromMe +} diff --git a/config.go b/config.go index 1cb8aa9..d2be0fb 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,7 @@ import ( type AppConfig struct { DataFolder string `yaml:"data_folder"` UserAddress string `yaml:"user"` + ShowFromMe bool `yaml:"show_from_me"` } type Config struct { diff --git a/message_handlers.go b/message_handlers.go index 9741b1d..3000c07 100644 --- a/message_handlers.go +++ b/message_handlers.go @@ -18,7 +18,7 @@ type MessageAction func() error func MakeTextMessageAction(b *BridgeContext, m whatsapp.TextMessage) MessageAction { return func() error { - if b.MessageWasSent(m.Info.Id) { + if !b.ShouldMessageBeSent(m.Info) { return nil } @@ -43,7 +43,7 @@ func MakeTextMessageAction(b *BridgeContext, m whatsapp.TextMessage) MessageActi func MakeImageMessageAction(b *BridgeContext, m whatsapp.ImageMessage) MessageAction { return func() error { - if b.MessageWasSent(m.Info.Id) { + if !b.ShouldMessageBeSent(m.Info) { return nil } @@ -84,7 +84,7 @@ func MakeImageMessageAction(b *BridgeContext, m whatsapp.ImageMessage) MessageAc func MakeDocumentMessageAction(b *BridgeContext, m whatsapp.DocumentMessage) MessageAction { return func() error { - if b.MessageWasSent(m.Info.Id) { + if !b.ShouldMessageBeSent(m.Info) { return nil } @@ -125,7 +125,7 @@ func MakeDocumentMessageAction(b *BridgeContext, m whatsapp.DocumentMessage) Mes func MakeAudioMessageAction(b *BridgeContext, m whatsapp.AudioMessage) MessageAction { return func() error { - if b.MessageWasSent(m.Info.Id) { + if !b.ShouldMessageBeSent(m.Info) { return nil } @@ -166,7 +166,7 @@ func MakeAudioMessageAction(b *BridgeContext, m whatsapp.AudioMessage) MessageAc func MakeVideoMessageAction(b *BridgeContext, m whatsapp.VideoMessage) MessageAction { return func() error { - if b.MessageWasSent(m.Info.Id) { + if !b.ShouldMessageBeSent(m.Info) { return nil } @@ -207,7 +207,7 @@ func MakeVideoMessageAction(b *BridgeContext, m whatsapp.VideoMessage) MessageAc func MakeContactMessageAction(b *BridgeContext, m whatsapp.ContactMessage) MessageAction { return func() error { - if b.MessageWasSent(m.Info.Id) { + if !b.ShouldMessageBeSent(m.Info) { return nil }