Name all storage interface params, return slices of pointers

This commit is contained in:
Ken-Håvard Lieng 2018-06-01 04:16:38 +02:00
parent 09248edd59
commit e0200a2b2a
11 changed files with 65 additions and 52 deletions

View File

@ -24,7 +24,7 @@ type connectDefaults struct {
type indexData struct { type indexData struct {
Defaults connectDefaults Defaults connectDefaults
Servers []Server Servers []Server
Channels []storage.Channel Channels []*storage.Channel
// Users in the selected channel // Users in the selected channel
Users *Userlist Users *Userlist
@ -116,7 +116,7 @@ func (d *indexData) addUsersAndMessages(server, channel string, state *State) {
} }
} }
func isInChannel(channels []storage.Channel, server, channel string) bool { func isInChannel(channels []*storage.Channel, server, channel string) bool {
if channel != "" { if channel != "" {
for _, ch := range channels { for _, ch := range channels {
if server == ch.Server && channel == ch.Name { if server == ch.Server && channel == ch.Name {

View File

@ -74,16 +74,24 @@ func easyjson7e607aefDecodeGithubComKhliengDispatchServer(in *jlexer.Lexer, out
in.Delim('[') in.Delim('[')
if out.Channels == nil { if out.Channels == nil {
if !in.IsDelim(']') { if !in.IsDelim(']') {
out.Channels = make([]storage.Channel, 0, 1) out.Channels = make([]*storage.Channel, 0, 8)
} else { } else {
out.Channels = []storage.Channel{} out.Channels = []*storage.Channel{}
} }
} else { } else {
out.Channels = (out.Channels)[:0] out.Channels = (out.Channels)[:0]
} }
for !in.IsDelim(']') { for !in.IsDelim(']') {
var v2 storage.Channel var v2 *storage.Channel
easyjson7e607aefDecodeGithubComKhliengDispatchStorage(in, &v2) if in.IsNull() {
in.Skip()
v2 = nil
} else {
if v2 == nil {
v2 = new(storage.Channel)
}
easyjson7e607aefDecodeGithubComKhliengDispatchStorage(in, &*v2)
}
out.Channels = append(out.Channels, v2) out.Channels = append(out.Channels, v2)
in.WantComma() in.WantComma()
} }
@ -170,7 +178,11 @@ func easyjson7e607aefEncodeGithubComKhliengDispatchServer(out *jwriter.Writer, i
if v5 > 0 { if v5 > 0 {
out.RawByte(',') out.RawByte(',')
} }
easyjson7e607aefEncodeGithubComKhliengDispatchStorage(out, v6) if v6 == nil {
out.RawString("null")
} else {
easyjson7e607aefEncodeGithubComKhliengDispatchStorage(out, *v6)
}
} }
out.RawByte(']') out.RawByte(']')
} }

View File

@ -20,7 +20,7 @@ type WSResponse struct {
} }
type Server struct { type Server struct {
storage.Server *storage.Server
Status ConnectionUpdate Status ConnectionUpdate
} }

View File

@ -766,6 +766,7 @@ func easyjson42239ddeDecodeGithubComKhliengDispatchServer7(in *jlexer.Lexer, out
in.Skip() in.Skip()
return return
} }
out.Server = new(storage.Server)
in.Delim('{') in.Delim('{')
for !in.IsDelim('}') { for !in.IsDelim('}') {
key := in.UnsafeString() key := in.UnsafeString()

View File

@ -59,8 +59,8 @@ func (d *Dispatch) loadUsers() {
log.Printf("Loading %d user(s)", len(users)) log.Printf("Loading %d user(s)", len(users))
for i := range users { for _, user := range users {
go d.loadUser(&users[i]) go d.loadUser(user)
} }
} }
@ -92,7 +92,7 @@ func (d *Dispatch) loadUser(user *storage.User) {
} }
for _, server := range servers { for _, server := range servers {
i := connectIRC(&server, state) i := connectIRC(server, state)
var joining []string var joining []string
for _, channel := range channels { for _, channel := range channels {

View File

@ -247,8 +247,8 @@ func newStateStore(sessionStore storage.SessionStore) *stateStore {
for _, session := range sessions { for _, session := range sessions {
if !session.Expired() { if !session.Expired() {
session.Init() session.Init()
store.sessions[session.Key()] = &session store.sessions[session.Key()] = session
go deleteSessionWhenExpired(&session, store) go deleteSessionWhenExpired(session, store)
} else { } else {
go sessionStore.DeleteSession(session.Key()) go sessionStore.DeleteSession(session.Key())
} }

View File

@ -90,9 +90,9 @@ func (h *wsHandler) connect(b []byte) {
if _, ok := h.state.getIRC(data.Host); !ok { if _, ok := h.state.getIRC(data.Host); !ok {
log.Println(h.addr, "[IRC] Add server", data.Host) log.Println(h.addr, "[IRC] Add server", data.Host)
connectIRC(&data.Server, h.state) connectIRC(data.Server, h.state)
go h.state.user.AddServer(&data.Server) go h.state.user.AddServer(data.Server)
} else { } else {
log.Println(h.addr, "[IRC]", data.Host, "already added") log.Println(h.addr, "[IRC]", data.Host, "already added")
} }

View File

@ -48,8 +48,8 @@ func (s *BoltStore) Close() {
s.db.Close() s.db.Close()
} }
func (s *BoltStore) GetUsers() ([]storage.User, error) { func (s *BoltStore) GetUsers() ([]*storage.User, error) {
var users []storage.User var users []*storage.User
s.db.View(func(tx *bolt.Tx) error { s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketUsers) b := tx.Bucket(bucketUsers)
@ -63,7 +63,7 @@ func (s *BoltStore) GetUsers() ([]storage.User, error) {
} }
copy(user.IDBytes, k) copy(user.IDBytes, k)
users = append(users, user) users = append(users, &user)
return nil return nil
}) })
@ -109,8 +109,8 @@ func (s *BoltStore) DeleteUser(user *storage.User) error {
}) })
} }
func (s *BoltStore) GetServers(user *storage.User) ([]storage.Server, error) { func (s *BoltStore) GetServers(user *storage.User) ([]*storage.Server, error) {
var servers []storage.Server var servers []*storage.Server
s.db.View(func(tx *bolt.Tx) error { s.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(bucketServers).Cursor() c := tx.Bucket(bucketServers).Cursor()
@ -118,7 +118,7 @@ func (s *BoltStore) GetServers(user *storage.User) ([]storage.Server, error) {
for k, v := c.Seek(user.IDBytes); bytes.HasPrefix(k, user.IDBytes); k, v = c.Next() { for k, v := c.Seek(user.IDBytes); bytes.HasPrefix(k, user.IDBytes); k, v = c.Next() {
server := storage.Server{} server := storage.Server{}
server.Unmarshal(v) server.Unmarshal(v)
servers = append(servers, server) servers = append(servers, &server)
} }
return nil return nil
@ -190,8 +190,8 @@ func (s *BoltStore) SetServerName(user *storage.User, name, address string) erro
}) })
} }
func (s *BoltStore) GetChannels(user *storage.User) ([]storage.Channel, error) { func (s *BoltStore) GetChannels(user *storage.User) ([]*storage.Channel, error) {
var channels []storage.Channel var channels []*storage.Channel
s.db.View(func(tx *bolt.Tx) error { s.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(bucketChannels).Cursor() c := tx.Bucket(bucketChannels).Cursor()
@ -199,7 +199,7 @@ func (s *BoltStore) GetChannels(user *storage.User) ([]storage.Channel, error) {
for k, v := c.Seek(user.IDBytes); bytes.HasPrefix(k, user.IDBytes); k, v = c.Next() { for k, v := c.Seek(user.IDBytes); bytes.HasPrefix(k, user.IDBytes); k, v = c.Next() {
channel := storage.Channel{} channel := storage.Channel{}
channel.Unmarshal(v) channel.Unmarshal(v)
channels = append(channels, channel) channels = append(channels, &channel)
} }
return nil return nil
@ -298,8 +298,8 @@ func (s *BoltStore) GetMessagesByID(server, channel string, ids []string) ([]sto
return messages, err return messages, err
} }
func (s *BoltStore) GetSessions() ([]session.Session, error) { func (s *BoltStore) GetSessions() ([]*session.Session, error) {
var sessions []session.Session var sessions []*session.Session
err := s.db.View(func(tx *bolt.Tx) error { err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketSessions) b := tx.Bucket(bucketSessions)
@ -307,7 +307,7 @@ func (s *BoltStore) GetSessions() ([]session.Session, error) {
return b.ForEach(func(_ []byte, v []byte) error { return b.ForEach(func(_ []byte, v []byte) error {
session := session.Session{} session := session.Session{}
_, err := session.Unmarshal(v) _, err := session.Unmarshal(v)
sessions = append(sessions, session) sessions = append(sessions, &session)
return err return err
}) })
}) })

View File

@ -11,23 +11,23 @@ func Initialize(dir string) {
} }
type Store interface { type Store interface {
GetUsers() ([]User, error) GetUsers() ([]*User, error)
SaveUser(*User) error SaveUser(user *User) error
DeleteUser(*User) error DeleteUser(user *User) error
GetServers(*User) ([]Server, error) GetServers(user *User) ([]*Server, error)
AddServer(*User, *Server) error AddServer(user *User, server *Server) error
RemoveServer(*User, string) error RemoveServer(user *User, host string) error
SetNick(*User, string, string) error SetNick(user *User, nick, host string) error
SetServerName(*User, string, string) error SetServerName(user *User, name, host string) error
GetChannels(*User) ([]Channel, error) GetChannels(user *User) ([]*Channel, error)
AddChannel(*User, *Channel) error AddChannel(user *User, channel *Channel) error
RemoveChannel(*User, string, string) error RemoveChannel(user *User, server, channel string) error
} }
type SessionStore interface { type SessionStore interface {
GetSessions() ([]session.Session, error) GetSessions() ([]*session.Session, error)
SaveSession(session *session.Session) error SaveSession(session *session.Session) error
DeleteSession(key string) error DeleteSession(key string) error
} }

View File

@ -35,15 +35,15 @@ func NewUser(store Store) (*User, error) {
return user, nil return user, nil
} }
func LoadUsers(store Store) ([]User, error) { func LoadUsers(store Store) ([]*User, error) {
users, err := store.GetUsers() users, err := store.GetUsers()
if err != nil { if err != nil {
return nil, err return nil, err
} }
for i := range users { for _, user := range users {
users[i].store = store user.store = store
users[i].loadCertificate() user.loadCertificate()
} }
return users, nil return users, nil
@ -79,7 +79,7 @@ type Server struct {
Realname string Realname string
} }
func (u *User) GetServers() ([]Server, error) { func (u *User) GetServers() ([]*Server, error) {
return u.store.GetServers(u) return u.store.GetServers(u)
} }
@ -105,7 +105,7 @@ type Channel struct {
Topic string Topic string
} }
func (u *User) GetChannels() ([]Channel, error) { func (u *User) GetChannels() ([]*Channel, error) {
return u.store.GetChannels(u) return u.store.GetChannels(u)
} }

View File

@ -27,29 +27,29 @@ func TestUser(t *testing.T) {
user, err := storage.NewUser(db) user, err := storage.NewUser(db)
assert.Nil(t, err) assert.Nil(t, err)
srv := storage.Server{ srv := &storage.Server{
Name: "Freenode", Name: "Freenode",
Host: "irc.freenode.net", Host: "irc.freenode.net",
Nick: "test", Nick: "test",
} }
chan1 := storage.Channel{ chan1 := &storage.Channel{
Server: srv.Host, Server: srv.Host,
Name: "#test", Name: "#test",
} }
chan2 := storage.Channel{ chan2 := &storage.Channel{
Server: srv.Host, Server: srv.Host,
Name: "#testing", Name: "#testing",
} }
user.AddServer(&srv) user.AddServer(srv)
user.AddChannel(&chan1) user.AddChannel(chan1)
user.AddChannel(&chan2) user.AddChannel(chan2)
users, err := storage.LoadUsers(db) users, err := storage.LoadUsers(db)
assert.Nil(t, err) assert.Nil(t, err)
assert.Len(t, users, 1) assert.Len(t, users, 1)
user = &users[0] user = users[0]
assert.Equal(t, uint64(1), user.ID) assert.Equal(t, uint64(1), user.ID)
servers, err := user.GetServers() servers, err := user.GetServers()