2022-01-07 13:32:00 +01:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2024-03-11 21:27:10 +01:00
|
|
|
// Copyright (C) 2024 Tulir Asokan
|
2022-01-07 13:32:00 +01: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"
|
2022-01-07 13:32:00 +01:00
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"maunium.net/go/mautrix/id"
|
2024-03-11 21:27:10 +01:00
|
|
|
|
|
|
|
"go.mau.fi/util/dbutil"
|
2022-01-07 13:32:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type DisappearingMessageQuery struct {
|
2024-03-11 21:27:10 +01:00
|
|
|
*dbutil.QueryHelper[*DisappearingMessage]
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func newDisappearingMessage(qh *dbutil.QueryHelper[*DisappearingMessage]) *DisappearingMessage {
|
2022-01-07 13:32:00 +01:00
|
|
|
return &DisappearingMessage{
|
2024-03-11 21:27:10 +01:00
|
|
|
qh: qh,
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 16:15:04 +01:00
|
|
|
func (dmq *DisappearingMessageQuery) NewWithValues(roomID id.RoomID, eventID id.EventID, expireIn time.Duration, expireAt time.Time) *DisappearingMessage {
|
2022-01-07 13:32:00 +01:00
|
|
|
dm := &DisappearingMessage{
|
2024-03-11 21:27:10 +01:00
|
|
|
qh: dmq.QueryHelper,
|
|
|
|
|
2022-01-07 13:32:00 +01:00
|
|
|
RoomID: roomID,
|
|
|
|
EventID: eventID,
|
|
|
|
ExpireIn: expireIn,
|
2023-01-16 16:15:04 +01:00
|
|
|
ExpireAt: expireAt,
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|
|
|
|
return dm
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
getAllScheduledDisappearingMessagesQuery = `
|
2022-01-07 13:46:53 +01:00
|
|
|
SELECT room_id, event_id, expire_in, expire_at FROM disappearing_message WHERE expire_at IS NOT NULL AND expire_at <= $1
|
2022-01-07 13:32:00 +01:00
|
|
|
`
|
2024-03-11 21:27:10 +01:00
|
|
|
insertDisappearingMessageQuery = `INSERT INTO disappearing_message (room_id, event_id, expire_in, expire_at) VALUES ($1, $2, $3, $4)`
|
|
|
|
updateDisappearingMessageExpiryQuery = "UPDATE disappearing_message SET expire_at=$1 WHERE room_id=$2 AND event_id=$3"
|
|
|
|
deleteDisappearingMessageQuery = "DELETE FROM disappearing_message WHERE room_id=$1 AND event_id=$2"
|
2022-01-07 13:32:00 +01:00
|
|
|
)
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (dmq *DisappearingMessageQuery) GetUpcomingScheduled(ctx context.Context, duration time.Duration) ([]*DisappearingMessage, error) {
|
|
|
|
return dmq.QueryMany(ctx, getAllScheduledDisappearingMessagesQuery, time.Now().Add(duration).UnixMilli())
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type DisappearingMessage struct {
|
2024-03-11 21:27:10 +01:00
|
|
|
qh *dbutil.QueryHelper[*DisappearingMessage]
|
2022-01-07 13:32:00 +01:00
|
|
|
|
|
|
|
RoomID id.RoomID
|
|
|
|
EventID id.EventID
|
|
|
|
ExpireIn time.Duration
|
|
|
|
ExpireAt time.Time
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (msg *DisappearingMessage) Scan(row dbutil.Scannable) (*DisappearingMessage, error) {
|
2022-01-07 13:32:00 +01:00
|
|
|
var expireIn int64
|
|
|
|
var expireAt sql.NullInt64
|
|
|
|
err := row.Scan(&msg.RoomID, &msg.EventID, &expireIn, &expireAt)
|
|
|
|
if err != nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
return nil, err
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|
|
|
|
msg.ExpireIn = time.Duration(expireIn) * time.Millisecond
|
|
|
|
if expireAt.Valid {
|
|
|
|
msg.ExpireAt = time.UnixMilli(expireAt.Int64)
|
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
return msg, nil
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (msg *DisappearingMessage) sqlVariables() []any {
|
|
|
|
return []any{msg.RoomID, msg.EventID, msg.ExpireIn.Milliseconds(), dbutil.UnixMilliPtr(msg.ExpireAt)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *DisappearingMessage) Insert(ctx context.Context) error {
|
|
|
|
return msg.qh.Exec(ctx, insertDisappearingMessageQuery, msg.sqlVariables()...)
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (msg *DisappearingMessage) StartTimer(ctx context.Context) error {
|
2022-01-07 13:32:00 +01:00
|
|
|
msg.ExpireAt = time.Now().Add(msg.ExpireIn * time.Second)
|
2024-03-11 21:27:10 +01:00
|
|
|
return msg.qh.Exec(ctx, updateDisappearingMessageExpiryQuery, msg.ExpireAt.Unix(), msg.RoomID, msg.EventID)
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (msg *DisappearingMessage) Delete(ctx context.Context) error {
|
|
|
|
return msg.qh.Exec(ctx, deleteDisappearingMessageQuery, msg.RoomID, msg.EventID)
|
2022-01-07 13:32:00 +01:00
|
|
|
}
|