Skip to content
Browse files

Removed RUID dependency in favour of noeq

  • Loading branch information...
1 parent 126b36f commit 42639dd8e39305f353b65df6261db362ffe04370 @paddyforan paddyforan committed
Showing with 195 additions and 189 deletions.
  1. +26 −26 account.go
  2. +10 −10 audit.go
  3. +12 −11 config.go
  4. +22 −23 device.go
  5. +63 −58 link.go
  6. +6 −7 notification.go
  7. +18 −16 twocloud.go
  8. +38 −38 user.go
View
52 account.go
@@ -7,13 +7,13 @@ import (
"github.com/fzzbt/radix/redis"
"io/ioutil"
"net/http"
- "secondbit.org/ruid"
+ "strconv"
"time"
)
type Account struct {
Added time.Time `json:"added,omitempty"`
- ID ruid.RUID `json:"id,omitempty"`
+ ID uint64 `json:"id,omitempty"`
Provider string `json:"provider,omitempty"`
// Provided by the provider
ForeignID string `json:"foreign_id,omitempty"`
@@ -27,14 +27,14 @@ type Account struct {
Timezone string `json:"timezone,omitempty"`
Gender string `json:"gender,omitempty"`
// private info that is stored, never shared
- UserID ruid.RUID `json:"-"`
+ UserID uint64 `json:"-"`
accessToken string
refreshToken string
expires time.Time
}
func (account *Account) IsEmpty() bool {
- return account.ID.String() == ruid.RUID(0).String()
+ return account.ID == 0
}
type googleAccount struct {
@@ -126,12 +126,12 @@ func (r *RequestBundle) GetAccount(access, refresh string, expiration time.Time)
Timezone: googAccount.Timezone,
Locale: googAccount.Locale,
Gender: googAccount.Gender,
- UserID: ruid.RUID(0),
+ UserID: 0,
accessToken: access,
refreshToken: refresh,
expires: expiration,
}
- id, err := gen.Generate([]byte(googAccount.ID))
+ id, err := r.GetID()
if err != nil {
r.Log.Error(err.Error())
return Account{}, err
@@ -219,7 +219,7 @@ func (r *RequestBundle) getAccountByForeignID(foreign_id string) (Account, error
r.Log.Error(err.Error())
return Account{}, nil
}
- id, err := ruid.RUIDFromString(account_id)
+ id, err := strconv.ParseUint(account_id, 10, 64)
if err != nil {
r.Log.Error(err.Error())
return Account{}, err
@@ -234,16 +234,16 @@ func (r *RequestBundle) getAccountByForeignID(foreign_id string) (Account, error
return account, nil
}
-func (r *RequestBundle) GetAccountByID(id ruid.RUID) (Account, error) {
+func (r *RequestBundle) GetAccountByID(id uint64) (Account, error) {
// start instrumentation
- reply := r.Repo.client.Hgetall("accounts:" + id.String())
+ reply := r.Repo.client.Hgetall("accounts:" + strconv.FormatUint(id, 10))
// report the request to the repo to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return Account{}, reply.Err
}
if reply.Type == redis.ReplyNil {
- r.Log.Warn("Account not found. ID: %s", id)
+ r.Log.Warn("Account not found. ID: %d", id)
return Account{}, nil
}
hash, err := reply.Hash()
@@ -261,7 +261,7 @@ func (r *RequestBundle) GetAccountByID(id ruid.RUID) (Account, error) {
r.Log.Error(err.Error())
return Account{}, err
}
- user_id, err := ruid.RUIDFromString(hash["user_id"])
+ user_id, err := strconv.ParseUint(hash["user_id"], 10, 64)
if err != nil {
return Account{}, err
}
@@ -336,8 +336,8 @@ func (r *RequestBundle) storeAccount(account Account, update bool) error {
from["gender"] = old_account.Gender
}
if old_account.UserID != account.UserID {
- changes["user_id"] = account.UserID.String()
- from["user_id"] = old_account.UserID.String()
+ changes["user_id"] = account.UserID
+ from["user_id"] = old_account.UserID
}
if old_account.accessToken != account.accessToken {
changes["access_token"] = account.accessToken
@@ -351,20 +351,20 @@ func (r *RequestBundle) storeAccount(account Account, update bool) error {
changes["expires"] = account.expires.Format(time.RFC3339)
from["expires"] = old_account.expires.Format(time.RFC3339)
}
- reply := r.Repo.client.Hmset("accounts:"+account.ID.String(), changes)
+ reply := r.Repo.client.Hmset("accounts:"+strconv.FormatUint(account.ID, 10), changes)
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- reply = r.Repo.client.Sadd("users:"+account.UserID.String()+":accounts", account.ID.String())
+ reply = r.Repo.client.Sadd("users:"+strconv.FormatUint(account.UserID, 10)+":accounts", account.ID, 10)
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- r.AuditMap("accounts:"+account.ID.String(), from, changes)
+ r.AuditMap("accounts:"+strconv.FormatUint(account.ID, 10), from, changes)
// add repo call to instrumentation
return nil
}
@@ -381,7 +381,7 @@ func (r *RequestBundle) storeAccount(account Account, update bool) error {
"locale": account.Locale,
"timezone": account.Timezone,
"gender": account.Gender,
- "user_id": account.UserID.String(),
+ "user_id": account.UserID,
"access_token": account.accessToken,
"refresh_token": account.refreshToken,
"expires": account.expires.Format(time.RFC3339),
@@ -405,17 +405,17 @@ func (r *RequestBundle) storeAccount(account Account, update bool) error {
"expires": "",
}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
- mc.Hmset("accounts:"+account.ID.String(), changes)
- mc.Hmset("oauth_foreign_ids_to_accounts", account.ForeignID, account.ID.String())
- mc.Sadd("users:"+account.UserID.String()+":accounts", account.ID.String())
+ mc.Hmset("accounts:"+strconv.FormatUint(account.ID, 10), changes)
+ mc.Hmset("oauth_foreign_ids_to_accounts", account.ForeignID, strconv.FormatUint(account.ID, 10))
+ mc.Sadd("users:"+strconv.FormatUint(account.UserID, 10)+":accounts", account.ID)
})
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- r.AuditMap("accounts:"+account.ID.String(), from, changes)
- r.Audit("oauth_foreign_ids_to_accounts", account.ForeignID, "", account.ID.String())
+ r.AuditMap("accounts:"+strconv.FormatUint(account.ID, 10), from, changes)
+ r.Audit("oauth_foreign_ids_to_accounts", account.ForeignID, "", strconv.FormatUint(account.ID, 10))
// report the repo request to instrumentation
// stop instrumentation
return nil
@@ -423,7 +423,7 @@ func (r *RequestBundle) storeAccount(account Account, update bool) error {
func (r *RequestBundle) GetAccountsByUser(user User) ([]Account, error) {
// start instrumentation
- reply := r.Repo.client.Smembers("users:" + user.ID.String() + ":accounts")
+ reply := r.Repo.client.Smembers("users:" + strconv.FormatUint(user.ID, 10) + ":accounts")
// report the repo request to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
@@ -469,12 +469,12 @@ func (r *RequestBundle) GetAccountsByUser(user User) ([]Account, error) {
r.Log.Error(err.Error())
continue
}
- user_id, err := ruid.RUIDFromString(hash["user_id"])
+ user_id, err := strconv.ParseUint(hash["user_id"], 10, 64)
if err != nil {
r.Log.Error(err.Error())
continue
}
- id, err := ruid.RUIDFromString(ids[pos])
+ id, err := strconv.ParseUint(ids[pos], 10, 64)
if err != nil {
r.Log.Error(err.Error())
continue
@@ -544,7 +544,7 @@ func (r *RequestBundle) UpdateAccountData(account Account) (Account, error) {
return account, nil
}
-func (r *RequestBundle) AssociateUserWithAccount(account Account, user ruid.RUID) error {
+func (r *RequestBundle) AssociateUserWithAccount(account Account, user uint64) error {
// begin instrumentation
account.UserID = user
err := r.storeAccount(account, true)
View
20 audit.go
@@ -2,7 +2,7 @@ package twocloud
import (
"github.com/fzzbt/radix/redis"
- "secondbit.org/ruid"
+ "strconv"
"time"
)
@@ -21,7 +21,7 @@ func (a *Auditor) Close() {
}
type Change struct {
- ID ruid.RUID `json:"id"`
+ ID uint64 `json:"id"`
Key string `json:"key"`
Field string `json:"field"`
From interface{} `json:"from"`
@@ -31,10 +31,10 @@ type Change struct {
Timestamp time.Time `json:"timestamp"`
}
-func (a *Auditor) Insert(key, ip string, user User, from, to map[string]interface{}) error {
+func (a *Auditor) Insert(r *RequestBundle, key, ip string, user User, from, to map[string]interface{}) error {
changes := []Change{}
for k, v := range to {
- id, err := gen.Generate([]byte(key))
+ id, err := r.GetID()
if err != nil {
return err
}
@@ -53,11 +53,11 @@ func (a *Auditor) Insert(key, ip string, user User, from, to map[string]interfac
reply := a.client.MultiCall(func(mc *redis.MultiCall) {
for _, change := range changes {
user_str := ""
- if change.User.ID != ruid.RUID(0) {
- user_str = change.User.ID.String()
+ if change.User.ID != 0 {
+ user_str = strconv.FormatUint(change.User.ID, 10)
}
- mc.Hmset("audit:"+change.Key+":item:"+change.ID.String(), "from", change.From, "to", change.To, "field", change.Field, "timestamp", change.Timestamp.Format(time.RFC3339), "user", user_str)
- mc.Lpush("audit:"+key, change.ID.String())
+ mc.Hmset("audit:"+change.Key+":item:"+strconv.FormatUint(change.ID, 10), "from", change.From, "to", change.To, "field", change.Field, "timestamp", change.Timestamp.Format(time.RFC3339), "user", user_str)
+ mc.Lpush("audit:"+key, change.ID)
}
})
return reply.Err
@@ -69,7 +69,7 @@ func (r *RequestBundle) Audit(key, field, fromstr, tostr string) {
from[field] = fromstr
to := map[string]interface{}{}
to[field] = tostr
- err := r.Auditor.Insert(key, r.Request.RemoteAddr, r.AuthUser, from, to)
+ err := r.Auditor.Insert(r, key, r.Request.RemoteAddr, r.AuthUser, from, to)
if err != nil {
r.Log.Error(err.Error())
}
@@ -78,7 +78,7 @@ func (r *RequestBundle) Audit(key, field, fromstr, tostr string) {
func (r *RequestBundle) AuditMap(key string, from, to map[string]interface{}) {
if r.Auditor != nil {
- err := r.Auditor.Insert(key, r.Request.RemoteAddr, r.AuthUser, from, to)
+ err := r.Auditor.Insert(r, key, r.Request.RemoteAddr, r.AuthUser, from, to)
if err != nil {
r.Log.Error(err.Error())
}
View
23 config.go
@@ -6,18 +6,19 @@ import (
)
type Config struct {
- UseSubscriptions bool `json:"subscriptions"`
- MaintenanceMode bool `json:"maintenance"`
- Database redis.Config `json:"db"`
- AuditDatabase redis.Config `json:"audit_db"`
- StatsDatabase redis.Config `json:"stats_db"`
- OAuth OAuthClient `json:"oauth"`
- TrialPeriod time.Duration `json:"trial_period"`
- GracePeriod time.Duration `json:"grace_period"`
+ UseSubscriptions bool `json:"subscriptions"`
+ MaintenanceMode bool `json:"maintenance"`
+ Database redis.Config `json:"db"`
+ AuditDatabase redis.Config `json:"audit_db"`
+ InstrumentationDatabase redis.Config `json:"instrumentation_db"`
+ OAuth OAuthClient `json:"oauth"`
+ TrialPeriod time.Duration `json:"trial_period"`
+ GracePeriod time.Duration `json:"grace_period"`
+ IDGenerators map[string]string `json:"id_gen"`
}
type OAuthClient struct {
- ClientID string
- ClientSecret string
- CallbackURL string
+ ClientID string `json:"client_id"`
+ ClientSecret string `json:"client_secret"`
+ CallbackURL string `json:"callback"`
}
View
45 device.go
@@ -3,21 +3,20 @@ package twocloud
import (
"errors"
"github.com/fzzbt/radix/redis"
- "secondbit.org/ruid"
"strconv"
"strings"
"time"
)
type Device struct {
- ID ruid.RUID `json:"id,omitempty"`
+ ID uint64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
LastSeen time.Time `json:"last_seen,omitempty"`
LastIP string `json:"last_ip,omitempty"`
ClientType string `json:"client_type,omitempty"`
Created time.Time `json:"created,omitempty"`
Pushers *Pushers `json:"pushers,omitempty"`
- UserID ruid.RUID `json:"user_id,omitempty"`
+ UserID uint64 `json:"user_id,omitempty"`
AuthError bool `json:"auth_error,omitempty"`
}
@@ -48,7 +47,7 @@ func (d *Device) ValidClientType() bool {
func (r *RequestBundle) GetDevicesByUser(user User) ([]Device, error) {
// start instrumentation
- reply := r.Repo.client.Zrevrange("users:"+user.ID.String()+":devices", 0, -1)
+ reply := r.Repo.client.Zrevrange("users:"+strconv.FormatUint(user.ID, 10)+":devices", 0, -1)
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
@@ -87,11 +86,11 @@ func (r *RequestBundle) GetDevicesByUser(user User) ([]Device, error) {
if err != nil {
return devices, err
}
- user_id, err := ruid.RUIDFromString(hash["user_id"])
+ user_id, err := strconv.ParseUint(hash["user_id"], 10, 64)
if err != nil {
return devices, err
}
- id, err := ruid.RUIDFromString(ids[pos])
+ id, err := strconv.ParseUint(ids[pos], 10, 64)
if err != nil {
return devices, err
}
@@ -138,12 +137,12 @@ func (r *RequestBundle) GetDevicesByUser(user User) ([]Device, error) {
return devices, nil
}
-func (r *RequestBundle) GetDevice(id ruid.RUID) (Device, error) {
+func (r *RequestBundle) GetDevice(id uint64) (Device, error) {
// start instrumentation
if r.Device.ID == id {
return r.Device, nil
}
- reply := r.Repo.client.Hgetall("devices:" + id.String())
+ reply := r.Repo.client.Hgetall("devices:" + strconv.FormatUint(id, 10))
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
@@ -167,7 +166,7 @@ func (r *RequestBundle) GetDevice(id ruid.RUID) (Device, error) {
r.Log.Error(err.Error())
return Device{}, err
}
- user_id, err := ruid.RUIDFromString(hash["user_id"])
+ user_id, err := strconv.ParseUint(hash["user_id"], 10, 64)
if err != nil {
r.Log.Error(err.Error())
return Device{}, err
@@ -214,7 +213,7 @@ func (r *RequestBundle) GetDevice(id ruid.RUID) (Device, error) {
}
func (r *RequestBundle) AddDevice(name, client_type, ip, gcm_key string, user User) (Device, error) {
- id, err := gen.Generate([]byte(user.ID.String()))
+ id, err := r.GetID()
if err != nil {
r.Log.Error(err.Error())
return Device{}, err
@@ -279,14 +278,14 @@ func (r *RequestBundle) storeDevice(device Device, update bool) error {
from["gcm_key"] = old_device.Pushers.GCM.Key
}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
- mc.Hmset("devices:"+device.ID.String(), changes)
+ mc.Hmset("devices:"+strconv.FormatUint(device.ID, 10), changes)
})
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- r.AuditMap("devices:"+device.ID.String(), from, changes)
+ r.AuditMap("devices:"+strconv.FormatUint(device.ID, 10), from, changes)
// add repo call to instrumentation
return nil
}
@@ -296,7 +295,7 @@ func (r *RequestBundle) storeDevice(device Device, update bool) error {
"last_ip": device.LastIP,
"client_type": device.ClientType,
"created": time.Now().Format(time.RFC3339),
- "user_id": device.UserID.String(),
+ "user_id": device.UserID,
}
from := map[string]interface{}{
"name": "",
@@ -311,15 +310,15 @@ func (r *RequestBundle) storeDevice(device Device, update bool) error {
from["gcm_key"] = ""
}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
- mc.Hmset("devices:"+device.ID.String(), changes)
- mc.Zadd("users:"+device.UserID.String()+":devices", device.LastSeen.Unix(), device.ID.String())
+ mc.Hmset("devices:"+strconv.FormatUint(device.ID, 10), changes)
+ mc.Zadd("users:"+strconv.FormatUint(device.UserID, 10)+":devices", device.LastSeen.Unix(), device.ID)
})
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- r.AuditMap("devices:"+device.ID.String(), from, changes)
+ r.AuditMap("devices:"+strconv.FormatUint(device.ID, 10), from, changes)
// add repo call to instrumentation
// stop instrumentation
return nil
@@ -369,7 +368,7 @@ func (r *RequestBundle) UpdateDevice(device Device, name, client_type, gcm_key s
func (r *RequestBundle) UpdateDeviceLastSeen(device Device, ip string) (Device, error) {
now := time.Now()
- reply := r.Repo.client.Hmset("devices:"+device.ID.String(), "last_seen", now.Format(time.RFC3339), "last_ip", ip)
+ reply := r.Repo.client.Hmset("devices:"+strconv.FormatUint(device.ID, 10), "last_seen", now.Format(time.RFC3339), "last_ip", ip)
// add repo call to instrumentation
if reply.Err != nil {
return Device{}, reply.Err
@@ -384,7 +383,7 @@ func (r *RequestBundle) UpdateDeviceLastSeen(device Device, ip string) (Device,
"last_seen": now.Format(time.RFC3339),
"last_ip": ip,
}
- r.AuditMap("devices:"+device.ID.String(), from, to)
+ r.AuditMap("devices:"+strconv.FormatUint(device.ID, 10), from, to)
// add repo call to instrumentation
// stop instrumentation
return device, nil
@@ -414,13 +413,13 @@ func (r *RequestBundle) updateDevicePusherLastUsed(device Device, pusher string)
was = device.Pushers.WebSockets.LastUsed
}
}
- reply := r.Repo.client.Hset("devices:"+device.ID.String(), pusher+"_last_used", now.Format(time.RFC3339))
+ reply := r.Repo.client.Hset("devices:"+strconv.FormatUint(device.ID, 10), pusher+"_last_used", now.Format(time.RFC3339))
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- r.Audit("devices:"+device.ID.String(), pusher+"_last_used", was.Format(time.RFC3339), now.Format(time.RFC3339))
+ r.Audit("devices:"+strconv.FormatUint(device.ID, 10), pusher+"_last_used", was.Format(time.RFC3339), now.Format(time.RFC3339))
// add repo call to instrumentation
// stop instrumentation
return nil
@@ -428,19 +427,19 @@ func (r *RequestBundle) updateDevicePusherLastUsed(device Device, pusher string)
func (r *RequestBundle) updateAuthErrorFlag(value bool) error {
// start instrumetnation
- if r.Device.ID == ruid.RUID(0) {
+ if r.Device.ID == 0 {
return nil
}
if r.Device.AuthError == value {
return nil
}
- reply := r.Repo.client.Hset("devices:"+r.Device.ID.String(), "auth_error", value)
+ reply := r.Repo.client.Hset("devices:"+strconv.FormatUint(r.Device.ID, 10), "auth_error", value)
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- r.Audit("devices:"+r.Device.ID.String(), "auth_error", strconv.FormatBool(r.Device.AuthError), strconv.FormatBool(value))
+ r.Audit("devices:"+strconv.FormatUint(r.Device.ID, 10), "auth_error", strconv.FormatBool(r.Device.AuthError), strconv.FormatBool(value))
// add repo call to instrumentation
// stop instrumentation
return nil
View
121 link.go
@@ -3,19 +3,19 @@ package twocloud
import (
"github.com/PuerkitoBio/purell"
"github.com/fzzbt/radix/redis"
- "secondbit.org/ruid"
+ "strconv"
"time"
)
type URL struct {
- ID ruid.RUID `json:"id,omitempty"`
+ ID uint64 `json:"id,omitempty"`
FirstSeen time.Time `json:"first_seen,omitempty"`
SentCounter int64 `json:"sent_counter,omitempty"`
Address string `json:"address,omitempty"`
}
type Link struct {
- ID ruid.RUID `json:"id,omitempty"`
+ ID uint64 `json:"id,omitempty"`
URL *URL `json:"url,omitempty"`
Unread bool `json:"unread,omitempty"`
TimeRead time.Time `json:"time_read,omitempty"`
@@ -33,24 +33,24 @@ const (
RoleReceiver
)
-func (r *RequestBundle) GetLinksByDevice(device Device, role RoleFlag, before, after ruid.RUID, count int) ([]Link, error) {
+func (r *RequestBundle) GetLinksByDevice(device Device, role RoleFlag, before, after uint64, count int) ([]Link, error) {
return []Link{}, nil
}
-func (r *RequestBundle) GetLinksByUser(user User, role RoleFlag, before, after ruid.RUID, count int) ([]Link, error) {
+func (r *RequestBundle) GetLinksByUser(user User, role RoleFlag, before, after uint64, count int) ([]Link, error) {
return []Link{}, nil
}
-func (r *RequestBundle) GetLink(id ruid.RUID) (Link, error) {
+func (r *RequestBundle) GetLink(id uint64) (Link, error) {
return Link{}, nil
}
func (r *RequestBundle) AddLinks(links []Link) ([]Link, error) {
urls := []*URL{}
- url_counts := map[ruid.RUID]int{}
+ url_counts := map[uint64]int{}
reservedAddress := []string{}
for pos, link := range links {
- id, err := gen.Generate([]byte(link.URL.Address))
+ id, err := r.GetID()
if err != nil {
r.Log.Error(err.Error())
for _, a := range reservedAddress {
@@ -92,7 +92,7 @@ func (r *RequestBundle) AddLinks(links []Link) ([]Link, error) {
link.URL.ID = newID
}
url_counts[link.URL.ID] = url_counts[link.URL.ID] + 1
- linkID, err := gen.Generate([]byte(link.URL.Address))
+ linkID, err := r.GetID()
if err != nil {
r.Log.Error(err.Error())
for _, a := range reservedAddress {
@@ -158,7 +158,7 @@ func (r *RequestBundle) AddLink(address, comment string, sender, receiver Device
}
func (r *RequestBundle) storeURLs(urls []*URL) error {
- auditlog := map[ruid.RUID]map[string]interface{}{}
+ auditlog := map[uint64]map[string]interface{}{}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
for _, url := range urls {
if url == nil {
@@ -169,7 +169,7 @@ func (r *RequestBundle) storeURLs(urls []*URL) error {
"sent_counter": 0,
"address": url.Address,
}
- mc.Hmset("urls:"+url.ID.String(), changes)
+ mc.Hmset("urls:"+strconv.FormatUint(url.ID, 10), changes)
auditlog[url.ID] = changes
}
})
@@ -179,12 +179,12 @@ func (r *RequestBundle) storeURLs(urls []*URL) error {
return reply.Err
}
from := map[string]interface{}{
- "first_seen": "",
+ "first_seen": "",
"sent_counter": "",
- "address": "",
+ "address": "",
}
for id, audit := range auditlog {
- r.AuditMap("urls:"+id.String(), audit, from)
+ r.AuditMap("urls:"+strconv.FormatUint(id, 10), audit, from)
}
// add repo calls to instrumentation
// stop instrumentation
@@ -194,12 +194,12 @@ func (r *RequestBundle) storeURLs(urls []*URL) error {
func (r *RequestBundle) storeLinks(links []Link, update bool) error {
// start instrumentation
if update {
- changes := map[ruid.RUID]map[string]interface{}{}
- from := map[ruid.RUID]map[string]interface{}{}
- linksFromID := map[ruid.RUID]Link{}
+ changes := map[uint64]map[string]interface{}{}
+ from := map[uint64]map[string]interface{}{}
+ linksFromID := map[uint64]Link{}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
for _, link := range links {
- mc.Hgetall("links:" + link.ID.String())
+ mc.Hgetall("links:" + strconv.FormatUint(link.ID, 10))
linksFromID[link.ID] = link
}
})
@@ -239,9 +239,9 @@ func (r *RequestBundle) storeLinks(links []Link, update bool) error {
}
reply = r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
for id, values := range changes {
- mc.Hmset("links:"+id.String(), values)
+ mc.Hmset("links:"+strconv.FormatUint(id, 10), values)
if unread, set := values["unread"]; set && !unread.(bool) {
- mc.Lrem("devices:"+linksFromID[id].Receiver.ID.String()+":links:unread", 0, id.String())
+ mc.Lrem("devices:"+strconv.FormatUint(linksFromID[id].Receiver.ID, 10)+":links:unread", 0, id)
}
}
})
@@ -251,39 +251,39 @@ func (r *RequestBundle) storeLinks(links []Link, update bool) error {
return reply.Err
}
for id, _ := range changes {
- r.AuditMap("links:"+id.String(), from[id], changes[id])
+ r.AuditMap("links:"+strconv.FormatUint(id, 10), from[id], changes[id])
}
// add repo calls to instrumentation
return nil
}
- changes := map[ruid.RUID]map[string]interface{}{}
- senders := map[ruid.RUID][]string{}
- receivers := map[ruid.RUID][]string{}
- unread := map[ruid.RUID][]string{}
- deviceIDs := map[ruid.RUID]string{}
- requestOrder := []ruid.RUID{}
+ changes := map[uint64]map[string]interface{}{}
+ senders := map[uint64][]uint64{}
+ receivers := map[uint64][]uint64{}
+ unread := map[uint64][]uint64{}
+ deviceIDs := map[uint64]uint64{}
+ requestOrder := []uint64{}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
for _, link := range links {
values := map[string]interface{}{
"unread": link.Unread,
"time_read": link.TimeRead.Format(time.RFC3339),
- "sender": link.Sender.ID.String(),
- "receiver": link.Receiver.ID.String(),
+ "sender": link.Sender.ID,
+ "receiver": link.Receiver.ID,
"comment": link.Comment,
"sent": link.Sent.Format(time.RFC3339),
}
if link.URL != nil {
- values["url"] = link.URL.ID.String()
+ values["url"] = link.URL.ID
}
changes[link.ID] = values
- mc.Hmset("links:"+link.ID.String(), values)
- senders[link.Sender.ID] = append(senders[link.Sender.ID], link.ID.String())
- receivers[link.Receiver.ID] = append(receivers[link.Receiver.ID], link.ID.String())
+ mc.Hmset("links:"+strconv.FormatUint(link.ID, 10), values)
+ senders[link.Sender.ID] = append(senders[link.Sender.ID], link.ID)
+ receivers[link.Receiver.ID] = append(receivers[link.Receiver.ID], link.ID)
if link.Unread {
- unread[link.Receiver.ID] = append(unread[link.Receiver.ID], link.ID.String())
+ unread[link.Receiver.ID] = append(unread[link.Receiver.ID], link.ID)
}
- deviceIDs[link.Sender.ID] = ""
- deviceIDs[link.Receiver.ID] = ""
+ deviceIDs[link.Sender.ID] = 0
+ deviceIDs[link.Receiver.ID] = 0
}
})
// add repo call to instrumentation
@@ -293,7 +293,7 @@ func (r *RequestBundle) storeLinks(links []Link, update bool) error {
}
reply = r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
for id, _ := range deviceIDs {
- mc.Hget("devices:"+id.String(), "user_id")
+ mc.Hget("devices:"+strconv.FormatUint(id, 10), "user_id")
requestOrder = append(requestOrder, id)
}
})
@@ -303,25 +303,30 @@ func (r *RequestBundle) storeLinks(links []Link, update bool) error {
return reply.Err
}
for pos, el := range reply.Elems {
- user_id, err := el.Str()
+ user_id_str, err := el.Str()
if err != nil {
- r.Log.Error(reply.Err.Error())
+ r.Log.Error(err.Error())
+ continue
+ }
+ user_id, err := strconv.ParseUint(user_id_str, 10, 64)
+ if err != nil {
+ r.Log.Error(err.Error())
continue
}
deviceIDs[requestOrder[pos]] = user_id
}
reply = r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
for deviceID, linkIDs := range senders {
- mc.Lpush("devices:"+deviceID.String()+":links:sent", linkIDs)
- mc.Lpush("users:"+deviceIDs[deviceID]+":links:sent", linkIDs)
+ mc.Lpush("devices:"+strconv.FormatUint(deviceID, 10)+":links:sent", linkIDs)
+ mc.Lpush("users:"+strconv.FormatUint(deviceIDs[deviceID], 10)+":links:sent", linkIDs)
}
for deviceID, linkIDs := range unread {
- mc.Lpush("devices:"+deviceID.String()+":links:unread", linkIDs)
- mc.Lpush("users:"+deviceIDs[deviceID]+":links:unread", linkIDs)
+ mc.Lpush("devices:"+strconv.FormatUint(deviceID, 10)+":links:unread", linkIDs)
+ mc.Lpush("users:"+strconv.FormatUint(deviceIDs[deviceID], 10)+":links:unread", linkIDs)
}
for deviceID, linkIDs := range receivers {
- mc.Lpush("devices:"+deviceID.String()+":links:received", linkIDs)
- mc.Lpush("users:"+deviceIDs[deviceID]+":links:received", linkIDs)
+ mc.Lpush("devices:"+strconv.FormatUint(deviceID, 10)+":links:received", linkIDs)
+ mc.Lpush("users:"+strconv.FormatUint(deviceIDs[deviceID], 10)+":links:received", linkIDs)
}
})
// add repo call to instrumentation
@@ -339,41 +344,41 @@ func (r *RequestBundle) storeLinks(links []Link, update bool) error {
"url": "",
}
for id, _ := range changes {
- r.AuditMap("links:"+id.String(), from, changes[id])
+ r.AuditMap("links:"+strconv.FormatUint(id, 10), from, changes[id])
}
// add repo calls to instrumentation
return nil
}
-func (r *RequestBundle) getIDFromAddress(address string) (ruid.RUID, error) {
+func (r *RequestBundle) getIDFromAddress(address string) (uint64, error) {
// start instrumentation
var err error
address, err = purell.NormalizeURLString(address, purell.FlagsSafe)
if err != nil {
r.Log.Error(err.Error())
- return ruid.RUID(0), err
+ return uint64(0), err
}
reply := r.Repo.client.Hget("urls_to_ids", address)
// report repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
- return ruid.RUID(0), reply.Err
+ return uint64(0), reply.Err
}
idstr, err := reply.Str()
if err != nil {
r.Log.Error(err.Error())
- return ruid.RUID(0), err
+ return uint64(0), err
}
- id, err := ruid.RUIDFromString(idstr)
+ id, err := strconv.ParseUint(idstr, 10, 64)
if err != nil {
r.Log.Error(err.Error())
- return ruid.RUID(0), err
+ return uint64(0), err
}
return id, nil
// stop instrumentation
}
-func (r *RequestBundle) reserveAddress(address string, id ruid.RUID) (bool, error) {
+func (r *RequestBundle) reserveAddress(address string, id uint64) (bool, error) {
// start instrumentation
var err error
address, err = purell.NormalizeURLString(address, purell.FlagsSafe)
@@ -381,13 +386,13 @@ func (r *RequestBundle) reserveAddress(address string, id ruid.RUID) (bool, erro
r.Log.Error(err.Error())
return false, err
}
- reply := r.Repo.client.Hsetnx("urls_to_ids", address, id.String())
+ reply := r.Repo.client.Hsetnx("urls_to_ids", address, id)
// report repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return false, reply.Err
}
- r.Audit("urls_to_ids", address, "", id.String())
+ r.Audit("urls_to_ids", address, "", strconv.FormatUint(id, 10))
// report repo calls to instrumentation
// stop instrumentation
return reply.Bool()
@@ -427,9 +432,9 @@ func (r *RequestBundle) releaseAddress(address string) error {
return nil
}
-func (r *RequestBundle) incrementURL(id ruid.RUID, count int) error {
- r.Log.Debug("About to increment urls:%s by %d", id.String(), count)
- reply := r.Repo.client.Hincrby("urls:"+id.String(), "sent_counter", count)
+func (r *RequestBundle) incrementURL(id uint64, count int) error {
+ r.Log.Debug("About to increment urls:%d by %d", id, count)
+ reply := r.Repo.client.Hincrby("urls:"+strconv.FormatUint(id, 10), "sent_counter", count)
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
View
13 notification.go
@@ -2,19 +2,18 @@ package twocloud
import (
"errors"
- "secondbit.org/ruid"
"time"
)
type Notification struct {
- ID ruid.RUID `json:"id,omitempty"`
+ ID uint64 `json:"id,omitempty"`
Nature string `json:"nature,omitempty"`
Body string `json:"body,omitempty"`
Unread bool `json:"unread,omitempty"`
- ReadBy ruid.RUID `json:"read_by,omitempty"`
+ ReadBy uint64 `json:"read_by,omitempty"`
TimeRead time.Time `json:"time_read,omitempty"`
Sent time.Time `json:"sent,omitempty"`
- Destination ruid.RUID `json:"owner,omitempty"`
+ Destination uint64 `json:"owner,omitempty"`
DestinationType string `json:"owner_type,omitempty"`
}
@@ -40,15 +39,15 @@ func (b *BroadcastFilter) IsValid() bool {
var InvalidBroadcastFilter = errors.New("Invalid broadcast filter.")
-func (r *RequestBundle) GetNotificationsByDevice(device Device, before, after ruid.RUID, count int) ([]Notification, error) {
+func (r *RequestBundle) GetNotificationsByDevice(device Device, before, after uint64, count int) ([]Notification, error) {
return []Notification{}, nil
}
-func (r *RequestBundle) GetNotificationsByUser(user User, before, after ruid.RUID, count int) ([]Notification, error) {
+func (r *RequestBundle) GetNotificationsByUser(user User, before, after uint64, count int) ([]Notification, error) {
return []Notification{}, nil
}
-func (r *RequestBundle) GetNotification(id ruid.RUID) (Notification, error) {
+func (r *RequestBundle) GetNotification(id uint64) (Notification, error) {
return Notification{}, nil
}
View
34 twocloud.go
@@ -1,26 +1,15 @@
package twocloud
import (
+ "github.com/noeq/noeq"
"net/http"
- "secondbit.org/ruid"
- "time"
)
-var gen *ruid.Generator
-
-func init() {
- location, err := time.LoadLocation("America/New_York")
- if err != nil {
- panic(err.Error())
- }
- epoch := time.Date(2010, time.December, 2, 0, 0, 0, 0, location)
- gen = ruid.NewGenerator(epoch)
-}
-
type RequestBundle struct {
- Repo *Radix
- Config Config
- Log *Log
+ Generator *noeq.Client
+ Repo *Radix
+ Config Config
+ Log *Log
// Cache
Auditor *Auditor
// Instrumentor
@@ -29,3 +18,16 @@ type RequestBundle struct {
AuthUser User
Device Device
}
+
+func (rb *RequestBundle) GetID() (id uint64, err error) {
+ for trys := 5; trys > 0; trys-- {
+ id, err = rb.Generator.GenOne()
+ if err != nil {
+ rb.Log.Error(err.Error())
+ continue
+ }
+ return
+ }
+ rb.Log.Error("No ID generated.")
+ return
+}
View
76 user.go
@@ -7,7 +7,7 @@ import (
"github.com/fzzbt/radix/redis"
"io"
"math/rand"
- "secondbit.org/ruid"
+ "strconv"
"strings"
"time"
)
@@ -18,7 +18,7 @@ type Name struct {
}
type User struct {
- ID ruid.RUID `json:"id,omitempty"`
+ ID uint64 `json:"id,omitempty"`
Username string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
EmailUnconfirmed bool `json:"email_unconfirmed,omitempty"`
@@ -169,11 +169,11 @@ func (r *RequestBundle) Authenticate(username, secret string) (User, error) {
return user, subscriptionError
}
-func (r *RequestBundle) updateUserLastActive(id ruid.RUID) error {
+func (r *RequestBundle) updateUserLastActive(id uint64) error {
// start instrumentation
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
- mc.Hset("users:"+id.String(), "last_active", time.Now().Format(time.RFC3339))
- mc.Zadd("users_by_last_active", time.Now().Unix(), id.String())
+ mc.Hset("users:"+strconv.FormatUint(id, 10), "last_active", time.Now().Format(time.RFC3339))
+ mc.Zadd("users_by_last_active", time.Now().Unix(), id)
})
// report repo call to instrumentation
if reply.Err != nil {
@@ -198,7 +198,7 @@ func (r *RequestBundle) Register(username, email, given_name, family_name string
if email == "" {
return User{}, MissingEmailError
}
- id, err := gen.Generate([]byte(username))
+ id, err := r.GetID()
if err != nil {
r.Log.Error(err.Error())
return User{}, err
@@ -259,15 +259,15 @@ func (r *RequestBundle) Register(username, email, given_name, family_name string
return user, nil
}
-func (r *RequestBundle) reserveUsername(username string, id ruid.RUID) (bool, error) {
+func (r *RequestBundle) reserveUsername(username string, id uint64) (bool, error) {
// start instrumentation
- reply := r.Repo.client.Hsetnx("usernames_to_ids", strings.ToLower(username), id.String())
+ reply := r.Repo.client.Hsetnx("usernames_to_ids", strings.ToLower(username), id)
// report repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return false, reply.Err
}
- r.Audit("usernames_to_ids", strings.ToLower(username), "", id.String())
+ r.Audit("usernames_to_ids", strings.ToLower(username), "", strconv.FormatUint(id, 10))
// report repo calls to instrumentation
// stop instrumentation
return reply.Bool()
@@ -336,14 +336,14 @@ func (r *RequestBundle) storeUser(user User, update bool) error {
from["given_name"] = old_user.Name.Given
}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
- mc.Hmset("users:"+user.ID.String(), changes)
+ mc.Hmset("users:"+strconv.FormatUint(user.ID, 10), changes)
})
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- r.AuditMap("users:"+user.ID.String(), from, changes)
+ r.AuditMap("users:"+strconv.FormatUint(user.ID, 10), from, changes)
// add repo call to instrumentation
return nil
}
@@ -376,24 +376,24 @@ func (r *RequestBundle) storeUser(user User, update bool) error {
"subscription_expires": "",
}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
- mc.Hmset("users:"+user.ID.String(), changes)
- mc.Zadd("users_by_join_date", user.Joined.Unix(), user.ID.String())
- mc.Zadd("users_by_subscription_expiration", user.Subscription.Expires.Unix(), user.ID.String())
+ mc.Hmset("users:"+strconv.FormatUint(user.ID, 10), changes)
+ mc.Zadd("users_by_join_date", user.Joined.Unix(), user.ID)
+ mc.Zadd("users_by_subscription_expiration", user.Subscription.Expires.Unix(), user.ID)
})
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- r.AuditMap("users:"+user.ID.String(), from, changes)
+ r.AuditMap("users:"+strconv.FormatUint(user.ID, 10), from, changes)
// add repo call to instrumentation
// stop instrumentation
return nil
}
-func (r *RequestBundle) GetUser(id ruid.RUID) (User, error) {
+func (r *RequestBundle) GetUser(id uint64) (User, error) {
// start instrumentation
- reply := r.Repo.client.Hgetall("users:" + id.String())
+ reply := r.Repo.client.Hgetall("users:" + strconv.FormatUint(id, 10))
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
@@ -446,7 +446,7 @@ func (r *RequestBundle) GetUser(id ruid.RUID) (User, error) {
return user, nil
}
-func (r *RequestBundle) GetUserID(username string) (ruid.RUID, error) {
+func (r *RequestBundle) GetUserID(username string) (uint64, error) {
var idstr string
var err error
// start instrumentation
@@ -457,22 +457,22 @@ func (r *RequestBundle) GetUserID(username string) (ruid.RUID, error) {
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
- return ruid.RUID(0), reply.Err
+ return uint64(0), reply.Err
}
if reply.Type == redis.ReplyNil {
- return ruid.RUID(0), UserNotFoundError
+ return uint64(0), UserNotFoundError
}
idstr, err = reply.Str()
if err != nil {
r.Log.Error(err.Error())
- return ruid.RUID(0), err
+ return uint64(0), err
}
// cache the user id
// add cache request to instrumentation
- id, err := ruid.RUIDFromString(idstr)
+ id, err := strconv.ParseUint(idstr, 10, 64)
if err != nil {
r.Log.Error(err.Error())
- return ruid.RUID(0), err
+ return uint64(0), err
}
// stop instrumentation
return id, nil
@@ -565,7 +565,7 @@ func (r *RequestBundle) GetUsersByActivity(count int, active_after, active_befor
r.Log.Error(err.Error())
return []User{}, err
}
- id, err := ruid.RUIDFromString(list[pos])
+ id, err := strconv.ParseUint(list[pos], 10, 64)
if err != nil {
r.Log.Error(err.Error())
continue
@@ -683,7 +683,7 @@ func (r *RequestBundle) GetUsersByJoinDate(count int, after, before time.Time) (
r.Log.Error(err.Error())
return []User{}, err
}
- id, err := ruid.RUIDFromString(list[pos])
+ id, err := strconv.ParseUint(list[pos], 10, 64)
if err != nil {
r.Log.Error(err.Error())
continue
@@ -821,7 +821,7 @@ func (r *RequestBundle) CreateTempCredentials(user User) ([2]string, error) {
cred2 = tmpcred1
}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
- mc.Set("tokens:"+cred1+":"+cred2, user.ID.String())
+ mc.Set("tokens:"+cred1+":"+cred2, user.ID)
mc.Expire("tokens:"+cred1+":"+cred2, "300")
})
// add the repo request to instrumentation
@@ -833,12 +833,12 @@ func (r *RequestBundle) CreateTempCredentials(user User) ([2]string, error) {
return [2]string{"", ""}, rep.Err
}
}
- r.Audit("tokens:"+user.ID.String(), cred1, "", cred2)
+ r.Audit("tokens:"+strconv.FormatUint(user.ID, 10), cred1, "", cred2)
// add the repo requests to instrumentation
return [2]string{cred1, cred2}, nil
}
-func (r *RequestBundle) CheckTempCredentials(cred1, cred2 string) (ruid.RUID, error) {
+func (r *RequestBundle) CheckTempCredentials(cred1, cred2 string) (uint64, error) {
// start instrumentation
firstcred := cred1
secondcred := cred2
@@ -850,22 +850,22 @@ func (r *RequestBundle) CheckTempCredentials(cred1, cred2 string) (ruid.RUID, er
// add the repo request to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
- return ruid.RUID(0), reply.Err
+ return uint64(0), reply.Err
}
if reply.Type == redis.ReplyNil {
// add invalid credential error to stats
// add the repo requests to instrumentation
- return ruid.RUID(0), InvalidCredentialsError
+ return uint64(0), InvalidCredentialsError
}
val, err := reply.Str()
if err != nil {
r.Log.Error(err.Error())
- return ruid.RUID(0), err
+ return uint64(0), err
}
- id, err := ruid.RUIDFromString(val)
+ id, err := strconv.ParseUint(val, 10, 64)
if err != nil {
r.Log.Error(err.Error())
- return ruid.RUID(0), err
+ return uint64(0), err
}
return id, nil
// stop instrumentation
@@ -974,7 +974,7 @@ func (r *RequestBundle) CancelSubscription(user User) error {
return nil
}
-func (r *RequestBundle) storeSubscription(userID ruid.RUID, subscription *Subscription) error {
+func (r *RequestBundle) storeSubscription(userID uint64, subscription *Subscription) error {
// start instrumentation
changes := map[string]interface{}{}
from := map[string]interface{}{}
@@ -993,19 +993,19 @@ func (r *RequestBundle) storeSubscription(userID ruid.RUID, subscription *Subscr
from["subscription_id"] = old_sub.ID
}
reply := r.Repo.client.MultiCall(func(mc *redis.MultiCall) {
- mc.Hmset("users:"+userID.String(), changes)
- mc.Zadd("users_by_subscription_expiration", subscription.Expires.Unix(), userID.String())
+ mc.Hmset("users:"+strconv.FormatUint(userID, 10), changes)
+ mc.Zadd("users_by_subscription_expiration", subscription.Expires.Unix(), userID)
})
// add repo call to instrumentation
if reply.Err != nil {
r.Log.Error(reply.Err.Error())
return reply.Err
}
- r.AuditMap("users:"+userID.String(), from, changes)
+ r.AuditMap("users:"+strconv.FormatUint(userID, 10), from, changes)
// stop instrumentation
return nil
}
-func (r *RequestBundle) GetGraceSubscriptions(after, before ruid.RUID, count int) ([]Subscription, error) {
+func (r *RequestBundle) GetGraceSubscriptions(after, before uint64, count int) ([]Subscription, error) {
return []Subscription{}, nil
}

0 comments on commit 42639dd

Please sign in to comment.
Something went wrong with that request. Please try again.