summaryrefslogtreecommitdiffstats
path: root/storage/storage.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage/storage.go')
-rw-r--r--storage/storage.go29
1 files changed, 27 insertions, 2 deletions
diff --git a/storage/storage.go b/storage/storage.go
index a32c4a0..18a968d 100644
--- a/storage/storage.go
+++ b/storage/storage.go
@@ -36,7 +36,7 @@ func InitDb(path string) (*Database, error) {
func (db *Database) AddMessage(msg core.Message) (err error) {
_, err = db.Exec(
"INSERT INTO messages (source, target, timestamp, content) VALUES (?, ?, ?, ?);",
- msg.Source, msg.Target, msg.Timestamp.Unix(), msg.Content,
+ msg.Source, msg.Target, msg.Timestamp.UnixMilli(), msg.Content,
)
return err
@@ -84,7 +84,7 @@ func (db *Database) getHistory(rows *sql.Rows, err error) ([]core.Message, error
if err = rows.Scan(&msg.Source, &msg.Target, &timestamp, &msg.Content); err != nil {
return history, err
}
- msg.Timestamp = time.Unix(timestamp, 0)
+ msg.Timestamp = time.UnixMilli(timestamp)
history = append(history, msg)
}
@@ -169,6 +169,31 @@ func (db *Database) GetChannelUsers(channel string) (users []string, err error)
return users, nil
}
+func (db *Database) GetUserChannels(user string, count, offset int) (channels []string, err error) {
+ rows, err := db.Query("SELECT channel FROM permissions WHERE user = ? AND write = 1;", user)
+ defer func() {
+ if rows == nil {
+ return
+ }
+ if err := rows.Close(); err != nil {
+ log.Println("cannot close database row:", err)
+ }
+ }()
+ if err != nil {
+ return channels, err
+ }
+
+ for rows.Next() {
+ var channel string
+ if err := rows.Scan(&channel); err != nil {
+ return channels, err
+ }
+ channels = append(channels, channel)
+ }
+
+ return channels, nil
+}
+
func itob(v int) bool {
if v == 1 {
return true