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

feat(pubsub): parse whisper thread details #845

Merged
merged 2 commits into from Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java
Expand Up @@ -71,6 +71,7 @@
import com.github.twitch4j.pubsub.domain.UserAutomodCaughtMessage;
import com.github.twitch4j.pubsub.domain.UserModerationActionData;
import com.github.twitch4j.pubsub.domain.VideoPlaybackData;
import com.github.twitch4j.pubsub.domain.WhisperThread;
import com.github.twitch4j.pubsub.enums.PubSubType;
import com.github.twitch4j.pubsub.events.AliasRestrictionUpdateEvent;
import com.github.twitch4j.pubsub.events.AutomodCaughtMessageEvent;
Expand Down Expand Up @@ -148,6 +149,7 @@
import com.github.twitch4j.pubsub.events.UserPresenceEvent;
import com.github.twitch4j.pubsub.events.UserUnbanRequestUpdateEvent;
import com.github.twitch4j.pubsub.events.VideoPlaybackEvent;
import com.github.twitch4j.pubsub.events.WhisperThreadUpdateEvent;
import com.github.twitch4j.util.IBackoffStrategy;
import lombok.Getter;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -432,23 +434,30 @@ protected void onTextMessage(String text) {
eventManager.publish(new ChannelSubscribeEvent(TypeConvert.jsonToObject(rawMessage, SubscriptionData.class)));
} else if ("channel-commerce-events-v1".equals(topicName)) {
eventManager.publish(new ChannelCommerceEvent(TypeConvert.jsonToObject(rawMessage, CommerceData.class)));
} else if ("whispers".equals(topicName) && (type.equals("whisper_sent") || type.equals("whisper_received"))) {
} else if ("whispers".equals(topicName)) {
// Whisper data is escaped Json cast into a String
JsonNode msgDataParsed = TypeConvert.jsonToObject(msgData.asText(), JsonNode.class);

//TypeReference<T> allows type parameters (unlike Class<T>) and avoids needing @SuppressWarnings("unchecked")
Map<String, Object> tags = TypeConvert.convertValue(msgDataParsed.path("tags"), new TypeReference<Map<String, Object>>() {});
if (type.equals("whisper_sent") || type.equals("whisper_received")) {
//TypeReference<T> allows type parameters (unlike Class<T>) and avoids needing @SuppressWarnings("unchecked")
Map<String, Object> tags = TypeConvert.convertValue(msgDataParsed.path("tags"), new TypeReference<Map<String, Object>>() {});

String fromId = msgDataParsed.get("from_id").asText();
String displayName = (String) tags.get("display_name");
EventUser eventUser = new EventUser(fromId, displayName);
String fromId = msgDataParsed.get("from_id").asText();
String displayName = (String) tags.get("display_name");
EventUser eventUser = new EventUser(fromId, displayName);

String body = msgDataParsed.get("body").asText();
String body = msgDataParsed.get("body").asText();

Set<CommandPermission> permissions = TwitchUtils.getPermissionsFromTags(tags, new HashMap<>(), fromId, botOwnerIds);
Set<CommandPermission> permissions = TwitchUtils.getPermissionsFromTags(tags, new HashMap<>(), fromId, botOwnerIds);

PrivateMessageEvent privateMessageEvent = new PrivateMessageEvent(eventUser, body, permissions);
eventManager.publish(privateMessageEvent);
PrivateMessageEvent privateMessageEvent = new PrivateMessageEvent(eventUser, body, permissions);
eventManager.publish(privateMessageEvent);
} else if ("thread".equals(type)) {
WhisperThread thread = TypeConvert.convertValue(msgDataParsed, WhisperThread.class);
eventManager.publish(new WhisperThreadUpdateEvent(lastTopicIdentifier, thread));
} else {
log.warn("Unparsable Message: " + message.getType() + "|" + message.getData());
}
} else if ("automod-levels-modification".equals(topicName) && topicParts.length > 1) {
if ("automod_levels_modified".equals(type)) {
AutomodLevelsModified data = TypeConvert.convertValue(msgData, AutomodLevelsModified.class);
Expand Down
@@ -0,0 +1,36 @@
package com.github.twitch4j.pubsub.domain;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

import java.time.Instant;

@Data
@Setter(AccessLevel.PRIVATE)
public class WhisperThread {
@JsonProperty("id")
private String threadId;
private Long lastRead;
private Boolean archived;
private Boolean muted;
private SpamClassification spamInfo;
@JsonAlias("allowlisted_until")
@JsonProperty("whitelisted_until")
private Instant allowlistedUntil;

public String getOtherUserId() {
int delimIndex = threadId.indexOf('_');
if (delimIndex < 0) return null;
return threadId.substring(delimIndex + 1);
}

@Data
@Setter(AccessLevel.PRIVATE)
public static class SpamClassification {
private String likelihood; // e.g., "low"
private Long lastMarkedNotSpam;
}
}
@@ -0,0 +1,13 @@
package com.github.twitch4j.pubsub.events;

import com.github.twitch4j.common.events.TwitchEvent;
import com.github.twitch4j.pubsub.domain.WhisperThread;
import lombok.EqualsAndHashCode;
import lombok.Value;

@Value
@EqualsAndHashCode(callSuper = false)
public class WhisperThreadUpdateEvent extends TwitchEvent {
String userId;
WhisperThread data;
}