Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to get the last synced timestamp for politeia #208

Merged
merged 10 commits into from
Oct 19, 2021
9 changes: 9 additions & 0 deletions politeia.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Politeia struct {
ctx context.Context
cancelSync context.CancelFunc
client *politeiaClient
lastSyncedTimestamp int64
dreacot marked this conversation as resolved.
Show resolved Hide resolved
dreacot marked this conversation as resolved.
Show resolved Hide resolved
notificationListenersMu sync.RWMutex
notificationListeners map[string]ProposalNotificationListener
}
Expand All @@ -42,6 +43,14 @@ func newPoliteia(mwRef *MultiWallet, host string) (*Politeia, error) {
return p, nil
}

func (p *Politeia) saveLastSyncedTimestamp(lastSyncedTimestamp int64) {
p.mwRef.SetLongConfigValueForKey("lastsyncedtimestamp", lastSyncedTimestamp)
}

func (p *Politeia) getLastSyncedTimestamp() int64 {
return p.mwRef.ReadLongConfigValueForKey("lastsyncedtimestamp", 0)
}

func (p *Politeia) saveOrOverwiteProposal(proposal *Proposal) error {
var oldProposal Proposal
err := p.mwRef.db.One("Token", proposal.Token, &oldProposal)
Expand Down
6 changes: 6 additions & 0 deletions politeia_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,17 @@ func (p *Politeia) Sync() error {
}

log.Info("Politeia sync: update complete")
p.lastSyncedTimestamp = time.Now().Unix()
p.saveLastSyncedTimestamp(p.lastSyncedTimestamp)
dreacot marked this conversation as resolved.
Show resolved Hide resolved
p.publishSynced()
return nil
}
}

func (p *Politeia) GetLastSyncedTimeStamp() int64 {
return p.getLastSyncedTimestamp()
}

func (p *Politeia) IsSyncing() bool {
p.mu.RLock()
defer p.mu.RUnlock()
Expand Down
120 changes: 120 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,123 @@ func initWalletLoader(chainParams *chaincfg.Params, walletDataDir, walletDbDrive

return walletLoader
}

// makePlural is used with the TimeElapsed function. makePlural checks if the arguments passed is > 1,
// if true, it adds "s" after the given time to make it plural
func makePlural(x float64) string {
if int(x) == 1 {
return ""
}
return "s"
}

// TimeElapsed returns the formatted time diffrence between two times as a string.
// If the argument `fullTime` is set to true, then the full time available is returned e.g 3 hours, 2 minutes, 20 seconds ago,
// as opposed to 3 hours ago.
// If the argument `abbreviationFormat` is set to `long` the time format is e.g 2 minutes
// If the argument `abbreviationFormat` is set to `short` the time format is e.g 2 mins
// If the argument `abbreviationFormat` is set to `shortest` the time format is e.g 2 m
func TimeElapsed(now, then time.Time, abbreviationFormat string, fullTime bool) string {
var parts []string
var text string

year2, month2, day2 := now.Date()
hour2, minute2, second2 := now.Clock()

year1, month1, day1 := then.Date()
hour1, minute1, second1 := then.Clock()

year := math.Abs(float64(year2 - year1))
month := math.Abs(float64(month2 - month1))
day := math.Abs(float64(day2 - day1))
hour := math.Abs(float64(hour2 - hour1))
minute := math.Abs(float64(minute2 - minute1))
second := math.Abs(float64(second2 - second1))

week := math.Floor(day / 7)

if year > 0 {
if abbreviationFormat == "long" {
parts = append(parts, strconv.Itoa(int(year))+" year"+makePlural(year))
} else if abbreviationFormat == "short" {
parts = append(parts, strconv.Itoa(int(year))+" yr"+makePlural(year))
} else if abbreviationFormat == "shortest" {
parts = append(parts, strconv.Itoa(int(year))+" y")
}
}

if month > 0 {
if abbreviationFormat == "long" {
parts = append(parts, strconv.Itoa(int(month))+" month"+makePlural(month))
} else if abbreviationFormat == "short" {
parts = append(parts, strconv.Itoa(int(month))+" mon"+makePlural(month))
} else if abbreviationFormat == "shortest" {
parts = append(parts, strconv.Itoa(int(month))+" m")
}
}

if week > 0 {
if abbreviationFormat == "long" {
parts = append(parts, strconv.Itoa(int(week))+" week"+makePlural(week))
} else if abbreviationFormat == "short" {
parts = append(parts, strconv.Itoa(int(week))+" wk"+makePlural(week))
} else if abbreviationFormat == "shortest" {
parts = append(parts, strconv.Itoa(int(week))+" w")
}
}
dreacot marked this conversation as resolved.
Show resolved Hide resolved

if day > 0 {
if abbreviationFormat == "long" {
parts = append(parts, strconv.Itoa(int(day))+" day"+makePlural(day))
} else if abbreviationFormat == "short" {
parts = append(parts, strconv.Itoa(int(day))+" dy"+makePlural(day))
} else if abbreviationFormat == "shortest" {
parts = append(parts, strconv.Itoa(int(day))+" d")
}
}

if hour > 0 {
if abbreviationFormat == "long" {
parts = append(parts, strconv.Itoa(int(hour))+" hour"+makePlural(hour))
} else if abbreviationFormat == "short" {
parts = append(parts, strconv.Itoa(int(hour))+" hr"+makePlural(hour))
} else if abbreviationFormat == "shortest" {
parts = append(parts, strconv.Itoa(int(hour))+" h")
}
}

if minute > 0 {
if abbreviationFormat == "long" {
parts = append(parts, strconv.Itoa(int(minute))+" minute"+makePlural(minute))
} else if abbreviationFormat == "short" {
parts = append(parts, strconv.Itoa(int(minute))+" min"+makePlural(minute))
} else if abbreviationFormat == "shortest" {
parts = append(parts, strconv.Itoa(int(minute))+" mi")
}
}

if second > 0 {
if abbreviationFormat == "long" {
parts = append(parts, strconv.Itoa(int(second))+" second"+makePlural(second))
} else if abbreviationFormat == "short" {
parts = append(parts, strconv.Itoa(int(second))+" sec"+makePlural(second))
} else if abbreviationFormat == "shortest" {
parts = append(parts, strconv.Itoa(int(second))+" s")
}
}

if now.After(then) {
text = " ago"
} else {
text = " after"
}

if len(parts) == 0 {
return "just now"
}

if fullTime {
return strings.Join(parts, ", ") + text
}
return parts[0] + text
}