generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 10
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
- 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.