Skip to content

Commit

Permalink
capabilities: update to new format
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jan 10, 2025
1 parent 0f2b608 commit c0df387
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 48 deletions.
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/lib/pq v1.10.9
github.com/rs/zerolog v1.33.0
go.mau.fi/util v0.8.4-0.20241217231624-e3dc7ee01c86
go.mau.fi/util v0.8.4-0.20250110124612-64d4dbbec957
go.mau.fi/webp v0.1.0
go.mau.fi/whatsmeow v0.0.0-20250104105216-918c879fcd19
golang.org/x/exp v0.0.0-20250103183323-7d7fa50e5329
golang.org/x/image v0.23.0
golang.org/x/net v0.33.0
golang.org/x/sync v0.10.0
google.golang.org/protobuf v1.36.1
gopkg.in/yaml.v3 v3.0.1
maunium.net/go/mautrix v0.22.2-0.20250104104814-012c24606103
maunium.net/go/mautrix v0.22.2-0.20250110154103-bbcb1904e268
)

require (
Expand All @@ -40,8 +39,9 @@ require (
github.com/yuin/goldmark v1.7.8 // indirect
go.mau.fi/libsignal v0.1.1 // indirect
go.mau.fi/zeroconfig v0.1.3 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/exp v0.0.0-20250103183323-7d7fa50e5329 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.mau.fi/libsignal v0.1.1 h1:m/0PGBh4QKP/I1MQ44ti4C0fMbLMuHb95cmDw01FIpI=
go.mau.fi/libsignal v0.1.1/go.mod h1:QLs89F/OA3ThdSL2Wz2p+o+fi8uuQUz0e1BRa6ExdBw=
go.mau.fi/util v0.8.4-0.20241217231624-e3dc7ee01c86 h1:pIeLc83N03ect2L06lDg+4MQ11oH0phEzRZ/58FdHMo=
go.mau.fi/util v0.8.4-0.20241217231624-e3dc7ee01c86/go.mod h1:c00Db8xog70JeIsEvhdHooylTkTkakgnAOsZ04hplQY=
go.mau.fi/util v0.8.4-0.20250110124612-64d4dbbec957 h1:tsLt3t6ARc55niz+JMgJy6U4sL210Z0K/nyxF09xT0E=
go.mau.fi/util v0.8.4-0.20250110124612-64d4dbbec957/go.mod h1:MOfGTs1CBuK6ERTcSL4lb5YU7/ujz09eOPVEDckuazY=
go.mau.fi/webp v0.1.0 h1:BHObH/DcFntT9KYun5pDr0Ot4eUZO8k2C7eP7vF4ueA=
go.mau.fi/webp v0.1.0/go.mod h1:e42Z+VMFrUMS9cpEwGRIor+lQWO8oUAyPyMtcL+NMt8=
go.mau.fi/whatsmeow v0.0.0-20250104105216-918c879fcd19 h1:uVS+Zct5fF8rSXV9lfs87zoXdge0JXTzVGNkjmZ61UU=
go.mau.fi/whatsmeow v0.0.0-20250104105216-918c879fcd19/go.mod h1:TLzm2XkwgufONEmiVAsFny+9uBqyEZnUoPrQAfMyuSU=
go.mau.fi/zeroconfig v0.1.3 h1:As9wYDKmktjmNZW5i1vn8zvJlmGKHeVxHVIBMXsm4kM=
go.mau.fi/zeroconfig v0.1.3/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/exp v0.0.0-20250103183323-7d7fa50e5329 h1:9kj3STMvgqy3YA4VQXBrN7925ICMxD5wzMRcgA30588=
golang.org/x/exp v0.0.0-20250103183323-7d7fa50e5329/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68=
Expand All @@ -86,8 +86,8 @@ golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
Expand All @@ -101,5 +101,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
maunium.net/go/mautrix v0.22.2-0.20250104104814-012c24606103 h1:VAQIN+2yHj1ObrgKN2STBxdsOWLnhGERZAaiWkJTzxA=
maunium.net/go/mautrix v0.22.2-0.20250104104814-012c24606103/go.mod h1:1rhqwH34Rz54ZqzdQYkmNW6rQUymNeTdaLA4l9LK6AI=
maunium.net/go/mautrix v0.22.2-0.20250110154103-bbcb1904e268 h1:p+3TofdhqiVYIkLjgzidayg2XriGUEbj+nbWs3/UQbk=
maunium.net/go/mautrix v0.22.2-0.20250110154103-bbcb1904e268/go.mod h1:07i96D7BALyuAqxFhRzvaId8FC9NABgRQBPY5HWndf4=
143 changes: 116 additions & 27 deletions pkg/connector/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"context"
"time"

"go.mau.fi/util/ffmpeg"
"go.mau.fi/util/jsontime"
"go.mau.fi/util/ptr"

"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/event"
)
Expand All @@ -17,47 +21,132 @@ func (wa *WhatsAppConnector) GetCapabilities() *bridgev2.NetworkGeneralCapabilit
return WhatsAppGeneralCaps
}

