Skip to content

Commit

Permalink
feat(pubsub): parse whisper thread details (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy committed Sep 6, 2023
1 parent e30cecd commit 194c77a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 10 deletions.
29 changes: 19 additions & 10 deletions pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.twitch4j.pubsub.domain;

import com.github.twitch4j.common.util.TypeConvert;
import org.junit.jupiter.api.Test;

import java.time.Instant;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class WhisperThreadTest {

@Test
void deserialize() {
String json = "{\"id\":\"53888434_268669435\",\"last_read\":13006,\"archived\":false,\"muted\":false," +
"\"spam_info\":{\"likelihood\":\"low\",\"last_marked_not_spam\":0}," +
"\"whitelisted_until\":\"2023-06-09T06:51:19Z\"}";
WhisperThread data = TypeConvert.jsonToObject(json, WhisperThread.class);
assertNotNull(data);
assertEquals(13006, data.getLastRead());
assertFalse(data.getArchived());
assertFalse(data.getMuted());
assertNotNull(data.getSpamInfo());
assertEquals("low", data.getSpamInfo().getLikelihood());
assertEquals(Instant.parse("2023-06-09T06:51:19Z"), data.getAllowlistedUntil());
assertEquals("268669435", data.getOtherUserId());
}

}

0 comments on commit 194c77a

Please sign in to comment.