44using FwLiteShared . Projects ;
55using LexCore . Sync ;
66using LcmCrdt ;
7+ using LcmCrdt . Data ;
78using LcmCrdt . MediaServer ;
89using LcmCrdt . RemoteSync ;
910using LcmCrdt . Utils ;
@@ -30,7 +31,7 @@ public class SyncService(
3031 LcmMediaService lcmMediaService ,
3132 IOptions < AuthConfig > authOptions ,
3233 ILogger < SyncService > logger ,
33- IDbContextFactory < LcmCrdtDbContext > dbContextFactory )
34+ SyncRepository syncRepository )
3435{
3536 public async Task < SyncResults > SafeExecuteSync ( bool skipNotifications = false )
3637 {
@@ -94,7 +95,7 @@ public async Task<SyncResults> ExecuteSync(bool skipNotifications = false)
9495 }
9596 logger . LogInformation ( "Synced project {ProjectName} with server" , project . Name ) ;
9697 UpdateSyncStatus ( SyncStatus . Success ) ;
97- await UpdateSyncDate ( syncDate ) ;
98+ await syncRepository . UpdateSyncDate ( syncDate ) ;
9899 //need to await this, otherwise the database connection will be closed before the notifications are sent
99100 if ( ! skipNotifications ) await SendNotifications ( syncResults ) ;
100101 return syncResults ;
@@ -137,7 +138,7 @@ public async Task<SyncJobResult> AwaitSyncFinished()
137138 var project = await currentProjectService . GetProjectData ( ) ;
138139 var localSyncState = await dataModel . GetSyncState ( ) ;
139140 if ( ! authOptions . Value . TryGetServer ( project , out var server ) ) return null ;
140- var localChangesPending = CountPendingCommits ( ) ; // Not awaited yet
141+ var localChangesPending = syncRepository . CountPendingCommits ( ) ; // Not awaited yet
141142 var remoteChangesPending = lexboxProjectService . CountPendingCrdtCommits ( server , project . Id , localSyncState ) ; // Not awaited yet
142143 await Task . WhenAll ( localChangesPending , remoteChangesPending ) ;
143144 var localChanges = await localChangesPending ;
@@ -220,70 +221,6 @@ private async Task SendNotifications(SyncResults syncResults)
220221 }
221222 }
222223
223- /// <summary>
224- /// Note this will update any commits, not just the ones that were synced. This includes ours which we just sent
225- /// </summary>
226- private async Task UpdateSyncDate ( DateTimeOffset syncDate )
227- {
228- try
229- {
230- //the prop name is hardcoded into the sql so we just want to assert it's what we expect
231- Debug . Assert ( CommitHelpers . SyncDateProp == "SyncDate" ) ;
232- await using var dbContext = await dbContextFactory . CreateDbContextAsync ( ) ;
233- await dbContext . Database . ExecuteSqlAsync (
234- $ """
235- UPDATE Commits
236- SET metadata = json_set(metadata, '$.ExtraMetadata.SyncDate', { syncDate . ToString ( "u" ) } )
237- WHERE json_extract(Metadata, '$.ExtraMetadata.SyncDate') IS NULL;
238- """ ) ;
239- }
240- catch ( Exception e )
241- {
242- logger . LogError ( e , "Failed to update sync date" ) ;
243- }
244- }
245-
246- public async Task < int ? > CountPendingCommits ( )
247- {
248- try
249- {
250- // Assert sync date prop for same reason as in UpdateSyncDate
251- Debug . Assert ( CommitHelpers . SyncDateProp == "SyncDate" ) ;
252- await using var dbContext = await dbContextFactory . CreateDbContextAsync ( ) ;
253- int count = await dbContext . Database . SqlQuery < int > (
254- $ """
255- SELECT COUNT(*) AS Value FROM Commits
256- WHERE json_extract(Metadata, '$.ExtraMetadata.SyncDate') IS NULL
257- """ ) . SingleAsync ( ) ;
258- return count ;
259- }
260- catch ( Exception e )
261- {
262- logger . LogError ( e , "Failed to count pending commits" ) ;
263- return null ;
264- }
265- }
266-
267- public async Task < DateTimeOffset ? > GetLatestCommitDate ( )
268- {
269- try
270- {
271- // Assert sync date prop for same reason as in UpdateSyncDate
272- Debug . Assert ( CommitHelpers . SyncDateProp == "SyncDate" ) ;
273- await using var dbContext = await dbContextFactory . CreateDbContextAsync ( ) ;
274- var date = await dbContext . Database . SqlQuery < DateTimeOffset > (
275- $ """
276- SELECT MAX(json_extract(Metadata, '$.ExtraMetadata.SyncDate')) AS Value FROM Commits
277- """ ) . SingleAsync ( ) ;
278- return date ;
279- }
280- catch ( Exception e )
281- {
282- logger . LogError ( e , "Failed to find most recent commit date" ) ;
283- return null ;
284- }
285- }
286-
287224 public async Task UploadProject ( Guid lexboxProjectId , LexboxServer server )
288225 {
289226 await currentProjectService . SetProjectSyncOrigin ( server . Authority , lexboxProjectId ) ;
0 commit comments