-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
222 lines (181 loc) · 5.49 KB
/
model.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package postgres
import (
"context"
"database/sql"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-protobuf-api/generated/go/user/v1"
"github.com/code-payments/code-server/pkg/code/data/twitter"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
)
const (
userTableName = "codewallet__core_twitteruser"
processedTweetsTableName = "codewallet__core_processedtweets"
usedNoncesTableName = "codewallet__core_usedtwitternonces"
)
type model struct {
Id sql.NullInt64 `db:"id"`
Username string `db:"username"`
Name string `db:"name"`
ProfilePicUrl string `db:"profile_pic_url"`
VerifiedType uint8 `db:"verified_type"`
FollowerCount uint32 `db:"follower_count"`
TipAddress string `db:"tip_address"`
CreatedAt time.Time `db:"created_at"`
LastUpdatedAt time.Time `db:"last_updated_at"`
}
func toModel(r *twitter.Record) (*model, error) {
if err := r.Validate(); err != nil {
return nil, err
}
return &model{
Username: r.Username,
Name: r.Name,
ProfilePicUrl: r.ProfilePicUrl,
VerifiedType: uint8(r.VerifiedType),
FollowerCount: r.FollowerCount,
TipAddress: r.TipAddress,
CreatedAt: r.CreatedAt,
LastUpdatedAt: r.LastUpdatedAt,
}, nil
}
func fromModel(m *model) *twitter.Record {
return &twitter.Record{
Id: uint64(m.Id.Int64),
Username: m.Username,
Name: m.Name,
ProfilePicUrl: m.ProfilePicUrl,
VerifiedType: user.TwitterUser_VerifiedType(m.VerifiedType),
FollowerCount: m.FollowerCount,
TipAddress: m.TipAddress,
CreatedAt: m.CreatedAt,
LastUpdatedAt: m.LastUpdatedAt,
}
}
func (m *model) dbSave(ctx context.Context, db *sqlx.DB) error {
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
query := `INSERT INTO ` + userTableName + `
(username, name, profile_pic_url, verified_type, follower_count, tip_address, created_at, last_updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (username)
DO UPDATE
SET name = $2, profile_pic_url = $3, verified_type = $4, follower_count = $5, tip_address = $6, created_at = $7, last_updated_at = $8
WHERE ` + userTableName + `.username = $1
RETURNING
id, username, name, profile_pic_url, verified_type, follower_count, tip_address, created_at, last_updated_at`
if m.CreatedAt.IsZero() {
m.CreatedAt = time.Now()
}
m.LastUpdatedAt = time.Now()
err := tx.QueryRowxContext(
ctx,
query,
m.Username,
m.Name,
m.ProfilePicUrl,
m.VerifiedType,
m.FollowerCount,
m.TipAddress,
m.CreatedAt,
m.LastUpdatedAt,
).StructScan(m)
return pgutil.CheckUniqueViolation(err, twitter.ErrDuplicateTipAddress)
})
}
func dbGetUserByUsername(ctx context.Context, db *sqlx.DB, username string) (*model, error) {
res := &model{}
query := `SELECT
id, username, name, profile_pic_url, verified_type, follower_count, tip_address, created_at, last_updated_at
FROM ` + userTableName + `
WHERE username = $1
LIMIT 1`
err := db.GetContext(ctx, res, query, username)
if err != nil {
return nil, pgutil.CheckNoRows(err, twitter.ErrUserNotFound)
}
return res, nil
}
func dbGetUserByTipAddress(ctx context.Context, db *sqlx.DB, tipAddress string) (*model, error) {
res := &model{}
query := `SELECT
id, username, name, profile_pic_url, verified_type, follower_count, tip_address, created_at, last_updated_at
FROM ` + userTableName + `
WHERE tip_address = $1
LIMIT 1`
err := db.GetContext(ctx, res, query, tipAddress)
if err != nil {
return nil, pgutil.CheckNoRows(err, twitter.ErrUserNotFound)
}
return res, nil
}
func dbGetStaleUsers(ctx context.Context, db *sqlx.DB, minAge time.Duration, limit int) ([]*model, error) {
res := []*model{}
query := `SELECT
id, username, name, profile_pic_url, verified_type, follower_count, tip_address, created_at, last_updated_at
FROM ` + userTableName + `
WHERE last_updated_at < $1
ORDER BY last_updated_at ASC
LIMIT $2`
err := db.SelectContext(ctx, &res, query, time.Now().Add(-1*minAge), limit)
if err != nil {
return nil, pgutil.CheckNoRows(err, twitter.ErrUserNotFound)
}
if len(res) == 0 {
return nil, twitter.ErrUserNotFound
}
return res, nil
}
func dbMarkTweetAsProcessed(ctx context.Context, db *sqlx.DB, tweetId string) error {
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
query := `INSERT INTO ` + processedTweetsTableName + `
(tweet_id, created_at)
VALUES ($1, $2)
ON CONFLICT (tweet_id) DO NOTHING
RETURNING
id, tweet_id, created_at`
_, err := tx.ExecContext(
ctx,
query,
tweetId,
time.Now(),
)
return pgutil.CheckNoRows(err, nil)
})
}
func dbIsTweetProcessed(ctx context.Context, db *sqlx.DB, tweetId string) (bool, error) {
res := struct {
Count int `db:"count"`
}{}
query := `SELECT COUNT(*) AS count
FROM ` + processedTweetsTableName + `
WHERE tweet_id = $1
LIMIT 1`
err := db.GetContext(
ctx,
&res,
query,
tweetId,
)
if err != nil {
return false, err
}
return res.Count > 0, nil
}
func dbMarkNonceAsUsed(ctx context.Context, db *sqlx.DB, tweetId string, nonce uuid.UUID) error {
return pgutil.ExecuteInTx(ctx, db, sql.LevelDefault, func(tx *sqlx.Tx) error {
query := `INSERT INTO ` + usedNoncesTableName + `
(tweet_id, nonce, created_at)
VALUES ($1, $2, $3)
RETURNING
id, tweet_id, nonce, created_at`
_, err := tx.ExecContext(
ctx,
query,
tweetId,
nonce,
time.Now(),
)
return pgutil.CheckUniqueViolation(err, twitter.ErrDuplicateNonce)
})
}