Skip to content

Commit

Permalink
feat: add rss telemetry recorder implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Dec 6, 2024
1 parent d90548c commit 233c740
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 17 deletions.
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id "com.github.node-gradle.node" version "5.0.0"
id "run.halo.plugin.devtools" version "0.0.9"
id "run.halo.plugin.devtools" version "0.4.1"
id "io.freefair.lombok" version "8.0.1"
id 'java'
}
Expand All @@ -13,13 +13,15 @@ java {
}

repositories {
mavenLocal()
maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots' }
mavenCentral()
}

dependencies {
implementation platform('run.halo.tools.platform:plugin:2.17.0-SNAPSHOT')
implementation platform('run.halo.tools.platform:plugin:2.20.11')
compileOnly 'run.halo.app:api'
compileOnly "run.halo.feed:api:0.0.2-SNAPSHOT"

testImplementation 'run.halo.app:api'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down Expand Up @@ -54,5 +56,5 @@ build {
}

halo {
version = "2.17"
version = "2.20.11"
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/run/halo/umami/BasicProp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package run.halo.umami;

import lombok.Data;
import reactor.core.publisher.Mono;
import run.halo.app.plugin.ReactiveSettingFetcher;

@Data
public class BasicProp {
private String websiteId;
private String endpoint;
private String scriptName;
private String url;

public static Mono<BasicProp> fetch(ReactiveSettingFetcher settingFetcher) {
return settingFetcher.fetch("basic", BasicProp.class);
}
}
63 changes: 63 additions & 0 deletions src/main/java/run/halo/umami/RssTelemetryUmamiRecorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package run.halo.umami;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import run.halo.app.infra.ExternalUrlSupplier;
import run.halo.app.plugin.ReactiveSettingFetcher;
import run.halo.feed.TelemetryEventInfo;
import run.halo.feed.TelemetryRecorder;

import java.net.URL;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

@RequiredArgsConstructor
public class RssTelemetryUmamiRecorder implements TelemetryRecorder {
private final WebClient webClient = WebClient.builder().build();
private final ReactiveSettingFetcher settingFetcher;
private final ExternalUrlSupplier externalUrlSupplier;

@Override
public void record(TelemetryEventInfo eventInfo) {
var propOpt = BasicProp.fetch(settingFetcher).blockOptional();
if (propOpt.isEmpty()) {
return;
}
var siteUrl = propOpt.get().getEndpoint();
var webSiteId = propOpt.get().getWebsiteId();
if (StringUtils.isBlank(siteUrl) || StringUtils.isNotBlank(webSiteId)) {
return;
}

// https://umami.is/docs/api/sending-stats
webClient.post()
.uri(StringUtils.removeEnd(siteUrl, "/") + "/api/send")
.body(Mono.just(createBody(webSiteId, eventInfo)), Map.class)
.retrieve()
.bodyToMono(String.class)
.block();
}

private Map<String, Object> createBody(String webSiteId, TelemetryEventInfo eventInfo) {
var hostname = Optional.ofNullable(externalUrlSupplier.getRaw())
.map(URL::getAuthority)
.orElse(StringUtils.EMPTY);

return Map.of("payload", Map.of(
"hostname", hostname,
"language", eventInfo.getLanguage(),
"referrer", eventInfo.getReferrer(),
"screen", eventInfo.getScreen(),
"title", eventInfo.getTitle(),
"url", eventInfo.getPageUrl(),
"website", webSiteId,
"name", "Rss Reader"
),
"type", "event"
);
}
}
15 changes: 3 additions & 12 deletions src/main/java/run/halo/umami/UmamiTrackerProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package run.halo.umami;

import lombok.Data;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
Expand All @@ -22,10 +21,10 @@ public UmamiTrackerProcessor(ReactiveSettingFetcher settingFetcher) {
@Override
public Mono<Void> process(ITemplateContext context, IModel model,
IElementModelStructureHandler structureHandler) {
return settingFetcher.fetch("basic", BasicConfig.class)
.doOnNext(basicConfig -> {
return BasicProp.fetch(settingFetcher)
.doOnNext(prop -> {
final IModelFactory modelFactory = context.getModelFactory();
model.add(modelFactory.createText(trackerScript(basicConfig.getWebsiteId(), basicConfig.endpoint, basicConfig.scriptName)));
model.add(modelFactory.createText(trackerScript(prop.getWebsiteId(), prop.getEndpoint(), prop.getScriptName())));
})
.then();
}
Expand All @@ -35,12 +34,4 @@ private String trackerScript(String websiteId, String endpoint, String scriptNam
<script async defer data-website-id="%s" src="%s/%s"></script>
""".formatted(websiteId, endpoint, scriptName);
}

@Data
public static class BasicConfig {
String websiteId;
String endpoint;
String scriptName;
String url;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
spec:
enabled: true
version: 1.0.0
requires: ">=2.17.0"
requires: ">=2.20.11"
author:
name: Halo
website: https://github.com/halo-dev
Expand Down

0 comments on commit 233c740

Please sign in to comment.