Skip to content

Commit

Permalink
handlewhatsapp: implement status broadcast related config options
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 16, 2024
1 parent b5df6c2 commit 07f7214
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/connector/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) {
return
}
for _, conv := range conversations {
if conv.ChatJID == types.StatusBroadcastJID && !wa.Main.Config.EnableStatusBroadcast {
continue
}
wrappedInfo, err := wa.getChatInfo(ctx, conv.ChatJID, conv)
if errors.Is(err, whatsmeow.ErrNotInGroup) {
log.Debug().Stringer("chat_jid", conv.ChatJID).
Expand Down
8 changes: 8 additions & 0 deletions pkg/connector/chatinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ func (wa *WhatsAppClient) wrapDMInfo(jid types.JID) *bridgev2.ChatInfo {
}

func (wa *WhatsAppClient) wrapStatusBroadcastInfo() *bridgev2.ChatInfo {
userLocal := &bridgev2.UserLocalPortalInfo{}
if wa.Main.Config.MuteStatusBroadcast {
userLocal.MutedUntil = ptr.Ptr(event.MutedForever)
}
if wa.Main.Config.StatusBroadcastTag != "" {
userLocal.Tag = ptr.Ptr(wa.Main.Config.StatusBroadcastTag)
}
return &bridgev2.ChatInfo{
Name: ptr.Ptr(StatusBroadcastName),
Topic: ptr.Ptr(StatusBroadcastTopic),
Expand All @@ -168,6 +175,7 @@ func (wa *WhatsAppClient) wrapStatusBroadcastInfo() *bridgev2.ChatInfo {
},
},
Type: ptr.Ptr(database.RoomTypeDefault),
UserLocal: userLocal,
CanBackfill: false,
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/connector/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ identity_change_notices: false
# users to see when the whatsapp user on the other side is typing during a conversation.
send_presence_on_typing: false
# Should WhatsApp status messages be bridged into a Matrix room?
# Disabling this won't affect already created status broadcast rooms.
enable_status_broadcast: true
# Should sending WhatsApp status messages be allowed?
# This can cause issues if the user has lots of contacts, so it's disabled by default.
Expand Down
14 changes: 14 additions & 0 deletions pkg/connector/handlematrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connector

import (
"context"
"errors"
"fmt"
"time"

Expand All @@ -14,6 +15,7 @@ import (
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
"maunium.net/go/mautrix/event"

"go.mau.fi/mautrix-whatsapp/pkg/waid"
)
Expand Down Expand Up @@ -62,12 +64,18 @@ func (wa *WhatsAppClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2
return wa.handleConvertedMatrixMessage(ctx, msg, waMsg)
}

var ErrBroadcastSendDisabled = bridgev2.WrapErrorInStatus(errors.New("sending status messages is disabled")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusUnsupported)
var ErrBroadcastReactionUnsupported = bridgev2.WrapErrorInStatus(errors.New("reacting to status messages is not currently supported")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusUnsupported)

func (wa *WhatsAppClient) handleConvertedMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage, waMsg *waE2E.Message) (*bridgev2.MatrixMessageResponse, error) {
messageID := wa.Client.GenerateMessageID()
chatJID, err := waid.ParsePortalID(msg.Portal.ID)
if err != nil {
return nil, err
}
if chatJID == types.StatusBroadcastJID && wa.Main.Config.DisableStatusBroadcastSend {
return nil, ErrBroadcastSendDisabled
}
wrappedMsgID := waid.MakeMessageID(chatJID, wa.JID, messageID)
msg.AddPendingToIgnore(networkid.TransactionID(wrappedMsgID))
resp, err := wa.Client.SendMessage(ctx, chatJID, waMsg, whatsmeow.SendRequestExtra{
Expand All @@ -91,6 +99,12 @@ func (wa *WhatsAppClient) handleConvertedMatrixMessage(ctx context.Context, msg
}

func (wa *WhatsAppClient) PreHandleMatrixReaction(_ context.Context, msg *bridgev2.MatrixReaction) (bridgev2.MatrixReactionPreResponse, error) {
portalJID, err := waid.ParsePortalID(msg.Portal.ID)
if err != nil {
return bridgev2.MatrixReactionPreResponse{}, err
} else if portalJID == types.StatusBroadcastJID {
return bridgev2.MatrixReactionPreResponse{}, ErrBroadcastReactionUnsupported
}
return bridgev2.MatrixReactionPreResponse{
SenderID: waid.MakeUserID(wa.JID),
Emoji: variationselector.Remove(msg.Content.RelatesTo.Key),
Expand Down
6 changes: 6 additions & 0 deletions pkg/connector/handlewhatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ func (wa *WhatsAppClient) handleWAMessage(evt *events.Message) {
if evt.Info.Chat.Server == types.HiddenUserServer || evt.Info.Sender.Server == types.HiddenUserServer {
return
}
if evt.Info.Chat == types.StatusBroadcastJID && !wa.Main.Config.EnableStatusBroadcast {
return
}
parsedMessageType := getMessageType(evt.Message)
if parsedMessageType == "ignore" || strings.HasPrefix(parsedMessageType, "unknown_protocol_") {
return
Expand All @@ -263,6 +266,9 @@ func (wa *WhatsAppClient) handleWAUndecryptableMessage(evt *events.Undecryptable
if evt.DecryptFailMode == events.DecryptFailHide || evt.Info.Chat.Server == types.HiddenUserServer || evt.Info.Sender.Server == types.HiddenUserServer {
return
}
if evt.Info.Chat == types.StatusBroadcastJID && !wa.Main.Config.EnableStatusBroadcast {
return
}
wa.Main.Bridge.QueueRemoteEvent(wa.UserLogin, &WAUndecryptableMessage{
MessageInfoWrapper: &MessageInfoWrapper{
Info: evt.Info,
Expand Down

0 comments on commit 07f7214

Please sign in to comment.