Skip to content

Commit

Permalink
Refactor various classes for improved readability and initialization
Browse files Browse the repository at this point in the history
Updated classes to use concise auto-property initialization, reduced redundant serialization patterns, and replaced inline object initializations with explicit types for clarity. Converted several virtual methods to non-virtual and streamlined foreach/loop constructs.
  • Loading branch information
pavelbannov committed Dec 27, 2024
1 parent 7083735 commit fbe17d1
Show file tree
Hide file tree
Showing 39 changed files with 204 additions and 255 deletions.
9 changes: 2 additions & 7 deletions common/ASC.Api.Core/Auth/ScopesRequirement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode

namespace ASC.Api.Core.Auth;
public class ScopesRequirement : IAuthorizationRequirement
public class ScopesRequirement(string scopes) : IAuthorizationRequirement
{
public string Scopes { get; }

public ScopesRequirement(string scopes)
{
Scopes = scopes ?? throw new ArgumentNullException(nameof(scopes));
}
public string Scopes { get; } = scopes ?? throw new ArgumentNullException(nameof(scopes));
}
7 changes: 3 additions & 4 deletions common/ASC.Api.Core/Cors/Services/DynamicCorsPolicyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ public class DynamicCorsPolicyService : IDynamicCorsPolicyService
private readonly CorsOptions _options;
private readonly IDynamicCorsPolicyResolver _dynamicCorsPolicyResolver;

public DynamicCorsPolicyService(IOptions<CorsOptions> options,
IDynamicCorsPolicyResolver dynamicCorsPolicyResolver)
public DynamicCorsPolicyService(IOptions<CorsOptions> options, IDynamicCorsPolicyResolver dynamicCorsPolicyResolver)
{
ArgumentNullException.ThrowIfNull(options);

Expand Down Expand Up @@ -163,9 +162,9 @@ private static void AddHeaderValues(IList<string> target, IList<string> headerVa
return;
}

for (var i = 0; i < headerValues.Count; i++)
foreach (var t in headerValues)
{
target.Add(headerValues[i]);
target.Add(t);
}
}

Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Api.Core/Middleware/CommonApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected internal SuccessApiResponse(HttpContext httpContext, object response)

Links =
[
new() { Href = httpContext.Request.GetDisplayUrl(), Action = httpContext.Request.Method }
new Link { Href = httpContext.Request.GetDisplayUrl(), Action = httpContext.Request.Method }
];
}
}
Expand Down
8 changes: 5 additions & 3 deletions common/ASC.Common/Caching/KafkaCacheNotify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode

using Confluent.Kafka.Admin;

namespace ASC.Common.Caching;

