实现两个用户需求:消息内容完整显示、按 Gotify Application 分类。 新增: - Models/GotifyApplication.cs:对应 GET /application 返回字段(id/name/description/defaultPriority/createdAt/internal) - Services/ApplicationCache.cs:在内存里缓存 id→Application 映射,提供 GetName/Get 解析,Updated 事件触发 UI 刷新 - Services/GotifyApiService.GetApplicationsAsync:GET /application?limit=999 拉取应用列表 - Converters/ValueConverters.cs: - PriorityToBrushConverter:优先级 0/1-4/5-7/8+ 分别用浅灰/灰蓝/橙/红区分 - AppNameToBrushConverter:对应用名做 FNV-1a 32-bit hash → HSL 色相,让每个应用在历史里视觉上可区分 - GotifyMessage.AppName 字段(JsonIgnore),由 UI 在加入历史前从缓存查后填入 主窗重构(两栏布局): - 左侧历史列表:用 DataTemplate 自定义每行(优先级徽章 + 应用名彩色 + 标题 + 消息预览 TextWrapping+Trimming),不再用 GridView 单行 - 右侧详情面板:选中消息后显示完整内容(应用名大标题、描述、优先级+说明、时间、标题、App ID、消息正文、URL 链接) - 顶部状态条:状态点颜色(绿/橙/红/灰)+ 应用数显示(实时更新) - 新增按钮「刷新应用」,手动 GET /application,用于用户新建 Gotify app 后同步 - URL 链接(如果有)在详情面板显示,点击用默认浏览器打开 - SaveButton 在保存配置后自动重新拉应用列表+历史,不用重启 App.xaml.cs 改造:创建 ApplicationCache,启动时后台异步刷新一次,订阅 Updated 事件让 MainWindow 重新解析已加载消息的 AppName(因为消息先到,缓存可能后到) Color/Brush/MessageBox 在 WPF+WinForms 双开项目里撞名,ValueConverters 和 MainWindow.xaml.cs 都加 using alias 固定走 WPF。 实测:启动后 ApplicationCache refreshed count=5,WebSocket connected,历史拉了 50 条股票分析报告,通知正常弹。
109 lines
4.2 KiB
C#
109 lines
4.2 KiB
C#
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Text.Json;
|
|
using WindowsGotify.Models;
|
|
|
|
namespace WindowsGotify.Services;
|
|
|
|
public sealed class GotifyApiService
|
|
{
|
|
private readonly AppConfig _config;
|
|
private readonly Logger _logger;
|
|
private readonly HttpClient _httpClient;
|
|
private readonly object _httpLock = new();
|
|
private string _appliedBaseUrl = "";
|
|
private string _appliedToken = "";
|
|
|
|
public GotifyApiService(AppConfig config, Logger logger)
|
|
{
|
|
_config = config;
|
|
_logger = logger;
|
|
_httpClient = new HttpClient
|
|
{
|
|
Timeout = TimeSpan.FromSeconds(15)
|
|
};
|
|
SyncAuthHeaders();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检测 URL/Token 是否变了,变了就刷新 HttpClient 的 Authorization。
|
|
/// 注意:HttpClient.BaseAddress 构造后只读,所以我们用绝对 URI 绕过。
|
|
/// </summary>
|
|
private void SyncAuthHeaders()
|
|
{
|
|
var baseUrl = (_config.Gotify.Url ?? "").TrimEnd('/');
|
|
var token = _config.Gotify.ClientToken ?? "";
|
|
|
|
if (_appliedBaseUrl == baseUrl && _appliedToken == token)
|
|
return;
|
|
|
|
lock (_httpLock)
|
|
{
|
|
if (_appliedBaseUrl == baseUrl && _appliedToken == token)
|
|
return;
|
|
|
|
// 显式给默认 header;实际请求走绝对 URI,所以 BaseAddress 永远为空
|
|
_httpClient.DefaultRequestHeaders.Authorization =
|
|
string.IsNullOrEmpty(token) ? null : new AuthenticationHeaderValue("Bearer", token);
|
|
_appliedBaseUrl = baseUrl;
|
|
_appliedToken = token;
|
|
}
|
|
}
|
|
|
|
private Uri BuildUri(string path)
|
|
{
|
|
var baseUrl = (_config.Gotify.Url ?? "").TrimEnd('/');
|
|
if (string.IsNullOrEmpty(baseUrl))
|
|
throw new InvalidOperationException("Gotify URL 未配置。请在主窗填入 URL 并保存。");
|
|
return new Uri(baseUrl + "/" + path);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拉取 lastId 之后的增量消息(去重补齐用)。
|
|
/// </summary>
|
|
public async Task<List<GotifyMessage>> GetMessagesSinceAsync(long sinceId, int limit, CancellationToken ct)
|
|
{
|
|
SyncAuthHeaders();
|
|
var path = $"message?limit={Math.Clamp(limit, 1, 100)}&since={Math.Max(0, sinceId)}";
|
|
using var response = await _httpClient.GetAsync(BuildUri(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)
|
|
{
|
|
SyncAuthHeaders();
|
|
var path = $"message?limit={Math.Clamp(limit, 1, 100)}&since=0";
|
|
using var response = await _httpClient.GetAsync(BuildUri(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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拉取所有 Gotify Application,供 UI 用来把消息 AppId 解析为应用名。
|
|
/// </summary>
|
|
public async Task<List<GotifyApplication>> GetApplicationsAsync(CancellationToken ct)
|
|
{
|
|
SyncAuthHeaders();
|
|
var path = "application?limit=999";
|
|
using var response = await _httpClient.GetAsync(BuildUri(path), ct);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(ct);
|
|
var list = await JsonSerializer.DeserializeAsync<List<GotifyApplication>>(stream, cancellationToken: ct);
|
|
return list ?? new List<GotifyApplication>();
|
|
}
|
|
}
|