Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<!-- Keep product libraries and standalone uninstaller builds on the same
release as V-Notch.csproj. Release-pipeline overrides take precedence. -->
<PropertyGroup Condition="'$(_VNotchIsToolProject)' != 'true'">
<Version Condition="'$(Version)' == ''">2.4.2</Version>
<AssemblyVersion Condition="'$(AssemblyVersion)' == ''">2.4.2.0</AssemblyVersion>
<FileVersion Condition="'$(FileVersion)' == ''">2.4.2.0</FileVersion>
<Version Condition="'$(Version)' == ''">2.5.0</Version>
<AssemblyVersion Condition="'$(AssemblyVersion)' == ''">2.5.0.0</AssemblyVersion>
<FileVersion Condition="'$(FileVersion)' == ''">2.5.0.0</FileVersion>
<AnalysisLevel Condition="'$(StrictAnalyzers)' == 'true'">latest-recommended</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Expand Down
138 changes: 138 additions & 0 deletions Interop/RepoAnnouncementsNative.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace VNotch.Interop;

internal sealed record PinnedAnnouncementDto(
[property: JsonPropertyName("id")] string Id,
[property: JsonPropertyName("number")] ulong Number,
[property: JsonPropertyName("title")] string Title,
[property: JsonPropertyName("summary")] string Summary,
[property: JsonPropertyName("url")] string Url,
[property: JsonPropertyName("category")] string Category);

internal sealed record FetchAnnouncementsResponseDto(
[property: JsonPropertyName("announcements")] List<PinnedAnnouncementDto>? Announcements,
[property: JsonPropertyName("totalPinned")] int TotalPinned,
[property: JsonPropertyName("fromCache")] bool FromCache);

/// <summary>
/// Thin ABI 2.2 P/Invoke wrapper for pinned repository announcements in <c>vnotch_rust.dll</c>.
/// </summary>
internal static partial class RepoAnnouncementsNative
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true
};

[LibraryImport(NativeAbi.RustLibraryName)]
private static unsafe partial int vn_announcements_fetch_unseen_utf8(
byte* reqJson,
uint reqLen,
out VnOwnedBytes outJson);

[LibraryImport(NativeAbi.RustLibraryName)]
private static unsafe partial int vn_announcements_mark_seen_utf8(
byte* reqJson,
uint reqLen,
out uint outCount);

[LibraryImport(NativeAbi.RustLibraryName)]
private static unsafe partial int vn_announcements_parse_pinned_utf8(
byte* htmlUtf8,
uint htmlLen,
out VnOwnedBytes outJson);

internal static unsafe FetchAnnouncementsResponseDto FetchUnseen(
string cachePath,
string? url = null,
bool forceRefresh = false)
{
byte[] reqBytes = JsonSerializer.SerializeToUtf8Bytes(new
{
cachePath,
url,
forceRefresh
});

VnOwnedBytes owned = default;
try
{
int status;
fixed (byte* ptr = reqBytes)
{
status = vn_announcements_fetch_unseen_utf8(ptr, checked((uint)reqBytes.Length), out owned);
}

NativeResult.RequireSuccess(status, nameof(vn_announcements_fetch_unseen_utf8));
ReadOnlySpan<byte> span = owned.AsSpan();
if (span.IsEmpty)
{
return new FetchAnnouncementsResponseDto([], 0, false);
}

return JsonSerializer.Deserialize<FetchAnnouncementsResponseDto>(span, JsonOptions)
?? new FetchAnnouncementsResponseDto([], 0, false);
}
finally
{
owned.Free();
}
}

internal static unsafe uint MarkSeen(string cachePath, IReadOnlyList<string> ids)
{
if (ids.Count == 0)
{
return 0;
}

byte[] reqBytes = JsonSerializer.SerializeToUtf8Bytes(new
{
cachePath,
ids
});

int status;
uint count;
fixed (byte* ptr = reqBytes)
{
status = vn_announcements_mark_seen_utf8(ptr, checked((uint)reqBytes.Length), out count);
}

NativeResult.RequireSuccess(status, nameof(vn_announcements_mark_seen_utf8));
return count;
}

