mautrix-whatsapp/database/portal.go

278 lines
8.5 KiB
Go
Raw Normal View History

// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
2021-10-22 19:14:34 +02:00
// Copyright (C) 2021 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package database
import (
2018-08-26 21:53:13 +02:00
"database/sql"
2022-06-24 16:53:28 +02:00
"fmt"
"time"
2018-08-26 21:53:13 +02:00
"go.mau.fi/util/dbutil"
"go.mau.fi/whatsmeow/types"
2019-01-11 20:17:31 +01:00
log "maunium.net/go/maulogger/v2"
2021-02-17 00:21:30 +01:00
"maunium.net/go/mautrix/id"
)
type PortalKey struct {
2021-10-22 19:14:34 +02:00
JID types.JID
Receiver types.JID
}
2021-10-22 19:14:34 +02:00
func NewPortalKey(jid, receiver types.JID) PortalKey {
if jid.Server == types.GroupServer {
receiver = jid
} else if jid.Server == types.LegacyUserServer {
jid.Server = types.DefaultUserServer
}
return PortalKey{
2021-10-22 19:14:34 +02:00
JID: jid.ToNonAD(),
Receiver: receiver.ToNonAD(),
}
}
func (key PortalKey) String() string {
if key.Receiver == key.JID {
2021-10-22 19:14:34 +02:00
return key.JID.String()
}
2021-10-22 19:14:34 +02:00
return key.JID.String() + "-" + key.Receiver.String()
}
type PortalQuery struct {
db *Database
2018-08-16 18:20:07 +02:00
log log.Logger
}
func (pq *PortalQuery) New() *Portal {
return &Portal{
db: pq.db,
log: pq.log,
}
}
2022-12-02 14:36:19 +01:00
const portalColumns = "jid, receiver, mxid, name, name_set, topic, topic_set, avatar, avatar_url, avatar_set, encrypted, last_sync, is_parent, parent_group, in_space, first_event_id, next_batch_id, relay_user_id, expiration_time"
2022-06-24 16:53:28 +02:00
func (pq *PortalQuery) GetAll() []*Portal {
return pq.getAll(fmt.Sprintf("SELECT %s FROM portal", portalColumns))
}
func (pq *PortalQuery) GetByJID(key PortalKey) *Portal {
2022-06-24 16:53:28 +02:00
return pq.get(fmt.Sprintf("SELECT %s FROM portal WHERE jid=$1 AND receiver=$2", portalColumns), key.JID, key.Receiver)
}
2020-05-08 21:32:22 +02:00
func (pq *PortalQuery) GetByMXID(mxid id.RoomID) *Portal {
2022-06-24 16:53:28 +02:00
return pq.get(fmt.Sprintf("SELECT %s FROM portal WHERE mxid=$1", portalColumns), mxid)
}
2021-10-22 19:14:34 +02:00
func (pq *PortalQuery) GetAllByJID(jid types.JID) []*Portal {
2022-06-24 16:53:28 +02:00
return pq.getAll(fmt.Sprintf("SELECT %s FROM portal WHERE jid=$1", portalColumns), jid.ToNonAD())
}
func (pq *PortalQuery) GetAllByParentGroup(jid types.JID) []*Portal {
return pq.getAll(fmt.Sprintf("SELECT %s FROM portal WHERE parent_group=$1", portalColumns), jid)
}
2021-10-22 19:14:34 +02:00
func (pq *PortalQuery) FindPrivateChats(receiver types.JID) []*Portal {
return pq.getAll(fmt.Sprintf("SELECT %s FROM portal WHERE receiver=$1 AND jid LIKE '%%@s.whatsapp.net'", portalColumns), receiver.ToNonAD())
}
func (pq *PortalQuery) FindPrivateChatsNotInSpace(receiver types.JID) (keys []PortalKey) {
receiver = receiver.ToNonAD()
rows, err := pq.db.Query(`
SELECT jid FROM portal
LEFT JOIN user_portal ON portal.jid=user_portal.portal_jid AND portal.receiver=user_portal.portal_receiver
2023-02-15 13:33:53 +01:00
WHERE mxid<>'' AND receiver=$1 AND (user_portal.in_space=false OR user_portal.in_space IS NULL)
`, receiver)
2023-02-15 13:33:53 +01:00
if err != nil {
pq.log.Errorfln("Failed to find private chats not in space for %s: %v", receiver, err)
return
} else if rows == nil {
return
}
for rows.Next() {
var key PortalKey
key.Receiver = receiver
err = rows.Scan(&key.JID)
if err == nil {
keys = append(keys, key)
}
}
return
}
func (pq *PortalQuery) getAll(query string, args ...interface{}) (portals []*Portal) {
rows, err := pq.db.Query(query, args...)
if err != nil || rows == nil {
return nil
}
defer rows.Close()
for rows.Next() {
portals = append(portals, pq.New().Scan(rows))
}
return
}
func (pq *PortalQuery) get(query string, args ...interface{}) *Portal {
row := pq.db.QueryRow(query, args...)
if row == nil {
return nil
}
return pq.New().Scan(row)
}
type Portal struct {
db *Database
2018-08-16 18:20:07 +02:00
log log.Logger
Key PortalKey
2020-05-08 21:32:22 +02:00
MXID id.RoomID
Name string
2022-06-28 13:37:49 +02:00
NameSet bool
Topic string
2022-06-28 13:37:49 +02:00
TopicSet bool
Avatar string
2020-05-08 21:32:22 +02:00
AvatarURL id.ContentURI
2022-06-28 13:37:49 +02:00
AvatarSet bool
Encrypted bool
LastSync time.Time
2022-12-02 14:36:19 +01:00
IsParent bool
ParentGroup types.JID
InSpace bool
2022-01-07 13:32:00 +01:00
2022-12-02 14:36:19 +01:00
FirstEventID id.EventID
NextBatchID id.BatchID
RelayUserID id.UserID
2022-01-07 13:32:00 +01:00
ExpirationTime uint32
}
func (portal *Portal) Scan(row dbutil.Scannable) *Portal {
2022-12-02 14:36:19 +01:00
var mxid, avatarURL, firstEventID, nextBatchID, relayUserID, parentGroupJID sql.NullString
var lastSyncTs int64
2022-12-02 14:36:19 +01:00
err := row.Scan(&portal.Key.JID, &portal.Key.Receiver, &mxid, &portal.Name, &portal.NameSet, &portal.Topic, &portal.TopicSet, &portal.Avatar, &avatarURL, &portal.AvatarSet, &portal.Encrypted, &lastSyncTs, &portal.IsParent, &parentGroupJID, &portal.InSpace, &firstEventID, &nextBatchID, &relayUserID, &portal.ExpirationTime)
if err != nil {
if err != sql.ErrNoRows {
portal.log.Errorln("Database scan failed:", err)
}
return nil
}
if lastSyncTs > 0 {
portal.LastSync = time.Unix(lastSyncTs, 0)
}
2020-05-08 21:32:22 +02:00
portal.MXID = id.RoomID(mxid.String)
portal.AvatarURL, _ = id.ParseContentURI(avatarURL.String)
2022-12-02 14:36:19 +01:00
if parentGroupJID.Valid {
portal.ParentGroup, _ = types.ParseJID(parentGroupJID.String)
}
portal.FirstEventID = id.EventID(firstEventID.String)
portal.NextBatchID = id.BatchID(nextBatchID.String)
portal.RelayUserID = id.UserID(relayUserID.String)
return portal
}
2020-05-08 21:32:22 +02:00
func (portal *Portal) mxidPtr() *id.RoomID {
if len(portal.MXID) > 0 {
return &portal.MXID
}
return nil
}
func (portal *Portal) relayUserPtr() *id.UserID {
if len(portal.RelayUserID) > 0 {
return &portal.RelayUserID
}
return nil
}
2022-12-02 14:36:19 +01:00
func (portal *Portal) parentGroupPtr() *string {
if !portal.ParentGroup.IsEmpty() {
val := portal.ParentGroup.String()
return &val
}
return nil
}
func (portal *Portal) lastSyncTs() int64 {
if portal.LastSync.IsZero() {
return 0
}
return portal.LastSync.Unix()
}
2019-01-21 22:55:16 +01:00
func (portal *Portal) Insert() {
2022-06-28 13:37:49 +02:00
_, err := portal.db.Exec(`
INSERT INTO portal (jid, receiver, mxid, name, name_set, topic, topic_set, avatar, avatar_url, avatar_set,
2022-12-02 14:36:19 +01:00
encrypted, last_sync, is_parent, parent_group, in_space, first_event_id, next_batch_id,
relay_user_id, expiration_time)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)
2022-06-28 13:37:49 +02:00
`,
portal.Key.JID, portal.Key.Receiver, portal.mxidPtr(), portal.Name, portal.NameSet, portal.Topic, portal.TopicSet,
portal.Avatar, portal.AvatarURL.String(), portal.AvatarSet, portal.Encrypted, portal.lastSyncTs(),
2022-12-02 14:36:19 +01:00
portal.IsParent, portal.parentGroupPtr(), portal.InSpace, portal.FirstEventID.String(), portal.NextBatchID.String(),
portal.relayUserPtr(), portal.ExpirationTime)
if err != nil {
portal.log.Warnfln("Failed to insert %s: %v", portal.Key, err)
}
}
func (portal *Portal) Update(txn dbutil.Execable) {
if txn == nil {
txn = portal.db
}
_, err := txn.Exec(`
UPDATE portal
2022-06-28 13:37:49 +02:00
SET mxid=$1, name=$2, name_set=$3, topic=$4, topic_set=$5, avatar=$6, avatar_url=$7, avatar_set=$8,
2022-12-02 14:36:19 +01:00
encrypted=$9, last_sync=$10, is_parent=$11, parent_group=$12, in_space=$13,
first_event_id=$14, next_batch_id=$15, relay_user_id=$16, expiration_time=$17
WHERE jid=$18 AND receiver=$19
`, portal.mxidPtr(), portal.Name, portal.NameSet, portal.Topic, portal.TopicSet, portal.Avatar, portal.AvatarURL.String(),
2022-12-02 14:36:19 +01:00
portal.AvatarSet, portal.Encrypted, portal.lastSyncTs(), portal.IsParent, portal.parentGroupPtr(), portal.InSpace,
portal.FirstEventID.String(), portal.NextBatchID.String(), portal.relayUserPtr(), portal.ExpirationTime,
portal.Key.JID, portal.Key.Receiver)
if err != nil {
portal.log.Warnfln("Failed to update %s: %v", portal.Key, err)
}
}
func (portal *Portal) Delete() {
txn, err := portal.db.Begin()
if err != nil {
portal.log.Errorfln("Failed to begin transaction to delete portal %v: %v", portal.Key, err)
return
}
defer func() {
if err != nil {
err = txn.Rollback()
if err != nil {
portal.log.Warnfln("Failed to rollback failed portal delete transaction: %v", err)
}
} else if err = txn.Commit(); err != nil {
portal.log.Warnfln("Failed to commit portal delete transaction: %v", err)
}
}()
2023-01-04 01:13:20 +01:00
_, err = txn.Exec("UPDATE portal SET in_space=false WHERE parent_group=$1", portal.Key.JID)
if err != nil {
portal.log.Warnfln("Failed to mark child groups of %v as not in space: %v", portal.Key.JID, err)
return
}
2023-01-04 01:13:20 +01:00
_, err = txn.Exec("DELETE FROM portal WHERE jid=$1 AND receiver=$2", portal.Key.JID, portal.Key.Receiver)
if err != nil {
portal.log.Warnfln("Failed to delete %v: %v", portal.Key, err)
}
}