2018-08-23 00:12:26 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2019-01-12 14:54:04 +01:00
|
|
|
// Copyright (C) 2019 Tulir Asokan
|
2018-08-23 00:12:26 +02:00
|
|
|
//
|
|
|
|
// 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-09-01 22:38:03 +02:00
|
|
|
"bytes"
|
2019-03-06 23:03:19 +01:00
|
|
|
"strings"
|
2018-08-26 21:53:13 +02:00
|
|
|
"database/sql"
|
2018-09-01 22:38:03 +02:00
|
|
|
"encoding/json"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2018-09-01 22:38:03 +02:00
|
|
|
waProto "github.com/Rhymen/go-whatsapp/binary/proto"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
|
|
|
log "maunium.net/go/maulogger/v2"
|
|
|
|
|
2018-08-23 00:12:26 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MessageQuery struct {
|
|
|
|
db *Database
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:22:54 +01:00
|
|
|
func (mq *MessageQuery) CreateTable(dbType string) error {
|
2019-03-06 23:03:19 +01:00
|
|
|
if strings.ToLower(dbType) == "postgres" {
|
|
|
|
_, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message (
|
|
|
|
chat_jid VARCHAR(255),
|
|
|
|
chat_receiver VARCHAR(255),
|
|
|
|
jid VARCHAR(255),
|
|
|
|
mxid VARCHAR(255) NOT NULL UNIQUE,
|
|
|
|
sender VARCHAR(255) NOT NULL,
|
|
|
|
content bytea NOT NULL,
|
|
|
|
|
|
|
|
PRIMARY KEY (chat_jid, chat_receiver, jid),
|
|
|
|
FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver)
|
|
|
|
)`)
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
_, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message (
|
|
|
|
chat_jid VARCHAR(255),
|
|
|
|
chat_receiver VARCHAR(255),
|
|
|
|
jid VARCHAR(255),
|
|
|
|
mxid VARCHAR(255) NOT NULL UNIQUE,
|
|
|
|
sender VARCHAR(255) NOT NULL,
|
|
|
|
content BLOB NOT NULL,
|
|
|
|
|
|
|
|
PRIMARY KEY (chat_jid, chat_receiver, jid),
|
|
|
|
FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver)
|
|
|
|
)`)
|
2018-08-23 00:12:26 +02:00
|
|
|
return err
|
2019-03-06 23:03:19 +01:00
|
|
|
}
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mq *MessageQuery) New() *Message {
|
|
|
|
return &Message{
|
|
|
|
db: mq.db,
|
|
|
|
log: mq.log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (mq *MessageQuery) GetAll(chat PortalKey) (messages []*Message) {
|
2019-03-06 16:33:42 +01:00
|
|
|
rows, err := mq.db.Query("SELECT * FROM message WHERE chat_jid=$1 AND chat_receiver=$2", chat.JID, chat.Receiver)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil || rows == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
messages = append(messages, mq.New().Scan(rows))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:40:54 +02:00
|
|
|
func (mq *MessageQuery) GetByJID(chat PortalKey, jid types.WhatsAppMessageID) *Message {
|
2019-03-06 16:33:42 +01:00
|
|
|
return mq.get("SELECT * FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3", chat.JID, chat.Receiver, jid)
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mq *MessageQuery) GetByMXID(mxid types.MatrixEventID) *Message {
|
2019-03-06 16:33:42 +01:00
|
|
|
return mq.get("SELECT * FROM message WHERE mxid=$1", mxid)
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mq *MessageQuery) get(query string, args ...interface{}) *Message {
|
|
|
|
row := mq.db.QueryRow(query, args...)
|
|
|
|
if row == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return mq.New().Scan(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
db *Database
|
|
|
|
log log.Logger
|
|
|
|
|
2018-09-01 22:38:03 +02:00
|
|
|
Chat PortalKey
|
2018-08-28 23:40:54 +02:00
|
|
|
JID types.WhatsAppMessageID
|
|
|
|
MXID types.MatrixEventID
|
2018-09-01 22:38:03 +02:00
|
|
|
Sender types.WhatsAppID
|
|
|
|
Content *waProto.Message
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Message) Scan(row Scannable) *Message {
|
2018-09-01 22:38:03 +02:00
|
|
|
var content []byte
|
|
|
|
err := row.Scan(&msg.Chat.JID, &msg.Chat.Receiver, &msg.JID, &msg.MXID, &msg.Sender, &content)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
2018-08-26 00:55:21 +02:00
|
|
|
msg.log.Errorln("Database scan failed:", err)
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-01 22:38:03 +02:00
|
|
|
|
2018-09-01 22:53:47 +02:00
|
|
|
msg.decodeBinaryContent(content)
|
2018-09-01 22:38:03 +02:00
|
|
|
|
2018-08-23 00:12:26 +02:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2018-09-01 22:53:47 +02:00
|
|
|
func (msg *Message) decodeBinaryContent(content []byte) {
|
2018-09-01 22:38:03 +02:00
|
|
|
msg.Content = &waProto.Message{}
|
|
|
|
reader := bytes.NewReader(content)
|
|
|
|
dec := json.NewDecoder(reader)
|
|
|
|
err := dec.Decode(msg.Content)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil {
|
2018-09-01 22:38:03 +02:00
|
|
|
msg.log.Warnln("Failed to decode message content:", err)
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-01 22:53:47 +02:00
|
|
|
func (msg *Message) encodeBinaryContent() []byte {
|
2018-09-01 22:38:03 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
|
|
|
err := enc.Encode(msg.Content)
|
|
|
|
if err != nil {
|
|
|
|
msg.log.Warnln("Failed to encode message content:", err)
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2019-01-21 22:55:16 +01:00
|
|
|
func (msg *Message) Insert() {
|
2019-03-06 16:33:42 +01:00
|
|
|
_, err := msg.db.Exec("INSERT INTO message VALUES ($1, $2, $3, $4, $5, $6)", msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, msg.Sender, msg.encodeBinaryContent())
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil {
|
2018-09-01 22:38:03 +02:00
|
|
|
msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err)
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 00:59:36 +02:00
|
|
|
|
|
|
|
func (msg *Message) Delete() {
|
|
|
|
_, err := msg.db.Exec("DELETE FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3", msg.Chat.JID, msg.Chat.Receiver, msg.JID)
|
|
|
|
if err != nil {
|
|
|
|
msg.log.Warnfln("Failed to delete %s@%s: %v", msg.Chat, msg.JID, err)
|
|
|
|
}
|
|
|
|
}
|