3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

Merge pull request #837 from slingamn/issue836_dm_cutoff.1

fix #836
This commit is contained in:
Shivaram Lingamneni 2020-03-01 01:44:41 -08:00 committed by GitHub
commit b0cd079790
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 5 deletions

View File

@ -378,7 +378,7 @@ func (am *AccountManager) Register(client *Client, account string, callbackNames
return err return err
} }
registeredTimeStr := strconv.FormatInt(time.Now().Unix(), 10) registeredTimeStr := strconv.FormatInt(time.Now().UnixNano(), 10)
callbackSpec := fmt.Sprintf("%s:%s", callbackNamespace, callbackValue) callbackSpec := fmt.Sprintf("%s:%s", callbackNamespace, callbackValue)
var setOptions *buntdb.SetOptions var setOptions *buntdb.SetOptions
@ -998,7 +998,7 @@ func (am *AccountManager) deserializeRawAccount(raw rawClientAccount, cfName str
result.Name = raw.Name result.Name = raw.Name
result.NameCasefolded = cfName result.NameCasefolded = cfName
regTimeInt, _ := strconv.ParseInt(raw.RegisteredAt, 10, 64) regTimeInt, _ := strconv.ParseInt(raw.RegisteredAt, 10, 64)
result.RegisteredAt = time.Unix(regTimeInt, 0).UTC() result.RegisteredAt = time.Unix(0, regTimeInt).UTC()
e := json.Unmarshal([]byte(raw.Credentials), &result.Credentials) e := json.Unmarshal([]byte(raw.Credentials), &result.Credentials)
if e != nil { if e != nil {
am.server.logger.Error("internal", "could not unmarshal credentials", e.Error()) am.server.logger.Error("internal", "could not unmarshal credentials", e.Error())

View File

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
@ -22,7 +23,7 @@ const (
// 'version' of the database schema // 'version' of the database schema
keySchemaVersion = "db.version" keySchemaVersion = "db.version"
// latest schema of the db // latest schema of the db
latestDbSchema = "9" latestDbSchema = "10"
) )
type SchemaChanger func(*Config, *buntdb.Tx) error type SchemaChanger func(*Config, *buntdb.Tx) error
@ -594,6 +595,32 @@ func schemaChangeV8ToV9(config *Config, tx *buntdb.Tx) error {
return nil return nil
} }
// #836: account registration time at nanosecond resolution
// (mostly to simplify testing)
func schemaChangeV9ToV10(config *Config, tx *buntdb.Tx) error {
prefix := "account.registered.time "
var accounts, times []string
tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
if !strings.HasPrefix(key, prefix) {
return false
}
account := strings.TrimPrefix(key, prefix)
accounts = append(accounts, account)
times = append(times, value)
return true
})
for i, account := range accounts {
time, err := strconv.ParseInt(times[i], 10, 64)
if err != nil {
log.Printf("corrupt registration time entry for %s: %v\n", account, err)
continue
}
time = time * 1000000000
tx.Set(prefix+account, strconv.FormatInt(time, 10), nil)
}
return nil
}
func init() { func init() {
allChanges := []SchemaChange{ allChanges := []SchemaChange{
{ {
@ -636,6 +663,11 @@ func init() {
TargetVersion: "9", TargetVersion: "9",
Changer: schemaChangeV8ToV9, Changer: schemaChangeV8ToV9,
}, },
{
InitialVersion: "9",
TargetVersion: "10",
Changer: schemaChangeV9ToV10,
},
} }
// build the index // build the index

View File

@ -919,14 +919,16 @@ func (server *Server) GetHistorySequence(providedChannel *Channel, client *Clien
if config.History.Restrictions.ExpireTime != 0 { if config.History.Restrictions.ExpireTime != 0 {
cutoff = time.Now().UTC().Add(-time.Duration(config.History.Restrictions.ExpireTime)) cutoff = time.Now().UTC().Add(-time.Duration(config.History.Restrictions.ExpireTime))
} }
if config.History.Restrictions.EnforceRegistrationDate { // #836: registration date cutoff is always enforced for DMs
if config.History.Restrictions.EnforceRegistrationDate || channel == nil {
regCutoff := client.historyCutoff() regCutoff := client.historyCutoff()
// take the later of the two cutoffs // take the later of the two cutoffs
if regCutoff.After(cutoff) { if regCutoff.After(cutoff) {
cutoff = regCutoff cutoff = regCutoff
} }
} }
if !cutoff.IsZero() { // #836 again: grace period is never applied to DMs
if !cutoff.IsZero() && channel != nil {
cutoff = cutoff.Add(-time.Duration(config.History.Restrictions.GracePeriod)) cutoff = cutoff.Add(-time.Duration(config.History.Restrictions.GracePeriod))
} }