Skip to content

Commit

Permalink
Merge pull request 'feature/index-saving' (#101) from feature/index-s…
Browse files Browse the repository at this point in the history
…aving into hotfix/v3.0.1

Reviewed-on: https://git.onlyoffice.com/ONLYOFFICE/DocSpace-server/pulls/101
Reviewed-by: pavelbannov <[email protected]>
  • Loading branch information
AlexeySafronov committed Nov 26, 2024
2 parents 90a54f3 + 76e4a1d commit 5d2d044
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 20 deletions.
16 changes: 14 additions & 2 deletions products/ASC.Files/Core/Core/EF/Queries/FileQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ select f
ctx.Files
.Where(r => r.TenantId == tenantId)
.Where(r => fileIds.Contains(r.Id) && r.CurrentVersion)

.Select(r => new DbFileQuery
{
File = r,
Expand All @@ -462,7 +461,20 @@ select t.ParentId
).FirstOrDefault()
where f.TenantId == r.TenantId
select f
).FirstOrDefault()
).FirstOrDefault(),
Order = (
from f in ctx.FileOrder
where (
from rs in ctx.RoomSettings
where rs.TenantId == f.TenantId && rs.RoomId ==
(from t in ctx.Tree
where t.FolderId == r.ParentId
orderby t.Level descending
select t.ParentId
).Skip(1).FirstOrDefault()
select rs.Indexing).FirstOrDefault() && f.EntryId == r.Id && f.TenantId == r.TenantId && f.EntryType == FileEntryType.File
select f.Order
).FirstOrDefault()
}));

public static readonly Func<FilesDbContext, int, int, IAsyncEnumerable<int>> FileIdsAsync =
Expand Down
11 changes: 11 additions & 0 deletions products/ASC.Files/Core/Core/Entries/HistoryEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public record HistoryEntry
MessageAction.RoomGroupAdded,
MessageAction.RoomInviteResend
];

private static readonly HashSet<MessageAction> _mergedActions =
[
MessageAction.FileIndexChanged,
MessageAction.FolderIndexChanged
];

private int _groupId;

Expand All @@ -61,6 +67,11 @@ public int GetGroupId()
{
return _groupId;
}

if (_mergedActions.Contains(Action.Id))
{
return _groupId = Data?.GetId() ?? 0;
}

if (_gropedActions.Contains(Action.Id))
{
Expand Down
51 changes: 51 additions & 0 deletions products/ASC.Files/Core/Core/FileStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,57 @@ public async Task SetFileOrder<T>(T fileId, int order)
}
}

public async Task SetOrderAsync<T>(IEnumerable<OrdersItemRequestDto<T>> items)
{
var contextId = Guid.NewGuid().ToString();

var folderDao = daoFactory.GetFolderDao<T>();
var fileDao = daoFactory.GetFileDao<T>();

var folders = await folderDao.GetFoldersAsync(items.Where(x => x.EntryType == FileEntryType.Folder)
.Select(x => x.EntryId))
.ToDictionaryAsync(x => x.Id);

var files = await fileDao.GetFilesAsync(items.Where(x => x.EntryType == FileEntryType.File)
.Select(x => x.EntryId))
.ToDictionaryAsync(x => x.Id);

foreach (var item in items)
{
FileEntry<T> entry = item.EntryType == FileEntryType.File ? files.Get(item.EntryId) : folders.Get(item.EntryId);
entry.NotFoundIfNull();

if (!await fileSecurity.CanEditAsync(entry))
{
throw new InvalidOperationException(FilesCommonResource.ErrorMessage_SecurityException);
}

switch (entry)
{
case File<T> file:
{
var newOrder = await fileDao.SetCustomOrder(file.Id, file.ParentId, item.Order);
if (newOrder != 0)
{
await filesMessageService.SendAsync(MessageAction.FileIndexChanged, file, file.Title, file.Order.ToString(), item.Order.ToString(), contextId);
}

break;
}
case Folder<T> folder:
{
var newOrder = await folderDao.SetCustomOrder(folder.Id, folder.ParentId, item.Order);
if (newOrder != 0)
{
await filesMessageService.SendAsync(MessageAction.FolderIndexChanged, folder, folder.Title, folder.Order.ToString(), item.Order.ToString(), contextId);
}

break;
}
}
}
}