[Singleton]
Expand Down Expand Up @@ -121,8 +123,8 @@ async Task ActionAsync()
//TODO: must add checking exist
await adminClient.CreateTopicsAsync(
[
new()
{
new TopicSpecification
{
Name = channelName,
NumPartitions = 1,
ReplicationFactor = 1
Expand Down Expand Up @@ -185,7 +187,7 @@ public void Dispose()
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
protected void Dispose(bool disposing)
{
if (!_disposedValue)
{
Expand Down
7 changes: 1 addition & 6 deletions common/ASC.Common/Threading/DistributedTaskQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,9 @@ public class DistributedTaskQueue(IServiceProvider serviceProvider,
/// </summary>
private int _maxThreadsCount = 1;
private string _name;
private int _timeUntilUnregisterInSeconds;
private TaskScheduler Scheduler { get; set; } = TaskScheduler.Default;

public int TimeUntilUnregisterInSeconds
{
get => _timeUntilUnregisterInSeconds;
set => _timeUntilUnregisterInSeconds = value;
}
public int TimeUntilUnregisterInSeconds { get; set; }

public string Name
{
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Core.Common/Billing/License/LicenseReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private async Task LicenseToDBAsync(License license)

var tariff = new Tariff
{
Quotas = [new(quota.TenantId, 1)],
Quotas = [new Quota(quota.TenantId, 1)],
DueDate = license.DueDate
};

Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Core.Common/Billing/TariffService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public async Task<Tariff> GetTariffAsync(int tenantId, bool withRequestToPayment

tariff = new Tariff
{
Quotas = [new(quota.TenantId, 1)],
Quotas = [new Quota(quota.TenantId, 1)],
DueDate = DateTime.UtcNow.AddDays(DefaultTrialPeriod)
};

Expand Down
6 changes: 2 additions & 4 deletions common/ASC.Core.Common/Data/DbSettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ private async Task<bool> SaveAsync<T>(T settings, int tenantId, Guid userId) whe
{
context.WebstudioSettings.Remove(s);
}

await context.SaveChangesAsync();
}
else
{
Expand All @@ -221,10 +219,10 @@ private async Task<bool> SaveAsync<T>(T settings, int tenantId, Guid userId) whe
};

await context.AddOrUpdateAsync(q => q.WebstudioSettings, s);

await context.SaveChangesAsync();
}

await context.SaveChangesAsync();

await dbSettingsManagerCache.RemoveAsync(key);

_cache.Insert(key, settings, _expirationTimeout);
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Core.Common/HostedSolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public async Task SetTariffAsync(int tenant, bool paid)
var quota = (await quotaService.GetTenantQuotasAsync()).FirstOrDefault(q => paid ? q.NonProfit : q.Trial);
if (quota != null)
{
await tariffService.SetTariffAsync(tenant, new Tariff { Quotas = [new(quota.TenantId, 1)], DueDate = DateTime.MaxValue });
await tariffService.SetTariffAsync(tenant, new Tariff { Quotas = [new Quota(quota.TenantId, 1)], DueDate = DateTime.MaxValue });
}
}

Expand Down
10 changes: 2 additions & 8 deletions common/ASC.Core.Common/Notify/Messages/NoticeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public class NoticeMessage : INoticeMessage
[NonSerialized]
private readonly List<ITagValue> _arguments = [];

[NonSerialized]
private IPattern _pattern;

public NoticeMessage() { }

public NoticeMessage(IDirectRecipient recipient, INotifyAction action, string objectID)
Expand Down Expand Up @@ -64,11 +61,8 @@ public NoticeMessage(IDirectRecipient recipient, string subject, string body, st

public IDirectRecipient Recipient { get; private set; }

public IPattern Pattern
{
get => _pattern;
internal set => _pattern = value;
}
[field: NonSerialized]
public IPattern Pattern { get; internal set; }

public INotifyAction Action { get; private set; }

Expand Down
37 changes: 18 additions & 19 deletions common/ASC.Core.Common/Notify/RecipientProviderImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,29 @@ namespace ASC.Core.Notify;
[Scope(typeof(IRecipientProvider))]
public class RecipientProviderImpl(UserManager userManager) : IRecipientProvider
{
public virtual async Task<IRecipient> GetRecipientAsync(string id)
public async Task<IRecipient> GetRecipientAsync(string id)
{
if (TryParseGuid(id, out var recID))
if (!TryParseGuid(id, out var recID))
{
var user = await userManager.GetUsersAsync(recID);
if (user.Id != Constants.LostUser.Id)
{
return new DirectRecipient(user.Id.ToString(), user.ToString());
}
return null;
}

var group = await userManager.GetGroupInfoAsync(recID);
if (group.ID != Constants.LostGroupInfo.ID)
{
return new RecipientsGroup(group.ID.ToString(), group.Name);
}
var user = await userManager.GetUsersAsync(recID);
if (user.Id != Constants.LostUser.Id)
{
return new DirectRecipient(user.Id.ToString(), user.ToString());
}

var group = await userManager.GetGroupInfoAsync(recID);
if (group.ID != Constants.LostGroupInfo.ID)
{
return new RecipientsGroup(group.ID.ToString(), group.Name);
}

return null;
}

public virtual async Task<IRecipient[]> GetGroupEntriesAsync(IRecipientsGroup group)
public async Task<IRecipient[]> GetGroupEntriesAsync(IRecipientsGroup group)
{
ArgumentNullException.ThrowIfNull(group);

Expand All @@ -69,7 +71,7 @@ public virtual async Task<IRecipient[]> GetGroupEntriesAsync(IRecipientsGroup gr
return result.ToArray();
}

public virtual async Task<IRecipientsGroup[]> GetGroupsAsync(IRecipient recipient)
public async Task<IRecipientsGroup[]> GetGroupsAsync(IRecipient recipient)
{
ArgumentNullException.ThrowIfNull(recipient);

Expand All @@ -87,17 +89,14 @@ public virtual async Task<IRecipientsGroup[]> GetGroupsAsync(IRecipient recipien
}
else if (recipient is IDirectRecipient)
{
foreach (var group in (await userManager.GetUserGroupsAsync(recID, IncludeType.Distinct)))
{
result.Add(new RecipientsGroup(group.ID.ToString(), group.Name));
}
result.AddRange((await userManager.GetUserGroupsAsync(recID, IncludeType.Distinct)).Select(group => new RecipientsGroup(group.ID.ToString(), group.Name)));
}
}

return result.ToArray();
}

public virtual async Task<string[]> GetRecipientAddressesAsync(IDirectRecipient recipient, string senderName)
public async Task<string[]> GetRecipientAddressesAsync(IDirectRecipient recipient, string senderName)
{
ArgumentNullException.ThrowIfNull(recipient);

Expand Down
22 changes: 10 additions & 12 deletions common/ASC.Core.Common/Notify/TopSubscriptionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

namespace ASC.Notify.Model;

public class TopSubscriptionProvider(IRecipientProvider recipientProvider,
ISubscriptionProvider directSubscriptionProvider)
: ISubscriptionProvider
public class TopSubscriptionProvider(IRecipientProvider recipientProvider, ISubscriptionProvider directSubscriptionProvider) : ISubscriptionProvider
{
private readonly string[] _defaultSenderMethods = [];
private readonly ISubscriptionProvider _subscriptionProvider = directSubscriptionProvider ?? throw new ArgumentNullException(nameof(directSubscriptionProvider));
Expand All @@ -42,7 +40,7 @@ public TopSubscriptionProvider(IRecipientProvider recipientProvider, ISubscripti
}


public virtual async Task<string[]> GetSubscriptionMethodAsync(INotifyAction action, IRecipient recipient)
public async Task<string[]> GetSubscriptionMethodAsync(INotifyAction action, IRecipient recipient)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(recipient);
Expand All @@ -64,7 +62,7 @@ public virtual async Task<string[]> GetSubscriptionMethodAsync(INotifyAction act
return senders is { Length: > 0 } ? senders : _defaultSenderMethods;
}

public virtual async Task<IRecipient[]> GetRecipientsAsync(INotifyAction action, string objectID)
public async Task<IRecipient[]> GetRecipientsAsync(INotifyAction action, string objectID)
{
ArgumentNullException.ThrowIfNull(action);

Expand All @@ -75,7 +73,7 @@ public virtual async Task<IRecipient[]> GetRecipientsAsync(INotifyAction action,
return recipents.ToArray();
}

public virtual async Task<bool> IsUnsubscribeAsync(IDirectRecipient recipient, INotifyAction action, string objectID)
public async Task<bool> IsUnsubscribeAsync(IDirectRecipient recipient, INotifyAction action, string objectID)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(recipient);
Expand All @@ -84,15 +82,15 @@ public virtual async Task<bool> IsUnsubscribeAsync(IDirectRecipient recipient, I
}


public virtual async Task SubscribeAsync(INotifyAction action, string objectID, IRecipient recipient)
public async Task SubscribeAsync(INotifyAction action, string objectID, IRecipient recipient)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(recipient);

await _subscriptionProvider.SubscribeAsync(action, objectID, recipient);
}

public virtual async Task UnSubscribeAsync(INotifyAction action, string objectID, IRecipient recipient)
public async Task UnSubscribeAsync(INotifyAction action, string objectID, IRecipient recipient)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(recipient);
Expand All @@ -114,7 +112,7 @@ public async Task UnSubscribeAsync(INotifyAction action)
await _subscriptionProvider.UnSubscribeAsync(action);
}

public virtual async Task UnSubscribeAsync(INotifyAction action, IRecipient recipient)
public async Task UnSubscribeAsync(INotifyAction action, IRecipient recipient)
{
var objects = await GetSubscriptionsAsync(action, recipient);
foreach (var objectID in objects)
Expand All @@ -123,7 +121,7 @@ public virtual async Task UnSubscribeAsync(INotifyAction action, IRecipient reci
}
}

public virtual async Task UpdateSubscriptionMethodAsync(INotifyAction action, IRecipient recipient, params string[] senderNames)
public async Task UpdateSubscriptionMethodAsync(INotifyAction action, IRecipient recipient, params string[] senderNames)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(recipient);
Expand All @@ -132,7 +130,7 @@ public virtual async Task UpdateSubscriptionMethodAsync(INotifyAction action, IR
await _subscriptionProvider.UpdateSubscriptionMethodAsync(action, recipient, senderNames);
}

public virtual async Task<object> GetSubscriptionRecordAsync(INotifyAction action, IRecipient recipient, string objectID)
public async Task<object> GetSubscriptionRecordAsync(INotifyAction action, IRecipient recipient, string objectID)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(recipient);
Expand All @@ -159,7 +157,7 @@ public virtual async Task<object> GetSubscriptionRecordAsync(INotifyAction actio
return subscriptionRecord;
}

public virtual async Task<string[]> GetSubscriptionsAsync(INotifyAction action, IRecipient recipient, bool checkSubscribe = true)
public async Task<string[]> GetSubscriptionsAsync(INotifyAction action, IRecipient recipient, bool checkSubscribe = true)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(recipient);
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Data.Backup.Core/BackupAjaxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public async Task<Schedule> GetScheduleAsync()
}

schedule.StorageType = BackupStorageType.ThirdPartyConsumer;
schedule.StorageParams = new();
schedule.StorageParams = new Dictionary<string, string>();
foreach (var r in consumer.AdditionalKeys)
{
schedule.StorageParams.Add(r, await consumer.GetAsync(r));
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Data.Storage/StorageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task InvokeAsync(HttpContext context, TenantManager tenantManager,
return;
}

var headers = header != null && header.Length > 0 ? header.Split('&').Select(HttpUtility.UrlDecode).ToList() : [];
var headers = header is { Length: > 0 } ? header.Split('&').Select(HttpUtility.UrlDecode).ToList() : [];

if (storage.ContentAsAttachment)
{
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Migration/Migrators/Model/MigrationGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MigrationGroup
public HashSet<string> UserKeys { get; set; }
public string ManagerKey { get; set; }
public bool ShouldImport { get; set; }
public virtual MigratingApiGroup ToApiInfo()
public MigratingApiGroup ToApiInfo()
{
return new MigratingApiGroup
{
Expand Down
4 changes: 2 additions & 2 deletions common/ASC.Migration/Migrators/Model/MigrationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MigrationInfo
public int FailedUsers { get; set; }
public List<string> Errors { get; set; } = [];

public virtual MigrationApiInfo ToApiInfo()
public MigrationApiInfo ToApiInfo()
{
return new MigrationApiInfo
{
Expand All @@ -61,7 +61,7 @@ public virtual MigrationApiInfo ToApiInfo()
};
}

public virtual void Merge(MigrationApiInfo apiInfo)
public void Merge(MigrationApiInfo apiInfo)
{
Users.AddRange(ExistUsers);
Users.AddRange(WithoutEmailUsers);
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Migration/Migrators/Model/MigrationStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class MigrationStorage
public bool ShouldImportSharedFiles { get; set; }
public bool ShouldImportSharedFolders { get; set; }

public virtual MigratingApiFiles ToApiInfo()
public MigratingApiFiles ToApiInfo()
{
return new MigratingApiFiles
{
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Migration/Migrators/Model/MigrationUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MigrationUser(DisplayUserSettingsHelper displayUserSettingsHelper)
public bool HasPhoto { get; set; }
public string PathToPhoto { get; set; }

public virtual MigratingApiUser ToApiInfo(string key)
public MigratingApiUser ToApiInfo(string key)
{
return new MigratingApiUser
{
Expand Down
Loading

0 comments on commit fbe17d1

Please sign in to comment.