feat: 落地 V1 全部源码 — WebSocket 通知 + 应用内历史查看
17 个源文件 + .gitignore + 默认 config.json 模板,严格按方案文档第 6 节实现。
用户核心场景:
- Windows 接收 Gotify 通知(WS /stream → AppNotificationBuilder)
- 应用内查看历史消息(打开 MainWindow 触发 GetRecentMessagesAsync,实时通过 MessageReceived 事件追加)
- 列表条目双击打开 extras.client::notification.click.url
关键变更点:
- GotifyMessage.Extras 类型由 Dictionary<string,object> 改为 Dictionary<string,JsonElement>,避免 System.Text.Json 反序列化时丢失嵌套结构
- 新增 HistoryConfig.ViewerLimit(默认 50,UI 硬上限 200)
- GotifyApiService.GetRecentMessagesAsync:since=0 拉最近 N 条,OrderByDescending(Id) 保证顺序
- GotifyWebSocketService 新增 MessageReceived 事件,MainWindow 用 Dispatcher.Invoke 切回 UI 线程追加
- NotificationService.AddArgument('url=...') 透传 click URL 给 OnNotificationInvoked
- MainWindow.xaml 增加 ListView + 刷新按钮,使用 ObservableCollection<GotifyMessage>
- 默认 config.json 用 https://gotify.example.com 占位 URL,ClientToken 留空,首启动自动写入 %LOCALAPPDATA%
依赖:Microsoft.WindowsAppSDK 2.4.0(方案文档固定版本)。
构建:本机 .NET 8 SDK 未安装,build 待用户在装好 SDK 后执行: dotnet restore && dotnet build -c Release
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using WindowsGotify.Models;
|
||||
|
||||
namespace WindowsGotify.Services;
|
||||
|
||||
public sealed class GotifyApiService
|
||||
{
|
||||
private readonly GotifyConfig _config;
|
||||
private readonly Logger _logger;
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public GotifyApiService(AppConfig config, Logger logger)
|
||||
{
|
||||
_config = config.Gotify;
|
||||
_logger = logger;
|
||||
_httpClient = new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(_config.Url.TrimEnd('/') + "/"),
|
||||
Timeout = TimeSpan.FromSeconds(15)
|
||||
};
|
||||
|
||||
_httpClient.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("Bearer", _config.ClientToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拉取 lastId 之后的增量消息(去重补齐用)。
|
||||
/// </summary>
|
||||
public async Task<List<GotifyMessage>> GetMessagesSinceAsync(long sinceId, int limit, CancellationToken ct)
|
||||
{
|
||||
var path = $"message?limit={Math.Clamp(limit, 1, 100)}&since={Math.Max(0, sinceId)}";
|
||||
using var response = await _httpClient.GetAsync(path, ct);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
await using var stream = await response.Content.ReadAsStreamAsync(ct);
|
||||
var page = await JsonSerializer.DeserializeAsync<GotifyMessagePage>(stream, cancellationToken: ct);
|
||||
return page?.Messages ?? new List<GotifyMessage>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拉取最近 N 条消息(历史查看用)。since=0 让服务器从最新往旧返回。
|
||||
/// </summary>
|
||||
public async Task<List<GotifyMessage>> GetRecentMessagesAsync(int limit, CancellationToken ct)
|
||||
{
|
||||
var path = $"message?limit={Math.Clamp(limit, 1, 100)}&since=0";
|
||||
using var response = await _httpClient.GetAsync(path, ct);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
await using var stream = await response.Content.ReadAsStreamAsync(ct);
|
||||
var page = await JsonSerializer.DeserializeAsync<GotifyMessagePage>(stream, cancellationToken: ct);
|
||||
var list = page?.Messages ?? new List<GotifyMessage>();
|
||||
|
||||
// Gotify 的 since=0 行为:返回数据库里所有消息中的 limit 条;按 id 降序再取一次保证正确
|
||||
return list.OrderByDescending(m => m.Id).Take(Math.Clamp(limit, 1, 100)).ToList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user