Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
0xF6 committed Jun 12, 2024
1 parent 6e6d1dd commit 7ebf6b2
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 86 deletions.
5 changes: 3 additions & 2 deletions src/controllers/PackageContentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ public async Task<IActionResult> DownloadReadmeAsync(string id, string version,
not null => NuGetVersion.Parse(version)
};

var readmeStream = await _content.GetPackageReadmeStreamOrNullAsync(id, ver, cancellationToken);
var readmeStream = await _content.GetPackageReadmeStreamOrNullAsync(id, ver, cancellationToken);

if (readmeStream == null)
return NotFound();

using var reader = new StreamReader(readmeStream);

var result = await reader.ReadToEndAsync();
var result = await reader.ReadToEndAsync(cancellationToken);

_cache.Set($"@/packages/{id}/{version}/readme", result,
ver.HasMetadata ? TimeSpan.FromMinutes(15) : TimeSpan.FromHours(6));
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/StateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public async Task<IActionResult> GetState()
var result = new PackagesState()
{
latest_packages = latest,
packages_state = new List<AnalyticsKeyValue>()
{
{ new("Package downloads", downloads) },
{ new("Package total", count) },
},
packages_state =
[
new("Package downloads", downloads),
new("Package total", count)
],
popular_packages = popular
};

Expand All @@ -49,5 +49,5 @@ public class PackagesState
{
public IReadOnlyCollection<string> popular_packages { get; set; }
public IReadOnlyCollection<string> latest_packages { get; set; }
public List<AnalyticsKeyValue> packages_state { get; set; } = new ();
public List<AnalyticsKeyValue> packages_state { get; set; } = [];
}
2 changes: 1 addition & 1 deletion src/logic/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void AddRegistryServices(this IServiceCollection services)
a.ProjectId = configuration.GetDatabaseConnectionString();
return a.Build();
});
services.AddSingleton(x => GetServiceFromProviders<FirestoreDb>(x));
services.AddSingleton(GetServiceFromProviders<FirestoreDb>);

