2018-08-13 22:24:44 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
|
|
|
// Copyright (C) 2018 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"
|
2018-08-28 23:40:54 +02:00
|
|
|
"strings"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
log "maunium.net/go/maulogger"
|
2018-08-16 23:11:28 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/types"
|
2018-08-13 22:24:44 +02:00
|
|
|
)
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
type PortalKey struct {
|
|
|
|
JID types.WhatsAppID
|
|
|
|
Receiver types.WhatsAppID
|
|
|
|
}
|
|
|
|
|
|
|
|
func GroupPortalKey(jid types.WhatsAppID) PortalKey {
|
|
|
|
return PortalKey{
|
|
|
|
JID: jid,
|
|
|
|
Receiver: jid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPortalKey(jid, receiver types.WhatsAppID) PortalKey {
|
|
|
|
if strings.HasSuffix(jid, "@g.us") {
|
|
|
|
receiver = jid
|
|
|
|
}
|
|
|
|
return PortalKey{
|
|
|
|
JID: jid,
|
|
|
|
Receiver: receiver,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key PortalKey) String() string {
|
|
|
|
if key.Receiver == key.JID {
|
|
|
|
return key.JID
|
|
|
|
}
|
|
|
|
return key.JID + "-" + key.Receiver
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:24:44 +02:00
|
|
|
type PortalQuery struct {
|
|
|
|
db *Database
|
2018-08-16 18:20:07 +02:00
|
|
|
log log.Logger
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PortalQuery) CreateTable() error {
|
|
|
|
_, err := pq.db.Exec(`CREATE TABLE IF NOT EXISTS portal (
|
2018-08-28 23:40:54 +02:00
|
|
|
jid VARCHAR(25),
|
|
|
|
receiver VARCHAR(25),
|
|
|
|
mxid VARCHAR(255) UNIQUE,
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2018-08-26 00:55:21 +02:00
|
|
|
name VARCHAR(255) NOT NULL,
|
|
|
|
topic VARCHAR(255) NOT NULL,
|
|
|
|
avatar VARCHAR(255) NOT NULL,
|
2018-08-13 22:24:44 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
PRIMARY KEY (jid, receiver),
|
|
|
|
FOREIGN KEY (receiver) REFERENCES user(mxid)
|
2018-08-13 22:24:44 +02:00
|
|
|
)`)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pq *PortalQuery) New() *Portal {
|
|
|
|
return &Portal{
|
|
|
|
db: pq.db,
|
|
|
|
log: pq.log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (pq *PortalQuery) GetAll() (portals []*Portal) {
|
|
|
|
rows, err := pq.db.Query("SELECT * FROM portal")
|
2018-08-13 22:24:44 +02:00
|
|
|
if err != nil || rows == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
portals = append(portals, pq.New().Scan(rows))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (pq *PortalQuery) GetByJID(key PortalKey) *Portal {
|
|
|
|
return pq.get("SELECT * FROM portal WHERE jid=? AND receiver=?", key.JID, key.Receiver)
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 23:11:28 +02:00
|
|
|
func (pq *PortalQuery) GetByMXID(mxid types.MatrixRoomID) *Portal {
|
2018-08-13 22:24:44 +02:00
|
|
|
return pq.get("SELECT * FROM portal WHERE mxid=?", mxid)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-08-13 22:24:44 +02:00
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
Key PortalKey
|
|
|
|
MXID types.MatrixRoomID
|
2018-08-18 21:57:08 +02:00
|
|
|
|
|
|
|
Name string
|
2018-08-23 00:12:26 +02:00
|
|
|
Topic string
|
2018-08-18 21:57:08 +02:00
|
|
|
Avatar string
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) Scan(row Scannable) *Portal {
|
2018-08-28 23:40:54 +02:00
|
|
|
err := row.Scan(&portal.Key.JID, &portal.Key.Receiver, &portal.MXID, &portal.Name, &portal.Topic, &portal.Avatar)
|
2018-08-13 22:24:44 +02:00
|
|
|
if err != nil {
|
2018-08-18 21:57:08 +02:00
|
|
|
if err != sql.ErrNoRows {
|
2018-08-26 00:55:21 +02:00
|
|
|
portal.log.Errorln("Database scan failed:", err)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
return nil
|
2018-08-13 22:24:44 +02:00
|
|
|
}
|
|
|
|
return portal
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (portal *Portal) mxidPtr() *string {
|
2018-08-23 00:12:26 +02:00
|
|
|
if len(portal.MXID) > 0 {
|
2018-08-28 23:40:54 +02:00
|
|
|
return &portal.MXID
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) Insert() error {
|
2018-08-23 00:12:26 +02:00
|
|
|
_, err := portal.db.Exec("INSERT INTO portal VALUES (?, ?, ?, ?, ?, ?)",
|
2018-08-28 23:40:54 +02:00
|
|
|
portal.Key.JID, portal.Key.Receiver, portal.mxidPtr(), portal.Name, portal.Topic, portal.Avatar)
|
2018-08-18 21:57:08 +02:00
|
|
|
if err != nil {
|
2018-08-28 23:40:54 +02:00
|
|
|
portal.log.Warnfln("Failed to insert %s: %v", portal.Key, err)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) Update() error {
|
2018-08-23 00:12:26 +02:00
|
|
|
var mxid *string
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
mxid = &portal.MXID
|
|
|
|
}
|
2018-08-28 23:40:54 +02:00
|
|
|
_, err := portal.db.Exec("UPDATE portal SET mxid=?, name=?, topic=?, avatar=? WHERE jid=? AND receiver=?",
|
|
|
|
mxid, portal.Name, portal.Topic, portal.Avatar, portal.Key.JID, portal.Key.Receiver)
|
2018-08-18 21:57:08 +02:00
|
|
|
if err != nil {
|
2018-08-28 23:40:54 +02:00
|
|
|
portal.log.Warnfln("Failed to update %s: %v", portal.Key, err)
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
2018-08-13 22:24:44 +02:00
|
|
|
return err
|
|
|
|
}
|