Skip to content

Commit

Permalink
Fix scripts page "Creator" display field and public filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed May 2, 2023
1 parent 9ee51e9 commit e2e92e9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Server/Components/Scripts/RunScript.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<span class="align-top">Show only mine</span>
</div>
</div>
<TreeView DataSource="ParentPage.TreeNodes"
<TreeView DataSource="ParentPage.FilteredScriptNodes"
ItemTypeSelector="x => x.ItemType"
ItemHeaderSelector="x => x.Name"
ItemSelected="ScriptSelected"
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Scripts/SavedScripts.razor
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<span class="align-top">Show only mine</span>
</div>
</div>
<TreeView DataSource="ParentPage.TreeNodes"
<TreeView DataSource="ParentPage.FilteredScriptNodes"
ItemTypeSelector="x => x.ItemType"
ItemHeaderSelector="x => x.Name"
ItemSelected="ScriptSelected"
Expand Down
4 changes: 2 additions & 2 deletions Server/Components/Scripts/SavedScripts.razor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.CodeAnalysis.Scripting;
using Remotely.Server.Pages;
using Remotely.Server.Services;
using Remotely.Shared.Models;
Expand Down Expand Up @@ -36,8 +37,7 @@ public partial class SavedScripts : AuthComponentBase
public IModalService ModalService { get; set; }

private bool CanModifyScript => _selectedScript.Id == Guid.Empty ||
_selectedScript.CreatorId == User.Id ||
User.IsAdministrator;
_selectedScript.CreatorId == User.Id || User.IsAdministrator;

private bool CanDeleteScript => !string.IsNullOrWhiteSpace(_selectedScript.CreatorId) &&
(_selectedScript.CreatorId == User.Id || User.IsAdministrator);
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Scripts/ScriptSchedules.razor
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<input type="checkbox" @bind="ParentPage.ShowOnlyMyScripts" />
<span class="align-top">Show only mine</span>
</div>
<TreeView DataSource="ParentPage.TreeNodes"
<TreeView DataSource="ParentPage.FilteredScriptNodes"
ItemTypeSelector="x => x.ItemType"
ItemHeaderSelector="x => x.Name"
ItemSelected="ScriptSelected"
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/TreeView/TreeView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Remotely.Server.Components.TreeView
public partial class TreeView<T> : ComponentBase
{
[Parameter]
public List<T> DataSource { get; set; }
public IEnumerable<T> DataSource { get; set; }

[Parameter]
public Func<T, List<T>> ChildItemSelector { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Server/Pages/ManageOrganization.razor
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
</div>
</div>
<div class="input-group-append">
<button type="submit" class="btn btn-secondary" @onclick="SendInvite">Add</button>
<button type="submit" class="btn btn-primary" @onclick="SendInvite">Add</button>
</div>
</div>
</div>
Expand Down
43 changes: 35 additions & 8 deletions Server/Pages/ScriptsPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,47 @@


@code {
private IEnumerable<ScriptTreeNode>? _filteredScriptNodes;
private bool _showOnlyMyScripts = true;

[Parameter]
public string ActiveTab { get; set; }

public bool ShowOnlyMyScripts { get; set; } = true;
public bool ShowOnlyMyScripts
{
get => _showOnlyMyScripts;
set
{
_filteredScriptNodes = null;
_showOnlyMyScripts = value;
}
}

public List<ScriptTreeNode> TreeNodes { get; } = new();

public IEnumerable<ScriptTreeNode> FilteredScriptNodes
{
get
{
if (_filteredScriptNodes?.Any() == true)
{
return _filteredScriptNodes;
}

if (ShowOnlyMyScripts)
{
_filteredScriptNodes = TreeNodes.Where(x =>
x.Script.CreatorId == User.Id);
}
else
{
_filteredScriptNodes = TreeNodes.Where(x =>
x.Script.IsPublic || x.Script.CreatorId == User.Id);
}

return _filteredScriptNodes;
}
}

public string GetItemIconCss(ScriptTreeNode viewModel)
{
Expand All @@ -53,18 +86,12 @@
public async Task RefreshScripts()
{
TreeNodes.Clear();
_filteredScriptNodes = null;

var allScripts = await DataService.GetSavedScriptsWithoutContent(User.Id, User.OrganizationID);

foreach (var script in allScripts)
{
if (ShowOnlyMyScripts &&
script.CreatorId != User.Id &&
!script.IsPublic)
{
continue;
}

var root = BuildFolderPath(script.FolderPath);
root.Add(new ScriptTreeNode()
{
Expand Down
2 changes: 1 addition & 1 deletion Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
context.Database.Migrate();
}

dataService.SetAllDevicesNotOnline();
await dataService.SetAllDevicesNotOnline();
dataService.CleanupOldRecords();

loggerFactory.AddProvider(new DbLoggerProvider(app.Environment, app.Services));
Expand Down
11 changes: 6 additions & 5 deletions Server/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public interface IDataService

Task ResetBranding(string organizationId);

void SetAllDevicesNotOnline();
Task SetAllDevicesNotOnline();

Task SetDisplayName(RemotelyUser user, string displayName);

Expand Down Expand Up @@ -1467,6 +1467,7 @@ public async Task<SavedScript> GetSavedScript(string userId, Guid scriptId)
using var dbContext = _appDbFactory.GetContext();

return await dbContext.SavedScripts
.Include(x => x.Creator)
.FirstOrDefaultAsync(x =>
x.Id == scriptId &&
(x.IsPublic || x.CreatorId == userId));
Expand Down Expand Up @@ -1714,15 +1715,15 @@ public async Task ResetBranding(string organizationId)
await dbContext.SaveChangesAsync();
}

public void SetAllDevicesNotOnline()
public async Task SetAllDevicesNotOnline()
{
using var dbContext = _appDbFactory.GetContext();

dbContext.Devices.ForEachAsync(x =>
await dbContext.Devices.ForEachAsync(x =>
{
x.IsOnline = false;
}).Wait();
dbContext.SaveChanges();
});
await dbContext.SaveChangesAsync();
}

public async Task SetDisplayName(RemotelyUser user, string displayName)
Expand Down

0 comments on commit e2e92e9

Please sign in to comment.