2023-07-12 21:44:57 +00:00
|
|
|
// mautrix-gmessages - A Matrix-Google Messages puppeting bridge.
|
2024-02-24 22:18:06 +00:00
|
|
|
// Copyright (C) 2024 Tulir Asokan
|
2023-07-12 21:44:57 +00: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 (
|
|
|
|
"context"
|
|
|
|
|
2023-08-08 13:13:44 +00:00
|
|
|
"go.mau.fi/util/dbutil"
|
2023-07-12 21:44:57 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ReactionQuery struct {
|
2024-02-24 22:18:06 +00:00
|
|
|
*dbutil.QueryHelper[*Reaction]
|
2023-07-12 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 22:18:06 +00:00
|
|
|
func newReaction(qh *dbutil.QueryHelper[*Reaction]) *Reaction {
|
|
|
|
return &Reaction{qh: qh}
|
2023-07-12 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
getReactionByIDQuery = `
|
|
|
|
SELECT conv_id, conv_receiver, msg_id, sender, reaction, mxid FROM reaction
|
2023-08-14 11:32:12 +00:00
|
|
|
WHERE conv_receiver=$1 AND msg_id=$2 AND sender=$3
|
2023-07-12 21:44:57 +00:00
|
|
|
`
|
|
|
|
getReactionByMXIDQuery = `
|
|
|
|
SELECT conv_id, conv_receiver, msg_id, sender, reaction, mxid FROM reaction
|
|
|
|
WHERE mxid=$1
|
|
|
|
`
|
|
|
|
getReactionsByMessageIDQuery = `
|
2023-08-14 11:32:12 +00:00
|
|
|
SELECT conv_id, conv_receiver, msg_id, sender, reaction, mxid FROM reaction
|
|
|
|
WHERE conv_receiver=$1 AND msg_id=$2
|
|
|
|
`
|
|
|
|
deleteReactionsByMessageIDQuery = `
|
2023-07-12 21:44:57 +00:00
|
|
|
SELECT conv_id, conv_receiver, msg_id, sender, reaction, mxid FROM reaction
|
|
|
|
WHERE conv_id=$1 AND conv_receiver=$2 AND msg_id=$3
|
|
|
|
`
|
2023-09-04 22:46:56 +00:00
|
|
|
insertReactionQuery = `
|
2023-07-12 21:44:57 +00:00
|
|
|
INSERT INTO reaction (conv_id, conv_receiver, msg_id, sender, reaction, mxid)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
2023-08-21 06:54:36 +00:00
|
|
|
ON CONFLICT (conv_receiver, msg_id, sender)
|
2023-07-12 21:44:57 +00:00
|
|
|
DO UPDATE SET reaction=excluded.reaction, mxid=excluded.mxid
|
|
|
|
`
|
2023-09-04 22:46:56 +00:00
|
|
|
deleteReactionQuery = "DELETE FROM reaction WHERE conv_id=$1 AND conv_receiver=$2 AND msg_id=$3 AND sender=$4"
|
2023-07-12 21:44:57 +00:00
|
|
|
)
|
|
|
|
|
2024-02-24 23:31:53 +00:00
|
|
|
var massInsertReactionBuilder = dbutil.NewMassInsertBuilder[*Reaction, [2]any](insertReactionQuery, "($1, $2, $%d, $%d, $%d, $%d)")
|
|
|
|
|
2023-08-14 11:32:12 +00:00
|
|
|
func (rq *ReactionQuery) GetByID(ctx context.Context, receiver int, messageID, sender string) (*Reaction, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return rq.QueryOne(ctx, getReactionByIDQuery, receiver, messageID, sender)
|
2023-07-12 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rq *ReactionQuery) GetByMXID(ctx context.Context, mxid id.EventID) (*Reaction, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return rq.QueryOne(ctx, getReactionByMXIDQuery, mxid)
|
2023-07-12 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 11:32:12 +00:00
|
|
|
func (rq *ReactionQuery) GetAllByMessage(ctx context.Context, receiver int, messageID string) ([]*Reaction, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return rq.QueryMany(ctx, getReactionsByMessageIDQuery, receiver, messageID)
|
2023-08-14 11:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rq *ReactionQuery) DeleteAllByMessage(ctx context.Context, chat Key, messageID string) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return rq.Exec(ctx, deleteReactionsByMessageIDQuery, chat.ID, chat.Receiver, messageID)
|
2023-07-12 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Reaction struct {
|
2024-02-24 22:18:06 +00:00
|
|
|
qh *dbutil.QueryHelper[*Reaction]
|
2023-07-12 21:44:57 +00:00
|
|
|
|
|
|
|
Chat Key
|
|
|
|
MessageID string
|
|
|
|
Sender string
|
|
|
|
Reaction string
|
|
|
|
MXID id.EventID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reaction) Scan(row dbutil.Scannable) (*Reaction, error) {
|
2024-02-24 22:18:06 +00:00
|
|
|
return dbutil.ValueOrErr(r, row.Scan(&r.Chat.ID, &r.Chat.Receiver, &r.MessageID, &r.Sender, &r.Reaction, &r.MXID))
|
2023-07-12 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reaction) Insert(ctx context.Context) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return r.qh.Exec(ctx, insertReactionQuery, r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender, r.Reaction, r.MXID)
|
2023-07-12 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 23:31:53 +00:00
|
|
|
func (r *Reaction) GetMassInsertValues() [4]any {
|
|
|
|
return [...]any{r.MessageID, r.Sender, r.Reaction, r.MXID}
|
|
|
|
}
|
|
|
|
|
2023-07-12 21:44:57 +00:00
|
|
|
func (rq *ReactionQuery) MassInsert(ctx context.Context, reactions []*Reaction) error {
|
2024-02-24 23:31:53 +00:00
|
|
|
query, params := massInsertReactionBuilder.Build([2]any{reactions[0].Chat.ID, reactions[0].Chat.Receiver}, reactions)
|
2024-02-24 22:18:06 +00:00
|
|
|
return rq.Exec(ctx, query, params...)
|
2023-07-12 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reaction) Delete(ctx context.Context) error {
|
2024-02-24 22:18:06 +00:00
|
|
|
return r.qh.Exec(ctx, deleteReactionQuery, r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender)
|
2023-07-12 21:44:57 +00:00
|
|
|
}
|