Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(chat): avoid duplicated tags parsing #792

Merged
merged 4 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.ApiStatus;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -44,15 +45,12 @@ public class IRCMessageEvent extends TwitchEvent {
@Unofficial
public static final String NONCE_TAG_NAME = "client-nonce";

/**
* Tags
*/
private Map<String, String> tags = new HashMap<>();

/**
* Raw Tags
* Tags
* <p>
* Most applications should utilize {@link #getTagValue(String)} rather than accessing this map directly.
*/
private Map<String, Object> rawTags = new HashMap<>();
private Map<String, String> tags = new HashMap<>();

/**
* Badges
Expand Down Expand Up @@ -159,7 +157,6 @@ private void parseRawMessage() {
if (matcher.matches()) {
// Parse Tags
tags = parseTags(matcher.group("tags"));
rawTags = parseTags(matcher.group("tags"));
clientName = parseClientName(matcher.group("clientName"));
commandType = matcher.group("command");
channelName = Optional.ofNullable(matcher.group("channel"));
Expand All @@ -173,7 +170,6 @@ private void parseRawMessage() {
if (matcherPM.matches()) {
// Parse Tags
tags = parseTags(matcherPM.group("tags"));
rawTags = parseTags(matcherPM.group("tags"));
clientName = parseClientName(matcherPM.group("clientName"));
commandType = matcherPM.group("command");
channelName = Optional.ofNullable(matcherPM.group("channel"));
Expand All @@ -188,12 +184,13 @@ private void parseRawMessage() {
* @param raw The raw list of tags.
* @return A key-value map of the tags.
*/
public Map parseTags(String raw) {
@ApiStatus.Internal
public Map<String, String> parseTags(String raw) {
Map<String, String> map = new HashMap<>();
if(StringUtils.isBlank(raw)) return map;

for (String tag: raw.split(";")) {
String[] val = tag.split("=");
String[] val = tag.split("=", 2);
final String key = val[0];
String value = (val.length > 1) ? val[1] : null;
map.put(key, value);
Expand Down Expand Up @@ -403,4 +400,13 @@ public EventChannel getChannel() {
return new EventChannel(getChannelId(), getChannelName().get());
}

/**
* @return IRCv3 tags
* @deprecated in favor of {@link #getTags()}
*/
@Deprecated
public Map<String, Object> getRawTags() {
return Collections.unmodifiableMap(tags);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public static Map<String, String> parseBadges(String raw) {
// Fix Whitespaces
raw = EscapeUtils.unescapeTagValue(raw);

for (String tag : StringUtils.split(raw, ',')) {
String[] val = StringUtils.split(tag, "/", 2);
for (String tag : raw.split(",")) {
String[] val = tag.split("/", 2);
final String key = val[0];
String value = (val.length > 1) ? val[1] : null;
map.put(key, value);
Expand Down