handlewhatsapp: implement status broadcast related config options

This commit is contained in:
Tulir Asokan 2024-10-16 16:44:57 +03:00
parent b5df6c2526
commit 07f721458d
5 changed files with 31 additions and 1 deletions

View file

@ -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).

View file

@ -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),
@ -168,6 +175,7 @@ func (wa *WhatsAppClient) wrapStatusBroadcastInfo() *bridgev2.ChatInfo {
},
},
Type: ptr.Ptr(database.RoomTypeDefault),
UserLocal: userLocal,
CanBackfill: false,
}
}

View file

@ -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.

View file

@ -2,6 +2,7 @@ package connector
import (
"context"
"errors"
"fmt"
"time"
@ -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"
)
@ -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{
@ -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),

View file

@ -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
@ -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,