-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
165 lines (142 loc) · 4.1 KB
/
database.go
File metadata and controls
165 lines (142 loc) · 4.1 KB
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
package main
import (
"database/sql"
"errors"
"math/rand/v2"
"os"
"time"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
func initDB() error {
var err error
dbURL := os.Getenv("DATABASE_URL")
if dbURL == "" {
dbURL = "/app/data/timerbot.db"
}
db, err = sql.Open("sqlite3", dbURL)
if err != nil {
return err
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS timers (
internalId INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT UNIQUE,
message TEXT,
user TEXT,
channel TEXT,
creation DATETIME,
due DATETIME,
snoozedDue DATETIME,
snoozeCount INTEGER DEFAULT 0,
shown BOOLEAN DEFAULT false
)
`)
return err
}
func createTimer(id string, message string, userId string, channelId string, due time.Time) (*Timer, error) {
created := time.Now()
_, err := db.Exec("INSERT INTO timers (id, message, user, channel, creation, due, snoozedDue) VALUES (?, ?, ?, ?, ?, ?, ?)", id, message, userId, channelId, created, due, due)
if err != nil {
return nil, err
}
return &Timer{
ID: id,
Message: message,
User: userId,
Channel: channelId,
Created: created,
Due: due,
SnoozedDue: due,
SnoozeCount: 0,
Shown: false,
}, nil
}
func getTimerByID(id string) (*Timer, error) {
row := db.QueryRow("SELECT internalId, id, message, user, channel, creation, due, snoozedDue, snoozeCount, shown FROM timers WHERE id = ?", id)
timer := &Timer{}
err := row.Scan(&timer.InternalID, &timer.ID, &timer.Message, &timer.User, &timer.Channel, &timer.Created, &timer.Due, &timer.SnoozedDue, &timer.SnoozeCount, &timer.Shown)
if err != nil {
return nil, err
}
return timer, nil
}
func getAllTimersForUser(userID string, onlyActive bool) ([]*Timer, error) {
query := "SELECT internalId, id, message, user, channel, creation, due, snoozedDue, snoozeCount, shown FROM timers WHERE user = ?"
if onlyActive {
query += " AND shown = false"
}
rows, err := db.Query(query, userID)
if err != nil {
return nil, err
}
defer func(rows *sql.Rows) {
err = rows.Close()
}(rows)
var timers []*Timer
for rows.Next() {
timer := &Timer{}
err := rows.Scan(&timer.InternalID, &timer.ID, &timer.Message, &timer.User, &timer.Channel, &timer.Created, &timer.Due, &timer.SnoozedDue, &timer.SnoozeCount, &timer.Shown)
if err != nil {
return nil, err
}
timers = append(timers, timer)
}
return timers, err
}
func updateTimer(timer *Timer) error {
_, err := db.Exec("UPDATE timers SET message = ?, due = ?, snoozedDue = ? WHERE id = ?", timer.Message, timer.Due, timer.SnoozedDue, timer.ID)
return err
}
func deleteTimer(id string) error {
_, err := db.Exec("DELETE FROM timers WHERE id = ?", id)
return err
}
func snoozeTimer(id string, newDueDate time.Time) error {
_, err := db.Exec("UPDATE timers SET snoozedDue = ?, snoozeCount = snoozeCount + 1, shown = false WHERE id = ?", newDueDate, id)
return err
}
func getDueTimers() ([]*Timer, error) {
rows, err := db.Query("SELECT internalId, id, message, user, channel, creation, due, snoozedDue, snoozeCount, shown FROM timers WHERE snoozedDue <= ? AND shown = false", time.Now())
if err != nil {
return nil, err
}
defer func(rows *sql.Rows) {
err = rows.Close()
}(rows)
var timers []*Timer
for rows.Next() {
timer := &Timer{}
err := rows.Scan(&timer.InternalID, &timer.ID, &timer.Message, &timer.User, &timer.Channel, &timer.Created, &timer.Due, &timer.SnoozedDue, &timer.SnoozeCount, &timer.Shown)
if err != nil {
return nil, err
}
timers = append(timers, timer)
}
return timers, nil
}
func markTimerAsShown(id string) error {
_, err := db.Exec("UPDATE timers SET shown = true WHERE id = ?", id)
return err
}
func newTimerID() (string, error) {
for {
id := randomString(4)
_, err := getTimerByID(id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return id, nil // ID is unique
}
return "", err // Other database error
}
// If no error, timer with this ID already exists, loop again
}
}
const letters = "abcdefghijklmnopqrstuvwxyz"
func randomString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.IntN(len(letters))]
}
return string(b)
}