2018-08-23 00:12:26 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2024-03-11 21:27:10 +01:00
|
|
|
// Copyright (C) 2024 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 (
|
2024-03-11 21:27:10 +01:00
|
|
|
"context"
|
2023-09-04 18:28:15 +02:00
|
|
|
"fmt"
|
2021-02-09 22:41:14 +01:00
|
|
|
"strings"
|
2021-05-18 19:09:12 +02:00
|
|
|
"time"
|
2018-08-26 21:53:13 +02:00
|
|
|
|
2023-08-05 17:59:17 +02:00
|
|
|
"go.mau.fi/util/dbutil"
|
|
|
|
"go.mau.fi/whatsmeow/types"
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
2018-08-23 00:12:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type MessageQuery struct {
|
2024-03-11 21:27:10 +01:00
|
|
|
*dbutil.QueryHelper[*Message]
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func newMessage(qh *dbutil.QueryHelper[*Message]) *Message {
|
|
|
|
return &Message{qh: qh}
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 17:31:33 +02:00
|
|
|
const (
|
|
|
|
getAllMessagesQuery = `
|
2023-05-26 14:55:53 +02:00
|
|
|
SELECT chat_jid, chat_receiver, jid, mxid, sender, sender_mxid, timestamp, sent, type, error, broadcast_list_jid FROM message
|
2021-10-27 17:31:33 +02:00
|
|
|
WHERE chat_jid=$1 AND chat_receiver=$2
|
|
|
|
`
|
|
|
|
getMessageByJIDQuery = `
|
2023-05-26 14:55:53 +02:00
|
|
|
SELECT chat_jid, chat_receiver, jid, mxid, sender, sender_mxid, timestamp, sent, type, error, broadcast_list_jid FROM message
|
2021-10-27 17:31:33 +02:00
|
|
|
WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3
|
|
|
|
`
|
|
|
|
getMessageByMXIDQuery = `
|
2023-05-26 14:55:53 +02:00
|
|
|
SELECT chat_jid, chat_receiver, jid, mxid, sender, sender_mxid, timestamp, sent, type, error, broadcast_list_jid FROM message
|
2021-10-27 17:31:33 +02:00
|
|
|
WHERE mxid=$1
|
|
|
|
`
|
|
|
|
getLastMessageInChatQuery = `
|
2023-05-26 14:55:53 +02:00
|
|
|
SELECT chat_jid, chat_receiver, jid, mxid, sender, sender_mxid, timestamp, sent, type, error, broadcast_list_jid FROM message
|
2021-10-27 17:31:33 +02:00
|
|
|
WHERE chat_jid=$1 AND chat_receiver=$2 AND timestamp<=$3 AND sent=true ORDER BY timestamp DESC LIMIT 1
|
|
|
|
`
|
|
|
|
getFirstMessageInChatQuery = `
|
2023-05-26 14:55:53 +02:00
|
|
|
SELECT chat_jid, chat_receiver, jid, mxid, sender, sender_mxid, timestamp, sent, type, error, broadcast_list_jid FROM message
|
2021-10-27 17:31:33 +02:00
|
|
|
WHERE chat_jid=$1 AND chat_receiver=$2 AND sent=true ORDER BY timestamp ASC LIMIT 1
|
|
|
|
`
|
2021-11-30 15:38:37 +01:00
|
|
|
getMessagesBetweenQuery = `
|
2023-05-26 14:55:53 +02:00
|
|
|
SELECT chat_jid, chat_receiver, jid, mxid, sender, sender_mxid, timestamp, sent, type, error, broadcast_list_jid FROM message
|
2022-05-18 00:00:23 +02:00
|
|
|
WHERE chat_jid=$1 AND chat_receiver=$2 AND timestamp>$3 AND timestamp<=$4 AND sent=true AND error='' ORDER BY timestamp ASC
|
2021-11-30 15:38:37 +01:00
|
|
|
`
|
2024-03-11 21:27:10 +01:00
|
|
|
insertMessageQuery = `
|
|
|
|
INSERT INTO message
|
|
|
|
(chat_jid, chat_receiver, jid, mxid, sender, sender_mxid, timestamp, sent, type, error, broadcast_list_jid)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
|
|
|
`
|
|
|
|
markMessageSentQuery = "UPDATE message SET sent=true, timestamp=$1 WHERE chat_jid=$2 AND chat_receiver=$3 AND jid=$4"
|
|
|
|
updateMessageMXIDQuery = "UPDATE message SET mxid=$1, type=$2, error=$3 WHERE chat_jid=$4 AND chat_receiver=$5 AND jid=$6"
|
|
|
|
deleteMessageQuery = "DELETE FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3"
|
2021-10-27 17:31:33 +02:00
|
|
|
)
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (mq *MessageQuery) GetAll(ctx context.Context, chat PortalKey) ([]*Message, error) {
|
|
|
|
return mq.QueryMany(ctx, getAllMessagesQuery, chat.JID, chat.Receiver)
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (mq *MessageQuery) GetByJID(ctx context.Context, chat PortalKey, jid types.MessageID) (*Message, error) {
|
|
|
|
return mq.QueryOne(ctx, getMessageByJIDQuery, chat.JID, chat.Receiver, jid)
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (mq *MessageQuery) GetByMXID(ctx context.Context, mxid id.EventID) (*Message, error) {
|
|
|
|
return mq.QueryOne(ctx, getMessageByMXIDQuery, mxid)
|
2019-05-22 15:46:18 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (mq *MessageQuery) GetLastInChat(ctx context.Context, chat PortalKey) (*Message, error) {
|
|
|
|
return mq.GetLastInChatBefore(ctx, chat, time.Now().Add(60*time.Second))
|
2021-05-18 19:09:12 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (mq *MessageQuery) GetLastInChatBefore(ctx context.Context, chat PortalKey, maxTimestamp time.Time) (*Message, error) {
|
|
|
|
msg, err := mq.QueryOne(ctx, getLastMessageInChatQuery, chat.JID, chat.Receiver, maxTimestamp.Unix())
|
|
|
|
if msg != nil && msg.Timestamp.IsZero() {
|
2019-05-22 15:46:18 +02:00
|
|
|
// Old db, we don't know what the last message is.
|
2024-03-11 21:27:10 +01:00
|
|
|
msg = nil
|
2019-05-22 15:46:18 +02:00
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
return msg, err
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (mq *MessageQuery) GetFirstInChat(ctx context.Context, chat PortalKey) (*Message, error) {
|
|
|
|
return mq.QueryOne(ctx, getFirstMessageInChatQuery, chat.JID, chat.Receiver)
|
2021-10-26 16:01:10 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (mq *MessageQuery) GetMessagesBetween(ctx context.Context, chat PortalKey, minTimestamp, maxTimestamp time.Time) ([]*Message, error) {
|
|
|
|
return mq.QueryMany(ctx, getMessagesBetweenQuery, chat.JID, chat.Receiver, minTimestamp.Unix(), maxTimestamp.Unix())
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 18:18:49 +01:00
|
|
|
type MessageErrorType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
MsgNoError MessageErrorType = ""
|
|
|
|
MsgErrDecryptionFailed MessageErrorType = "decryption_failed"
|
|
|
|
MsgErrMediaNotFound MessageErrorType = "media_not_found"
|
|
|
|
)
|
|
|
|
|
2022-03-05 20:22:31 +01:00
|
|
|
type MessageType string
|
|
|
|
|
|
|
|
const (
|
2023-09-04 18:28:15 +02:00
|
|
|
MsgUnknown MessageType = ""
|
|
|
|
MsgFake MessageType = "fake"
|
|
|
|
MsgNormal MessageType = "message"
|
|
|
|
MsgReaction MessageType = "reaction"
|
|
|
|
MsgEdit MessageType = "edit"
|
|
|
|
MsgMatrixPoll MessageType = "matrix-poll"
|
|
|
|
MsgBeeperGallery MessageType = "beeper-gallery"
|
2022-03-05 20:22:31 +01:00
|
|
|
)
|
|
|
|
|
2018-08-23 00:12:26 +02:00
|
|
|
type Message struct {
|
2024-03-11 21:27:10 +01:00
|
|
|
qh *dbutil.QueryHelper[*Message]
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2023-05-26 14:55:53 +02:00
|
|
|
Chat PortalKey
|
|
|
|
JID types.MessageID
|
|
|
|
MXID id.EventID
|
|
|
|
Sender types.JID
|
|
|
|
SenderMXID id.UserID
|
|
|
|
Timestamp time.Time
|
|
|
|
Sent bool
|
|
|
|
Type MessageType
|
|
|
|
Error MessageErrorType
|
2021-10-27 17:31:33 +02:00
|
|
|
|
2023-09-04 18:28:15 +02:00
|
|
|
GalleryPart int
|
|
|
|
|
2021-12-25 19:50:36 +01:00
|
|
|
BroadcastListJID types.JID
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2021-02-09 22:41:14 +01:00
|
|
|
func (msg *Message) IsFakeMXID() bool {
|
|
|
|
return strings.HasPrefix(msg.MXID.String(), "net.maunium.whatsapp.fake::")
|
|
|
|
}
|
|
|
|
|
2021-11-02 14:46:31 +01:00
|
|
|
func (msg *Message) IsFakeJID() bool {
|
2022-02-15 23:11:38 +01:00
|
|
|
return strings.HasPrefix(msg.JID, "FAKE::") || msg.JID == string(msg.MXID)
|
2021-11-02 14:46:31 +01:00
|
|
|
}
|
|
|
|
|
2023-09-04 18:28:15 +02:00
|
|
|
const fakeGalleryMXIDFormat = "com.beeper.gallery::%d:%s"
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (msg *Message) Scan(row dbutil.Scannable) (*Message, error) {
|
2021-10-26 16:01:10 +02:00
|
|
|
var ts int64
|
2023-05-26 14:55:53 +02:00
|
|
|
err := row.Scan(&msg.Chat.JID, &msg.Chat.Receiver, &msg.JID, &msg.MXID, &msg.Sender, &msg.SenderMXID, &ts, &msg.Sent, &msg.Type, &msg.Error, &msg.BroadcastListJID)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err != nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
return nil, err
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
2023-09-04 18:28:15 +02:00
|
|
|
if strings.HasPrefix(msg.MXID.String(), "com.beeper.gallery::") {
|
|
|
|
_, err = fmt.Sscanf(msg.MXID.String(), fakeGalleryMXIDFormat, &msg.GalleryPart, &msg.MXID)
|
|
|
|
if err != nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
return nil, fmt.Errorf("failed to parse gallery MXID: %w", err)
|
2023-09-04 18:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 16:01:10 +02:00
|
|
|
if ts != 0 {
|
|
|
|
msg.Timestamp = time.Unix(ts, 0)
|
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
return msg, nil
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (msg *Message) sqlVariables() []any {
|
2023-09-04 18:28:15 +02:00
|
|
|
mxid := msg.MXID.String()
|
|
|
|
if msg.GalleryPart != 0 {
|
|
|
|
mxid = fmt.Sprintf(fakeGalleryMXIDFormat, msg.GalleryPart, mxid)
|
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
return []any{msg.Chat.JID, msg.Chat.Receiver, msg.JID, mxid, msg.Sender, msg.SenderMXID, msg.Timestamp.Unix(), msg.Sent, msg.Type, msg.Error, msg.BroadcastListJID}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Message) Insert(ctx context.Context) error {
|
|
|
|
return msg.qh.Exec(ctx, insertMessageQuery, msg.sqlVariables()...)
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
2019-05-16 00:59:36 +02:00
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (msg *Message) MarkSent(ctx context.Context, ts time.Time) error {
|
2021-02-17 00:22:06 +01:00
|
|
|
msg.Sent = true
|
2021-10-31 22:36:41 +01:00
|
|
|
msg.Timestamp = ts
|
2024-03-11 21:27:10 +01:00
|
|
|
return msg.qh.Exec(ctx, markMessageSentQuery, ts.Unix(), msg.Chat.JID, msg.Chat.Receiver, msg.JID)
|
2021-02-17 00:22:06 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (msg *Message) UpdateMXID(ctx context.Context, mxid id.EventID, newType MessageType, newError MessageErrorType) error {
|
2021-10-27 17:31:33 +02:00
|
|
|
msg.MXID = mxid
|
2022-03-05 20:22:31 +01:00
|
|
|
msg.Type = newType
|
2022-02-10 18:18:49 +01:00
|
|
|
msg.Error = newError
|
2024-03-11 21:27:10 +01:00
|
|
|
return msg.qh.Exec(ctx, updateMessageMXIDQuery, mxid, newType, newError, msg.Chat.JID, msg.Chat.Receiver, msg.JID)
|
2021-10-27 17:31:33 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (msg *Message) Delete(ctx context.Context) error {
|
|
|
|
return msg.qh.Exec(ctx, deleteMessageQuery, msg.Chat.JID, msg.Chat.Receiver, msg.JID)
|
2019-05-16 00:59:36 +02:00
|
|
|
}
|