345 lines
16 KiB
Ziran
345 lines
16 KiB
Ziran
#import "std/go_types"
|
|
#import "protocol"
|
|
sql :: #import "std/sql_go";
|
|
ctx :: #import "std/context_go";
|
|
state :: #import "account_state";
|
|
timestamp :: #import "timestamp";
|
|
clock :: #import "std/time_go";
|
|
scope :: #import "scope";
|
|
native :: #system_library "go:database/sql";
|
|
builtin :: #system_library "go:builtin";
|
|
|
|
#scope_file
|
|
CollectionNamesResult :: struct {
|
|
value: []string
|
|
error: Error
|
|
}
|
|
AllocateStrings :: (count: isize) -> []string #foreign builtin "make";
|
|
AppendString :: (values: []string, value: string) -> []string #foreign builtin "append";
|
|
CloseRowsAtReturn :: (rows: *sql.Rows) #go_defer #foreign native "(*Rows).Close";
|
|
|
|
ReadCollections :: (rows: *sql.Rows) -> CollectionNamesResult {
|
|
result: CollectionNamesResult
|
|
CloseRowsAtReturn(rows)
|
|
values := AllocateStrings(cast(isize)0)
|
|
while sql.Next(rows) {
|
|
value: string
|
|
destinations: [1]Any = .[*value]
|
|
result.error = sql.ScanRows(rows, destinations[:])
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
values = AppendString(values, value)
|
|
}
|
|
result.error = sql.RowsError(rows)
|
|
if result.error == null {
|
|
result.value = values
|
|
}
|
|
return result
|
|
}
|
|
|
|
#scope_export
|
|
SyncWriteResult :: struct {
|
|
applied: isize
|
|
error: Error
|
|
}
|
|
|
|
// A full re-sync replaces only the calling app's data. Released Inbe clients
|
|
// send no app_id; Inbe owns the typed tables, the whole-sync envelopes and the
|
|
// account-wide records. Any other app clears only its own collections.
|
|
ReplaceData :: (transaction: *sql.Transaction, context: ctx.Context, userID: string, appID: string) -> Error {
|
|
owner := appID
|
|
if owner == "" {
|
|
owner = "inbe"
|
|
}
|
|
arguments: [1]Any = .[userID]
|
|
if owner == "inbe" {
|
|
queries: [7]string = .[
|
|
"DELETE FROM server_session_rounds WHERE user_id_hash=?1",
|
|
"DELETE FROM server_sessions WHERE user_id_hash=?1",
|
|
"DELETE FROM server_habit_days WHERE user_id_hash=?1",
|
|
"DELETE FROM server_habits WHERE user_id_hash=?1",
|
|
"DELETE FROM server_meditation_logs WHERE user_id_hash=?1",
|
|
"DELETE FROM server_social_snapshots WHERE user_id_hash=?1",
|
|
"DELETE FROM server_encrypted_payloads WHERE user_id_hash=?1",
|
|
]
|
|
for query: queries {
|
|
removed := sql.ExecTx(transaction, context, query, arguments[:])
|
|
if removed.error != null {
|
|
return removed.error
|
|
}
|
|
}
|
|
}
|
|
queried := sql.QueryTx(transaction, context,
|
|
"SELECT DISTINCT collection FROM server_encrypted_records WHERE user_id_hash=?1", arguments[:])
|
|
if queried.error != null {
|
|
return queried.error
|
|
}
|
|
collections := ReadCollections(queried.value)
|
|
if collections.error != null {
|
|
return collections.error
|
|
}
|
|
for collection: collections.value {
|
|
collectionOwner := scope.CollectionOwnerApp(collection)
|
|
replaced := collectionOwner == owner || (collectionOwner == "" && owner == "inbe")
|
|
if !replaced {
|
|
continue
|
|
}
|
|
values: [2]Any = .[userID, collection]
|
|
removed := sql.ExecTx(transaction, context,
|
|
"DELETE FROM server_encrypted_records WHERE user_id_hash=?1 AND collection=?2", values[:])
|
|
if removed.error != null {
|
|
return removed.error
|
|
}
|
|
}
|
|
version := state.NextVersion(transaction, context, userID)
|
|
return version.error
|
|
}
|
|
|
|
UpsertSession :: (transaction: *sql.Transaction, context: ctx.Context, userID: string, session: Session) -> SyncWriteResult {
|
|
result: SyncWriteResult
|
|
version := state.NextVersion(transaction, context, userID)
|
|
result.error = version.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
arguments: [17]Any = .[
|
|
userID, session.ID, timestamp.NormalizeTime(session.StartedAt, ""), session.LocalDate,
|
|
session.Topic, session.Activity, session.Source, session.RoundsHash, session.MoodBefore,
|
|
session.MoodAfter, session.Energy, session.Stress, session.Note, session.Tags,
|
|
session.DeletedAt, timestamp.NormalizeTime(session.UpdatedAt, ""), version.value,
|
|
]
|
|
written := sql.ExecTx(transaction, context,
|
|
"\n\tINSERT INTO server_sessions(user_id_hash,id,started_at,local_date,topic,activity,source,\n\t\trounds_hash,mood_before,mood_after,energy,stress,note,tags,deleted_at,updated_at,server_version)\n\tVALUES(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16,?17)\n\tON CONFLICT(user_id_hash,id) DO UPDATE SET\n\t\tstarted_at=excluded.started_at,\n\t\tlocal_date=excluded.local_date,\n\t\ttopic=excluded.topic,\n\t\tactivity=excluded.activity,\n\t\tsource=excluded.source,\n\t\trounds_hash=excluded.rounds_hash,\n\t\tmood_before=excluded.mood_before,\n\t\tmood_after=excluded.mood_after,\n\t\tenergy=excluded.energy,\n\t\tstress=excluded.stress,\n\t\tnote=excluded.note,\n\t\ttags=excluded.tags,\n\t\tdeleted_at=excluded.deleted_at,\n\t\tupdated_at=excluded.updated_at,\n\t\tserver_version=excluded.server_version\n\tWHERE excluded.updated_at >= server_sessions.updated_at", arguments[:])
|
|
result.error = written.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
applied := state.Affected(written.value)
|
|
if applied == cast(isize)0 || session.Rounds.count == 0 {
|
|
result.applied = applied
|
|
return result
|
|
}
|
|
identifiers: [2]Any = .[userID, session.ID]
|
|
cleared := sql.ExecTx(transaction, context,
|
|
"DELETE FROM server_session_rounds WHERE user_id_hash=?1 AND session_id=?2", identifiers[:])
|
|
result.error = cleared.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
for round: session.Rounds {
|
|
values: [5]Any = .[userID, session.ID, round.RoundIndex, round.Breaths, round.HoldSeconds]
|
|
inserted := sql.ExecTx(transaction, context,
|
|
"\nINSERT INTO server_session_rounds(user_id_hash,session_id,round_index,breaths,hold_seconds)\nVALUES(?1,?2,?3,?4,?5)", values[:])
|
|
result.error = inserted.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
}
|
|
result.applied = applied
|
|
return result
|
|
}
|
|
|
|
UpsertRecord :: (transaction: *sql.Transaction, context: ctx.Context, userID: string, item: EncryptedRecord) -> SyncWriteResult {
|
|
result: SyncWriteResult
|
|
version := state.NextVersion(transaction, context, userID)
|
|
result.error = version.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
now := clock.Now()
|
|
arguments: [14]Any = .[
|
|
userID, item.Collection, item.ID, item.KeyID, item.Nonce, item.Ciphertext,
|
|
timestamp.NormalizeWriteTime(item.UpdatedAt, now), item.DeletedAt, item.ContentHash,
|
|
item.SchemaVersion, item.ParentID, version.value, timestamp.WriteTimeLimit(now), item.FreeBytes,
|
|
]
|
|
written := sql.ExecTx(transaction, context,
|
|
"\nINSERT INTO server_encrypted_records(user_id_hash,collection,id,key_id,nonce,ciphertext,updated_at,deleted_at,content_hash,schema_version,parent_id,server_version,free_bytes)\nVALUES(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?14)\nON CONFLICT(user_id_hash,collection,id) DO UPDATE SET\n\tkey_id=excluded.key_id,\n\tnonce=excluded.nonce,\n\tciphertext=excluded.ciphertext,\n\tupdated_at=excluded.updated_at,\n\tdeleted_at=excluded.deleted_at,\n\tcontent_hash=excluded.content_hash,\n\tschema_version=excluded.schema_version,\n\tparent_id=excluded.parent_id,\n\tfree_bytes=excluded.free_bytes,\n\tserver_version=excluded.server_version\nWHERE excluded.updated_at >= server_encrypted_records.updated_at OR server_encrypted_records.updated_at > ?13", arguments[:])
|
|
result.error = written.error
|
|
if result.error == null {
|
|
result.applied = state.Affected(written.value)
|
|
}
|
|
return result
|
|
}
|
|
|
|
DeleteHabit :: (transaction: *sql.Transaction, context: ctx.Context, userID: string, habit: Habit) -> SyncWriteResult {
|
|
result: SyncWriteResult
|
|
updatedAt := timestamp.NormalizeTime(habit.UpdatedAt, "")
|
|
arguments: [3]Any = .[userID, habit.ID, updatedAt]
|
|
removed := sql.ExecTx(transaction, context,
|
|
"\nDELETE FROM server_habits\nWHERE user_id_hash=?1 AND id=?2 AND updated_at<=?3", arguments[:])
|
|
result.error = removed.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
applied := state.Affected(removed.value)
|
|
if applied == cast(isize)0 {
|
|
return result
|
|
}
|
|
identifiers: [2]Any = .[userID, habit.ID]
|
|
children := sql.ExecTx(transaction, context,
|
|
"\nDELETE FROM server_habit_days\nWHERE user_id_hash=?1 AND habit_id=?2", identifiers[:])
|
|
result.error = children.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
applied += state.Affected(children.value)
|
|
version := state.NextVersion(transaction, context, userID)
|
|
result.error = version.error
|
|
if result.error == null {
|
|
result.applied = applied
|
|
}
|
|
return result
|
|
}
|
|
|
|
DeleteHabitDay :: (transaction: *sql.Transaction, context: ctx.Context, userID: string, day: HabitDay) -> SyncWriteResult {
|
|
result: SyncWriteResult
|
|
updatedAt := timestamp.NormalizeTime(day.UpdatedAt, "")
|
|
arguments: [4]Any = .[userID, day.HabitID, day.LocalDate, updatedAt]
|
|
removed := sql.ExecTx(transaction, context,
|
|
"\nDELETE FROM server_habit_days\nWHERE user_id_hash=?1 AND habit_id=?2 AND local_date=?3 AND updated_at<=?4", arguments[:])
|
|
result.error = removed.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
applied := state.Affected(removed.value)
|
|
if applied == cast(isize)0 {
|
|
return result
|
|
}
|
|
version := state.NextVersion(transaction, context, userID)
|
|
result.error = version.error
|
|
if result.error == null {
|
|
result.applied = applied
|
|
}
|
|
return result
|
|
}
|
|
|
|
DeleteSession :: (transaction: *sql.Transaction, context: ctx.Context, userID: string, session: Session) -> SyncWriteResult {
|
|
result: SyncWriteResult
|
|
updatedAt := timestamp.NormalizeTime(session.UpdatedAt, session.StartedAt)
|
|
arguments: [3]Any = .[userID, session.ID, updatedAt]
|
|
removed := sql.ExecTx(transaction, context,
|
|
"\nDELETE FROM server_sessions\nWHERE user_id_hash=?1 AND id=?2 AND updated_at<=?3", arguments[:])
|
|
result.error = removed.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
applied := state.Affected(removed.value)
|
|
if applied == cast(isize)0 {
|
|
return result
|
|
}
|
|
identifiers: [2]Any = .[userID, session.ID]
|
|
children := sql.ExecTx(transaction, context,
|
|
"\nDELETE FROM server_session_rounds\nWHERE user_id_hash=?1 AND session_id=?2", identifiers[:])
|
|
result.error = children.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
applied += state.Affected(children.value)
|
|
version := state.NextVersion(transaction, context, userID)
|
|
result.error = version.error
|
|
if result.error == null {
|
|
result.applied = applied
|
|
}
|
|
return result
|
|
}
|
|
|
|
CompletedValue :: (completed: bool) -> isize {
|
|
if completed {
|
|
return cast(isize)1
|
|
}
|
|
return cast(isize)0
|
|
}
|
|
|
|
NormalizedDayCount :: (day: HabitDay) -> isize {
|
|
if !day.Completed {
|
|
return cast(isize)0
|
|
}
|
|
if day.Count > cast(isize)0 {
|
|
return day.Count
|
|
}
|
|
return cast(isize)1
|
|
}
|
|
|
|
UpsertMeditation :: (transaction: *sql.Transaction, context: ctx.Context, userID: string,
|
|
item: MeditationLog) -> SyncWriteResult {
|
|
result: SyncWriteResult
|
|
version := state.NextVersion(transaction, context, userID)
|
|
result.error = version.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
arguments: [6]Any = .[userID, item.ID, item.SessionID, item.DurationSeconds,
|
|
timestamp.NormalizeTime(item.CompletedAt, item.Timestamp), version.value]
|
|
written := sql.ExecTx(transaction, context,
|
|
"\nINSERT INTO server_meditation_logs(user_id_hash,id,session_id,duration_seconds,completed_at,server_version)\nVALUES(?1,?2,?3,?4,?5,?6)\nON CONFLICT(user_id_hash,id) DO NOTHING", arguments[:])
|
|
result.error = written.error
|
|
if result.error == null {
|
|
result.applied = state.Affected(written.value)
|
|
}
|
|
return result
|
|
}
|
|
|
|
#scope_file
|
|
WriteHabit :: (transaction: *sql.Transaction, context: ctx.Context, userID: string,
|
|
habit: Habit, query: string) -> SyncWriteResult {
|
|
result: SyncWriteResult
|
|
version := state.NextVersion(transaction, context, userID)
|
|
result.error = version.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
arguments: [13]Any = .[userID, habit.ID, habit.Name, habit.ColorR, habit.ColorG,
|
|
habit.ColorB, habit.SyncMode, habit.SyncActivity, habit.CounterEnabled,
|
|
habit.SortOrder, habit.DeletedAt, timestamp.NormalizeTime(habit.UpdatedAt, ""), version.value]
|
|
written := sql.ExecTx(transaction, context, query, arguments[:])
|
|
result.error = written.error
|
|
if result.error == null {
|
|
result.applied = state.Affected(written.value)
|
|
}
|
|
return result
|
|
}
|
|
|
|
WriteDay :: (transaction: *sql.Transaction, context: ctx.Context, userID: string,
|
|
day: HabitDay, query: string) -> SyncWriteResult {
|
|
result: SyncWriteResult
|
|
version := state.NextVersion(transaction, context, userID)
|
|
result.error = version.error
|
|
if result.error != null {
|
|
return result
|
|
}
|
|
arguments: [7]Any = .[userID, day.HabitID, day.LocalDate, CompletedValue(day.Completed),
|
|
NormalizedDayCount(day), timestamp.NormalizeTime(day.UpdatedAt, ""), version.value]
|
|
written := sql.ExecTx(transaction, context, query, arguments[:])
|
|
result.error = written.error
|
|
if result.error == null {
|
|
result.applied = state.Affected(written.value)
|
|
}
|
|
return result
|
|
}
|
|
|
|
#scope_export
|
|
UpsertHabit :: (transaction: *sql.Transaction, context: ctx.Context, userID: string,
|
|
habit: Habit) -> SyncWriteResult {
|
|
return WriteHabit(transaction, context, userID, habit,
|
|
"\nINSERT INTO server_habits(user_id_hash,id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at,server_version)\nVALUES(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13)\nON CONFLICT(user_id_hash,id) DO UPDATE SET\n\tname=excluded.name,\n\tcolor_r=excluded.color_r,\n\tcolor_g=excluded.color_g,\n\tcolor_b=excluded.color_b,\n\tsync_mode=excluded.sync_mode,\n\tsync_activity=excluded.sync_activity,\n\tcounter_enabled=excluded.counter_enabled,\n\tsort_order=excluded.sort_order,\n\tdeleted_at=excluded.deleted_at,\n\tupdated_at=excluded.updated_at,\n\tserver_version=excluded.server_version\nWHERE excluded.updated_at >= server_habits.updated_at")
|
|
}
|
|
|
|
UpsertHabitOp :: (transaction: *sql.Transaction, context: ctx.Context, userID: string,
|
|
habit: Habit) -> SyncWriteResult {
|
|
return WriteHabit(transaction, context, userID, habit,
|
|
"\nINSERT INTO server_habits(user_id_hash,id,name,color_r,color_g,color_b,sync_mode,sync_activity,counter_enabled,sort_order,deleted_at,updated_at,server_version)\nVALUES(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13)\nON CONFLICT(user_id_hash,id) DO UPDATE SET\n\tname=excluded.name,color_r=excluded.color_r,color_g=excluded.color_g,color_b=excluded.color_b,\n\tsync_mode=excluded.sync_mode,sync_activity=excluded.sync_activity,counter_enabled=excluded.counter_enabled,\n\tsort_order=excluded.sort_order,deleted_at=excluded.deleted_at,updated_at=excluded.updated_at,\n\tserver_version=excluded.server_version\nWHERE excluded.updated_at >= server_habits.updated_at")
|
|
}
|
|
|
|
UpsertDay :: (transaction: *sql.Transaction, context: ctx.Context, userID: string,
|
|
day: HabitDay) -> SyncWriteResult {
|
|
return WriteDay(transaction, context, userID, day,
|
|
"\nINSERT INTO server_habit_days(user_id_hash,habit_id,local_date,completed,count,updated_at,server_version)\nVALUES(?1,?2,?3,?4,?5,?6,?7)\nON CONFLICT(user_id_hash,habit_id,local_date) DO UPDATE SET\n\tcompleted=excluded.completed,\n\tcount=excluded.count,\n\tupdated_at=excluded.updated_at,\n\tserver_version=excluded.server_version\nWHERE excluded.updated_at >= server_habit_days.updated_at")
|
|
}
|
|
|
|
UpsertDayOp :: (transaction: *sql.Transaction, context: ctx.Context, userID: string,
|
|
day: HabitDay) -> SyncWriteResult {
|
|
return WriteDay(transaction, context, userID, day,
|
|
"\nINSERT INTO server_habit_days(user_id_hash,habit_id,local_date,completed,count,updated_at,server_version)\nVALUES(?1,?2,?3,?4,?5,?6,?7)\nON CONFLICT(user_id_hash,habit_id,local_date) DO UPDATE SET\n\tcompleted=excluded.completed,count=excluded.count,updated_at=excluded.updated_at,server_version=excluded.server_version\nWHERE excluded.updated_at >= server_habit_days.updated_at")
|
|
}
|