Add support for newsletters in join and open commands

This commit is contained in:
Tulir Asokan 2023-10-18 14:17:41 +03:00
parent e94f611002
commit 1d9d516b78
3 changed files with 48 additions and 20 deletions

View file

@ -238,18 +238,32 @@ func fnJoin(ce *WrappedCommandEvent) {
if len(ce.Args) == 0 {
ce.Reply("**Usage:** `join <invite link>`")
return
} else if !strings.HasPrefix(ce.Args[0], whatsmeow.InviteLinkPrefix) {
ce.Reply("That doesn't look like a WhatsApp invite link")
return
}
jid, err := ce.User.Client.JoinGroupWithLink(ce.Args[0])
if err != nil {
ce.Reply("Failed to join group: %v", err)
return
if strings.HasPrefix(ce.Args[0], whatsmeow.InviteLinkPrefix) {
jid, err := ce.User.Client.JoinGroupWithLink(ce.Args[0])
if err != nil {
ce.Reply("Failed to join group: %v", err)
return
}
ce.Log.Debugln("%s successfully joined group %s", ce.User.MXID, jid)
ce.Reply("Successfully joined group `%s`, the portal should be created momentarily", jid)
} else if strings.HasPrefix(ce.Args[0], whatsmeow.NewsletterLinkPrefix) {
info, err := ce.User.Client.GetNewsletterInfoWithInvite(ce.Args[0])
if err != nil {
ce.Reply("Failed to get channel info: %v", err)
return
}
err = ce.User.Client.FollowNewsletter(info.ID)
if err != nil {
ce.Reply("Failed to follow channel: %v", err)
return
}
ce.Log.Debugln("%s successfully followed channel %s", ce.User.MXID, info.ID)
ce.Reply("Successfully followed channel `%s`, the portal should be created momentarily", info.ID)
} else {
ce.Reply("That doesn't look like a WhatsApp invite link")
}
ce.Log.Debugln("%s successfully joined group %s", ce.User.MXID, jid)
ce.Reply("Successfully joined group `%s`, the portal should be created momentarily", jid)
}
func tryDecryptEvent(crypto bridge.Crypto, evt *event.Event) (json.RawMessage, error) {
@ -998,23 +1012,37 @@ func fnOpen(ce *WrappedCommandEvent) {
} else {
jid = types.NewJID(ce.Args[0], types.GroupServer)
}
if jid.Server != types.GroupServer || (!strings.ContainsRune(jid.User, '-') && len(jid.User) < 15) {
if (jid.Server != types.GroupServer && jid.Server != types.NewsletterServer) || (!strings.ContainsRune(jid.User, '-') && len(jid.User) < 15) {
ce.Reply("That does not look like a group JID")
return
}
info, err := ce.User.Client.GetGroupInfo(jid)
if err != nil {
ce.Reply("Failed to get group info: %v", err)
return
var err error
var groupInfo *types.GroupInfo
var newsletterMetadata *types.NewsletterMetadata
switch jid.Server {
case types.GroupServer:
groupInfo, err = ce.User.Client.GetGroupInfo(jid)
if err != nil {
ce.Reply("Failed to get group info: %v", err)
return
}
jid = groupInfo.JID
case types.NewsletterServer:
newsletterMetadata, err = ce.User.Client.GetNewsletterInfo(jid)
if err != nil {
ce.Reply("Failed to get channel info: %v", err)
return
}
jid = newsletterMetadata.ID
}
ce.Log.Debugln("Importing", jid, "for", ce.User.MXID)
portal := ce.User.GetPortalByJID(info.JID)
portal := ce.User.GetPortalByJID(jid)
if len(portal.MXID) > 0 {
portal.UpdateMatrixRoom(ce.User, info, nil)
portal.UpdateMatrixRoom(ce.User, groupInfo, newsletterMetadata)
ce.Reply("Portal room synced.")
} else {
err = portal.CreateMatrixRoom(ce.User, info, nil, true, true)
err = portal.CreateMatrixRoom(ce.User, groupInfo, newsletterMetadata, true, true)
if err != nil {
ce.Reply("Failed to create room: %v", err)
} else {

2
go.mod
View file

@ -13,7 +13,7 @@ require (
github.com/tidwall/gjson v1.17.0
go.mau.fi/util v0.2.0
go.mau.fi/webp v0.1.0
go.mau.fi/whatsmeow v0.0.0-20231016102050-3a30a129b626
go.mau.fi/whatsmeow v0.0.0-20231018112044-306bf6fef4d2
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/image v0.13.0
golang.org/x/net v0.17.0

4
go.sum
View file

@ -68,8 +68,8 @@ go.mau.fi/util v0.2.0 h1:AMGBEdg9Ya/smb/09dljo9wBwKr432EpfjDWF7aFQg0=
go.mau.fi/util v0.2.0/go.mod h1:AxuJUMCxpzgJ5eV9JbPWKRH8aAJJidxetNdUj7qcb84=
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-20231016102050-3a30a129b626 h1:uqurXYTURlhbpEHldNqP8EC15EIP1zWuUABRL2Xqkbs=
go.mau.fi/whatsmeow v0.0.0-20231016102050-3a30a129b626/go.mod h1:u557d2vph8xcLrk3CKTBknUHoB6icUpqazA4w+binRU=
go.mau.fi/whatsmeow v0.0.0-20231018112044-306bf6fef4d2 h1:27IohJQeoxrcJE44yEE2B2fOsiakqBeQpJDRHF483oM=
go.mau.fi/whatsmeow v0.0.0-20231018112044-306bf6fef4d2/go.mod h1:u557d2vph8xcLrk3CKTBknUHoB6icUpqazA4w+binRU=
go.mau.fi/zeroconfig v0.1.2 h1:DKOydWnhPMn65GbXZOafgkPm11BvFashZWLct0dGFto=
go.mau.fi/zeroconfig v0.1.2/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=