Skip to content

Commit

Permalink
added token forwarding on re-tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksimChegulov committed Oct 7, 2024
1 parent 6ab30c7 commit 8e358c3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,15 @@ public async Task<string> GetCallbackUrlAsync<T>(T fileId)
return callbackUrl;
}

public async Task<bool> StartTrackAsync<T>(T fileId, string docKeyForTrack)
public async Task<bool> StartTrackAsync<T>(T fileId, string docKeyForTrack, string token = null)
{
var callbackUrl = await GetCallbackUrlAsync(fileId);

if (!string.IsNullOrEmpty(token))
{
callbackUrl = QueryHelpers.AddQueryString(callbackUrl, FilesLinkUtility.ShareKey, token);
}

return await documentServiceConnector.CommandAsync(CommandMethod.Info, docKeyForTrack, fileId, callbackUrl);
}

Expand Down
12 changes: 6 additions & 6 deletions products/ASC.Files/Core/Utils/EntryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ public class EntryManager(IDaoFactory daoFactory,
TenantManager tenantManager,
FileChecker fileChecker,
IDistributedCache distributedCache,
NotifyClient notifyClient)
NotifyClient notifyClient,
ExternalShare externalShare)
{
private const string UpdateList = "filesUpdateList";

Expand Down Expand Up @@ -1503,9 +1504,6 @@ public async Task<File<T>> SaveEditingAsync<T>(T fileId, string fileExtension, s
pdfFile.Category = (int)FilterType.Pdf;

File<T> result;

var user = await userManager.GetUsersAsync(userId);

if (tmpStream.CanSeek)
{
pdfFile.ContentLength = tmpStream.Length;
Expand Down Expand Up @@ -1612,10 +1610,12 @@ public async Task<File<T>> SaveEditingAsync<T>(T fileId, string fileExtension, s

public async Task<File<T>> TrackEditingAsync<T>(T fileId, Guid tabId, Guid userId, int tenantId, bool editingAlone = false)
{
var token = externalShare.GetKey();

bool checkRight;
if ((await fileTracker.GetEditingByAsync(fileId)).Contains(userId))
{
checkRight = await fileTracker.ProlongEditingAsync(fileId, tabId, userId, tenantId, commonLinkUtility.ServerRootPath, editingAlone);
checkRight = await fileTracker.ProlongEditingAsync(fileId, tabId, userId, tenantId, commonLinkUtility.ServerRootPath, editingAlone, token);
if (!checkRight)
{
return null;
Expand Down Expand Up @@ -1643,7 +1643,7 @@ public async Task<File<T>> TrackEditingAsync<T>(T fileId, Guid tabId, Guid userI
throw new Exception(FilesCommonResource.ErrorMessage_ViewTrashItem);
}

checkRight = await fileTracker.ProlongEditingAsync(fileId, tabId, userId, tenantId, commonLinkUtility.ServerRootPath, editingAlone);
checkRight = await fileTracker.ProlongEditingAsync(fileId, tabId, userId, tenantId, commonLinkUtility.ServerRootPath, editingAlone, token);
if (checkRight)
{
await fileTracker.ChangeRight(fileId, userId, false);
Expand Down
23 changes: 17 additions & 6 deletions products/ASC.Files/Core/Utils/FileTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public FileTrackerHelper(ICacheNotify<FileTrackerNotify> cacheNotify, ICache cac
_callbackAction = EvictionCallback();
}

public async Task<bool> ProlongEditingAsync<T>(T fileId, Guid tabId, Guid userId, int tenantId, string baseUri, bool editingAlone = false)
public async Task<bool> ProlongEditingAsync<T>(T fileId, Guid tabId, Guid userId, int tenantId, string baseUri, bool editingAlone = false, string token = null)
{
var checkRight = true;
var tracker = GetTracker(fileId);
Expand All @@ -72,13 +72,14 @@ public async Task<bool> ProlongEditingAsync<T>(T fileId, Guid tabId, Guid userId
NewScheme = tabId == userId,
EditingAlone = editingAlone,
TenantId = tenantId,
BaseUri = baseUri
BaseUri = baseUri,
Token = token
});
}
}
else
{
tracker = new FileTracker(tabId, userId, tabId == userId, editingAlone, tenantId, baseUri);
tracker = new FileTracker(tabId, userId, tabId == userId, editingAlone, tenantId, baseUri, token);
}

await SetTrackerAsync(fileId, tracker);
Expand Down Expand Up @@ -233,6 +234,12 @@ async Task Callback<T>(T fileId, FileTracker fileTracker)
}

var editedBy = fileTracker.EditingBy.FirstOrDefault();

var token = fileTracker.EditingBy
.OrderByDescending(x => x.Value.TrackTime)
.Where(x => !string.IsNullOrEmpty(x.Value.Token))
.Select(x => x.Value.Token)
.FirstOrDefault();

await using var scope = _serviceProvider.CreateAsyncScope();
var tenantManager = scope.ServiceProvider.GetRequiredService<TenantManager>();
Expand All @@ -248,7 +255,7 @@ async Task Callback<T>(T fileId, FileTracker fileTracker)
var docKey = await helper.GetDocKeyAsync(await daoFactory.GetFileDao<T>().GetFileAsync(fileId));
using (_logger.BeginScope(new[] { new KeyValuePair<string, object>("DocumentServiceConnector", $"{fileId}") }))
{
if (await tracker.StartTrackAsync(fileId.ToString(), docKey))
if (await tracker.StartTrackAsync(fileId.ToString(), docKey, token))
{
await SetTrackerAsync(fileId, fileTracker);
}
Expand Down Expand Up @@ -285,7 +292,7 @@ public record FileTracker

public FileTracker() { }

internal FileTracker(Guid tabId, Guid userId, bool newScheme, bool editingAlone, int tenantId, string baseUri)
internal FileTracker(Guid tabId, Guid userId, bool newScheme, bool editingAlone, int tenantId, string baseUri, string token = null)
{
EditingBy = new Dictionary<Guid, TrackInfo>
{
Expand All @@ -297,7 +304,8 @@ internal FileTracker(Guid tabId, Guid userId, bool newScheme, bool editingAlone,
NewScheme = newScheme,
EditingAlone = editingAlone,
TenantId = tenantId,
BaseUri = baseUri
BaseUri = baseUri,
Token = token
}
}
};
Expand Down Expand Up @@ -326,5 +334,8 @@ public class TrackInfo

[ProtoMember(7)]
public required bool EditingAlone { get; init; }

[ProtoMember(8)]
public string Token { get; init; }
}
}

0 comments on commit 8e358c3

Please sign in to comment.