public async Task SetFolderOrder<T>(T folderId, int order)
{
var folderDao = daoFactory.GetFolderDao<T>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public record FileIndexChangedData : EntryData
public int NewIndex { get; }
public IDictionary<Accessibility, bool> Accessibility { get; }
public string ViewUrl { get; }
private readonly string _context;

public FileIndexChangedData(
int oldIndex,
Expand All @@ -173,7 +174,8 @@ public FileIndexChangedData(
string parentTitle = null,
int? parentType = null,
IDictionary<Accessibility, bool> accessibility = null,
string viewUrl = null) : base(id,
string viewUrl = null,
string context = null) : base(id,
title,
parentId,
parentTitle,
Expand All @@ -183,10 +185,16 @@ public FileIndexChangedData(
NewIndex = newIndex;
Accessibility = accessibility;
ViewUrl = viewUrl;
_context = context;
}

public override int GetId()
{
if (!string.IsNullOrEmpty(_context))
{
return _context.GetHashCode();
}

return ParentId.HasValue ? ParentId.GetHashCode() : 0;
}
}
Expand Down Expand Up @@ -379,6 +387,12 @@ protected override ValueTask<HistoryData> GetDataAsync(IServiceProvider serviceP
var accessibility = GetAccessibility(serviceProvider, description[0]);
var viewUrl = GetViewUrl(serviceProvider, target);

string context = null;
if (description.Count >= 4)
{
context = description[3];
}

return new ValueTask<HistoryData>(new FileIndexChangedData(
oldIndex,
newIndex,
Expand All @@ -388,7 +402,8 @@ protected override ValueTask<HistoryData> GetDataAsync(IServiceProvider serviceP
desc.ParentTitle,
desc.ParentType,
accessibility,
viewUrl));
viewUrl,
context));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public record FolderIndexChangedData : EntryData
{
public int OldIndex { get; }
public int NewIndex { get; }
private readonly string _context;

public FolderIndexChangedData(
int oldIndex,
Expand All @@ -38,18 +39,25 @@ public FolderIndexChangedData(
string title,
int? parentId = null,
string parentTitle = null,
int? parentType = null) : base(id,
int? parentType = null,
string context = null) : base(id,
title,
parentId,
parentTitle,
parentType)
{
NewIndex = newIndex;
OldIndex = oldIndex;
_context = context;
}

public override int GetId()
{
if (!string.IsNullOrEmpty(_context))
{
return _context.GetHashCode();
}

return ParentId.HasValue ? ParentId.GetHashCode() : 0;
}
}
Expand Down Expand Up @@ -145,9 +153,23 @@ protected override ValueTask<HistoryData> GetDataAsync(IServiceProvider serviceP
{
var oldIndex = int.Parse(description[1]);
var newIndex = int.Parse(description[2]);

string context = null;
if (description.Count >= 4)
{
context = description[3];
}

var desc = GetAdditionalDescription(description);

return new ValueTask<HistoryData>(new FolderIndexChangedData(oldIndex, newIndex, target, description[0], desc.ParentId, desc.ParentTitle, desc.ParentType));
return new ValueTask<HistoryData>(new FolderIndexChangedData(
oldIndex,
newIndex,
target,
description[0],
desc.ParentId,
desc.ParentTitle,
desc.ParentType,
context));
}
}
15 changes: 1 addition & 14 deletions products/ASC.Files/Server/Api/FilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,20 +473,7 @@ public async Task SetOrder(OrderFileRequestDto<T> inDto)
[HttpPut("order")]
public async Task SetOrder(OrdersRequestDto<T> inDto)
{
foreach (var item in inDto.Items)
{
switch (item.EntryType)
{
case FileEntryType.Folder:
await fileStorageService.SetFolderOrder(item.EntryId, item.Order);
break;
case FileEntryType.File:
await fileStorageService.SetFileOrder(item.EntryId, item.Order);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
await fileStorageService.SetOrderAsync(inDto.Items);
}

/// <summary>
Expand Down

0 comments on commit 5d2d044

Please sign in to comment.