Make bridging receipts togglable separately from presence
This commit is contained in:
parent
f40a91594d
commit
b7275a763a
51
commands.go
51
commands.go
@ -129,8 +129,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) {
|
|||||||
handler.CommandSetPowerLevel(ce)
|
handler.CommandSetPowerLevel(ce)
|
||||||
case "logout":
|
case "logout":
|
||||||
handler.CommandLogout(ce)
|
handler.CommandLogout(ce)
|
||||||
case "toggle-presence":
|
case "toggle":
|
||||||
handler.CommandPresence(ce)
|
handler.CommandToggle(ce)
|
||||||
case "login-matrix", "sync", "list", "open", "pm", "invite-link", "join", "create":
|
case "login-matrix", "sync", "list", "open", "pm", "invite-link", "join", "create":
|
||||||
if !ce.User.HasSession() {
|
if !ce.User.HasSession() {
|
||||||
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
|
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
|
||||||
@ -408,9 +408,13 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
|
|||||||
ce.Reply("Logged out successfully.")
|
ce.Reply("Logged out successfully.")
|
||||||
}
|
}
|
||||||
|
|
||||||
const cmdPresenceHelp = `toggle-presence - Toggle bridging of presence and read receipts`
|
const cmdToggleHelp = `toggle <presence|receipts> - Toggle bridging of presence or read receipts`
|
||||||
|
|
||||||
func (handler *CommandHandler) CommandPresence(ce *CommandEvent) {
|
func (handler *CommandHandler) CommandToggle(ce *CommandEvent) {
|
||||||
|
if len(ce.Args) == 0 || (ce.Args[0] != "presence" && ce.Args[0] != "receipts") {
|
||||||
|
ce.Reply("**Usage:** `toggle <presence|receipts>`")
|
||||||
|
return
|
||||||
|
}
|
||||||
if ce.User.Session == nil {
|
if ce.User.Session == nil {
|
||||||
ce.Reply("You're not logged in.")
|
ce.Reply("You're not logged in.")
|
||||||
return
|
return
|
||||||
@ -420,22 +424,31 @@ func (handler *CommandHandler) CommandPresence(ce *CommandEvent) {
|
|||||||
ce.Reply("You're not logged in with your Matrix account.")
|
ce.Reply("You're not logged in with your Matrix account.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
customPuppet.EnablePresence = !customPuppet.EnablePresence
|
if ce.Args[0] == "presence" {
|
||||||
customPuppet.Update()
|
customPuppet.EnablePresence = !customPuppet.EnablePresence
|
||||||
var newPresence whatsapp.Presence
|
var newPresence whatsapp.Presence
|
||||||
if customPuppet.EnablePresence {
|
if customPuppet.EnablePresence {
|
||||||
newPresence = whatsapp.PresenceAvailable
|
newPresence = whatsapp.PresenceAvailable
|
||||||
ce.Reply("Enabled presence and read receipt bridging")
|
ce.Reply("Enabled presence bridging")
|
||||||
} else {
|
} else {
|
||||||
newPresence = whatsapp.PresenceUnavailable
|
newPresence = whatsapp.PresenceUnavailable
|
||||||
ce.Reply("Disabled presence and read receipt bridging")
|
ce.Reply("Disabled presence bridging")
|
||||||
}
|
}
|
||||||
if ce.User.IsConnected() {
|
if ce.User.IsConnected() {
|
||||||
_, err := ce.User.Conn.Presence("", newPresence)
|
_, err := ce.User.Conn.Presence("", newPresence)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ce.User.log.Warnln("Failed to set presence:", err)
|
ce.User.log.Warnln("Failed to set presence:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if ce.Args[0] == "receipts" {
|
||||||
|
customPuppet.EnableReceipts = !customPuppet.EnableReceipts
|
||||||
|
if customPuppet.EnableReceipts {
|
||||||
|
ce.Reply("Enabled read receipt bridging")
|
||||||
|
} else {
|
||||||
|
ce.Reply("Disabled read receipt bridging")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
customPuppet.Update()
|
||||||
}
|
}
|
||||||
|
|
||||||
const cmdDeleteSessionHelp = `delete-session - Delete session information and disconnect from WhatsApp without sending a logout request`
|
const cmdDeleteSessionHelp = `delete-session - Delete session information and disconnect from WhatsApp without sending a logout request`
|
||||||
@ -604,7 +617,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
|
|||||||
cmdPrefix + cmdPingHelp,
|
cmdPrefix + cmdPingHelp,
|
||||||
cmdPrefix + cmdLoginMatrixHelp,
|
cmdPrefix + cmdLoginMatrixHelp,
|
||||||
cmdPrefix + cmdLogoutMatrixHelp,
|
cmdPrefix + cmdLogoutMatrixHelp,
|
||||||
cmdPrefix + cmdPresenceHelp,
|
cmdPrefix + cmdToggleHelp,
|
||||||
cmdPrefix + cmdSyncHelp,
|
cmdPrefix + cmdSyncHelp,
|
||||||
cmdPrefix + cmdListHelp,
|
cmdPrefix + cmdListHelp,
|
||||||
cmdPrefix + cmdOpenHelp,
|
cmdPrefix + cmdOpenHelp,
|
||||||
|
@ -58,6 +58,8 @@ type BridgeConfig struct {
|
|||||||
SyncChatMaxAge uint64 `yaml:"sync_max_chat_age"`
|
SyncChatMaxAge uint64 `yaml:"sync_max_chat_age"`
|
||||||
|
|
||||||
SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
|
SyncWithCustomPuppets bool `yaml:"sync_with_custom_puppets"`
|
||||||
|
DefaultBridgeReceipts bool `yaml:"default_bridge_receipts"`
|
||||||
|
DefaultBridgePresence bool `yaml:"default_bridge_presence"`
|
||||||
LoginSharedSecret string `yaml:"login_shared_secret"`
|
LoginSharedSecret string `yaml:"login_shared_secret"`
|
||||||
|
|
||||||
InviteOwnPuppetForBackfilling bool `yaml:"invite_own_puppet_for_backfilling"`
|
InviteOwnPuppetForBackfilling bool `yaml:"invite_own_puppet_for_backfilling"`
|
||||||
@ -105,6 +107,8 @@ func (bc *BridgeConfig) setDefaults() {
|
|||||||
bc.SyncChatMaxAge = 259200
|
bc.SyncChatMaxAge = 259200
|
||||||
|
|
||||||
bc.SyncWithCustomPuppets = true
|
bc.SyncWithCustomPuppets = true
|
||||||
|
bc.DefaultBridgePresence = true
|
||||||
|
bc.DefaultBridgeReceipts = true
|
||||||
bc.LoginSharedSecret = ""
|
bc.LoginSharedSecret = ""
|
||||||
|
|
||||||
bc.InviteOwnPuppetForBackfilling = true
|
bc.InviteOwnPuppetForBackfilling = true
|
||||||
|
@ -56,6 +56,8 @@ func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid id.UserID) error
|
|||||||
if len(puppet.CustomMXID) > 0 {
|
if len(puppet.CustomMXID) > 0 {
|
||||||
puppet.bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
puppet.bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
||||||
}
|
}
|
||||||
|
puppet.EnablePresence = puppet.bridge.Config.Bridge.DefaultBridgePresence
|
||||||
|
puppet.EnableReceipts = puppet.bridge.Config.Bridge.DefaultBridgeReceipts
|
||||||
puppet.bridge.AS.StateStore.MarkRegistered(puppet.CustomMXID)
|
puppet.bridge.AS.StateStore.MarkRegistered(puppet.CustomMXID)
|
||||||
puppet.Update()
|
puppet.Update()
|
||||||
// TODO leave rooms with default puppet
|
// TODO leave rooms with default puppet
|
||||||
@ -170,7 +172,7 @@ func (puppet *Puppet) ProcessResponse(resp *mautrix.RespSync, _ string) error {
|
|||||||
}
|
}
|
||||||
switch evt.Type {
|
switch evt.Type {
|
||||||
case event.EphemeralEventReceipt:
|
case event.EphemeralEventReceipt:
|
||||||
if puppet.EnablePresence {
|
if puppet.EnableReceipts {
|
||||||
go puppet.handleReceiptEvent(portal, evt)
|
go puppet.handleReceiptEvent(portal, evt)
|
||||||
}
|
}
|
||||||
case event.EphemeralEventTyping:
|
case event.EphemeralEventTyping:
|
||||||
|
@ -37,11 +37,12 @@ func (pq *PuppetQuery) New() *Puppet {
|
|||||||
log: pq.log,
|
log: pq.log,
|
||||||
|
|
||||||
EnablePresence: true,
|
EnablePresence: true,
|
||||||
|
EnableReceipts: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pq *PuppetQuery) GetAll() (puppets []*Puppet) {
|
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, enable_presence FROM puppet")
|
rows, err := pq.db.Query("SELECT jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence, enable_receipts FROM puppet")
|
||||||
if err != nil || rows == nil {
|
if err != nil || rows == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -53,7 +54,7 @@ func (pq *PuppetQuery) GetAll() (puppets []*Puppet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pq *PuppetQuery) Get(jid types.WhatsAppID) *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, enable_presence 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, enable_receipts FROM puppet WHERE jid=$1", jid)
|
||||||
if row == nil {
|
if row == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -61,7 +62,7 @@ func (pq *PuppetQuery) Get(jid types.WhatsAppID) *Puppet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *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, enable_presence 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, enable_receipts FROM puppet WHERE custom_mxid=$1", mxid)
|
||||||
if row == nil {
|
if row == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -69,7 +70,7 @@ func (pq *PuppetQuery) GetByCustomMXID(mxid id.UserID) *Puppet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pq *PuppetQuery) GetAllWithCustomMXID() (puppets []*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, enable_presence 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, enable_receipts FROM puppet WHERE custom_mxid<>''")
|
||||||
if err != nil || rows == nil {
|
if err != nil || rows == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -94,13 +95,14 @@ type Puppet struct {
|
|||||||
AccessToken string
|
AccessToken string
|
||||||
NextBatch string
|
NextBatch string
|
||||||
EnablePresence bool
|
EnablePresence bool
|
||||||
|
EnableReceipts bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (puppet *Puppet) Scan(row Scannable) *Puppet {
|
func (puppet *Puppet) Scan(row Scannable) *Puppet {
|
||||||
var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString
|
var displayname, avatar, avatarURL, customMXID, accessToken, nextBatch sql.NullString
|
||||||
var quality sql.NullInt64
|
var quality sql.NullInt64
|
||||||
var enablePresence sql.NullBool
|
var enablePresence, enableReceipts sql.NullBool
|
||||||
err := row.Scan(&puppet.JID, &avatar, &avatarURL, &displayname, &quality, &customMXID, &accessToken, &nextBatch, &enablePresence)
|
err := row.Scan(&puppet.JID, &avatar, &avatarURL, &displayname, &quality, &customMXID, &accessToken, &nextBatch, &enablePresence, &enableReceipts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != sql.ErrNoRows {
|
if err != sql.ErrNoRows {
|
||||||
puppet.log.Errorln("Database scan failed:", err)
|
puppet.log.Errorln("Database scan failed:", err)
|
||||||
@ -115,20 +117,21 @@ func (puppet *Puppet) Scan(row Scannable) *Puppet {
|
|||||||
puppet.AccessToken = accessToken.String
|
puppet.AccessToken = accessToken.String
|
||||||
puppet.NextBatch = nextBatch.String
|
puppet.NextBatch = nextBatch.String
|
||||||
puppet.EnablePresence = enablePresence.Bool
|
puppet.EnablePresence = enablePresence.Bool
|
||||||
|
puppet.EnableReceipts = enableReceipts.Bool
|
||||||
return puppet
|
return puppet
|
||||||
}
|
}
|
||||||
|
|
||||||
func (puppet *Puppet) Insert() {
|
func (puppet *Puppet) Insert() {
|
||||||
_, 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)",
|
_, err := puppet.db.Exec("INSERT INTO puppet (jid, avatar, avatar_url, displayname, name_quality, custom_mxid, access_token, next_batch, enable_presence, enable_receipts) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||||
puppet.JID, puppet.Avatar, puppet.AvatarURL.String(), puppet.Displayname, puppet.NameQuality, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence)
|
puppet.JID, puppet.Avatar, puppet.AvatarURL.String(), puppet.Displayname, puppet.NameQuality, puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.EnableReceipts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, err)
|
puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (puppet *Puppet) Update() {
|
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, enable_presence=$8 WHERE jid=$9",
|
_, 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, enable_receipts=$9 WHERE jid=$10",
|
||||||
puppet.Displayname, puppet.NameQuality, puppet.Avatar, puppet.AvatarURL.String(), puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.JID)
|
puppet.Displayname, puppet.NameQuality, puppet.Avatar, puppet.AvatarURL.String(), puppet.CustomMXID, puppet.AccessToken, puppet.NextBatch, puppet.EnablePresence, puppet.EnableReceipts, puppet.JID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
puppet.log.Warnfln("Failed to update %s->%s: %v", puppet.JID, err)
|
puppet.log.Warnfln("Failed to update %s->%s: %v", puppet.JID, err)
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package upgrades
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
upgrades[17] = upgrade{"Add enable_receipts column for puppets", func(tx *sql.Tx, ctx context) error {
|
||||||
|
_, err := tx.Exec(`ALTER TABLE puppet ADD COLUMN enable_receipts BOOLEAN NOT NULL DEFAULT true`)
|
||||||
|
return err
|
||||||
|
}}
|
||||||
|
}
|
@ -39,7 +39,7 @@ type upgrade struct {
|
|||||||
fn upgradeFunc
|
fn upgradeFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
const NumberOfUpgrades = 17
|
const NumberOfUpgrades = 18
|
||||||
|
|
||||||
var upgrades [NumberOfUpgrades]upgrade
|
var upgrades [NumberOfUpgrades]upgrade
|
||||||
|
|
||||||
|
@ -136,6 +136,11 @@ bridge:
|
|||||||
# Whether or not to sync with custom puppets to receive EDUs that
|
# Whether or not to sync with custom puppets to receive EDUs that
|
||||||
# are not normally sent to appservices.
|
# are not normally sent to appservices.
|
||||||
sync_with_custom_puppets: true
|
sync_with_custom_puppets: true
|
||||||
|
# When double puppeting is enabled, users can use `!wa toggle` to change whether or not
|
||||||
|
# presence and read receipts are bridged. These settings set the default values.
|
||||||
|
# Existing users won't be affected when these are changed.
|
||||||
|
default_bridge_receipts: true
|
||||||
|
default_bridge_presence: true
|
||||||
# Shared secret for https://github.com/devture/matrix-synapse-shared-secret-auth
|
# Shared secret for https://github.com/devture/matrix-synapse-shared-secret-auth
|
||||||
#
|
#
|
||||||
# If set, custom puppets will be enabled automatically for local users
|
# If set, custom puppets will be enabled automatically for local users
|
||||||
|
Loading…
Reference in New Issue
Block a user