-
Notifications
You must be signed in to change notification settings - Fork 69
/
filetransfer.go
100 lines (84 loc) · 2.96 KB
/
filetransfer.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
// beeper-imessage - A Matrix-iMessage puppeting bridge.
// Copyright (C) 2023 Beeper, Inc.
//
// 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"
"database/sql"
"errors"
"go.mau.fi/util/dbutil"
"maunium.net/go/mautrix/id"
"github.com/beeper/imessage/imessage/direct/ids"
)
type FileTransferQuery struct {
db *Database
}
func (ftq *FileTransferQuery) New() *FileTransfer {
return &FileTransfer{db: ftq.db}
}
func (ftq *FileTransferQuery) getDB() *Database {
return ftq.db
}
const (
getFileTransfer = `
SELECT user_id, identifier, mxc_uri, temp_path, error, state
FROM file_transfer WHERE user_id=$1 AND identifier=$2
`
getAllPendingFileTransfers = `
SELECT user_id, identifier, mxc_uri, temp_path, error, state
FROM file_transfer WHERE user_id=$1 AND state<>'null' AND error=''
`
insertFileTransfer = `
INSERT INTO file_transfer (user_id, identifier, mxc_uri, temp_path, error, state) VALUES ($1, $2, $3, $4, $5, $6)
`
updateFileTransfer = `
UPDATE file_transfer SET error=$5, state=$6 WHERE user_id=$1 AND identifier=$2
`
)
func (ftq *FileTransferQuery) Get(ctx context.Context, userID int, identifier string) (*FileTransfer, error) {
return get[*FileTransfer](ftq, ctx, getFileTransfer, userID, identifier)
}
func (ftq *FileTransferQuery) GetAllPending(ctx context.Context, userID int) ([]*FileTransfer, error) {
return getAll[*FileTransfer](ftq, ctx, getAllPendingFileTransfers, userID)
}
type FileTransfer struct {
db *Database
UserID int
Identifier string
State *ids.AttachmentDownloader
MXC id.ContentURIString
TempPath string
Error string
}
func (ft *FileTransfer) sqlVariables() []any {
return []any{ft.UserID, ft.Identifier, ft.MXC, ft.TempPath, ft.Error, dbutil.JSON{ft.State}}
}
func (ft *FileTransfer) Scan(row dbutil.Scannable) (*FileTransfer, error) {
err := row.Scan(&ft.UserID, &ft.Identifier, &ft.MXC, &ft.TempPath, &ft.Error, dbutil.JSON{&ft.State})
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
} else if err != nil {
return nil, err
}
return ft, err
}
func (ft *FileTransfer) Insert(ctx context.Context) error {
_, err := ft.db.Conn(ctx).ExecContext(ctx, insertFileTransfer, ft.sqlVariables()...)
return err
}
func (ft *FileTransfer) Update(ctx context.Context) error {
_, err := ft.db.Conn(ctx).ExecContext(ctx, updateFileTransfer, ft.sqlVariables()...)
return err
}