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

Add PubSub support for moderation actions #133

Merged
merged 9 commits into from
Jun 2, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class IRCMessageEvent extends TwitchEvent {
/**
* Client Permissions
*/
private final Set<CommandPermission> clientPermissions = new HashSet<>();
private final Set<CommandPermission> clientPermissions = EnumSet.noneOf(CommandPermission.class);

/**
* RAW Message
Expand Down
35 changes: 35 additions & 0 deletions pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import com.github.twitch4j.common.util.TwitchUtils;
import com.github.twitch4j.common.util.TypeConvert;
import com.github.twitch4j.pubsub.domain.ChannelPointsRedemption;
import com.github.twitch4j.pubsub.domain.ChatModerationAction;
import com.github.twitch4j.pubsub.domain.PubSubRequest;
import com.github.twitch4j.pubsub.domain.PubSubResponse;
import com.github.twitch4j.pubsub.enums.PubSubType;
import com.github.twitch4j.pubsub.enums.TMIConnectionState;
import com.github.twitch4j.pubsub.events.ChannelPointsRedemptionEvent;
import com.github.twitch4j.pubsub.events.ChatModerationEvent;
import com.github.twitch4j.pubsub.events.RedemptionStatusUpdateEvent;
import com.github.twitch4j.pubsub.events.RewardRedeemedEvent;
import com.neovisionaries.ws.client.WebSocket;
Expand Down Expand Up @@ -300,6 +302,10 @@ public void onTextMessage(WebSocket ws, String text) {
default: eventManager.publish(new ChannelPointsRedemptionEvent(timestamp, redemption));
}

} else if (topic.startsWith("chat_moderator_actions")) {
String channelId = topic.substring(topic.lastIndexOf('.') + 1);
ChatModerationAction data = TypeConvert.convertValue(msgData, ChatModerationAction.class);
eventManager.publish(new ChatModerationEvent(channelId, data));
} else {
log.warn("Unparseable Message: " + message.getType() + "|" + message.getData());
}
Expand Down Expand Up @@ -484,6 +490,35 @@ public PubSubSubscription listenForWhisperEvents(OAuth2Credential credential, St
return listenOnTopic(request);
}

/**
* Event Listener: A moderator performs an action in the channel
*
* @param credential Credential (for channelId, scope: channel:moderate)
* @param channelId Target Channel Id
* @return PubSubSubscription
*/
public PubSubSubscription listenForModerationEvents(OAuth2Credential credential, String channelId) {
final PubSubRequest request = new PubSubRequest();
request.setType(PubSubType.LISTEN);
request.setNonce(UUID.randomUUID().toString());
request.getData().put("auth_token", credential.getAccessToken());
request.getData().put("topics", Collections.singletonList("chat_moderator_actions." + channelId));

return listenOnTopic(request);
}

/**
* Event Listener: A moderator performs an action in the channel
*
* @param credential Credential (for userId, scope: channel:moderate)
* @param userId The user id associated with the credential
* @param roomId The user id associated with the target channel
* @return PubSubSubscription
*/
public PubSubSubscription listenForModerationEvents(OAuth2Credential credential, String userId, String roomId) {
return listenForModerationEvents(credential, userId + "." + roomId);
}

/**
* Event Listener: Anyone makes a channel points redemption on a channel.
*
Expand Down