-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
129 lines (103 loc) · 3.45 KB
/
store.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
package postgres
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/chat"
)
type store struct {
db *sqlx.DB
}
// New returns a new postgres-backed chat.Store
func New(db *sql.DB) chat.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// PutChat implements chat.Store.PutChat
func (s *store) PutChat(ctx context.Context, record *chat.Chat) error {
model, err := toChatModel(record)
if err != nil {
return err
}
err = model.dbPut(ctx, s.db)
if err != nil {
return err
}
fromChatModel(model).CopyTo(record)
return nil
}
// GetChatById implements chat.Store.GetChatById
func (s *store) GetChatById(ctx context.Context, id chat.ChatId) (*chat.Chat, error) {
model, err := dbGetChatById(ctx, s.db, id)
if err != nil {
return nil, err
}
return fromChatModel(model), nil
}
// GetAllChatsForUser implements chat.Store.GetAllChatsForUser
func (s *store) GetAllChatsForUser(ctx context.Context, user string, cursor query.Cursor, direction query.Ordering, limit uint64) ([]*chat.Chat, error) {
models, err := dbGetAllChatsForUser(ctx, s.db, user, cursor, direction, limit)
if err != nil {
return nil, err
}
var res []*chat.Chat
for _, model := range models {
res = append(res, fromChatModel(model))
}
return res, nil
}
// PutMessage implements chat.Store.PutMessage
func (s *store) PutMessage(ctx context.Context, record *chat.Message) error {
model, err := toMessageModel(record)
if err != nil {
return err
}
err = model.dbPut(ctx, s.db)
if err != nil {
return err
}
fromMessageModel(model).CopyTo(record)
return nil
}
// DeleteMessage implements chat.Store.DeleteMessage
func (s *store) DeleteMessage(ctx context.Context, chatId chat.ChatId, messageId string) error {
return dbDeleteMessage(ctx, s.db, chatId, messageId)
}
// GetMessageById implements chat.Store.GetMessageById
func (s *store) GetMessageById(ctx context.Context, chatId chat.ChatId, messageId string) (*chat.Message, error) {
model, err := dbGetMessageById(ctx, s.db, chatId, messageId)
if err != nil {
return nil, err
}
return fromMessageModel(model), nil
}
// GetAllMessagesByChat implements chat.Store.GetAllMessagesByChat
func (s *store) GetAllMessagesByChat(ctx context.Context, chatId chat.ChatId, cursor query.Cursor, direction query.Ordering, limit uint64) ([]*chat.Message, error) {
models, err := dbGetAllMessagesByChat(ctx, s.db, chatId, cursor, direction, limit)
if err != nil {
return nil, err
}
var res []*chat.Message
for _, model := range models {
res = append(res, fromMessageModel(model))
}
return res, nil
}
// AdvancePointer implements chat.Store.AdvancePointer
func (s *store) AdvancePointer(ctx context.Context, chatId chat.ChatId, pointer string) error {
return dbAdvancePointer(ctx, s.db, chatId, pointer)
}
// GetUnreadCount implements chat.Store.GetUnreadCount
func (s *store) GetUnreadCount(ctx context.Context, chatId chat.ChatId) (uint32, error) {
return dbGetUnreadCount(ctx, s.db, chatId)
}
// SetMuteState implements chat.Store.SetMuteState
func (s *store) SetMuteState(ctx context.Context, chatId chat.ChatId, isMuted bool) error {
return dbSetMuteState(ctx, s.db, chatId, isMuted)
}
// SetSubscriptionState implements chat.Store.SetSubscriptionState
func (s *store) SetSubscriptionState(ctx context.Context, chatId chat.ChatId, isSubscribed bool) error {
return dbSetSubscriptionState(ctx, s.db, chatId, isSubscribed)
}