608 lines
23 KiB
Go
608 lines
23 KiB
Go
// Code generated by zi2go from sync_http.zi. DO NOT EDIT.
|
|
package main
|
|
|
|
import (
|
|
slog "log/slog"
|
|
strconv "strconv"
|
|
strings "strings"
|
|
)
|
|
|
|
// #import std_go_types
|
|
// #import protocol
|
|
// #import sync_types
|
|
// #import transaction
|
|
type Synchronization struct {
|
|
Database *Database
|
|
Configuration *Config
|
|
Counters *ServerMetrics
|
|
Notifications *SyncHub
|
|
Verify VerifySignature
|
|
ReplayError Error
|
|
MissingUser Error
|
|
}
|
|
|
|
type SyncCompletion struct {
|
|
Complete bool
|
|
Signed bool
|
|
Transaction SignedTxEnvelope
|
|
}
|
|
|
|
type zir_88e9bac24ed1cd0d_FinishCallback func(*SyncCompletion, Synchronization, *Request)
|
|
|
|
type zir_f42dd54e2a1e2480_IntegerResult struct {
|
|
Value int64
|
|
Error Error
|
|
}
|
|
|
|
func SyncHttp_FinishAtReturn(Callback zir_88e9bac24ed1cd0d_FinishCallback, Completion *SyncCompletion, Sync Synchronization, Request *Request) {
|
|
Callback(Completion, Sync, Request)
|
|
}
|
|
|
|
func SyncHttp_ParseInteger(Value string, Base int, Bits int) zir_f42dd54e2a1e2480_IntegerResult {
|
|
var result zir_f42dd54e2a1e2480_IntegerResult
|
|
result.Value, result.Error = strconv.ParseInt(Value, Base, Bits)
|
|
return result
|
|
}
|
|
|
|
func SyncHttp_Finish(completion *SyncCompletion, sync Synchronization, request *Request) {
|
|
if !completion.Complete {
|
|
StdAtomicGo_Add(&(sync.Counters.SyncFailures), 1)
|
|
if completion.Signed {
|
|
var value_0 *Database = sync.Database
|
|
var value_1 Context = StdHttpGo_Context(request)
|
|
SignedTx_Forget(value_0, value_1, completion.Transaction)
|
|
}
|
|
}
|
|
}
|
|
|
|
func SyncHttp_StorageFailure(writer ResponseWriter, message string, user string, error Error, publicMessage string) {
|
|
slog.Error(message, "user", LogSafety_LogText(user), "error", error)
|
|
Response_Error(writer, int(500), publicMessage)
|
|
}
|
|
|
|
func SyncHttp_Applied(result SyncResult) bool {
|
|
var value_0 bool = result.MeditationLogs > int(0) || result.Habits > int(0) || result.HabitDays > int(0) || result.Sessions > int(0)
|
|
return value_0 || result.SocialCache > int(0) || result.EncryptedRecords > int(0)
|
|
}
|
|
|
|
func SyncHttp_ChangesResult(changes SyncChanges) SyncResult {
|
|
var result SyncResult = SyncResult{}
|
|
result.MeditationLogs = int(int64(len(changes.MeditationLogs)))
|
|
result.Habits = int(int64(len(changes.Habits)))
|
|
result.HabitDays = int(int64(len(changes.HabitDays)))
|
|
result.Sessions = int(int64(len(changes.Sessions)))
|
|
result.SocialCache = int(int64(len(changes.SocialCache)))
|
|
result.EncryptedRecords = int(int64(len(changes.EncryptedRecords)))
|
|
return result
|
|
}
|
|
|
|
func SyncHttp_ProjectClean(value *SyncResponse, request SyncRequest) {
|
|
var value_0 bool = SyncRequest_IncludeLegacyPrivateData(request)
|
|
if !value_0 {
|
|
var value_1 []Habit = make([]Habit, int(int(0)))
|
|
value.Data.Habits = value_1
|
|
var value_2 []CleanHabitDay = make([]CleanHabitDay, int(int(0)))
|
|
value.Data.HabitDays = value_2
|
|
var value_3 []Session = make([]Session, int(int(0)))
|
|
value.Data.Sessions = value_3
|
|
var value_4 []MeditationLog = make([]MeditationLog, int(int(0)))
|
|
value.Data.MeditationLogs = value_4
|
|
}
|
|
value.Changes.Habits = value.Data.Habits
|
|
var value_5 []HabitDay = make([]HabitDay, int(int(0)), int(int(int64(len(value.Data.HabitDays)))))
|
|
value.Changes.HabitDays = value_5
|
|
{
|
|
value_6 := value.Data.HabitDays[:]
|
|
if int64(0) < 0 || int64(int64(len(value_6))) < int64(0) || int64(int64(len(value_6))) > int64(len(value_6)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_8 := value_6[0:int64(len(value_6)):int64(len(value_6))]
|
|
loop_count_8 := int64(len(loop_view_8))
|
|
var loop_cursor_8 int64 = 0
|
|
for loop_cursor_8 < loop_count_8 {
|
|
loop_index_8 := loop_cursor_8
|
|
clean := loop_view_8[loop_index_8]
|
|
var day HabitDay = HabitDay{}
|
|
day.HabitID = clean.HabitID
|
|
day.LocalDate = clean.LocalDate
|
|
day.Completed = clean.Completed
|
|
day.Count = clean.Count
|
|
day.UpdatedAt = clean.UpdatedAt
|
|
var value_7 []HabitDay = append(value.Changes.HabitDays, day)
|
|
value.Changes.HabitDays = value_7
|
|
loop_cursor_8++
|
|
}
|
|
}
|
|
value.Changes.Sessions = value.Data.Sessions
|
|
value.Changes.MeditationLogs = value.Data.MeditationLogs
|
|
value.Changes.SocialCache = value.Data.Social
|
|
value.Changes.EncryptedRecords = value.Data.EncryptedRecords
|
|
}
|
|
|
|
func SyncHttp_Handle(sync Synchronization, writer ResponseWriter, request *Request) {
|
|
StdAtomicGo_Add(&(sync.Counters.SyncRequests), 1)
|
|
var completion SyncCompletion = SyncCompletion{}
|
|
var value_3 zir_88e9bac24ed1cd0d_FinishCallback = func(value_0 *SyncCompletion, value_1 Synchronization, value_2 *Request) {
|
|
SyncHttp_Finish(value_0, value_1, value_2)
|
|
}
|
|
defer SyncHttp_FinishAtReturn(value_3, &(completion), sync, request)
|
|
read := HttpBody_ReadJSON(writer, request, sync.Configuration.MaxBodyBytes)
|
|
if read.Error != nil {
|
|
var value_4 ResponseWriter = writer
|
|
Response_Error(value_4, int(400), StdErrorsGo_Message(read.Error))
|
|
return
|
|
}
|
|
if SyncRequest_IsEncryptedEnvelope(read.Value) {
|
|
if SyncHttp_EncryptedEnvelope(sync, writer, request, read.Value) {
|
|
completion.Complete = true
|
|
}
|
|
return
|
|
}
|
|
parsed := SyncRequest_ParseSync(read.Value)
|
|
if parsed.Error != nil {
|
|
var value_5 ResponseWriter = writer
|
|
Response_Error(value_5, int(400), StdErrorsGo_Message(parsed.Error))
|
|
return
|
|
}
|
|
input := parsed.Value
|
|
error := SyncRequest_ApplyHeaderUser(request, &(input.UserIDHash))
|
|
if error != nil {
|
|
var value_6 ResponseWriter = writer
|
|
Response_Error(value_6, int(400), StdErrorsGo_Message(error))
|
|
return
|
|
}
|
|
var value_7 bool = Identity_ValidClientID(input.ClientID)
|
|
if !value_7 {
|
|
Response_Error(writer, int(400), "invalid client_id")
|
|
return
|
|
}
|
|
authenticated := HttpAuth_AuthenticateToken(sync.Database, request, sync.Configuration.TokenSecret)
|
|
if authenticated.Authentication.Error != nil || authenticated.Authentication.Status != int(0) {
|
|
HttpAuth_Respond(writer, sync.Counters, authenticated.Authentication)
|
|
return
|
|
}
|
|
if authenticated.Value != input.UserIDHash {
|
|
Response_Error(writer, int(401), "token user mismatch")
|
|
return
|
|
}
|
|
publicKey := SyncRequest_PublicKey(input)
|
|
if publicKey.Error != nil {
|
|
var value_8 ResponseWriter = writer
|
|
Response_Error(value_8, int(400), StdErrorsGo_Message(publicKey.Error))
|
|
return
|
|
}
|
|
context := StdHttpGo_Context(request)
|
|
error = SyncValidation_Validate(sync.Database, context, input)
|
|
if error != nil {
|
|
var value_9 ResponseWriter = writer
|
|
Response_Error(value_9, int(400), StdErrorsGo_Message(error))
|
|
return
|
|
}
|
|
if input.ProtocolVersion >= int(6) {
|
|
header := SignedTx_ReadHeader(request)
|
|
if header.Authentication.Error != nil || header.Authentication.Status != int(0) {
|
|
HttpAuth_Respond(writer, sync.Counters, header.Authentication)
|
|
return
|
|
}
|
|
verified := SignedTx_Verify(sync.Database, context, request, read.Value, header.Value, input.UserIDHash, input.AppID, sync.Verify, sync.ReplayError)
|
|
if verified.Error != nil || verified.Status != int(0) {
|
|
HttpAuth_Respond(writer, sync.Counters, verified)
|
|
return
|
|
}
|
|
completion.Signed = true
|
|
completion.Transaction = header.Value
|
|
}
|
|
SyncRequest_NormalizeMeditationDurations(input.MeditationLogs)
|
|
base := StateHash_State(sync.Database, context, input.UserIDHash)
|
|
if base.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "hash sync state", input.UserIDHash, base.Error, "state hash failed")
|
|
return
|
|
}
|
|
var result SyncResult = SyncResult{}
|
|
accepted := make([]string, int(int(0)))
|
|
remote := make([]SyncOp, int(int(0)))
|
|
fullSnapshot := false
|
|
completeChanges := true
|
|
sinceVersion := input.SinceServerVersion
|
|
recordedClock := input.ClientClock
|
|
snapshotReason := ""
|
|
var compactedThrough int64 = 0
|
|
apply := true
|
|
var value_10 bool = input.LastServerStateHash != ""
|
|
var value_11 bool = value_10
|
|
if value_11 {
|
|
var value_12 bool = strings.EqualFold(input.LastServerStateHash, base.Value)
|
|
value_11 = !value_12
|
|
}
|
|
if value_11 && !input.FullSyncRequested {
|
|
fullSnapshot = true
|
|
completeChanges = false
|
|
sinceVersion = 0
|
|
snapshotReason = "state_hash_mismatch"
|
|
apply = false
|
|
} else if input.ProtocolVersion >= int(2) && !input.FullSyncRequested {
|
|
compacted := SyncClients_Compacted(sync.Database, context, input.UserIDHash, input.ClientClock)
|
|
if compacted.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "check sync op compaction", input.UserIDHash, compacted.Error, "compaction check failed")
|
|
return
|
|
}
|
|
compactedThrough = compacted.Through
|
|
if compacted.Compacted {
|
|
fullSnapshot = true
|
|
completeChanges = false
|
|
sinceVersion = 0
|
|
snapshotReason = "sync_ops_compacted"
|
|
apply = false
|
|
}
|
|
}
|
|
if apply {
|
|
applied := SyncApplication_Apply(sync.Database, context, input, publicKey.Value, sync.MissingUser)
|
|
_ = applied
|
|
if applied.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "apply sync", input.UserIDHash, applied.Error, "sync failed")
|
|
return
|
|
}
|
|
result = applied.Value
|
|
accepted = applied.Accepted
|
|
}
|
|
var value_13 bool = fullSnapshot
|
|
var value_14 bool = value_13
|
|
if value_14 {
|
|
var value_15 bool = SyncRequest_HasLocalChanges(input)
|
|
value_14 = value_15
|
|
}
|
|
if value_14 {
|
|
applied := SyncApplication_Apply(sync.Database, context, input, publicKey.Value, sync.MissingUser)
|
|
_ = applied
|
|
if applied.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "apply stale sync uploads", input.UserIDHash, applied.Error, "sync failed")
|
|
return
|
|
}
|
|
result = applied.Value
|
|
accepted = applied.Accepted
|
|
}
|
|
if fullSnapshot {
|
|
Metrics_RecordFullSnapshot(sync.Counters, snapshotReason)
|
|
}
|
|
loaded := SyncViews_Changes(sync.Database, context, input.UserIDHash, sinceVersion)
|
|
if loaded.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load sync changes", input.UserIDHash, loaded.Error, "changes failed")
|
|
return
|
|
}
|
|
changes := loaded.Value
|
|
serverVersion := loaded.Version
|
|
social := FriendStore_Authoritative(sync.Database, context, input.UserIDHash)
|
|
if social.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load authoritative social state", input.UserIDHash, social.Error, "social state failed")
|
|
return
|
|
}
|
|
changes.SocialCache = social.Value
|
|
if input.ProtocolVersion >= int(2) {
|
|
if fullSnapshot {
|
|
remote = make([]SyncOp, int(int(0)))
|
|
recordedClock = serverVersion
|
|
} else {
|
|
operations := SyncViews_OperationsSince(sync.Database, context, input.UserIDHash, input.ClientClock)
|
|
if operations.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load sync ops", input.UserIDHash, operations.Error, "ops failed")
|
|
return
|
|
}
|
|
remote = operations.Value
|
|
}
|
|
}
|
|
serverHash := StateHash_State(sync.Database, context, input.UserIDHash)
|
|
if serverHash.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "hash sync response", input.UserIDHash, serverHash.Error, "state hash failed")
|
|
return
|
|
}
|
|
error = SyncClients_RecordSync(sync.Database, context, input.UserIDHash, input.ClientID, input.SinceServerVersion, serverVersion, input.ProtocolVersion, recordedClock)
|
|
if error != nil {
|
|
var value_16 string = LogSafety_LogText(input.UserIDHash)
|
|
var value_17 string = LogSafety_LogText(input.ClientID)
|
|
slog.Error("record sync client", "user", value_16, "client", value_17, "error", error)
|
|
Response_Error(writer, int(500), "client state failed")
|
|
return
|
|
}
|
|
if input.ProtocolVersion >= int(2) {
|
|
error = SyncClients_Compact(sync.Database, context, input.UserIDHash)
|
|
if error != nil {
|
|
SyncHttp_StorageFailure(writer, "compact sync ops", input.UserIDHash, error, "compaction failed")
|
|
return
|
|
}
|
|
}
|
|
if SyncHttp_Applied(result) {
|
|
SyncHub_Publish(sync.Notifications, input.UserIDHash, serverVersion)
|
|
}
|
|
alias := AccountProfile_Alias(sync.Database, context, input.UserIDHash)
|
|
if alias.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load account alias", input.UserIDHash, alias.Error, "alias failed")
|
|
return
|
|
}
|
|
icon := AccountProfile_Icon(sync.Database, context, input.UserIDHash)
|
|
if icon.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load profile icon", input.UserIDHash, icon.Error, "profile icon failed")
|
|
return
|
|
}
|
|
var value SyncResponse = SyncResponse{}
|
|
value.ProtocolVersion = input.ProtocolVersion
|
|
value.Status = "ok"
|
|
var value_18 []string = NodeInfo_Capabilities()
|
|
value.ServerCapabilities = value_18
|
|
var value_19 string = SyncRequest_TransitionMode(input)
|
|
value.TransitionMode = value_19
|
|
value.Applied = result
|
|
value.AccountAlias = alias.Value
|
|
value.ProfileIcon = icon.Value
|
|
value.ServerVersion = serverVersion
|
|
value.ServerClock = serverVersion
|
|
value.ServerStateHash = serverHash.Value
|
|
value.BaseStateHash = base.Value
|
|
value.ChangesComplete = completeChanges
|
|
value.FullSnapshotRequired = fullSnapshot
|
|
value.AcceptedOps = accepted
|
|
value.Ops = remote
|
|
value.Changes = changes
|
|
value.MinSupportedProtocol = 1
|
|
value.LatestProtocol = input.ProtocolVersion
|
|
value.ServerLatestProtocol = 6
|
|
var diagnostics SyncDiagnostics = SyncDiagnostics{}
|
|
diagnostics.SnapshotReason = snapshotReason
|
|
diagnostics.RequestedSinceServerVersion = input.SinceServerVersion
|
|
diagnostics.EffectiveSinceServerVersion = sinceVersion
|
|
diagnostics.ClientClock = input.ClientClock
|
|
diagnostics.CompactedThroughVersion = compactedThrough
|
|
var value_20 bool = SyncRequest_HasLocalChanges(input)
|
|
diagnostics.HasLocalChanges = value_20
|
|
diagnostics.AcceptedOps = int(int64(len(accepted)))
|
|
diagnostics.RemoteOps = int(int64(len(remote)))
|
|
diagnostics.AppliedInput = result
|
|
var value_21 SyncResult = SyncHttp_ChangesResult(changes)
|
|
diagnostics.ReturnedChanges = value_21
|
|
value.Diagnostics = &(diagnostics)
|
|
if input.ProtocolVersion >= int(3) {
|
|
error = HabitMigration_ForProtocol(sync.Database, context, input.UserIDHash, input.ProtocolVersion)
|
|
if error != nil {
|
|
SyncHttp_StorageFailure(writer, "auto migrate account", input.UserIDHash, error, "migration failed")
|
|
return
|
|
}
|
|
current := AccountState_CurrentVersion(sync.Database, context, input.UserIDHash)
|
|
if current.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load migrated server version", input.UserIDHash, current.Error, "version failed")
|
|
return
|
|
}
|
|
migratedHash := StateHash_State(sync.Database, context, input.UserIDHash)
|
|
if migratedHash.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "hash migrated sync response", input.UserIDHash, migratedHash.Error, "state hash failed")
|
|
return
|
|
}
|
|
value.ServerVersion = current.Value
|
|
value.ServerClock = current.Value
|
|
value.ServerStateHash = migratedHash.Value
|
|
clean := SyncViews_Clean(sync.Database, context, input.UserIDHash)
|
|
if clean.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load clean data", input.UserIDHash, clean.Error, "clean data failed")
|
|
return
|
|
}
|
|
value.Data = clean.Value
|
|
SyncHttp_ProjectClean(&(value), input)
|
|
logEntries := SyncAudit_Logs(sync.Database, context, input.UserIDHash, input.ClientClock)
|
|
if logEntries.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load sync logs", input.UserIDHash, logEntries.Error, "logs failed")
|
|
return
|
|
}
|
|
value.Logs = logEntries.Value
|
|
deleted := SyncAudit_Deletes(sync.Database, context, input.UserIDHash, input.ClientClock)
|
|
if deleted.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load delete logs", input.UserIDHash, deleted.Error, "delete logs failed")
|
|
return
|
|
}
|
|
value.Deletes = deleted.Value
|
|
legacy := SyncClients_Legacy(sync.Database, context, input.UserIDHash, 6)
|
|
if legacy.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load legacy clients", input.UserIDHash, legacy.Error, "legacy clients failed")
|
|
return
|
|
}
|
|
value.LegacyClients = legacy.Value
|
|
if int64(len(legacy.Value)) > 0 {
|
|
StdAtomicGo_Add(&(sync.Counters.LegacyClientHints), uint64(int64(len(legacy.Value))))
|
|
}
|
|
}
|
|
policy := SyncClients_LegacyWritePolicy(sync.Database, context, input.UserIDHash)
|
|
if policy.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load legacy write policy", input.UserIDHash, policy.Error, "legacy write policy failed")
|
|
return
|
|
}
|
|
value.LegacyWriteRequired = policy.Required
|
|
value.LegacyProjectionEpoch = policy.Epoch
|
|
if value.Diagnostics != nil {
|
|
var value_22 SyncResult = SyncHttp_ChangesResult(value.Changes)
|
|
value.Diagnostics.ReturnedChanges = value_22
|
|
}
|
|
if result.EncryptedRecords > int(0) {
|
|
StdAtomicGo_Add(&(sync.Counters.SyncEncryptedRecords), uint64(result.EncryptedRecords))
|
|
}
|
|
var entry SyncAuditEntry = SyncAuditEntry{}
|
|
entry.UserIDHash = input.UserIDHash
|
|
entry.ClientID = input.ClientID
|
|
entry.AppID = input.AppID
|
|
entry.ProtocolVersion = input.ProtocolVersion
|
|
entry.SinceServerVersion = input.SinceServerVersion
|
|
entry.ClientClock = input.ClientClock
|
|
entry.ServerVersion = value.ServerVersion
|
|
entry.Applied = result
|
|
entry.RemoteOps = int(int64(len(remote)))
|
|
entry.FullSnapshotRequired = fullSnapshot
|
|
entry.SnapshotReason = snapshotReason
|
|
error = SyncAudit_Record(sync.Database, context, entry)
|
|
if error != nil {
|
|
var value_23 string = LogSafety_LogText(input.UserIDHash)
|
|
var value_24 string = LogSafety_LogText(input.ClientID)
|
|
slog.Error("record sync audit", "user", value_23, "client", value_24, "error", error)
|
|
}
|
|
completion.Complete = true
|
|
Response_JSON(writer, int(200), value)
|
|
}
|
|
|
|
func SyncHttp_EncryptedEnvelope(sync Synchronization, writer ResponseWriter, request *Request, bytes []uint8) bool {
|
|
header := HttpAuth_UserHeader(request)
|
|
user := header.Value
|
|
if user == "" {
|
|
Response_Error(writer, int(400), "missing X-Daochi-User")
|
|
return false
|
|
}
|
|
var value_0 bool = Identity_ValidUserID(user)
|
|
if !value_0 {
|
|
var value_1 [2]string
|
|
value_1[0] = "invalid "
|
|
value_1[1] = header.Name
|
|
parts := value_1
|
|
_ = parts
|
|
var value_2 ResponseWriter = writer
|
|
Response_Error(value_2, int(400), strings.Join(parts[0:2:2], ""))
|
|
return false
|
|
}
|
|
authenticated := HttpAuth_AuthenticateToken(sync.Database, request, sync.Configuration.TokenSecret)
|
|
if authenticated.Authentication.Error != nil || authenticated.Authentication.Status != int(0) {
|
|
HttpAuth_Respond(writer, sync.Counters, authenticated.Authentication)
|
|
return false
|
|
}
|
|
if authenticated.Value != user {
|
|
var value_3 ResponseWriter = writer
|
|
var value_4 *ServerMetrics = sync.Counters
|
|
HttpAuth_Respond(value_3, value_4, Authentication_Failure(int(401), "token user mismatch"))
|
|
return false
|
|
}
|
|
var value_5 [2]string
|
|
value_5[0] = "X-Daochi-Client"
|
|
value_5[1] = "X-Ksync-Client"
|
|
clientNames := value_5
|
|
_ = clientNames
|
|
client := HttpAuth_HeaderAlias(request, clientNames[0:2:2])
|
|
var value_6 bool = client != ""
|
|
var value_7 bool = value_6
|
|
if value_7 {
|
|
var value_8 bool = Identity_ValidClientID(client)
|
|
value_7 = !value_8
|
|
}
|
|
if value_7 {
|
|
Response_Error(writer, int(400), "invalid X-Daochi-Client")
|
|
return false
|
|
}
|
|
var sinceVersion int64 = 0
|
|
var value_9 [2]string
|
|
value_9[0] = "X-Daochi-Since-Version"
|
|
value_9[1] = "X-Ksync-Since-Version"
|
|
versionNames := value_9
|
|
_ = versionNames
|
|
versionText := HttpAuth_HeaderAlias(request, versionNames[0:2:2])
|
|
if versionText != "" {
|
|
parsed := SyncHttp_ParseInteger(versionText, int(int(10)), int(int(64)))
|
|
if parsed.Error != nil || parsed.Value < 0 {
|
|
Response_Error(writer, int(400), "invalid X-Daochi-Since-Version")
|
|
return false
|
|
}
|
|
sinceVersion = parsed.Value
|
|
}
|
|
limit := SyncRequest_EncryptedPayloadLimit(request, sync.Configuration.EncryptedPayloadMaxReturn)
|
|
if limit.Error != nil {
|
|
var value_10 ResponseWriter = writer
|
|
Response_Error(value_10, int(400), StdErrorsGo_Message(limit.Error))
|
|
return false
|
|
}
|
|
context := StdHttpGo_Context(request)
|
|
if sync.Configuration.EncryptedPayloadMaxAccountBytes > 0 {
|
|
usage := EncryptedPayloads_Bytes(sync.Database, context, user)
|
|
if usage.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load encrypted payload usage", user, usage.Error, "encrypted sync failed")
|
|
return false
|
|
}
|
|
if int64(len(bytes)) > sync.Configuration.EncryptedPayloadMaxAccountBytes || usage.Value+int64(len(bytes)) > sync.Configuration.EncryptedPayloadMaxAccountBytes {
|
|
Response_Error(writer, int(413), "encrypted payload quota exceeded")
|
|
return false
|
|
}
|
|
}
|
|
alias := AccountProfile_Alias(sync.Database, context, user)
|
|
if alias.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load encrypted sync account alias", user, alias.Error, "alias failed")
|
|
return false
|
|
}
|
|
icon := AccountProfile_Icon(sync.Database, context, user)
|
|
if icon.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load encrypted sync profile icon", user, icon.Error, "profile icon failed")
|
|
return false
|
|
}
|
|
social := FriendStore_Authoritative(sync.Database, context, user)
|
|
if social.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load encrypted sync social state", user, social.Error, "social state failed")
|
|
return false
|
|
}
|
|
stored := EncryptedPayloads_Store(sync.Database, context, user, client, bytes, sync.MissingUser)
|
|
if stored.Error != nil {
|
|
if StdErrorsGo_Is(stored.Error, sync.MissingUser) {
|
|
Response_Error(writer, int(404), "sync account not found")
|
|
return false
|
|
}
|
|
SyncHttp_StorageFailure(writer, "store encrypted payload", user, stored.Error, "encrypted sync failed")
|
|
return false
|
|
}
|
|
pruned := EncryptedPayloads_Prune(sync.Database, context, user, sync.Configuration.EncryptedPayloadRetention, 0)
|
|
if pruned.Error != nil {
|
|
var value_11 string = LogSafety_LogText(user)
|
|
slog.Error("prune encrypted payloads", "user", value_11, "error", pruned.Error)
|
|
} else if pruned.Value.Deleted > 0 {
|
|
var value_12 string = LogSafety_LogText(user)
|
|
slog.Info("pruned encrypted payloads", "user", value_12, "deleted", int64(pruned.Value.Deleted))
|
|
}
|
|
loaded := EncryptedPayloads_Since(sync.Database, context, user, sinceVersion, limit.Value)
|
|
if loaded.Error != nil {
|
|
SyncHttp_StorageFailure(writer, "load encrypted payloads", user, loaded.Error, "encrypted sync failed")
|
|
return false
|
|
}
|
|
error := SyncClients_RecordSync(sync.Database, context, user, client, sinceVersion, stored.Version, 6, stored.Version)
|
|
if error != nil {
|
|
var value_13 string = LogSafety_LogText(user)
|
|
var value_14 string = LogSafety_LogText(client)
|
|
slog.Error("record encrypted sync client", "user", value_13, "client", value_14, "error", error)
|
|
}
|
|
var entry SyncAuditEntry = SyncAuditEntry{}
|
|
entry.UserIDHash = user
|
|
entry.ClientID = client
|
|
entry.ProtocolVersion = 6
|
|
entry.SinceServerVersion = sinceVersion
|
|
entry.ClientClock = sinceVersion
|
|
entry.ServerVersion = stored.Version
|
|
entry.EncryptedPayload = true
|
|
entry.EncryptedPayloadBytes = int64(len(bytes))
|
|
error = SyncAudit_Record(sync.Database, context, entry)
|
|
if error != nil {
|
|
var value_15 string = LogSafety_LogText(user)
|
|
var value_16 string = LogSafety_LogText(client)
|
|
slog.Error("record encrypted sync audit", "user", value_15, "client", value_16, "error", error)
|
|
}
|
|
StdAtomicGo_Add(&(sync.Counters.SyncEncryptedPayloads), 1)
|
|
SyncHub_Publish(sync.Notifications, user, stored.Version)
|
|
var value SyncResponse = SyncResponse{}
|
|
value.ProtocolVersion = 6
|
|
value.Status = "ok"
|
|
var value_17 []string = NodeInfo_Capabilities()
|
|
value.ServerCapabilities = value_17
|
|
value.TransitionMode = "encrypted_payload"
|
|
value.AccountAlias = alias.Value
|
|
value.ProfileIcon = icon.Value
|
|
value.ServerVersion = stored.Version
|
|
value.ServerClock = stored.Version
|
|
value.ChangesComplete = true
|
|
var value_18 SyncChanges = SyncRequest_EmptyChanges()
|
|
value.Changes = value_18
|
|
value.EncryptedPayloads = loaded.Value
|
|
value.MinSupportedProtocol = 1
|
|
value.ServerLatestProtocol = 6
|
|
value.Changes.SocialCache = social.Value
|
|
if loaded.Truncated {
|
|
value.EncryptedPayloadsTruncated = true
|
|
value.EncryptedPayloadsNextSinceVersion = (loaded.Value[(int64(len(loaded.Value)) - 1)]).ServerVersion
|
|
}
|
|
Response_JSON(writer, int(200), value)
|
|
return true
|
|
}
|