forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.go
163 lines (146 loc) · 4.02 KB
/
notifications.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
package db
import (
"database/sql"
"encoding/json"
"fmt"
notif "github.com/OpenBazaar/openbazaar-go/api/notifications"
"github.com/OpenBazaar/openbazaar-go/repo"
"strconv"
"strings"
"sync"
"time"
)
type NotficationsDB struct {
modelStore
}
func NewNotificationStore(db *sql.DB, lock *sync.Mutex) repo.NotificationStore {
return &NotficationsDB{modelStore{db, lock}}
}
func (n *NotficationsDB) Put(notifID string, notification notif.Data, notifType string, timestamp time.Time) error {
ser, err := json.Marshal(notification)
if err != nil {
return err
}
n.lock.Lock()
defer n.lock.Unlock()
_, err = n.ExecuteQuery("insert into notifications(notifID, serializedNotification, type, timestamp, read) values(?,?,?,?,?)", notifID, string(ser), strings.ToLower(notifType), int(timestamp.Unix()), 0)
if err != nil {
return err
}
return nil
}
func (n *NotficationsDB) GetAll(offsetId string, limit int, typeFilter []string) ([]notif.Notification, int, error) {
var ret []notif.Notification
n.lock.Lock()
defer n.lock.Unlock()
var stm string
var cstm string
var filter string
typeFilterClause := ""
var types []string
if len(typeFilter) > 0 {
typeFilterClauseParts := make([]string, 0, len(typeFilter))
for i := 0; i < len(typeFilter); i++ {
types = append(types, strings.ToLower(typeFilter[i]))
typeFilterClauseParts = append(typeFilterClauseParts, "?")
}
typeFilterClause = "type in (" + strings.Join(typeFilterClauseParts, ",") + ")"
}
var args []interface{}
if offsetId != "" {
args = append(args, offsetId)
if len(types) > 0 {
filter = " and " + typeFilterClause
for _, a := range types {
args = append(args, a)
}
}
stm = "select serializedNotification, timestamp, read from notifications where timestamp<(select timestamp from notifications where notifID=?)" + filter + " order by timestamp desc limit " + strconv.Itoa(limit) + ";"
cstm = "select Count(*) from notifications where timestamp<(select timestamp from notifications where notifID=?)" + filter + " order by timestamp desc;"
} else {
if len(types) > 0 {
filter = " where " + typeFilterClause
for _, a := range types {
args = append(args, a)
}
}
stm = "select serializedNotification, timestamp, read from notifications" + filter + " order by timestamp desc limit " + strconv.Itoa(limit) + ";"
cstm = "select Count(*) from notifications" + filter + " order by timestamp desc;"
}
rows, err := n.db.Query(stm, args...)
if err != nil {
return ret, 0, err
}
for rows.Next() {
var data []byte
var timestampInt int
var readInt int
if err := rows.Scan(&data, ×tampInt, &readInt); err != nil {
fmt.Println(err)
continue
}
var read bool
if readInt == 1 {
read = true
}
timestamp := time.Unix(int64(timestampInt), 0)
var ni interface{}
err := json.Unmarshal(data, &ni)
if err != nil {
fmt.Println(err)
continue
}
n := notif.Notification{
Data: ni,
Timestamp: timestamp,
Read: read,
}
ret = append(ret, n)
}
row := n.db.QueryRow(cstm, args...)
var count int
err = row.Scan(&count)
if err != nil {
return ret, 0, err
}
return ret, count, nil
}
func (n *NotficationsDB) MarkAsRead(notifID string) error {
n.lock.Lock()
defer n.lock.Unlock()
tx, err := n.db.Begin()
if err != nil {
return err
}
stmt, _ := tx.Prepare("update notifications set read=1 where notifID=?")
defer stmt.Close()
_, err = stmt.Exec(notifID)
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (n *NotficationsDB) MarkAllAsRead() error {
n.lock.Lock()
defer n.lock.Unlock()
_, err := n.ExecuteQuery("update notifications set read=1")
return err
}
func (n *NotficationsDB) Delete(notifID string) error {
n.lock.Lock()
defer n.lock.Unlock()
n.ExecuteQuery("delete from notifications where notifID=?", notifID)
return nil
}
func (n *NotficationsDB) GetUnreadCount() (int, error) {
stm := "select Count(*) from notifications where read=0;"
row := n.db.QueryRow(stm)
var count int
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}