generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add 1px transparent image in RSS item descriptions for telemetr…
…y content views (#44) ### What this PR does? 传统网站即使使用 Umami 等访问统计工具,也难以覆盖 RSS 订阅用户的阅读行为,导致访问量数据不完整,影响对内容表现的准确评估。为解决这一短板,我们新增了在 RSS 订阅中统计访问量的功能支持。 通过在每个 RSS 条目的内容中插入 1 像素透明图片,系统可匿名统计订阅内容的实际阅读量。这种轻量化设计不会影响用户体验,帮助内容创作者更全面的了解内容阅读量情况。 Umami 适配参考 halo-sigs/plugin-umami#30 ```release-note 为 RSS 订阅内容统计访问量提供扩展支持,本插件并不提供任何存储和分析访问量的功能但允许其他插件扩展并获取访问量数据上报给诸如 Umami 之类的应用 ```
- Loading branch information
Showing
13 changed files
with
696 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package run.halo.feed; | ||
|
||
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) | ||
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 | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
TelemetryEventInfo that = (TelemetryEventInfo) o; | ||
return Objects.equals(pageUrl, that.pageUrl) && Objects.equals(title, that.title) | ||
&& Objects.equals(referrer, that.referrer) && Objects.equals(ip, that.ip) | ||
&& Objects.equals(userAgent, that.userAgent); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(pageUrl, title, referrer, ip, userAgent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package run.halo.feed; | ||
|
||
import org.pf4j.ExtensionPoint; | ||
|
||
public interface TelemetryRecorder extends ExtensionPoint { | ||
|
||
void record(TelemetryEventInfo eventInfo); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/src/main/java/run/halo/feed/telemetry/AcceptLanguageParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package run.halo.feed.telemetry; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import lombok.experimental.UtilityClass; | ||
import org.springframework.lang.NonNull; | ||
|
||
@UtilityClass | ||
public class AcceptLanguageParser { | ||
|
||
public record Language(String code, String script, String region, double quality) { | ||
@Override | ||
public String toString() { | ||
return "Language{" + | ||
"code='" + code + '\'' + | ||
", script='" + script + '\'' + | ||
", region='" + region + '\'' + | ||
", quality=" + quality + | ||
'}'; | ||
} | ||
} | ||
|
||
private static final Pattern REGEX = Pattern.compile( | ||
"((([a-zA-Z]+(-[a-zA-Z0-9]+){0,2})|\\*)(;q=[0-1](\\.[0-9]+)?)?)*"); | ||
|
||
@NonNull | ||
public static List<Language> parseAcceptLanguage(String acceptLanguage) { | ||
if (acceptLanguage == null || acceptLanguage.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
List<Language> languages = new ArrayList<>(); | ||
Matcher matcher = REGEX.matcher(acceptLanguage); | ||
|
||
while (matcher.find()) { | ||
String match = matcher.group(); | ||
if (match == null || match.isEmpty()) { | ||
continue; | ||
} | ||
|
||
String[] parts = match.split(";"); | ||
String ietfTag = parts[0]; | ||
String[] ietfComponents = ietfTag.split("-"); | ||
String code = ietfComponents[0]; | ||
String script = ietfComponents.length == 3 ? ietfComponents[1] : null; | ||
String region = ietfComponents.length == 3 ? ietfComponents[2] | ||
: ietfComponents.length == 2 ? ietfComponents[1] : null; | ||
|
||
double quality = 1.0; | ||
if (parts.length > 1 && parts[1].startsWith("q=")) { | ||
try { | ||
quality = Double.parseDouble(parts[1].substring(2)); | ||
} catch (NumberFormatException e) { | ||
// ignore | ||
} | ||
} | ||
|
||
languages.add(new Language(code, script, region, quality)); | ||
} | ||
|
||
return languages.stream() | ||
.filter(Objects::nonNull) | ||
.sorted(Comparator.comparingDouble((Language l) -> l.quality).reversed()) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Oops, something went wrong.