From 62922bbfe9b354ef7d863d4d60c83da2db413465 Mon Sep 17 00:00:00 2001 From: guqing <1484563614@qq.com> Date: Fri, 6 Dec 2024 17:38:16 +0800 Subject: [PATCH] refactor: add extension point definition --- .../run/halo/feed/TelemetryEventInfo.java | 5 +++ .../java/run/halo/feed/RssXmlBuilder.java | 2 +- .../feed/telemetry/TelemetryEndpoint.java | 32 ++++++++++++------ app/src/main/resources/1pixel.png | Bin 0 -> 68 bytes .../resources/extensions/ext-definition.yaml | 11 ++++++ 5 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 app/src/main/resources/1pixel.png diff --git a/api/src/main/java/run/halo/feed/TelemetryEventInfo.java b/api/src/main/java/run/halo/feed/TelemetryEventInfo.java index 5c54876..6da15ad 100644 --- a/api/src/main/java/run/halo/feed/TelemetryEventInfo.java +++ b/api/src/main/java/run/halo/feed/TelemetryEventInfo.java @@ -2,8 +2,10 @@ import java.util.Objects; import lombok.Data; +import lombok.Getter; import lombok.experimental.Accessors; import org.springframework.http.HttpHeaders; +import org.springframework.lang.NonNull; @Data @Accessors(chain = true) @@ -11,12 +13,15 @@ public class TelemetryEventInfo { private String pageUrl; private String screen; private String language; + private String languageRegion; private String title; private String referrer; private String ip; private String userAgent; private String browser; private String os; + + @Getter(onMethod_ = @NonNull) private HttpHeaders headers; @Override diff --git a/app/src/main/java/run/halo/feed/RssXmlBuilder.java b/app/src/main/java/run/halo/feed/RssXmlBuilder.java index 8e7ce1f..01f714d 100644 --- a/app/src/main/java/run/halo/feed/RssXmlBuilder.java +++ b/app/src/main/java/run/halo/feed/RssXmlBuilder.java @@ -232,7 +232,7 @@ private String getDescriptionWithTelemetry(RSS2.Item item) { ); // Append telemetry image to description - return item.getDescription() + telemetryImageHtml; + return telemetryImageHtml + item.getDescription(); } static List nullSafeList(List list) { diff --git a/app/src/main/java/run/halo/feed/telemetry/TelemetryEndpoint.java b/app/src/main/java/run/halo/feed/telemetry/TelemetryEndpoint.java index c75b5e3..d0a8e45 100644 --- a/app/src/main/java/run/halo/feed/telemetry/TelemetryEndpoint.java +++ b/app/src/main/java/run/halo/feed/telemetry/TelemetryEndpoint.java @@ -2,10 +2,13 @@ import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; import org.springframework.http.CacheControl; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerRequest; @@ -16,10 +19,15 @@ @RequiredArgsConstructor public class TelemetryEndpoint { public static final String TELEMETRY_PATH = "/plugins/feed/assets/telemetry.gif"; - static final String ONE_PIXEL_GIF_BASE64 = - "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"; + static final Resource ONE_PIXEL; private final TelemetryRecorderDelegator telemetryRecorderDelegator; + static { + // RSS readers may thumbnail images, and using base64 images may cause RSS readers to + // fail to parse correctly. + ONE_PIXEL = new ClassPathResource("1pixel.png", TelemetryEndpoint.class.getClassLoader()); + } + @Bean public RouterFunction telemetryImageRouter() { return RouterFunctions.route() @@ -28,7 +36,7 @@ public RouterFunction telemetryImageRouter() { return ServerResponse.ok() .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_GIF_VALUE) .cacheControl(CacheControl.noCache()) - .bodyValue(ONE_PIXEL_GIF_BASE64); + .bodyValue(ONE_PIXEL); }) .build(); } @@ -36,8 +44,7 @@ public RouterFunction telemetryImageRouter() { private TelemetryEventInfo createEventInfo(ServerRequest request) { var userAgent = request.headers().firstHeader(HttpHeaders.USER_AGENT); var browser = BrowserDetector.detectBrowser(userAgent); - var acceptLang = request.headers().firstHeader(HttpHeaders.ACCEPT_LANGUAGE); - return new TelemetryEventInfo() + var eventInfo = new TelemetryEventInfo() .setTitle(queryParamOrNull(request, "title")) .setPageUrl(queryParamOrNull(request, "url")) .setBrowser(browser.nameVersion()) @@ -46,13 +53,18 @@ private TelemetryEventInfo createEventInfo(ServerRequest request) { .setReferrer(request.headers().firstHeader(HttpHeaders.REFERER)) .setScreen(browser.screen()) .setUserAgent(userAgent) - .setLanguage(parseLanguage(acceptLang)) .setHeaders(request.headers().asHttpHeaders()); - } - private String parseLanguage(String acceptLanguage) { - var languages = AcceptLanguageParser.parseAcceptLanguage(acceptLanguage); - return languages.isEmpty() ? null : languages.get(0).code(); + var acceptLang = request.headers().firstHeader(HttpHeaders.ACCEPT_LANGUAGE); + var languages = AcceptLanguageParser.parseAcceptLanguage(acceptLang); + if (!CollectionUtils.isEmpty(languages)) { + var lang = languages.get(0); + eventInfo.setLanguage(languages.get(0).code()); + if (lang.region() != null) { + eventInfo.setLanguageRegion(lang.code() + "-" + lang.region()); + } + } + return eventInfo; } private static String queryParamOrNull(ServerRequest request, String name) { diff --git a/app/src/main/resources/1pixel.png b/app/src/main/resources/1pixel.png new file mode 100644 index 0000000000000000000000000000000000000000..909c66db1740b7c1b41eb4db6c414a7ab5bb6a23 GIT binary patch literal 68 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx0wlM}@Gt=>Zci7-kcwN$DG5Lh8v~O;;{|;n Oi^0>?&t;ucLK6U5DhwL{ literal 0 HcmV?d00001 diff --git a/app/src/main/resources/extensions/ext-definition.yaml b/app/src/main/resources/extensions/ext-definition.yaml index cb4211f..e587a7c 100644 --- a/app/src/main/resources/extensions/ext-definition.yaml +++ b/app/src/main/resources/extensions/ext-definition.yaml @@ -10,6 +10,17 @@ spec: icon: "/plugins/PluginFeed/assets/logo.svg" --- apiVersion: plugin.halo.run/v1alpha1 +kind: ExtensionPointDefinition +metadata: + name: feed-telemetry-recorder +spec: + className: run.halo.feed.TelemetryRecorder + displayName: "遥测内容访问量记录器" + description: "用于扩展 RSS 内容访问量的存储方式,如上报到 Umami" + type: MULTI_INSTANCE + icon: "/plugins/PluginFeed/assets/logo.svg" +--- +apiVersion: plugin.halo.run/v1alpha1 kind: ExtensionDefinition metadata: name: feed-category-post-rss-item