Skip to content

Commit

Permalink
Update token cache when subscriptions change
Browse files Browse the repository at this point in the history
  • Loading branch information
khlieng committed Jul 2, 2015
1 parent b524991 commit babec50
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
20 changes: 11 additions & 9 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ func Serve() {
crawl = newCrawler()
crawl.start(config.MaxDownloadConnections)

store.AddUser(&User{
Username: "test",
Password: "pass",
})
store.AddClient(1, &Client{
Token: "token",
UUID: "real_unique",
Name: "Castcloud",
})
if user := store.GetUser("test"); user == nil {
store.AddUser(&User{
Username: "test",
Password: "pass",
})
store.AddClient(1, &Client{
Token: "token",
UUID: "real_unique",
Name: "Castcloud",
})
}

log.Println("API listening on port", config.Port)
createRouter().Run(":" + strconv.Itoa(config.Port))
Expand Down
2 changes: 2 additions & 0 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func auth() echo.HandlerFunc {
user := authCache.get(token)
if user != nil {
c.Set("user", user)
c.Set("token", token)
return nil
}

Expand All @@ -27,6 +28,7 @@ func auth() echo.HandlerFunc {

authCache.set(token, user)
c.Set("user", user)
c.Set("token", token)
}

return nil
Expand Down
8 changes: 6 additions & 2 deletions api/casts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ func addCast(c *echo.Context) error {
}
}

err := store.AddSubscription(user.ID, cast.ID)
user, err := store.AddSubscription(user.ID, cast.ID)
if err != nil && err != ErrSubscriptionExists {
return err
}

authCache.set(c.Get("token").(string), user)

return c.JSON(200, cast)
}

Expand Down Expand Up @@ -67,5 +69,7 @@ func removeCast(c *echo.Context) error {
}

user := c.Get("user").(*User)
return store.RemoveSubscription(user.ID, id)
user, err = store.RemoveSubscription(user.ID, id)
authCache.set(c.Get("token").(string), user)
return err
}
4 changes: 2 additions & 2 deletions api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type APIStore interface {
AddUser(user *User) error
RemoveUser(username string) error
AddClient(userid uint64, client *Client) error
AddSubscription(userid, castid uint64) error
RemoveSubscription(userid, castid uint64) error
AddSubscription(userid, castid uint64) (*User, error)
RemoveSubscription(userid, castid uint64) (*User, error)

GetCast(id uint64) *Cast
GetCasts(ids []uint64) []Cast
Expand Down
24 changes: 16 additions & 8 deletions api/store_bolt_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ func (s *BoltStore) AddClient(userid uint64, client *Client) error {
})
}

func (s *BoltStore) AddSubscription(userid, castid uint64) error {
return s.db.Update(func(tx *bolt.Tx) error {
func (s *BoltStore) AddSubscription(userid, castid uint64) (*User, error) {
var user *User

err := s.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(boltBucketCasts)
id := uint64Bytes(castid)
v := b.Get(id)
Expand All @@ -171,8 +173,8 @@ func (s *BoltStore) AddSubscription(userid, castid uint64) error {
return ErrUserNotFound
}

var user User
json.Unmarshal(v, &user)
user = &User{}
json.Unmarshal(v, user)

for _, subid := range user.Subscriptions {
if castid == subid {
Expand All @@ -189,10 +191,14 @@ func (s *BoltStore) AddSubscription(userid, castid uint64) error {

return b.Put(id, v)
})

return user, err
}

func (s *BoltStore) RemoveSubscription(userid, castid uint64) error {
return s.db.Update(func(tx *bolt.Tx) error {
func (s *BoltStore) RemoveSubscription(userid, castid uint64) (*User, error) {
var user *User

err := s.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(boltBucketCasts)
id := uint64Bytes(castid)
v := b.Get(id)
Expand All @@ -207,8 +213,8 @@ func (s *BoltStore) RemoveSubscription(userid, castid uint64) error {
return ErrUserNotFound
}

var user User
json.Unmarshal(v, &user)
user = &User{}
json.Unmarshal(v, user)

for i, subid := range user.Subscriptions {
if castid == subid {
Expand All @@ -225,4 +231,6 @@ func (s *BoltStore) RemoveSubscription(userid, castid uint64) error {

return ErrSubsctiptionNotFound
})

return user, err
}

0 comments on commit babec50

Please sign in to comment.