internal static unsafe IReadOnlyList<PinnedAnnouncementDto> ParsePinnedHtml(string html)
{
byte[] htmlBytes = Encoding.UTF8.GetBytes(html ?? string.Empty);
VnOwnedBytes owned = default;
try
{
int status;
fixed (byte* ptr = htmlBytes)
{
status = vn_announcements_parse_pinned_utf8(ptr, checked((uint)htmlBytes.Length), out owned);
}

NativeResult.RequireSuccess(status, nameof(vn_announcements_parse_pinned_utf8));
ReadOnlySpan<byte> span = owned.AsSpan();
if (span.IsEmpty)
{
return [];
}

return JsonSerializer.Deserialize<List<PinnedAnnouncementDto>>(span, JsonOptions) ?? [];
}
finally
{
owned.Free();
}
}
}
47 changes: 47 additions & 0 deletions Services/RepoAnnouncementService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using VNotch.Interop;

namespace VNotch.Services;

/// <summary>
/// Thin async facade over Rust pinned GitHub Discussions announcements fetcher and local seen-state.
/// </summary>
internal static class RepoAnnouncementService
{
private const string CacheFileName = "announcements-cache.json";

internal static string DefaultCachePath => Path.Combine(AppPaths.LocalAppDataRoot, CacheFileName);

internal static Task<IReadOnlyList<PinnedAnnouncementDto>> FetchUnseenAsync(
bool forceRefresh = false,
CancellationToken cancellationToken = default)
{
return Task.Run<IReadOnlyList<PinnedAnnouncementDto>>(() =>
{
cancellationToken.ThrowIfCancellationRequested();
var response = RepoAnnouncementsNative.FetchUnseen(DefaultCachePath, url: null, forceRefresh: forceRefresh);
return response.Announcements ?? [];
}, cancellationToken);
}

internal static void MarkSeen(string announcementId)
{
if (string.IsNullOrWhiteSpace(announcementId))
{
return;
}

try
{
_ = RepoAnnouncementsNative.MarkSeen(DefaultCachePath, [announcementId]);
}
catch (Exception ex)
{
RuntimeLog.Warn("ANNOUNCEMENT", $"Failed to record seen announcement '{announcementId}': {ex.Message}");
}
}
}
6 changes: 3 additions & 3 deletions Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@
"Microsoft.Extensions.DependencyInjection": "[8.0.1, )",
"Microsoft.ML.OnnxRuntime.DirectML": "[1.24.4, )",
"Microsoft.Web.WebView2": "[1.0.4078.44, )",
"VNotch.Application": "[2.4.2, )",
"VNotch.Core": "[2.4.2, )",
"VNotch.Application": "[2.5.0, )",
"VNotch.Core": "[2.5.0, )",
"Vortice.Direct3D9": "[3.8.3, )"
}
},
"vnotch.application": {
"type": "Project",
"dependencies": {
"VNotch.Core": "[2.4.2, )"
"VNotch.Core": "[2.5.0, )"
}
},
"vnotch.core": {
Expand Down
2 changes: 1 addition & 1 deletion Tools/Assert-Coverage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ param(

$ErrorActionPreference = 'Stop'
if ($AllProductionAssemblies) {
foreach ($assembly in 'V-Notch', 'VNotch.Core', 'VNotch.Application', 'VNotch.Infrastructure.Windows') {
foreach ($assembly in 'V-Notch', 'VNotch.Core', 'VNotch.Application') {
& $PSCommandPath -ResultsDirectory $ResultsDirectory -MinimumPercent $MinimumPercent -AssemblyName $assembly
}
return
Expand Down
6 changes: 3 additions & 3 deletions V-Notch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
reflection, COM interop or CPU/GPU translation backends. -->
<EnableCompressionInSingleFile Condition="'$(Configuration)' == 'Release' And '$(SelfContained)' == 'true'">true</EnableCompressionInSingleFile>
<PublishReferencesDocumentationFiles>false</PublishReferencesDocumentationFiles>
<Version>2.4.2</Version>
<AssemblyVersion>2.4.2.0</AssemblyVersion>
<FileVersion>2.4.2.0</FileVersion>
<Version>2.5.0</Version>
<AssemblyVersion>2.5.0.0</AssemblyVersion>
<FileVersion>2.5.0.0</FileVersion>
<Authors>w0otdev, viper1aa</Authors>
<Company>V-Notch</Company>
<Product>V-Notch</Product>
Expand Down
Loading
Loading