Skip to content

Commit

Permalink
pass client Id to server when adding changes so that project update n…
Browse files Browse the repository at this point in the history
…otifications can be filtered by client id
  • Loading branch information
hahn-kev committed Aug 26, 2024
1 parent 3d52abf commit 263f86f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
7 changes: 6 additions & 1 deletion backend/FwLite/LocalWebApp/BackgroundSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ public class BackgroundSyncService(
{
private readonly Channel<CrdtProject> _syncResultsChannel = Channel.CreateUnbounded<CrdtProject>();

public void TriggerSync(Guid projectId)
public void TriggerSync(Guid projectId, Guid? ignoredClientId = null)
{
var projectData = CurrentProjectService.LookupProjectById(memoryCache, projectId);
if (projectData is null)
{
logger.LogWarning("Received project update for unknown project {ProjectId}", projectId);
return;
}
if (ignoredClientId == projectData.ClientId)
{
logger.LogInformation("Received project update for {ProjectId} triggered by my own change, ignoring", projectId);
return;
}

var crdtProject = projectsService.GetProject(projectData.Name);
if (crdtProject is null)
Expand Down
8 changes: 4 additions & 4 deletions backend/FwLite/LocalWebApp/CrdtHttpSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public async ValueTask<ISyncable> CreateProjectSyncable(ProjectData project)
return NullSyncable.Instance;
}

return new CrdtProjectSync(RestService.For<ISyncHttp>(client, refitSettings), project.Id, project.OriginDomain, this);
return new CrdtProjectSync(RestService.For<ISyncHttp>(client, refitSettings), project.Id, project.ClientId , project.OriginDomain, this);
}
}

public class CrdtProjectSync(ISyncHttp restSyncClient, Guid projectId, string originDomain, CrdtHttpSyncService httpSyncService) : ISyncable
public class CrdtProjectSync(ISyncHttp restSyncClient, Guid projectId, Guid clientId, string originDomain, CrdtHttpSyncService httpSyncService) : ISyncable

Check warning on line 64 in backend/FwLite/LocalWebApp/CrdtHttpSyncService.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

Parameter 'originDomain' is unread.

Check warning on line 64 in backend/FwLite/LocalWebApp/CrdtHttpSyncService.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app

Parameter 'originDomain' is unread.

Check warning on line 64 in backend/FwLite/LocalWebApp/CrdtHttpSyncService.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app

Parameter 'originDomain' is unread.

Check warning on line 64 in backend/FwLite/LocalWebApp/CrdtHttpSyncService.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app

Parameter 'originDomain' is unread.

Check warning on line 64 in backend/FwLite/LocalWebApp/CrdtHttpSyncService.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app

Parameter 'originDomain' is unread.

Check warning on line 64 in backend/FwLite/LocalWebApp/CrdtHttpSyncService.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app

Parameter 'originDomain' is unread.

Check warning on line 64 in backend/FwLite/LocalWebApp/CrdtHttpSyncService.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app

Parameter 'originDomain' is unread.
{
public ValueTask<bool> ShouldSync()
{
Expand All @@ -70,7 +70,7 @@ public ValueTask<bool> ShouldSync()

async Task ISyncable.AddRangeFromSync(IEnumerable<Commit> commits)
{
await restSyncClient.AddRange(projectId, commits);
await restSyncClient.AddRange(projectId, commits, clientId);
}

async Task<ChangesResult<Commit>> ISyncable.GetChanges(SyncState otherHeads)
Expand Down Expand Up @@ -104,7 +104,7 @@ public interface ISyncHttp
Task<HttpResponseMessage> HealthCheck();

[Post("/api/crdt/{id}/add")]
internal Task AddRange(Guid id, IEnumerable<Commit> commits);
internal Task AddRange(Guid id, IEnumerable<Commit> commits, Guid? clientId);

[Get("/api/crdt/{id}/get")]
internal Task<SyncState> GetSyncState(Guid id);
Expand Down
4 changes: 2 additions & 2 deletions backend/FwLite/LocalWebApp/Services/LexboxProjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public async Task ListenForProjectChanges(ProjectData projectData, CancellationT

//it would be cleaner to pass the callback in to this method however it's not supposed to be generic, it should always trigger a sync
connection.On(nameof(IProjectChangeListener.OnProjectUpdated),
(Guid projectId) =>
(Guid projectId, Guid? clientId) =>
{
logger.LogInformation("Received project update for {ProjectId}, triggering sync", projectId);
backgroundSyncService.TriggerSync(projectId);
backgroundSyncService.TriggerSync(projectId, clientId);
return Task.CompletedTask;
});

Expand Down
2 changes: 1 addition & 1 deletion backend/FwLite/MiniLcm/Push/IProjectChangeListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface IProjectChangeListener
{
Task OnProjectUpdated(Guid projectId);
Task OnProjectUpdated(Guid projectId, Guid? clientId);
}
4 changes: 2 additions & 2 deletions backend/LexBoxApi/Controllers/CrdtController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task<ActionResult<SyncState>> GetSyncState(Guid projectId)
}

[HttpPost("{projectId}/add")]
public async Task<ActionResult> Add(Guid projectId, [FromBody] ServerCommit[] commits)
public async Task<ActionResult> Add(Guid projectId, [FromBody] ServerCommit[] commits, Guid? clientId)
{
await permissionService.AssertCanSyncProject(projectId);
foreach (var commit in commits)
Expand All @@ -38,7 +38,7 @@ public async Task<ActionResult> Add(Guid projectId, [FromBody] ServerCommit[] co
}

await dbContext.SaveChangesAsync();
await hubContext.Clients.Group(CrdtProjectChangeHub.ProjectGroup(projectId)).OnProjectUpdated(projectId);
await hubContext.Clients.Group(CrdtProjectChangeHub.ProjectGroup(projectId)).OnProjectUpdated(projectId, clientId);
return Ok();
}

Expand Down

0 comments on commit 263f86f

Please sign in to comment.