func (wa *WhatsAppConnector) GetBridgeInfoVersion() (info, caps int) {
return 1, 1
}

const WAMaxFileSize = 2000 * 1024 * 1024
const EditMaxAge = 15 * time.Minute
const MaxTextLength = 65536

func supportedIfFFmpeg() event.CapabilitySupportLevel {
if ffmpeg.Supported() {
return event.CapLevelPartialSupport
}
return event.CapLevelRejected
}

func capID() string {
base := "fi.mau.whatsapp.capabilities.2025_01_10"
if ffmpeg.Supported() {
return base + "+ffmpeg"
}
return base
}

var whatsappCaps = &event.RoomFeatures{
ID: capID(),

Formatting: map[event.FormattingFeature]event.CapabilitySupportLevel{
event.FmtBold: event.CapLevelFullySupported,
event.FmtItalic: event.CapLevelFullySupported,
event.FmtStrikethrough: event.CapLevelFullySupported,
event.FmtInlineCode: event.CapLevelFullySupported,
event.FmtCodeBlock: event.CapLevelFullySupported,
event.FmtUserLink: event.CapLevelFullySupported,
event.FmtUnorderedList: event.CapLevelFullySupported,
event.FmtOrderedList: event.CapLevelFullySupported,
event.FmtListStart: event.CapLevelFullySupported,
event.FmtBlockquote: event.CapLevelFullySupported,

var whatsappCaps = &bridgev2.NetworkRoomCapabilities{
FormattedText: true,
UserMentions: true,
LocationMessages: true,
Captions: true,
Replies: true,
Polls: true,
Edits: true,
EditMaxCount: 10,
EditMaxAge: EditMaxAge,
Deletes: true,
DeleteMaxAge: 48 * time.Hour,
DefaultFileRestriction: &bridgev2.FileRestriction{
MaxSize: WAMaxFileSize,
event.FmtInlineLink: event.CapLevelPartialSupport,
event.FmtHeaders: event.CapLevelPartialSupport,
},
Files: map[event.MessageType]bridgev2.FileRestriction{
File: map[event.CapabilityMsgType]*event.FileFeatures{
event.MsgImage: {
MaxSize: WAMaxFileSize,
// webp isn't actually allowed, but will be converted to png
MimeTypes: []string{"image/png", "image/jpeg", "image/webp"},
MimeTypes: map[string]event.CapabilitySupportLevel{
"image/png": event.CapLevelFullySupported,
"image/jpeg": event.CapLevelFullySupported,
"image/webp": event.CapLevelPartialSupport,
"image/gif": supportedIfFFmpeg(),
},
Caption: event.CapLevelFullySupported,
MaxCaptionLength: MaxTextLength,
MaxSize: WAMaxFileSize,
},
event.MsgAudio: {
MaxSize: WAMaxFileSize,
MimeTypes: []string{"audio/mpeg", "audio/mp4", "audio/ogg", "audio/aac", "audio/amr"},
MimeTypes: map[string]event.CapabilitySupportLevel{
"audio/mpeg": event.CapLevelFullySupported,
"audio/mp4": event.CapLevelFullySupported,
"audio/ogg": event.CapLevelFullySupported,
"audio/aac": event.CapLevelFullySupported,
"audio/amr": event.CapLevelFullySupported,
},
Caption: event.CapLevelDropped,
MaxSize: WAMaxFileSize,
},
event.CapMsgVoice: {
MimeTypes: map[string]event.CapabilitySupportLevel{
"audio/ogg; codecs=opus": event.CapLevelFullySupported,
},
Caption: event.CapLevelDropped,
MaxSize: WAMaxFileSize,
},
event.CapMsgSticker: {
MimeTypes: map[string]event.CapabilitySupportLevel{
"image/webp": event.CapLevelFullySupported,
// TODO see if sending lottie is possible
//"image/lottie+json": event.CapLevelFullySupported,
"image/png": event.CapLevelPartialSupport,
"image/jpeg": event.CapLevelPartialSupport,
},
Caption: event.CapLevelDropped,
MaxSize: WAMaxFileSize,
},
event.CapMsgGIF: {
MimeTypes: map[string]event.CapabilitySupportLevel{
"video/mp4": event.CapLevelFullySupported,
"image/gif": supportedIfFFmpeg(),
},
Caption: event.CapLevelFullySupported,
MaxCaptionLength: MaxTextLength,
MaxSize: WAMaxFileSize,
},
event.MsgVideo: {
MaxSize: WAMaxFileSize,
MimeTypes: []string{"video/mp4", "video/3gpp"},
MimeTypes: map[string]event.CapabilitySupportLevel{
"video/mp4": event.CapLevelFullySupported,
"video/3gpp": event.CapLevelFullySupported,
"video/webm": supportedIfFFmpeg(),
},
Caption: event.CapLevelFullySupported,
MaxCaptionLength: MaxTextLength,
MaxSize: WAMaxFileSize,
},
event.MsgFile: {
MaxSize: WAMaxFileSize,
MimeTypes: map[string]event.CapabilitySupportLevel{
"*/*": event.CapLevelFullySupported,
},
Caption: event.CapLevelFullySupported,
MaxCaptionLength: MaxTextLength,
MaxSize: WAMaxFileSize,
},
},
ReadReceipts: true,
Reactions: true,
ReactionCount: 1,
MaxTextLength: MaxTextLength,
LocationMessage: event.CapLevelFullySupported,
Poll: event.CapLevelFullySupported,
Reply: event.CapLevelFullySupported,
Edit: event.CapLevelFullySupported,
EditMaxCount: 10,
EditMaxAge: ptr.Ptr(jsontime.S(EditMaxAge)),
Delete: event.CapLevelFullySupported,
DeleteForMe: false,
DeleteMaxAge: ptr.Ptr(jsontime.S(2 * 24 * time.Hour)),
Reaction: event.CapLevelFullySupported,
ReactionCount: 1,
ReadReceipts: true,
TypingNotifications: true,
}

func (wa *WhatsAppClient) GetCapabilities(ctx context.Context, portal *bridgev2.Portal) *bridgev2.NetworkRoomCapabilities {
func (wa *WhatsAppClient) GetCapabilities(ctx context.Context, portal *bridgev2.Portal) *event.RoomFeatures {
return whatsappCaps
}
11 changes: 3 additions & 8 deletions pkg/msgconv/from-matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"image/color"
"image/png"
"net/http"
"slices"
"strconv"
"strings"

Expand All @@ -35,7 +36,6 @@ import (
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/proto/waE2E"
"go.mau.fi/whatsmeow/types"
"golang.org/x/exp/slices"
"golang.org/x/image/webp"
"google.golang.org/protobuf/proto"
"maunium.net/go/mautrix/bridgev2"
Expand Down Expand Up @@ -190,20 +190,15 @@ func (mc *MessageConverter) constructMediaMessage(
},
}
case event.MsgVideo:
isGif := false
if mime == "video/mp4" && content.Info.MimeType == "image/gif" {
isGif = true
} else if rawInfo, ok := evt.Content.Raw["info"].(map[string]any); ok && rawInfo["fi.mau.gif"] == true {
isGif = true
}
isGIF := mime == "video/mp4" && (content.Info.MimeType == "image/gif" || content.Info.MauGIF)

width := uint32(content.Info.Width)
height := uint32(content.Info.Height)
seconds := uint32(content.Info.Duration / 1000)

return &waE2E.Message{
VideoMessage: &waE2E.VideoMessage{
GifPlayback: proto.Bool(isGif),
GifPlayback: proto.Bool(isGIF),
Width: &width,
Height: &height,
Seconds: &seconds,
Expand Down

0 comments on commit c0df387

Please sign in to comment.