- Pairing, peer and namespace endpoints no longer treat a proxied request as the local operator. Without DAOCHI_ADMIN_TOKEN a loopback caller must arrive directly: no forwarding header, a loopback Host and no cross-site Origin. - A sync request may write only its own app's collections. Requests without app_id count as Inbe, so a bearer token alone can no longer skip another app's signed transactions. - full_sync_requested replaces only the calling app's data instead of every app's encrypted records. - Record and deletion times may run at most five minutes ahead of the node's clock, for client sync and mesh import alike, so a far-future time can no longer freeze a record. Records frozen before this limit yield to the next edit.
512 lines
16 KiB
Go
512 lines
16 KiB
Go
// Code generated by zi2go from sync_application.zi. DO NOT EDIT.
|
|
package main
|
|
|
|
import sql "database/sql"
|
|
|
|
// #import std_go_types
|
|
// #import std_map_go
|
|
// #import protocol
|
|
// #import sync_types
|
|
type SyncApplicationResult struct {
|
|
Value SyncResult
|
|
Accepted []string
|
|
Storage AccountStorage
|
|
Error Error
|
|
}
|
|
|
|
type OperationsWriteResult struct {
|
|
Value []string
|
|
Error Error
|
|
}
|
|
|
|
type OperationExistsResult struct {
|
|
Value bool
|
|
Error Error
|
|
}
|
|
|
|
func SyncApplication_OperationExists(transaction *Transaction, context Context, userID string, operationID string) OperationExistsResult {
|
|
var result OperationExistsResult = OperationExistsResult{}
|
|
var exists int = 0
|
|
var value_0 [2]Any
|
|
value_0[0] = userID
|
|
value_0[1] = operationID
|
|
arguments := value_0
|
|
_ = arguments
|
|
var value_1 [1]Any
|
|
value_1[0] = &(exists)
|
|
destinations := value_1
|
|
_ = destinations
|
|
row := StdSqlGo_QueryRowTx(transaction, context, "SELECT EXISTS(SELECT 1 FROM server_sync_ops WHERE user_id_hash=?1 AND op_id=?2)", arguments[0:2:2])
|
|
result.Error = StdSqlGo_ScanRow(row, destinations[0:1:1])
|
|
result.Value = exists != int(0)
|
|
return result
|
|
}
|
|
|
|
func SyncApplication_MaterializeOperation(transaction *Transaction, context Context, userID string, operation SyncOp, result *SyncResult) Error {
|
|
bytes := []uint8(operation.Payload)
|
|
if operation.EntityType == "habit" {
|
|
if int64(len(bytes)) == 0 {
|
|
return StdErrorsGo_New("habit op missing payload")
|
|
}
|
|
var habit Habit = Habit{}
|
|
error := StdJsonGo_Unmarshal(bytes, &(habit))
|
|
_ = error
|
|
if error != nil {
|
|
return error
|
|
}
|
|
if habit.ID == "" {
|
|
habit.ID = operation.EntityID
|
|
}
|
|
if operation.OpType == "delete" && habit.DeletedAt <= 0 {
|
|
var value_0 int64 = StdTimeGo_Unix(StdTimeGo_Now())
|
|
habit.DeletedAt = value_0
|
|
}
|
|
if habit.DeletedAt > 0 || operation.OpType == "delete" {
|
|
deleted := SyncWrites_DeleteHabit(transaction, context, userID, habit)
|
|
_ = deleted
|
|
if deleted.Error != nil {
|
|
return deleted.Error
|
|
}
|
|
result.Habits += deleted.Applied
|
|
return nil
|
|
}
|
|
written := SyncWrites_UpsertHabitOp(transaction, context, userID, habit)
|
|
_ = written
|
|
if written.Error != nil {
|
|
return written.Error
|
|
}
|
|
result.Habits += written.Applied
|
|
} else if operation.EntityType == "habit_day" {
|
|
if int64(len(bytes)) == 0 {
|
|
return StdErrorsGo_New("habit_day op missing payload")
|
|
}
|
|
var day HabitDay = HabitDay{}
|
|
error := StdJsonGo_Unmarshal(bytes, &(day))
|
|
_ = error
|
|
if error != nil {
|
|
return error
|
|
}
|
|
if day.HabitID == "" {
|
|
day.HabitID = operation.EntityID
|
|
}
|
|
if day.LocalDate == int(0) {
|
|
day.LocalDate = operation.LocalDate
|
|
}
|
|
if operation.OpType == "delete" {
|
|
deleted := SyncWrites_DeleteHabitDay(transaction, context, userID, day)
|
|
_ = deleted
|
|
if deleted.Error != nil {
|
|
return deleted.Error
|
|
}
|
|
result.HabitDays += deleted.Applied
|
|
return nil
|
|
}
|
|
written := SyncWrites_UpsertDayOp(transaction, context, userID, day)
|
|
_ = written
|
|
if written.Error != nil {
|
|
return written.Error
|
|
}
|
|
result.HabitDays += written.Applied
|
|
} else if operation.EntityType == "session" {
|
|
if int64(len(bytes)) == 0 {
|
|
return StdErrorsGo_New("session op missing payload")
|
|
}
|
|
var session Session = Session{}
|
|
error := StdJsonGo_Unmarshal(bytes, &(session))
|
|
_ = error
|
|
if error != nil {
|
|
return error
|
|
}
|
|
if session.ID == "" {
|
|
session.ID = operation.EntityID
|
|
}
|
|
if operation.OpType == "delete" && session.DeletedAt <= 0 {
|
|
var value_1 int64 = StdTimeGo_Unix(StdTimeGo_Now())
|
|
session.DeletedAt = value_1
|
|
}
|
|
if session.DeletedAt > 0 || operation.OpType == "delete" {
|
|
deleted := SyncWrites_DeleteSession(transaction, context, userID, session)
|
|
_ = deleted
|
|
if deleted.Error != nil {
|
|
return deleted.Error
|
|
}
|
|
result.Sessions += deleted.Applied
|
|
return nil
|
|
}
|
|
written := SyncWrites_UpsertSession(transaction, context, userID, session)
|
|
_ = written
|
|
if written.Error != nil {
|
|
return written.Error
|
|
}
|
|
result.Sessions += written.Applied
|
|
} else if operation.EntityType == "social_snapshots" {
|
|
return StdErrorsGo_New("social_cache is server-owned")
|
|
} else {
|
|
version := AccountState_NextVersion(transaction, context, userID)
|
|
return version.Error
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func SyncApplication_ApplyOperations(transaction *Transaction, context Context, userID string, operations []SyncOp, applied *SyncResult) OperationsWriteResult {
|
|
var result OperationsWriteResult = OperationsWriteResult{}
|
|
accepted := make([]string, int(int(0)))
|
|
for _, operation := range operations {
|
|
if operation.OpID == "" || operation.ClientID == "" || operation.Seq <= 0 {
|
|
result.Error = StdErrorsGo_New("invalid sync op identity")
|
|
return result
|
|
}
|
|
exists := SyncApplication_OperationExists(transaction, context, userID, operation.OpID)
|
|
result.Error = exists.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
if exists.Value {
|
|
accepted = append(accepted, operation.OpID)
|
|
continue
|
|
}
|
|
result.Error = HabitId_CanonicalizeOperation(transaction, context, userID, &(operation))
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
result.Error = SyncApplication_MaterializeOperation(transaction, context, userID, operation, applied)
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
version := AccountState_CurrentVersionTx(transaction, context, userID)
|
|
result.Error = version.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
if version.Value <= 0 {
|
|
version = AccountState_NextVersion(transaction, context, userID)
|
|
result.Error = version.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
}
|
|
payload := StdTextGo_FromBytes([]uint8(operation.Payload))
|
|
createdAt := Timestamp_NormalizeTime(operation.CreatedAt, "")
|
|
var value_0 [11]Any
|
|
value_0[0] = userID
|
|
value_0[1] = operation.OpID
|
|
value_0[2] = operation.ClientID
|
|
value_0[3] = operation.Seq
|
|
value_0[4] = operation.EntityType
|
|
value_0[5] = operation.EntityID
|
|
value_0[6] = operation.LocalDate
|
|
value_0[7] = operation.OpType
|
|
value_0[8] = payload
|
|
value_0[9] = createdAt
|
|
value_0[10] = version.Value
|
|
arguments := value_0
|
|
_ = arguments
|
|
var value_1 string = "\nINSERT INTO server_sync_ops(user_id_hash,op_id,client_id,seq,entity_type,entity_id,local_date,op_type,payload_json,created_at,server_version)\nVALUES(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11)"
|
|
inserted := StdSqlGo_ExecTx(transaction, context, value_1, arguments[0:11:11])
|
|
result.Error = inserted.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
accepted = append(accepted, operation.OpID)
|
|
}
|
|
result.Value = accepted
|
|
return result
|
|
}
|
|
|
|
func SyncApplication_Apply(database *Database, context Context, request SyncRequest, publicKey []uint8, missingUser Error) SyncApplicationResult {
|
|
return SyncApplication_ApplyWithinLimit(database, context, request, publicKey, missingUser, 0)
|
|
}
|
|
|
|
func SyncApplication_ApplyWithinLimit(database *Database, context Context, request SyncRequest, publicKey []uint8, missingUser Error, storageLimit int64) SyncApplicationResult {
|
|
var result SyncApplicationResult = SyncApplicationResult{}
|
|
started := StdSqlGo_Begin(database, context, nil)
|
|
result.Error = started.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
transaction := started.Value
|
|
defer (*sql.Tx).Rollback(transaction)
|
|
var value_0 bool = StdTextGo_IsNil(publicKey)
|
|
if !value_0 {
|
|
result.Error = AccountState_Upsert(transaction, context, request.UserIDHash, publicKey)
|
|
} else {
|
|
result.Error = AccountState_Touch(transaction, context, request.UserIDHash, missingUser)
|
|
}
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
var usedBefore int64 = 0
|
|
if storageLimit > 0 {
|
|
measured := AccountStorage_UsedTx(transaction, context, request.UserIDHash)
|
|
result.Error = measured.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
usedBefore = measured.Value
|
|
}
|
|
if request.FullSyncRequested {
|
|
result.Error = SyncWrites_ReplaceData(transaction, context, request.UserIDHash, request.AppID)
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
}
|
|
if int64(len(request.SocialCache)) > 0 {
|
|
result.Error = StdErrorsGo_New("social_cache is server-owned")
|
|
return result
|
|
}
|
|
{
|
|
value_1 := request.EncryptedRecords[:]
|
|
if int64(0) < 0 || int64(int64(len(value_1))) < int64(0) || int64(int64(len(value_1))) > int64(len(value_1)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_36 := value_1[0:int64(len(value_1)):int64(len(value_1))]
|
|
loop_count_36 := int64(len(loop_view_36))
|
|
var loop_cursor_36 int64 = 0
|
|
for loop_cursor_36 < loop_count_36 {
|
|
loop_index_36 := loop_cursor_36
|
|
item := loop_view_36[loop_index_36]
|
|
_ = item
|
|
var value_2 bool = EncryptedRecord_ValidForProtocol(item, request.ProtocolVersion)
|
|
if !value_2 {
|
|
result.Error = StdErrorsGo_New("invalid encrypted record")
|
|
return result
|
|
}
|
|
loop_cursor_36++
|
|
}
|
|
}
|
|
var applied SyncResult = SyncResult{}
|
|
var deletedHabits __type_c922d3f56b74fd5a = *new(__type_c922d3f56b74fd5a)
|
|
{
|
|
value_3 := request.MeditationLogs[:]
|
|
if int64(0) < 0 || int64(int64(len(value_3))) < int64(0) || int64(int64(len(value_3))) > int64(len(value_3)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_44 := value_3[0:int64(len(value_3)):int64(len(value_3))]
|
|
loop_count_44 := int64(len(loop_view_44))
|
|
var loop_cursor_44 int64 = 0
|
|
for loop_cursor_44 < loop_count_44 {
|
|
loop_index_44 := loop_cursor_44
|
|
item := loop_view_44[loop_index_44]
|
|
_ = item
|
|
written := SyncWrites_UpsertMeditation(transaction, context, request.UserIDHash, item)
|
|
_ = written
|
|
result.Error = written.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
applied.MeditationLogs += written.Applied
|
|
loop_cursor_44++
|
|
}
|
|
}
|
|
{
|
|
value_4 := request.Habits[:]
|
|
if int64(0) < 0 || int64(int64(len(value_4))) < int64(0) || int64(int64(len(value_4))) > int64(len(value_4)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_52 := value_4[0:int64(len(value_4)):int64(len(value_4))]
|
|
loop_count_52 := int64(len(loop_view_52))
|
|
var loop_cursor_52 int64 = 0
|
|
for loop_cursor_52 < loop_count_52 {
|
|
loop_index_52 := loop_cursor_52
|
|
habit := loop_view_52[loop_index_52]
|
|
originalID := habit.ID
|
|
canonical := HabitId_ForWrite(transaction, context, request.UserIDHash, habit.ID, "legacy-write")
|
|
_ = canonical
|
|
result.Error = canonical.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
habit.ID = canonical.Value
|
|
if request.Bootstrap && habit.DeletedAt > 0 {
|
|
var value_5 *__type_c922d3f56b74fd5a = &(deletedHabits)
|
|
var value_6 string = habit.ID
|
|
var value_7 bool = true
|
|
if (*value_5) == nil {
|
|
(*value_5) = make(__type_c922d3f56b74fd5a)
|
|
}
|
|
(*value_5)[value_6] = value_7
|
|
var value_8 *__type_c922d3f56b74fd5a = &(deletedHabits)
|
|
var value_9 string = originalID
|
|
var value_10 bool = true
|
|
if (*value_8) == nil {
|
|
(*value_8) = make(__type_c922d3f56b74fd5a)
|
|
}
|
|
(*value_8)[value_9] = value_10
|
|
loop_cursor_52++
|
|
continue
|
|
}
|
|
if habit.DeletedAt > 0 {
|
|
var value_11 *__type_c922d3f56b74fd5a = &(deletedHabits)
|
|
var value_12 string = habit.ID
|
|
var value_13 bool = true
|
|
if (*value_11) == nil {
|
|
(*value_11) = make(__type_c922d3f56b74fd5a)
|
|
}
|
|
(*value_11)[value_12] = value_13
|
|
var value_14 *__type_c922d3f56b74fd5a = &(deletedHabits)
|
|
var value_15 string = originalID
|
|
var value_16 bool = true
|
|
if (*value_14) == nil {
|
|
(*value_14) = make(__type_c922d3f56b74fd5a)
|
|
}
|
|
(*value_14)[value_15] = value_16
|
|
deleted := SyncWrites_DeleteHabit(transaction, context, request.UserIDHash, habit)
|
|
_ = deleted
|
|
result.Error = deleted.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
applied.Habits += deleted.Applied
|
|
loop_cursor_52++
|
|
continue
|
|
}
|
|
written := SyncWrites_UpsertHabit(transaction, context, request.UserIDHash, habit)
|
|
_ = written
|
|
result.Error = written.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
applied.Habits += written.Applied
|
|
loop_cursor_52++
|
|
}
|
|
}
|
|
{
|
|
value_17 := request.HabitDays[:]
|
|
if int64(0) < 0 || int64(int64(len(value_17))) < int64(0) || int64(int64(len(value_17))) > int64(len(value_17)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_83 := value_17[0:int64(len(value_17)):int64(len(value_17))]
|
|
loop_count_83 := int64(len(loop_view_83))
|
|
var loop_cursor_83 int64 = 0
|
|
for loop_cursor_83 < loop_count_83 {
|
|
loop_index_83 := loop_cursor_83
|
|
day := loop_view_83[loop_index_83]
|
|
var value_18 __type_c922d3f56b74fd5a = deletedHabits
|
|
var value_19 string = day.HabitID
|
|
var value_20 bool = value_18[value_19]
|
|
if value_20 {
|
|
loop_cursor_83++
|
|
continue
|
|
}
|
|
canonical := HabitId_ForWrite(transaction, context, request.UserIDHash, day.HabitID, "legacy-day-write")
|
|
_ = canonical
|
|
result.Error = canonical.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
day.HabitID = canonical.Value
|
|
var value_21 __type_c922d3f56b74fd5a = deletedHabits
|
|
var value_22 string = day.HabitID
|
|
var value_23 bool = value_21[value_22]
|
|
if value_23 {
|
|
loop_cursor_83++
|
|
continue
|
|
}
|
|
written := SyncWrites_UpsertDay(transaction, context, request.UserIDHash, day)
|
|
_ = written
|
|
result.Error = written.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
applied.HabitDays += written.Applied
|
|
loop_cursor_83++
|
|
}
|
|
}
|
|
{
|
|
value_24 := request.Sessions[:]
|
|
if int64(0) < 0 || int64(int64(len(value_24))) < int64(0) || int64(int64(len(value_24))) > int64(len(value_24)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_103 := value_24[0:int64(len(value_24)):int64(len(value_24))]
|
|
loop_count_103 := int64(len(loop_view_103))
|
|
var loop_cursor_103 int64 = 0
|
|
for loop_cursor_103 < loop_count_103 {
|
|
loop_index_103 := loop_cursor_103
|
|
session := loop_view_103[loop_index_103]
|
|
if request.Bootstrap && session.DeletedAt > 0 {
|
|
loop_cursor_103++
|
|
continue
|
|
}
|
|
if session.DeletedAt > 0 {
|
|
deleted := SyncWrites_DeleteSession(transaction, context, request.UserIDHash, session)
|
|
_ = deleted
|
|
result.Error = deleted.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
applied.Sessions += deleted.Applied
|
|
loop_cursor_103++
|
|
continue
|
|
}
|
|
written := SyncWrites_UpsertSession(transaction, context, request.UserIDHash, session)
|
|
_ = written
|
|
result.Error = written.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
applied.Sessions += written.Applied
|
|
loop_cursor_103++
|
|
}
|
|
}
|
|
{
|
|
value_25 := request.EncryptedRecords[:]
|
|
if int64(0) < 0 || int64(int64(len(value_25))) < int64(0) || int64(int64(len(value_25))) > int64(len(value_25)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
loop_view_123 := value_25[0:int64(len(value_25)):int64(len(value_25))]
|
|
loop_count_123 := int64(len(loop_view_123))
|
|
var loop_cursor_123 int64 = 0
|
|
for loop_cursor_123 < loop_count_123 {
|
|
loop_index_123 := loop_cursor_123
|
|
item := loop_view_123[loop_index_123]
|
|
_ = item
|
|
written := SyncWrites_UpsertRecord(transaction, context, request.UserIDHash, item)
|
|
_ = written
|
|
result.Error = written.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
applied.EncryptedRecords += written.Applied
|
|
loop_cursor_123++
|
|
}
|
|
}
|
|
operations := SyncApplication_ApplyOperations(transaction, context, request.UserIDHash, request.Ops, &(applied))
|
|
result.Error = operations.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
if storageLimit > 0 {
|
|
usedAfter := AccountStorage_UsedTx(transaction, context, request.UserIDHash)
|
|
result.Error = usedAfter.Error
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
result.Storage = AccountStorage_Report(usedAfter.Value, storageLimit)
|
|
if AccountStorage_Exceeds(usedBefore, usedAfter.Value, storageLimit) {
|
|
result.Storage = AccountStorage_Report(usedBefore, storageLimit)
|
|
result.Error = ErrStorageFull
|
|
return result
|
|
}
|
|
}
|
|
result.Error = StdSqlGo_Commit(transaction)
|
|
if result.Error == nil {
|
|
result.Value = applied
|
|
result.Accepted = operations.Value
|
|
}
|
|
return result
|
|
}
|
|
|
|
var SyncApplication_ziranInitState uint8
|
|
|
|
func SyncApplication_ziranInit() {
|
|
if SyncApplication_ziranInitState == 2 {
|
|
return
|
|
}
|
|
if SyncApplication_ziranInitState == 1 {
|
|
panic("cyclic module startup")
|
|
}
|
|
SyncApplication_ziranInitState = 1
|
|
AccountStorage_ziranInit()
|
|
SyncApplication_ziranInitState = 2
|
|
}
|