Files
mesh/sync_request.go
T

445 lines
13 KiB
Go

// Code generated by zi2go from sync_request.zi. DO NOT EDIT.
package main
import (
strconv "strconv"
strings "strings"
)
// #import std_go_types
// #import protocol
// #import sync_types
type SyncRequestResult struct {
Body []uint8
Value SyncRequest
Error Error
}
type LoginRequestResult struct {
Body []uint8
Value LoginRequest
Error Error
}
type DeleteRequestResult struct {
Body []uint8
Value DeleteRequest
Error Error
}
type DeleteWithKeyRequestResult struct {
Value DeleteWithKeyRequest
Error Error
}
type RequestSignature struct {
Value string
Context string
}
type ExportedAccountKey struct {
PublicID string
PrivateKey []uint8
}
type ExportedAccountKeyResult struct {
Value ExportedAccountKey
Error Error
}
type PayloadLimitResult struct {
Value int
Error Error
}
type KeyValuePair struct {
Key string
Value string
Found bool
}
type EnvelopeHeader struct {
V int "json:\"v\""
Nonce string "json:\"nonce\""
Ciphertext string "json:\"ciphertext\""
}
func SyncRequest_Cut(Value string, Separator string) KeyValuePair {
var result KeyValuePair
result.Key, result.Value, result.Found = strings.Cut(Value, Separator)
return result
}
func SyncRequest_Atoi(Value string) PayloadLimitResult {
var result PayloadLimitResult
result.Value, result.Error = strconv.Atoi(Value)
return result
}
func SyncRequest_ApplyHeaderUser(request *Request, bodyUser *string) Error {
header := HttpAuth_UserHeader(request)
if header.Value == "" {
return StdErrorsGo_New("missing X-Daochi-User")
}
if *(bodyUser) == "" {
(*(bodyUser)) = header.Value
return nil
}
var value_0 string = strings.ToLower(strings.TrimSpace(*(bodyUser)))
(*(bodyUser)) = value_0
if *(bodyUser) != header.Value {
var value_1 [2]string
value_1[0] = header.Name
value_1[1] = " does not match user_id_hash"
parts := value_1
_ = parts
return StdErrorsGo_New(strings.Join(parts[0:2:2], ""))
}
return nil
}
func SyncRequest_SignatureHeader(request *Request) RequestSignature {
var result RequestSignature = RequestSignature{}
result.Context = "ksync-sync-v1"
headers := StdHttpGo_Headers(request)
var value_0 [3]string
value_0[0] = "X-Daochi-Signature"
value_0[1] = "X-Ksync-Signature"
value_0[2] = "X-Inbe-Signature"
names := value_0
var value_1 [3]string
value_1[0] = "daochi-sync-v1"
value_1[1] = "ksync-sync-v1"
value_1[2] = "inbe-sync-v1"
contexts := value_1
var index int64 = 0
for index < 3 {
value := strings.TrimSpace(StdHttpGo_HeaderValue(headers, names[index]))
if value != "" {
result.Value = value
result.Context = contexts[index]
return result
}
index++
}
return result
}
func SyncRequest_ParseSync(bytes []uint8) SyncRequestResult {
var result SyncRequestResult = SyncRequestResult{}
var value_0 Error = StdJsonGo_Unmarshal(bytes, &(result.Value))
if value_0 != nil {
var value_1 Error = StdErrorsGo_New("invalid json")
result.Error = value_1
return result
}
var value_2 string = strings.ToLower(strings.TrimSpace(result.Value.UserIDHash))
result.Value.UserIDHash = value_2
var value_3 string = strings.TrimSpace(result.Value.AppID)
result.Value.AppID = value_3
var value_4 string = strings.TrimSpace(result.Value.PublicKey)
result.Value.PublicKey = value_4
var value_5 string = strings.TrimSpace(result.Value.ClientID)
result.Value.ClientID = value_5
var index int64 = 0
for index < int64(len(result.Value.EncryptedRecords)) {
item := &(result.Value.EncryptedRecords[index])
var value_6 string = strings.TrimSpace(item.Collection)
item.Collection = value_6
var value_7 string = strings.TrimSpace(item.ID)
item.ID = value_7
var value_8 string = strings.TrimSpace(item.KeyID)
item.KeyID = value_8
var value_9 string = strings.TrimSpace(item.Nonce)
item.Nonce = value_9
var value_10 string = strings.TrimSpace(item.UpdatedAt)
item.UpdatedAt = value_10
var value_11 string = strings.ToLower(strings.TrimSpace(item.ContentHash))
item.ContentHash = value_11
var value_12 string = strings.TrimSpace(item.ParentID)
item.ParentID = value_12
index++
}
return result
}
func SyncRequest_ReadSync(writer ResponseWriter, request *Request, limit int64) SyncRequestResult {
var result SyncRequestResult = SyncRequestResult{}
read := HttpBody_ReadJSON(writer, request, limit)
if read.Error != nil {
result.Error = read.Error
return result
}
result = SyncRequest_ParseSync(read.Value)
result.Body = read.Value
return result
}
func SyncRequest_IsEncryptedEnvelope(bytes []uint8) bool {
var envelope EnvelopeHeader = EnvelopeHeader{}
var value_0 bool = StdJsonGo_Valid(bytes)
var value_1 bool = !value_0
if !value_1 {
var value_2 Error = StdJsonGo_Unmarshal(bytes, &(envelope))
value_1 = (value_2 != nil)
}
if value_1 {
return false
}
var value_3 bool = envelope.V == int(1) || envelope.V == int(2)
var value_4 bool = value_3
if value_4 {
var value_5 bool = strings.TrimSpace(envelope.Nonce) != ""
value_4 = value_5
}
var value_6 bool = value_4
var value_7 bool = value_6
if value_7 {
var value_8 bool = strings.TrimSpace(envelope.Ciphertext) != ""
value_7 = value_8
}
return value_7
}
func SyncRequest_EmptyChanges() SyncChanges {
var result SyncChanges = SyncChanges{}
result.Habits = make([]Habit, int(int(0)))
result.HabitDays = make([]HabitDay, int(int(0)))
result.Sessions = make([]Session, int(int(0)))
result.MeditationLogs = make([]MeditationLog, int(int(0)))
result.SocialCache = make([]SocialSnapshot, int(int(0)))
result.EncryptedRecords = make([]EncryptedRecord, int(int(0)))
return result
}
func SyncRequest_ReadLogin(writer ResponseWriter, request *Request, limit int64) LoginRequestResult {
var result LoginRequestResult = LoginRequestResult{}
read := HttpBody_ReadJSON(writer, request, limit)
if read.Error != nil {
result.Error = read.Error
return result
}
var value_0 Error = StdJsonGo_Unmarshal(read.Value, &(result.Value))
if value_0 != nil {
var value_1 Error = StdErrorsGo_New("invalid json")
result.Error = value_1
return result
}
var value_2 string = strings.ToLower(strings.TrimSpace(result.Value.UserIDHash))
result.Value.UserIDHash = value_2
var value_3 string = strings.TrimSpace(result.Value.PublicKey)
result.Value.PublicKey = value_3
var value_4 string = strings.TrimSpace(result.Value.ClientID)
result.Value.ClientID = value_4
result.Body = read.Value
return result
}
func SyncRequest_ReadDelete(writer ResponseWriter, request *Request, limit int64) DeleteRequestResult {
var result DeleteRequestResult = DeleteRequestResult{}
read := HttpBody_ReadJSON(writer, request, limit)
if read.Error != nil {
result.Error = read.Error
return result
}
var value_0 Error = StdJsonGo_Unmarshal(read.Value, &(result.Value))
if value_0 != nil {
var value_1 Error = StdErrorsGo_New("invalid json")
result.Error = value_1
return result
}
var value_2 string = strings.ToLower(strings.TrimSpace(result.Value.UserIDHash))
result.Value.UserIDHash = value_2
result.Body = read.Value
return result
}
func SyncRequest_ReadDeleteWithKey(writer ResponseWriter, request *Request, limit int64) DeleteWithKeyRequestResult {
var result DeleteWithKeyRequestResult = DeleteWithKeyRequestResult{}
read := HttpBody_ReadJSON(writer, request, limit)
if read.Error != nil {
result.Error = read.Error
return result
}
var value_0 Error = StdJsonGo_Unmarshal(read.Value, &(result.Value))
if value_0 != nil {
var value_1 Error = StdErrorsGo_New("invalid json")
result.Error = value_1
return result
}
var value_2 string = strings.ToLower(strings.TrimSpace(result.Value.UserIDHash))
result.Value.UserIDHash = value_2
var value_3 string = strings.TrimSpace(result.Value.ExportedKey)
result.Value.ExportedKey = value_3
var value_4 bool = Identity_ValidUserID(result.Value.UserIDHash)
if !value_4 {
var value_5 Error = StdErrorsGo_New("invalid user_id_hash")
result.Error = value_5
return result
}
if result.Value.ExportedKey == "" {
var value_6 Error = StdErrorsGo_New("exported_key required")
result.Error = value_6
}
return result
}
func SyncRequest_NormalizeMeditationDurations(logs []MeditationLog) {
var index int64 = 0
for index < int64(len(logs)) {
if (logs[index]).DurationSeconds == int(0) && (logs[index]).Duration != int(0) {
(logs[index]).DurationSeconds = (logs[index]).Duration
}
index++
}
}
func SyncRequest_ParseExportedKey(value string) ExportedAccountKeyResult {
var result ExportedAccountKeyResult = ExportedAccountKeyResult{}
lines := strings.Split(strings.ReplaceAll(value, "\r\n", "\n"), "\n")
if int64(len(lines)) == 0 {
var value_0 Error = StdErrorsGo_New("invalid account key file")
result.Error = value_0
return result
}
header := strings.TrimSpace(lines[0])
var value_1 bool = header != "ksync-account-key-v1" && header != "lyra-account-key-v1" && header != "account-key-v1"
if value_1 && header != "inbe-sync-key-v1" {
var value_2 Error = StdErrorsGo_New("invalid account key file")
result.Error = value_2
return result
}
algorithmOK := false
publicID := ""
privateKeyText := ""
{
if int64(1) < 0 || int64(int64(len(lines))) < int64(1) || int64(int64(len(lines))) > int64(len(lines)) {
panic("slice range out of bounds")
}
value_4 := lines[1:int64(len(lines)):int64(len(lines))][:]
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_14 := value_4[0:int64(len(value_4)):int64(len(value_4))]
loop_count_14 := int64(len(loop_view_14))
var loop_cursor_14 int64 = 0
for loop_cursor_14 < loop_count_14 {
loop_index_14 := loop_cursor_14
line := loop_view_14[loop_index_14]
part := SyncRequest_Cut(strings.TrimSpace(line), "=")
if !part.Found {
loop_cursor_14++
continue
}
key := strings.TrimSpace(part.Key)
if key == "algorithm" {
algorithmOK = strings.TrimSpace(part.Value) == "ML-DSA-44"
} else if key == "public_id" {
publicID = strings.ToLower(strings.TrimSpace(part.Value))
} else if key == "private_key" {
privateKeyText = strings.TrimSpace(part.Value)
}
loop_cursor_14++
}
}
if !algorithmOK {
var value_5 Error = StdErrorsGo_New("account key algorithm must be ML-DSA-44")
result.Error = value_5
return result
}
var value_6 bool = publicID != ""
var value_7 bool = value_6
if value_7 {
var value_8 bool = Identity_ValidUserID(publicID)
value_7 = !value_8
}
if value_7 {
var value_9 Error = StdErrorsGo_New("invalid public_id")
result.Error = value_9
return result
}
decoded := Codec_DecodeBinaryField(privateKeyText)
if decoded.Error != "" {
var value_10 Error = StdErrorsGo_New("invalid private_key")
result.Error = value_10
return result
}
if int64(len(decoded.Value)) != 2560 {
var value_11 Error = StdErrorsGo_New("wrong private_key size")
result.Error = value_11
return result
}
result.Value.PublicID = publicID
var value_12 []uint8 = StdTextGo_ToBytes(decoded.Value)
result.Value.PrivateKey = value_12
return result
}
func SyncRequest_EncryptedPayloadLimit(request *Request, configuredMax int) PayloadLimitResult {
var result PayloadLimitResult = PayloadLimitResult{}
result.Value = configuredMax
var value_0 [2]string
value_0[0] = "X-Daochi-Limit"
value_0[1] = "X-Ksync-Limit"
names := value_0
_ = names
value := HttpAuth_HeaderAlias(request, names[0:2:2])
if value != "" {
parsed := SyncRequest_Atoi(value)
if parsed.Error != nil || parsed.Value <= int(0) {
result.Value = int(0)
result.Error = StdErrorsGo_New("invalid X-Daochi-Limit")
return result
}
result.Value = parsed.Value
}
if configuredMax > int(0) && (result.Value == int(0) || result.Value > configuredMax) {
result.Value = configuredMax
}
return result
}
func SyncRequest_TransitionMode(request SyncRequest) string {
if request.ProtocolVersion >= int(5) {
return "encrypted_primary"
}
if request.ProtocolVersion >= int(4) {
return "dual_write"
}
return ""
}
func SyncRequest_IncludeLegacyPrivateData(request SyncRequest) bool {
return request.ProtocolVersion < int(5) || request.IncludeLegacyData
}
func SyncRequest_HasLocalChanges(request SyncRequest) bool {
var value_0 bool = request.FullSyncRequested || int64(len(request.MeditationLogs)) > 0 || int64(len(request.Habits)) > 0
var value_1 bool = value_0 || int64(len(request.HabitDays)) > 0 || int64(len(request.Sessions)) > 0 || int64(len(request.EncryptedRecords)) > 0
return value_1 || int64(len(request.Ops)) > 0
}
func SyncRequest_PublicKey(request SyncRequest) JSONResult {
var result JSONResult = JSONResult{}
if strings.TrimSpace(request.PublicKey) == "" {
return result
}
decoded := Codec_DecodeBinaryField(request.PublicKey)
if decoded.Error != "" {
result.Error = StdErrorsGo_New("invalid public_key")
return result
}
if int64(len(decoded.Value)) != 1312 {
result.Error = StdErrorsGo_New("wrong public_key size")
return result
}
key := StdTextGo_ToBytes(decoded.Value)
var value_0 Error = EncryptedRecord_ValidateAccountKey(request.UserIDHash, key)
if value_0 != nil {
result.Error = StdErrorsGo_New("public_key does not match user_id_hash")
return result
}
result.Value = key
return result
}