Add command to disable bridging presence and read receipts
This commit is contained in:
parent
4a673b92fa
commit
7eb4cfb946
33
commands.go
33
commands.go
@ -129,6 +129,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
|
||||
handler.CommandSetPowerLevel(ce)
|
||||
case "logout":
|
||||
handler.CommandLogout(ce)
|
||||
case "toggle-presence":
|
||||
handler.CommandPresence(ce)
|
||||
case "login-matrix", "sync", "list", "open", "pm", "invite-link", "join":
|
||||
if !ce.User.HasSession() {
|
||||
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
|
||||
@ -335,6 +337,36 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
|
||||
ce.Reply("Logged out successfully.")
|
||||
}
|
||||
|
||||
const cmdPresenceHelp = `toggle-presence - Toggle bridging of presence and read receipts`
|
||||
|
||||
func (handler *CommandHandler) CommandPresence(ce *CommandEvent) {
|
||||
if ce.User.Session == nil {
|
||||
ce.Reply("You're not logged in.")
|
||||
return
|
||||
}
|
||||
customPuppet := handler.bridge.GetPuppetByCustomMXID(ce.User.MXID)
|
||||
if customPuppet == nil {
|
||||
ce.Reply("You're not logged in with your Matrix account.")
|
||||
return
|
||||
}
|
||||
customPuppet.EnablePresence = !customPuppet.EnablePresence
|
||||
customPuppet.Update()
|
||||
var newPresence whatsapp.Presence
|
||||
if customPuppet.EnablePresence {
|
||||
newPresence = whatsapp.PresenceAvailable
|
||||
ce.Reply("Enabled presence and read receipt bridging")
|
||||
} else {
|
||||
newPresence = whatsapp.PresenceUnavailable
|
||||
ce.Reply("Disabled presence and read receipt bridging")
|
||||
}
|
||||
if ce.User.IsConnected() {
|
||||
_, err := ce.User.Conn.Presence("", newPresence)
|
||||
if err != nil {
|
||||
ce.User.log.Warnln("Failed to set presence:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const cmdDeleteSessionHelp = `delete-session - Delete session information and disconnect from WhatsApp without sending a logout request`
|
||||
|
||||
func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) {
|
||||
@ -501,6 +533,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
|
||||
cmdPrefix + cmdPingHelp,
|
||||
cmdPrefix + cmdLoginMatrixHelp,
|
||||
cmdPrefix + cmdLogoutMatrixHelp,
|
||||
cmdPrefix + cmdPresenceHelp,
|
||||
cmdPrefix + cmdSyncHelp,
|
||||
cmdPrefix + cmdListHelp,
|
||||
cmdPrefix + cmdOpenHelp,
|
||||
|
@ -153,7 +153,7 @@ func (puppet *Puppet) stopSyncing() {
|
||||
puppet.customIntent.StopSync()
|
||||
}
|
||||
|
||||
func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, since string) error {
|
||||
func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, _ string) error {
|
||||
if !puppet.customUser.IsConnected() {
|
||||
puppet.log.Debugln("Skipping sync processing: custom user not connected to whatsapp")
|
||||
return nil
|
||||
@ -170,12 +170,15 @@ func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, since string) erro
|
||||
}
|
||||
switch evt.Type {
|
||||
case event.EphemeralEventReceipt:
|
||||
if puppet.EnablePresence {
|
||||
go puppet.handleReceiptEvent(portal, evt)
|
||||
}
|
||||
case event.EphemeralEventTyping:
|
||||
go puppet.handleTypingEvent(portal, evt)
|
||||
}
|
||||
}
|
||||
}
|
||||
if puppet.EnablePresence {
|
||||
for _, evt := range resp.Presence.Events {
|
||||
if evt.Sender != puppet.CustomMXID {
|
||||
continue
|
||||
@ -186,6 +189,7 @@ func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, since string) erro
|
||||
}
|
||||
go puppet.handlePresenceEvent(evt)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -244,7 +248,7 @@ func (puppet *Puppet) handleTypingEvent(portal *Portal, evt *event.Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (puppet *Puppet) OnFailedSync(res *mautrix.RespSync, err error) (time.Duration, error) {
|
||||
func (puppet *Puppet) OnFailedSync(_ *mautrix.RespSync, err error) (time.Duration, error) {
|
||||
puppet.log.Warnln("Sync error:", err)
|
||||
return 10 * time.Second, nil
|
||||
}
|
||||
@ -269,7 +273,7 @@ func (puppet *Puppet) GetFilterJSON(_ id.UserID) *mautrix.Filter {
|
||||
|
||||
func (puppet *Puppet) SaveFilterID(_ id.UserID, _ string) {}
|
||||
func (puppet *Puppet) SaveNextBatch(_ id.UserID, nbt string) { puppet.NextBatch = nbt; puppet.Update() }
|
||||
func (puppet *Puppet) SaveRoom(room *mautrix.Room) {}
|
||||
func (puppet *Puppet) SaveRoom(_ *mautrix.Room) {}
|
||||
func (puppet *Puppet) LoadFilterID(_ id.UserID) string { return "" }
|
||||
func (puppet *Puppet) LoadNextBatch(_ id.UserID) string { return puppet.NextBatch }
|
||||
func (puppet *Puppet) LoadRoom(roomID id.RoomID) *mautrix.Room { return nil }
|
||||
func (puppet *Puppet) LoadRoom(_ id.RoomID) *mautrix.Room { return nil }
|
||||
|
@ -21,8 +21,9 @@ import (
|
||||
|
||||
log "maunium.net/go/maulogger/v2"
|
||||
|
||||
"maunium.net/go/mautrix-whatsapp/types"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"maunium.net/go/mautrix-whatsapp/types"
|
||||
)
|
||||
|
||||
type PuppetQuery struct {
|
||||
@ -34,11 +35,13 @@ func (pq *PuppetQuery) New() *Puppet {
|
||||
return &Puppet{
|
||||
db: pq.db,
|
||||
log: pq.log,
|
||||
|
||||
EnablePresence: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (pq *PuppetQuery) GetAll() (puppets []*Puppet) {
|
||||
rows, err := pq.db.Query("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch FROM puppet")
|
||||
rows, err := pq.db.Query("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence FROM puppet")
|
||||
if err != nil || rows == nil {
|
||||
return nil
|
||||
}
|
||||
@ -50,7 +53,7 @@ func (pq *PuppetQuery) GetAll() (puppets []*Puppet) {
|
||||
}
|
||||
|
||||
func (pq *PuppetQuery) Get(jid types.WhatsAppID) *Puppet {
|
||||
row := pq.db.QueryRow("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch FROM puppet WHERE jid=$1", jid)
|
||||
row := pq.db.QueryRow("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence FROM puppet WHERE jid=$1", jid)
|
||||
if row == nil {
|
||||
return nil
|
||||
}
|
||||
@ -58,7 +61,7 @@ func (pq *PuppetQuery) Get(jid types.WhatsAppID) *Puppet {
|
||||
}
|
||||
|
||||
func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *Puppet {
|
||||
row := pq.db.QueryRow("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch FROM puppet WHERE custom_mxid=$1", mxid)
|
||||
row := pq.db.QueryRow("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence FROM puppet WHERE custom_mxid=$1", mxid)
|
||||
if row == nil {
|
||||
return nil
|
||||
}
|
||||
@ -66,7 +69,7 @@ func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *Puppet {
|
||||
}
|
||||
|
||||
func (pq *PuppetQuery) GetAllWithCustomMXID() (puppets []*Puppet) {
|
||||
rows, err := pq.db.Query("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch FROM puppet WHERE custom_mxid<>''")
|
||||
rows, err := pq.db.Query("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence FROM puppet WHERE custom_mxid<>''")
|
||||
if err != nil || rows == nil {
|
||||
return nil
|
||||
}
|
||||
@ -90,12 +93,14 @@ type Puppet struct {
|
||||
CustomMXID id.UserID
|
||||
AccessToken string
|
||||
NextBatch string
|
||||
EnablePresence bool
|
||||
}
|
||||
|
||||
func (puppet *Puppet) Scan(row Scannable) *Puppet {
|
||||
var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString
|
||||
var quality sql.NullInt64
|
||||
err := row.Scan(&puppet.JID, &avatar, &avatarURL, &displayname, &quality, &customMXID, &accessToken, &nextBatch)
|
||||
var enablePresence sql.NullBool
|
||||
err := row.Scan(&puppet.JID, &avatar, &avatarURL, &displayname, &quality, &customMXID, &accessToken, &nextBatch, &enablePresence)
|
||||
if err != nil {
|
||||
if err != sql.ErrNoRows {
|
||||
puppet.log.Errorln("Database scan failed:", err)
|
||||
@ -109,20 +114,21 @@ func (puppet *Puppet) Scan(row Scannable) *Puppet {
|
||||
puppet.CustomMXID = id.UserID(customMXID.String)
|
||||
puppet.AccessToken = accessToken.String
|
||||
puppet.NextBatch = nextBatch.String
|
||||
puppet.EnablePresence = enablePresence.Bool
|
||||
return puppet
|
||||
}
|
||||
|
||||
func (puppet *Puppet) Insert() {
|
||||
_, err := puppet.db.Exec("INSERT INTO puppet (jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
|
||||
puppet.JID, puppet.Avatar, puppet.AvatarURL.String(), puppet.Displayname, puppet.NameQuality, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch)
|
||||
_, err := puppet.db.Exec("INSERT INTO puppet (jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
||||
puppet.JID, puppet.Avatar, puppet.AvatarURL.String(), puppet.Displayname, puppet.NameQuality, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence)
|
||||
if err != nil {
|
||||
puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (puppet *Puppet) Update() {
|
||||
_, err := puppet.db.Exec("UPDATE puppet SET displayname=$1, name_quality=$2, avatar=$3, avatar_url=$4, custom_mxid=$5, access_token=$6, next_batch=$7 WHERE jid=$8",
|
||||
puppet.Displayname, puppet.NameQuality, puppet.Avatar, puppet.AvatarURL.String(), puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.JID)
|
||||
_, err := puppet.db.Exec("UPDATE puppet SET displayname=$1, name_quality=$2, avatar=$3, avatar_url=$4, custom_mxid=$5, access_token=$6, next_batch=$7, enable_presence=$8 WHERE jid=$9",
|
||||
puppet.Displayname, puppet.NameQuality, puppet.Avatar, puppet.AvatarURL.String(), puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.JID)
|
||||
if err != nil {
|
||||
puppet.log.Warnfln("Failed to update %s->%s: %v", puppet.JID, err)
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
package upgrades
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
func init() {
|
||||
upgrades[15] = upgrade{"Add enable_presence column for puppets", func(tx *sql.Tx, ctx context) error {
|
||||
_, err := tx.Exec(`ALTER TABLE puppet ADD COLUMN enable_presence BOOLEAN NOT NULL DEFAULT true`)
|
||||
return err
|
||||
}}
|
||||
}
|
@ -28,7 +28,7 @@ type upgrade struct {
|
||||
fn upgradeFunc
|
||||
}
|
||||
|
||||
const NumberOfUpgrades = 15
|
||||
const NumberOfUpgrades = 16
|
||||
|
||||
var upgrades [NumberOfUpgrades]upgrade
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user