services.TryAddSingleton<NullSearchIndexer>();
services.TryAddSingleton<NullSearchService>();
Expand Down
8 changes: 4 additions & 4 deletions src/logic/services/ImageDomainValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace core.services;
public class ImageDomainValidator
{
private static readonly TimeSpan RegexTimeout = TimeSpan.FromMinutes(1);
private static readonly Regex GithubBadgeUrlRegEx = new Regex("^(https|http):\\/\\/github\\.com\\/[^/]+\\/[^/]+(\\/actions)?\\/workflows\\/.*badge\\.svg", RegexOptions.IgnoreCase, RegexTimeout);
private static readonly Regex GithubBadgeUrlRegEx = new("^(https|http):\\/\\/github\\.com\\/[^/]+\\/[^/]+(\\/actions)?\\/workflows\\/.*badge\\.svg", RegexOptions.IgnoreCase, RegexTimeout);

public bool TryPrepareImageUrlForRendering(string uriString, out string readyUriString)
{
Expand Down Expand Up @@ -40,8 +40,8 @@ private bool IsTrustedImageDomain(Uri uri) =>
IsGitHubBadge(uri);


private static List<string> TrustedImageDomains = new List<string>()
{
private static List<string> TrustedImageDomains =
[
"api.bintray.com",
"api.codacy.com",
"app.codacy.com",
Expand Down Expand Up @@ -75,7 +75,7 @@ private bool IsTrustedImageDomain(Uri uri) =>
"raw.githubusercontent.com",
"user-images.githubusercontent.com",
"camo.githubusercontent.com"
};
];

private bool IsGitHubBadge(Uri uri)
{
Expand Down
6 changes: 3 additions & 3 deletions src/logic/services/MarkdownService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class MarkdownService
{
private readonly ImageDomainValidator _imageDomainValidator;
private static readonly TimeSpan RegexTimeout = TimeSpan.FromMinutes(1);
private static readonly Regex EncodedBlockQuotePattern = new Regex("^ {0,3}&gt;", RegexOptions.Multiline, RegexTimeout);
private static readonly Regex LinkPattern = new Regex("<a href=([\"\']).*?\\1", RegexOptions.None, RegexTimeout);
private static readonly Regex HtmlCommentPattern = new Regex("<!--.*?-->", RegexOptions.Singleline, RegexTimeout);
private static readonly Regex EncodedBlockQuotePattern = new("^ {0,3}&gt;", RegexOptions.Multiline, RegexTimeout);
private static readonly Regex LinkPattern = new("<a href=([\"\']).*?\\1", RegexOptions.None, RegexTimeout);
private static readonly Regex HtmlCommentPattern = new("<!--.*?-->", RegexOptions.Singleline, RegexTimeout);

public bool IsMarkdigMdRenderingEnabled { get; set; } = true;

Expand Down
4 changes: 2 additions & 2 deletions src/logic/services/NullPackageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public Task<PackageAddResult> AddAsync(Package package, UserRecord owner, Cancel
public async Task<IReadOnlyList<Package>> FindAsync(string id, bool includeUnlisted, CancellationToken cancellationToken)
=> new List<Package>().AsReadOnly();

public async Task<List<Package>> GetLatestPackagesByUserAsync(UserRecord user, CancellationToken token = default)
=> new List<Package>();
public async Task<List<Package>> GetLatestPackagesByUserAsync(UserRecord user, CancellationToken token = default) =>
[];

public Task<bool> ExistsAsync(string id, CancellationToken cancellationToken)
=> Task.FromResult(default(bool));
Expand Down
8 changes: 4 additions & 4 deletions src/logic/services/PackageValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ public class PackageValidator
private const long MaxAllowedContentForUploading = 1024 * 1024 * 300; // 300 MB
private static Profanity _censor = new ("./content");

private static readonly List<string> Reserved = new()
{
private static readonly List<string> Reserved =
[
"com0", "com1", "com2",
"com3", "com4", "com5",
"com6","com7", "com8", "com9",
"com6", "com7", "com8", "com9",
"builtins", "collections",
"debug", "std", "test", "bump",
"nul", "null", "prn", "aux",
"vin", "http", "grpc", "logger",
"core", "kernel", "live", "docker"
};
];

public static async Task ValidateExistAsync(Shard shard)
{
Expand Down
2 changes: 1 addition & 1 deletion src/logic/services/ServiceIndexService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public RegistryServiceIndex(IUrlGenerator url)
=> _url = url ?? throw new ArgumentNullException(nameof(url));

private ServiceIndexItem BuildResource(string name, string url) =>
new ServiceIndexItem
new()
{
ResourceUrl = url,
Type = name,
Expand Down
4 changes: 2 additions & 2 deletions src/logic/services/searchs/FirestoreSearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private async Task<List<List<PackageEntity>>> SearchInternalAsync(
.ToList();

if (packages.Count == 0)
return new List<List<PackageEntity>>();
return [];

var segment = await packages.ToAsyncEnumerable()
.Select(x => x.Collection("v"))
Expand All @@ -332,7 +332,7 @@ private async Task<List<List<PackageEntity>>> SearchInternalAsync(

if (lastPartitionKey != pkgID)
{
results.Add(new List<PackageEntity>());
results.Add([]);
lastPartitionKey = pkgID;
}

Expand Down
6 changes: 3 additions & 3 deletions src/logic/services/searchs/models/AutocompleteContext.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace core.services.searchs.models;
namespace core.services.searchs.models;

using Newtonsoft.Json;

public class AutocompleteContext
{
public static readonly AutocompleteContext Default = new AutocompleteContext
public static readonly AutocompleteContext Default = new()
{
Vocab = "http://schema.registry.vein-lang.org/schema#"
};

[JsonProperty("@vocab")]
public string Vocab { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/logic/services/searchs/models/SearchContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace core.services.searchs.models;

public class SearchContext
{
public static SearchContext Default(string registrationBaseUrl) => new SearchContext
public static SearchContext Default(string registrationBaseUrl) => new()
{
Vocab = "http://schema.registry.vein-lang.org/schema#",
Base = registrationBaseUrl
Expand Down
82 changes: 25 additions & 57 deletions src/profanity/Profanity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ public class Profanity

// Characters for creating variants
private readonly Dictionary<char, List<char>> _charsMapping =
new Dictionary<char, List<char>>()
new()
{
{ 'a', new List<char>() { 'a', '@', '*', '4' } },
{ 'i', new List<char>() { 'i', '*', 'l', '1' } },
{ 'o', new List<char>() { 'o', '*', '0', '@' } },
{ 'u', new List<char>() { 'u', '*', 'v' } },
{ 'v', new List<char>() { 'v', '*', 'u' } },
{ 'l', new List<char>() { 'l', '1' } },
{ 'e', new List<char>() { 'e', '*', '3' } },
{ 's', new List<char>() { 's', '$', '5' } }
{ 'a', ['a', '@', '*', '4'] },
{ 'i', ['i', '*', 'l', '1'] },
{ 'o', ['o', '*', '0', '@'] },
{ 'u', ['u', '*', 'v'] },
{ 'v', ['v', '*', 'u'] },
{ 'l', ['l', '1'] },
{ 'e', ['e', '*', '3'] },
{ 's', ['s', '$', '5'] }
};

// Data directory, in case the user wants to override the data path
private string dataDir = null;
private readonly string? dataDir = null;

/// <summary>
/// The profanity filter
/// </summary>
/// <param name="dataDir">Optional directory to search for alphabetic_unicode.json and profanity_wordlist.txt in</param>
public Profanity(string dataDir = null)
public Profanity(string? dataDir = null)
{
this.dataDir = dataDir;
Utils.LoadUnicodeSymbols(dataDir);
Expand All @@ -55,36 +55,18 @@ public Profanity(string dataDir = null)
/// Sets the original behavior mode flag
/// </summary>
/// <param name="value">originalBehaviorMode value</param>
public void SetOriginalBehaviorMode(bool value)
{
originalBehaviorMode = value;
}
public void SetOriginalBehaviorMode(bool value) => originalBehaviorMode = value;

private int CountNonAllowedCharacters(string word)
{
var count = 0;

foreach (var c in word)
{
if (!Utils.AllowedCharacters.Contains(c.ToString()))
{
count++;
}
}

return count;
}
private int CountNonAllowedCharacters(string word)
=> word.Count(c => !Utils.AllowedCharacters.Contains(c.ToString()));

/// <summary>
/// Adds words to the censored word set.
/// </summary>
/// <param name="newWords">An IEnumerable<string> of words to add</param>
public void AddCensorWords(IEnumerable<string> newWords)
{
foreach (var word in newWords)
{
CensorWordSet.Add(word);
}
foreach (var word in newWords) CensorWordSet.Add(word);
}

/// <summary>
Expand Down Expand Up @@ -129,28 +111,16 @@ private List<string> ReadWordList()
.ToList();
}

private List<string> GeneratePatternsFromWord(string word)
private IEnumerable<string> GeneratePatternsFromWord(string word)
{
var combos = new List<List<char>>();
foreach (var c in word)
{
combos.Add(!_charsMapping.ContainsKey(c) ? new List<char>() { c } : _charsMapping[c]);
}

var combos = word.Select(c => !_charsMapping.ContainsKey(c) ? [c] : _charsMapping[c]).ToList();
var product = combos.CartesianProduct();
return product.Select(combo => string.Join("", combo)).ToList();
}

private string GetReplacementForSwearWord(char censorChar, int count = 4)
{
private string GetReplacementForSwearWord(char censorChar, int count = 4) =>
// 4 is hardcoded for original behavior mode
if (originalBehaviorMode)
{
return new string(censorChar, 4);
}

return new string(censorChar, count);
}
originalBehaviorMode ? new string(censorChar, 4) : new string(censorChar, count);

/// <summary>
/// Return true if the input text has any swear words.
Expand All @@ -170,9 +140,7 @@ private string GetReplacementForSwearWord(char censorChar, int count = 4)
{
// Python: not set() = true
if (wordsIndices == null || wordsIndices.Count < 1)
{
wordsIndices = Utils.GetNextWords(text, startIndex, maxNumberCombinations);
}
else
{
wordsIndices.RemoveRange(0, 2);
Expand Down Expand Up @@ -300,7 +268,7 @@ public string Censor(string s, char censorChar = '*')

public static class Utils
{
public static readonly HashSet<string> AllowedCharacters = new HashSet<string>();
public static readonly HashSet<string> AllowedCharacters = [];

/// <summary>
/// Add ascii_letters, digits, and @$*"' to the allowed character list (AllowedCharacters).
Expand Down Expand Up @@ -442,11 +410,11 @@ public static (bool, int) AnyNextWordsFormSwearWord(string curWord, string text,
// Return an empty string if there are no other words
if (nextWordStartIndex >= s.Length - 1)
{
return new List<(string, int)>()
{
("", nextWordStartIndex),
("", nextWordStartIndex)
};
return
[
("", nextWordStartIndex),
("", nextWordStartIndex)
];
}

// Combine the words into a list
Expand Down

0 comments on commit 7ebf6b2

Please sign in to comment.