Implement WhatsApp->Matrix power level bridging
This commit is contained in:
parent
d3a178ecf3
commit
f78fb72622
@ -31,7 +31,7 @@
|
|||||||
* [x] Presence
|
* [x] Presence
|
||||||
* [x] Typing notifications
|
* [x] Typing notifications
|
||||||
* [x] Read receipts
|
* [x] Read receipts
|
||||||
* [ ] Admin/superadmin status
|
* [x] Admin/superadmin status
|
||||||
* [ ] Membership actions
|
* [ ] Membership actions
|
||||||
* [ ] Invite
|
* [ ] Invite
|
||||||
* [ ] Join
|
* [ ] Join
|
||||||
|
103
portal.go
103
portal.go
@ -119,10 +119,29 @@ type Portal struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (portal *Portal) SyncParticipants(metadata *whatsappExt.GroupInfo) {
|
func (portal *Portal) SyncParticipants(metadata *whatsappExt.GroupInfo) {
|
||||||
|
changed := false
|
||||||
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
||||||
|
if err != nil {
|
||||||
|
levels = portal.GetBasePowerLevels()
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
for _, participant := range metadata.Participants {
|
for _, participant := range metadata.Participants {
|
||||||
intent := portal.user.GetPuppetByJID(participant.JID).Intent()
|
puppet := portal.user.GetPuppetByJID(participant.JID)
|
||||||
intent.EnsureJoined(portal.MXID)
|
puppet.Intent().EnsureJoined(portal.MXID)
|
||||||
// TODO set power levels
|
level := levels.GetUserLevel(puppet.MXID)
|
||||||
|
expectedLevel := 0
|
||||||
|
if participant.IsSuperAdmin {
|
||||||
|
expectedLevel = 95
|
||||||
|
} else if participant.IsAdmin {
|
||||||
|
expectedLevel = 50
|
||||||
|
}
|
||||||
|
if level != expectedLevel {
|
||||||
|
levels.SetUserLevel(puppet.MXID, expectedLevel)
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,6 +244,77 @@ func (portal *Portal) Sync(contact whatsapp.Contact) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (portal *Portal) GetBasePowerLevels() *gomatrix.PowerLevels {
|
||||||
|
anyone := 0
|
||||||
|
nope := 99
|
||||||
|
return &gomatrix.PowerLevels{
|
||||||
|
UsersDefault: anyone,
|
||||||
|
EventsDefault: anyone,
|
||||||
|
RedactPtr: &anyone,
|
||||||
|
StateDefaultPtr: &nope,
|
||||||
|
BanPtr: &nope,
|
||||||
|
InvitePtr: &nope,
|
||||||
|
Users: map[string]int{
|
||||||
|
portal.MainIntent().UserID: 100,
|
||||||
|
},
|
||||||
|
Events: map[gomatrix.EventType]int{
|
||||||
|
gomatrix.StateRoomName: anyone,
|
||||||
|
gomatrix.StateRoomAvatar: anyone,
|
||||||
|
gomatrix.StateTopic: anyone,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (portal *Portal) ChangeAdminStatus(jids []string, setAdmin bool) {
|
||||||
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
||||||
|
if err != nil {
|
||||||
|
levels = portal.GetBasePowerLevels()
|
||||||
|
}
|
||||||
|
newLevel := 0
|
||||||
|
if setAdmin {
|
||||||
|
newLevel = 50
|
||||||
|
}
|
||||||
|
changed := false
|
||||||
|
for _, jid := range jids {
|
||||||
|
puppet := portal.user.GetPuppetByJID(jid)
|
||||||
|
changed = levels.EnsureUserLevel(puppet.MXID, newLevel) || changed
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (portal *Portal) RestrictMessageSending(restrict bool) {
|
||||||
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
||||||
|
if err != nil {
|
||||||
|
levels = portal.GetBasePowerLevels()
|
||||||
|
}
|
||||||
|
if restrict {
|
||||||
|
levels.EventsDefault = 50
|
||||||
|
} else {
|
||||||
|
levels.EventsDefault = 0
|
||||||
|
}
|
||||||
|
portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (portal *Portal) RestrictMetadataChanges(restrict bool) {
|
||||||
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
||||||
|
if err != nil {
|
||||||
|
levels = portal.GetBasePowerLevels()
|
||||||
|
}
|
||||||
|
newLevel := 0
|
||||||
|
if restrict {
|
||||||
|
newLevel = 50
|
||||||
|
}
|
||||||
|
changed := false
|
||||||
|
changed = levels.EnsureEventLevel(gomatrix.StateRoomName, true, newLevel) || changed
|
||||||
|
changed = levels.EnsureEventLevel(gomatrix.StateRoomAvatar, true, newLevel) || changed
|
||||||
|
changed = levels.EnsureEventLevel(gomatrix.StateTopic, true, newLevel) || changed
|
||||||
|
if changed {
|
||||||
|
portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (portal *Portal) CreateMatrixRoom() error {
|
func (portal *Portal) CreateMatrixRoom() error {
|
||||||
portal.roomCreateLock.Lock()
|
portal.roomCreateLock.Lock()
|
||||||
defer portal.roomCreateLock.Unlock()
|
defer portal.roomCreateLock.Unlock()
|
||||||
@ -241,6 +331,7 @@ func (portal *Portal) CreateMatrixRoom() error {
|
|||||||
topic = "WhatsApp private chat"
|
topic = "WhatsApp private chat"
|
||||||
isPrivateChat = true
|
isPrivateChat = true
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := portal.MainIntent().CreateRoom(&gomatrix.ReqCreateRoom{
|
resp, err := portal.MainIntent().CreateRoom(&gomatrix.ReqCreateRoom{
|
||||||
Visibility: "private",
|
Visibility: "private",
|
||||||
Name: name,
|
Name: name,
|
||||||
@ -248,6 +339,12 @@ func (portal *Portal) CreateMatrixRoom() error {
|
|||||||
Invite: invite,
|
Invite: invite,
|
||||||
Preset: "private_chat",
|
Preset: "private_chat",
|
||||||
IsDirect: isPrivateChat,
|
IsDirect: isPrivateChat,
|
||||||
|
InitialState: []*gomatrix.Event{{
|
||||||
|
Type: gomatrix.StatePowerLevels,
|
||||||
|
Content: gomatrix.Content{
|
||||||
|
PowerLevels: portal.GetBasePowerLevels(),
|
||||||
|
},
|
||||||
|
}},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -67,7 +67,7 @@ func (store *AutosavingStateStore) SetMembership(roomID, userID, membership stri
|
|||||||
store.Save()
|
store.Save()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *AutosavingStateStore) SetPowerLevels(roomID string, levels gomatrix.PowerLevels) {
|
func (store *AutosavingStateStore) SetPowerLevels(roomID string, levels *gomatrix.PowerLevels) {
|
||||||
store.BasicStateStore.SetPowerLevels(roomID, levels)
|
store.BasicStateStore.SetPowerLevels(roomID, levels)
|
||||||
store.Save()
|
store.Save()
|
||||||
}
|
}
|
9
user.go
9
user.go
@ -321,7 +321,14 @@ func (user *User) HandleChatUpdate(cmd whatsappExt.ChatUpdate) {
|
|||||||
portal.UpdateTopic(cmd.Data.AddTopic.Topic, cmd.Data.SenderJID)
|
portal.UpdateTopic(cmd.Data.AddTopic.Topic, cmd.Data.SenderJID)
|
||||||
case whatsappExt.ChatActionRemoveTopic:
|
case whatsappExt.ChatActionRemoveTopic:
|
||||||
portal.UpdateTopic("", cmd.Data.SenderJID)
|
portal.UpdateTopic("", cmd.Data.SenderJID)
|
||||||
// TODO power level updates
|
case whatsappExt.ChatActionPromote:
|
||||||
|
portal.ChangeAdminStatus(cmd.Data.PermissionChange.JIDs, true)
|
||||||
|
case whatsappExt.ChatActionDemote:
|
||||||
|
portal.ChangeAdminStatus(cmd.Data.PermissionChange.JIDs, false)
|
||||||
|
case whatsappExt.ChatActionAnnounce:
|
||||||
|
portal.RestrictMessageSending(cmd.Data.Announce)
|
||||||
|
case whatsappExt.ChatActionRestrict:
|
||||||
|
portal.RestrictMetadataChanges(cmd.Data.Restrict)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,13 +17,14 @@
|
|||||||
package whatsappExt
|
package whatsappExt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/Rhymen/go-whatsapp"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"io/ioutil"
|
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Rhymen/go-whatsapp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
Loading…
Reference in New Issue
Block a user