Skip to content

Commit

Permalink
add automod related topics to pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
d-fischer committed Jun 13, 2021
1 parent f75c8b2 commit b50490a
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 30 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Expand Up @@ -35,7 +35,7 @@ const memberNames = [
'^community_ids$',
'^position_seconds$',
'^(default|profile)_image$',
'^background_color$',
'^(?:background|chat)_color$',
'^max_per_(user_per_)?stream(_setting)?$',
'^should_redemptions_skip_request_queue$',
'^global_cooldown_(seconds|setting)$',
Expand Down Expand Up @@ -79,6 +79,9 @@ const memberNames = [
'^prediction_window$',
'^channel_points(?:_used|_won)?$',
'^top_predictors$',
'^content_classification$',
'^reason_code$',
'^resolver_login$',
// HTTP
'^Accept$'
];
Expand Down
@@ -0,0 +1,163 @@
import { Enumerable } from '@d-fischer/shared-utils';
import { rtfm } from 'twitch-common';

/** @private */
export interface PubSubAutoModQueueMessageAutoModInternals {
topics: Record<string, number>;
}

/** @private */
export interface PubSubAutoModQueueMessageFragment {
text: string;
automod: PubSubAutoModQueueMessageAutoModInternals;
}

/** @private */
export interface PubSubAutoModQueueMessageDetailContent {
text: string;
fragments: PubSubAutoModQueueMessageFragment[];
}

/** @private */
export interface PubSubAutoModQueueMessageSenderData {
user_id: string;
login: string;
display_name: string;
chat_color: string;
}

/** @private */
export interface PubSubAutoModQueueMessageDetail {
id: string;
content: PubSubAutoModQueueMessageDetailContent;
sender: PubSubAutoModQueueMessageSenderData;
sent_at: string;
}

/** @private */
export interface PubSubAutoModQueueMessageContentClassification {
category: string;
level: number;
}

export type PubSubAutoModQueueStatus = 'PENDING' | 'ALLOWED' | 'DENIED' | 'EXPIRED';

/** @private */
export interface PubSubAutoModQueueMessageContent {
message: PubSubAutoModQueueMessageDetail;
content_classification: PubSubAutoModQueueMessageContentClassification;
status: PubSubAutoModQueueStatus;
reason_code: string;
resolver_id: string;
resolver_login: string;
}

/** @private */
export interface PubSubAutoModQueueMessageData {
type: 'automod_caught_message';
data: PubSubAutoModQueueMessageContent;
}

/**
* A message that informs about a message being processed in the AutoMod queue.
*/
@rtfm<PubSubAutoModQueueMessage>('twitch-pubsub-client', 'PubSubAutoModQueueMessage', 'messageId')
export class PubSubAutoModQueueMessage {
@Enumerable(false) private readonly _data: PubSubAutoModQueueMessageData;

/** @private */
constructor(data: PubSubAutoModQueueMessageData, private readonly _channelId: string) {
this._data = data;
}

/**
* The ID of the channel where the message was posted.
*/
get channelId(): string {
return this._channelId;
}

/**
* The ID of the message.
*/
get messageId(): string {
return this._data.data.message.id;
}

/**
* The content of the message.
*/
get messageContent(): string {
return this._data.data.message.content.text;
}

/**
* The fragments of the message that were found to be against the moderation level of the channel.
*/
get foundMessageFragments(): PubSubAutoModQueueMessageFragment[] {
return this._data.data.message.content.fragments;
}

/**
* The ID of the user that sent the message.
*/
get senderId(): string {
return this._data.data.message.sender.user_id;
}

/**
* The name of the user that sent the message.
*/
get senderName(): string {
return this._data.data.message.sender.login;
}

/**
* The display name of the user that sent the message.
*/
get senderDisplayName(): string {
return this._data.data.message.sender.display_name;
}

/**
* The chat color of the user that sent the message.
*/
get senderColor(): string {
return this._data.data.message.sender.chat_color;
}

/**
* The date when the message was sent.
*/
get sendDate(): Date {
return new Date(this._data.data.message.sent_at);
}

/**
* The classification of the message content.
*/
get contentClassification(): PubSubAutoModQueueMessageContentClassification {
return this._data.data.content_classification;
}

/**
* The status of the queue entry.
*/
get status(): PubSubAutoModQueueStatus {
return this._data.data.status;
}

/**
* The ID of the user that resolved the queue entry, or null if it was not resolved or timed out.
*/
get resolverId(): string | null {
return this._data.data.resolver_id || null;
}

/**
* The name of the user that resolved the queue entry, or null if it was not resolved or timed out.
*/
get resolverName(): string | null {
return this._data.data.resolver_login || null;
}
}
9 changes: 9 additions & 0 deletions packages/twitch-pubsub-client/src/Messages/PubSubMessage.ts
@@ -1,8 +1,13 @@
import type { PubSubAutoModQueueMessage, PubSubAutoModQueueMessageData } from './PubSubAutoModQueueMessage';
import type { PubSubBitsBadgeUnlockMessage, PubSubBitsBadgeUnlockMessageData } from './PubSubBitsBadgeUnlockMessage';
import type { PubSubBitsMessage, PubSubBitsMessageData } from './PubSubBitsMessage';
import type { PubSubChatModActionMessage, PubSubChatModActionMessageData } from './PubSubChatModActionMessage';
import type { PubSubRedemptionMessage, PubSubRedemptionMessageData } from './PubSubRedemptionMessage';
import type { PubSubSubscriptionMessage, PubSubSubscriptionMessageData } from './PubSubSubscriptionMessage';
import type {
PubSubUserModerationNotificationMessage,
PubSubUserModerationNotificationMessageData
} from './PubSubUserModerationNotificationMessage';
import type { PubSubWhisperMessage, PubSubWhisperMessageData } from './PubSubWhisperMessage';

/** @private */
Expand Down Expand Up @@ -35,18 +40,22 @@ export interface PubSubChatMessage {

/** @private */
export type PubSubMessageData =
| PubSubAutoModQueueMessageData
| PubSubBitsMessageData
| PubSubBitsBadgeUnlockMessageData
| PubSubChatModActionMessageData
| PubSubRedemptionMessageData
| PubSubSubscriptionMessageData
| PubSubUserModerationNotificationMessageData
| PubSubWhisperMessageData;

/** @private */
export type PubSubMessage =
| PubSubAutoModQueueMessage
| PubSubBitsMessage
| PubSubBitsBadgeUnlockMessage
| PubSubChatModActionMessage
| PubSubRedemptionMessage
| PubSubSubscriptionMessage
| PubSubUserModerationNotificationMessage
| PubSubWhisperMessage;
@@ -0,0 +1,54 @@
import { Enumerable } from '@d-fischer/shared-utils';
import { rtfm } from 'twitch-common';

export type PubSubUserModerationNotificationMessageStatus = 'PENDING' | 'ALLOWED' | 'DENIED' | 'EXPIRED';

/** @private */
export interface PubSubUserModerationNotificationMessageContent {
message_id: string;
status: PubSubUserModerationNotificationMessageStatus;
}

/** @private */
export interface PubSubUserModerationNotificationMessageData {
type: 'automod_caught_message';
data: PubSubUserModerationNotificationMessageContent;
}

/**
* A message that informs about a moderation action on your message..
*/
@rtfm<PubSubUserModerationNotificationMessage>(
'twitch-pubsub-client',
'PubSubUserModerationNotificationMessage',
'messageId'
)
export class PubSubUserModerationNotificationMessage {
@Enumerable(false) private readonly _data: PubSubUserModerationNotificationMessageData;

/** @private */
constructor(data: PubSubUserModerationNotificationMessageData, private readonly _channelId: string) {
this._data = data;
}

/**
* The ID of the channel where the message was posted.
*/
get channelId(): string {
return this._channelId;
}

/**
* The ID of the message.
*/
get messageId(): string {
return this._data.data.message_id;
}

/**
* The status of the queue entry.
*/
get status(): PubSubUserModerationNotificationMessageStatus {
return this._data.data.status;
}
}
63 changes: 49 additions & 14 deletions packages/twitch-pubsub-client/src/PubSubClient.ts
Expand Up @@ -4,6 +4,7 @@ import { getValidTokenFromProvider, InvalidTokenTypeError } from 'twitch-auth';
import type { UserIdResolvable } from 'twitch-common';
import { extractUserId, rtfm } from 'twitch-common';
import { BasicPubSubClient } from './BasicPubSubClient';
import type { PubSubAutoModQueueMessage } from './Messages/PubSubAutoModQueueMessage';
import type { PubSubBitsBadgeUnlockMessage } from './Messages/PubSubBitsBadgeUnlockMessage';
import type { PubSubBitsMessage } from './Messages/PubSubBitsMessage';
import type { PubSubChatModActionMessage } from './Messages/PubSubChatModActionMessage';
Expand Down Expand Up @@ -65,6 +66,23 @@ Register one using:
return this._userClients.get(userId)!;
}

/**
* Adds a listener to AutoMod queue events to the client.
*
* @param user The user the event will be subscribed for.
* @param channel The channel to listen to.
* @param callback A function to be called when an AutoMod queue event is sent to the user.
*
* It receives a {@PubSubAutoModQueueMessage} object.
*/
async onAutoModQueue(
user: UserIdResolvable,
channel: UserIdResolvable,
callback: (message: PubSubAutoModQueueMessage) => void
): Promise<PubSubListener<never>> {
return this.getUserListener(user).onAutoModQueue(channel, callback);
}

/**
* Adds a listener to bits events to the client.
*
Expand Down Expand Up @@ -95,6 +113,23 @@ Register one using:
return this.getUserListener(user).onBitsBadgeUnlock(callback);
}

/**
* Adds a listener to mod action events to the client.
*
* @param user The user the event will be subscribed for.
* @param channel The channel the event will be subscribed for.
* @param callback A function to be called when a mod action event is sent to the user.
*
* It receives a {@PubSubChatModActionMessage} object.
*/
async onModAction(
user: UserIdResolvable,
channel: UserIdResolvable,
callback: (message: PubSubChatModActionMessage) => void
): Promise<PubSubListener<never>> {
return this.getUserListener(user).onModAction(channel, callback);
}

/**
* Adds a listener to redemption events to the client.
*
Expand Down Expand Up @@ -126,35 +161,35 @@ Register one using:
}

/**
* Adds a listener to whisper events to the client.
* Adds a listener to user moderation events to the client.
*
* @param user The user the event will be subscribed for.
* @param callback A function to be called when a whisper is sent to the user.
* @param channel The channel to listen to.
* @param callback A function to be called when a user moderation event is sent to the user.
*
* It receives a {@PubSubWhisperMessage} object.
* It receives a {@PubSubUserModerationNotificationMessage} object.
*/
async onWhisper(
async onUserModeration(
user: UserIdResolvable,
callback: (message: PubSubWhisperMessage) => void
channel: UserIdResolvable,
callback: (message: PubSubSubscriptionMessage) => void
): Promise<PubSubListener<never>> {
return this.getUserListener(user).onWhisper(callback);
return this.getUserListener(user).onUserModeration(channel, callback);
}

/**
* Adds a listener to mod action events to the client.
* Adds a listener to whisper events to the client.
*
* @param user The user the event will be subscribed for.
* @param channel The channel the event will be subscribed for.
* @param callback A function to be called when a mod action event is sent to the user.
* @param callback A function to be called when a whisper is sent to the user.
*
* It receives a {@PubSubChatModActionMessage} object.
* It receives a {@PubSubWhisperMessage} object.
*/
async onModAction(
async onWhisper(
user: UserIdResolvable,
channel: UserIdResolvable,
callback: (message: PubSubChatModActionMessage) => void
callback: (message: PubSubWhisperMessage) => void
): Promise<PubSubListener<never>> {
return this.getUserListener(user).onModAction(channel, callback);
return this.getUserListener(user).onWhisper(callback);
}

private static async _getCorrectUserId(apiClient: ApiClient, user?: UserIdResolvable): Promise<string> {
Expand Down

0 comments on commit b50490a

Please sign in to comment.