@@ -592,6 +592,40 @@ class SyncStreamRepository extends DriftDatabaseRepository {
592592 rethrow ;
593593 }
594594 }
595+
596+ Future <void > pruneAssets () async {
597+ try {
598+ await _db.transaction (() async {
599+ final authQuery = _db.authUserEntity.selectOnly ()
600+ ..addColumns ([_db.authUserEntity.id])
601+ ..limit (1 );
602+ final currentUserId = await authQuery.map ((row) => row.read (_db.authUserEntity.id)).getSingleOrNull ();
603+ if (currentUserId == null ) {
604+ _logger.warning ('No authenticated user found during pruneAssets. Skipping asset pruning.' );
605+ return ;
606+ }
607+
608+ final partnerQuery = _db.partnerEntity.selectOnly ()
609+ ..addColumns ([_db.partnerEntity.sharedById])
610+ ..where (_db.partnerEntity.sharedWithId.equals (currentUserId));
611+ final partnerIds = await partnerQuery.map ((row) => row.read (_db.partnerEntity.sharedById)).get ();
612+
613+ final validUsers = {currentUserId, ...partnerIds.nonNulls};
614+
615+ // Asset is not owned by the current user or any of their partners and is not part of any (shared) album
616+ // Likely a stale asset that was previously shared but has been removed
617+ await _db.remoteAssetEntity.deleteWhere ((asset) {
618+ return asset.ownerId.isNotIn (validUsers) &
619+ asset.id.isNotInQuery (
620+ _db.remoteAlbumAssetEntity.selectOnly ()..addColumns ([_db.remoteAlbumAssetEntity.assetId]),
621+ );
622+ });
623+ });
624+ } catch (error, stack) {
625+ _logger.severe ('Error: pruneAssets' , error, stack);
626+ // We do not rethrow here as this is a client-only cleanup and should not affect the sync process
627+ }
628+ }
595629}
596630
597631extension on AssetTypeEnum {
0 commit comments