-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
tapback.go
118 lines (101 loc) · 4.14 KB
/
tapback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// mautrix-imessage - A Matrix-iMessage puppeting bridge.
// Copyright (C) 2021 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 (
"database/sql"
log "maunium.net/go/maulogger/v2"
"go.mau.fi/util/dbutil"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-imessage/imessage"
)
type TapbackQuery struct {
db *Database
log log.Logger
}
func (mq *TapbackQuery) New() *Tapback {
return &Tapback{
db: mq.db,
log: mq.log,
}
}
func (mq *TapbackQuery) GetByGUID(chat, message string, part int, sender string) *Tapback {
return mq.get("SELECT portal_guid, guid, message_guid, message_part, sender_guid, handle_guid, type, mxid "+
"FROM tapback WHERE portal_guid=$1 AND message_guid=$2 AND message_part=$3 AND sender_guid=$4",
chat, message, part, sender)
}
func (mq *TapbackQuery) GetByTapbackGUID(chat, tapback string) *Tapback {
return mq.get("SELECT portal_guid, guid, message_guid, message_part, sender_guid, handle_guid, type, mxid "+
"FROM tapback WHERE portal_guid=$1 AND guid=$2",
chat, tapback)
}
func (mq *TapbackQuery) GetByMXID(mxid id.EventID) *Tapback {
return mq.get("SELECT portal_guid, guid, message_guid, message_part, sender_guid, handle_guid, type, mxid "+
"FROM tapback WHERE mxid=$1", mxid)
}
func (mq *TapbackQuery) get(query string, args ...interface{}) *Tapback {
row := mq.db.QueryRow(query, args...)
if row == nil {
return nil
}
return mq.New().Scan(row)
}
type Tapback struct {
db *Database
log log.Logger
PortalGUID string
GUID string
MessageGUID string
MessagePart int
SenderGUID string
HandleGUID string
Type imessage.TapbackType
MXID id.EventID
}
func (tapback *Tapback) Scan(row dbutil.Scannable) *Tapback {
var nullishGUID sql.NullString
err := row.Scan(&tapback.PortalGUID, &nullishGUID, &tapback.MessageGUID, &tapback.MessagePart, &tapback.SenderGUID, &tapback.HandleGUID, &tapback.Type, &tapback.MXID)
if err != nil {
if err != sql.ErrNoRows {
tapback.log.Errorln("Database scan failed:", err)
}
return nil
}
tapback.GUID = nullishGUID.String
return tapback
}
func (tapback *Tapback) Insert(txn dbutil.Execable) {
if txn == nil {
txn = tapback.db
}
_, err := txn.Exec("INSERT INTO tapback (portal_guid, guid, message_guid, message_part, sender_guid, handle_guid, type, mxid) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
tapback.PortalGUID, tapback.GUID, tapback.MessageGUID, tapback.MessagePart, tapback.SenderGUID, tapback.HandleGUID, tapback.Type, tapback.MXID)
if err != nil {
tapback.log.Warnfln("Failed to insert tapback %s/%s.%d/%s: %v", tapback.PortalGUID, tapback.MessageGUID, tapback.MessagePart, tapback.SenderGUID, err)
}
}
func (tapback *Tapback) Update() {
_, err := tapback.db.Exec("UPDATE tapback SET guid=?5, type=?6, mxid=?7 WHERE portal_guid=?1 AND message_guid=?2 AND message_part=?3 AND sender_guid=?4",
tapback.PortalGUID, tapback.MessageGUID, tapback.MessagePart, tapback.SenderGUID, tapback.GUID, tapback.Type, tapback.MXID)
if err != nil {
tapback.log.Warnfln("Failed to update tapback %s/%s.%d/%s: %v", tapback.PortalGUID, tapback.MessageGUID, tapback.MessagePart, tapback.SenderGUID, err)
}
}
func (tapback *Tapback) Delete() {
_, err := tapback.db.Exec("DELETE FROM tapback WHERE portal_guid=$1 AND message_guid=$2 AND message_part=$3 AND sender_guid=$4", tapback.PortalGUID, tapback.MessageGUID, tapback.MessagePart, tapback.SenderGUID)
if err != nil {
tapback.log.Warnfln("Failed to delete tapback %s/%s.%d/%s: %v", tapback.PortalGUID, tapback.MessageGUID, tapback.MessagePart, tapback.SenderGUID, err)
}
}