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(alerts): add queue-able events #5685

Merged
merged 10 commits into from
Jan 24, 2024
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
31 changes: 30 additions & 1 deletion src/database/entity/overlay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';
import { BeforeInsert, BeforeUpdate, Column, Entity, PrimaryColumn } from 'typeorm';

import { BotEntity } from '../BotEntity.js';

Expand Down Expand Up @@ -26,7 +26,9 @@ type Font = {
};

export interface EmitData {
eventId: string | null;
alertId?: string;
queueId?: string;
name: string;
amount: number;
tier: null | 'Prime' | '1' | '2' | '3';
Expand Down Expand Up @@ -691,6 +693,33 @@ export interface Group {
}[],
}

@Entity()
export class AlertQueue extends BotEntity {
@PrimaryColumn({ generated: 'uuid' })
id: string;

@Column({ type: (process.env.TYPEORM_CONNECTION ?? 'better-sqlite3') !== 'better-sqlite3' ? 'json' : 'simple-json' })
emitData: EmitData[];

@Column({ type: (process.env.TYPEORM_CONNECTION ?? 'better-sqlite3') !== 'better-sqlite3' ? 'json' : 'simple-json', nullable: true })
filter: { [x: string]: Filter };

@Column()
passthrough: boolean;

@Column()
play: boolean;

@Column({ nullable: false, type: 'varchar', length: '2022-07-27T00:30:34.569259834Z'.length })
updatedAt?: string;

@BeforeInsert()
@BeforeUpdate()
generateUpdatedAt() {
this.updatedAt = new Date().toISOString();
}
}

@Entity()
export class Overlay extends BotEntity {
@PrimaryColumn({ generated: 'uuid' })
Expand Down
15 changes: 15 additions & 0 deletions src/database/migration/mysql/22.x/1678892044039-alertsQueue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class alertsQueue1678892044039 implements MigrationInterface {
name = 'alertsQueue1678892044039';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE \`alert_queue\` (\`id\` varchar(36) NOT NULL, \`emitData\` json NOT NULL, \`filter\` json NULL, \`passthrough\` tinyint NOT NULL, \`play\` tinyint NOT NULL, \`updatedAt\` varchar(30) NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
return;
}

public async down(queryRunner: QueryRunner): Promise<void> {
return;
}

}
15 changes: 15 additions & 0 deletions src/database/migration/postgres/22.x/1678892044039-alertsQueue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class alertsQueue1678892044039 implements MigrationInterface {
name = 'alertsQueue1678892044039';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "alert_queue" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "emitData" json NOT NULL, "filter" json, "passthrough" boolean NOT NULL, "play" boolean NOT NULL, "updatedAt" character varying(30) NOT NULL, CONSTRAINT "PK_81342c2e2eddba1575f7a475494" PRIMARY KEY ("id"))`);
return;
}

public async down(queryRunner: QueryRunner): Promise<void> {
return;
}

}
15 changes: 15 additions & 0 deletions src/database/migration/sqlite/22.x/1678892044039-alertsQueue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class alertsQueue1678892044039 implements MigrationInterface {
name = 'alertsQueue1678892044039';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "alert_queue" ("id" varchar PRIMARY KEY NOT NULL, "emitData" text NOT NULL, "filter" text, "passthrough" boolean NOT NULL, "play" boolean NOT NULL, "updatedAt" varchar(30) NOT NULL)`);
return;
}

public async down(queryRunner: QueryRunner): Promise<void> {
return;
}

}
5 changes: 3 additions & 2 deletions src/filters/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { info } from '../helpers/log.js';

import type { ResponseFilter } from './index.js';

import { AppDataSource } from '~/database.js';
import { EmitData } from '~/database/entity/overlay.js';
import { Price } from '~/database/entity/price.js';
import { AppDataSource } from '~/database.js';
import alerts from '~/registries/alerts.js';

const selectedItemRegex = /\$triggerAlert\((?<uuid>[0-9A-F]{8}(?:-[0-9A-F]{4}){3}-[0-9A-F]{12}),? ?(?<options>.*)?\)/mi;
Expand Down Expand Up @@ -38,10 +38,11 @@ export const operation: ResponseFilter = {
const price = await AppDataSource.getRepository(Price).findOneBy({ command: attributes.command, enabled: true });

await alerts.trigger({
eventId: null,
amount: price ? price.price : 0,
alertId: match.groups.uuid,
currency: 'CZK',
event: 'custom',
alertId: match.groups.uuid,
message: attributes.param || '',
monthsName: '',
name: attributes.command,
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/events/cheer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function cheer(event: EventSubChannelCheerEventData) {
user = await changelog.get(userId);
}

eventlist.add({
const eventData = await eventlist.add({
event: 'cheer',
userId: userId,
bits,
Expand Down Expand Up @@ -97,6 +97,7 @@ export async function cheer(event: EventSubChannelCheerEventData) {
redeemTriggered = true;
debug('tmi.cmdredeems', messageFromUser);
alerts.trigger({
eventId: eventData?.id ?? null,
event: 'custom',
recipient: username,
name: price.command,
Expand All @@ -114,6 +115,7 @@ export async function cheer(event: EventSubChannelCheerEventData) {
}
if (!redeemTriggered) {
alerts.trigger({
eventId: eventData?.id ?? null,
event: 'cheer',
name: username,
amount: bits,
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/events/follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function follow(userId: string, userName: string, followedAt: strin
// trigger events only if follow was in hour
if (Date.now() - new Date(followedAt).getTime() < HOUR) {
debug('follow', `User ${userName}#${userId} triggered follow event.`);
eventlist.add({
const eventData = await eventlist.add({
event: 'follow',
userId: userId,
timestamp: new Date(followedAt).getTime(),
Expand All @@ -55,6 +55,7 @@ export async function follow(userId: string, userName: string, followedAt: strin
followLog(`${userName}#${userId}`);
eventEmitter.emit('follow', { userName, userId });
alerts.trigger({
eventId: eventData?.id ?? null,
event: 'follow',
name: userName,
amount: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/events/raid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export async function raid(event: EventSubChannelRaidEventData) {
event: 'raid',
timestamp: Date.now(),
};

eventlist.add({
const eventData = await eventlist.add({
userId: String(await users.getIdByName(userName) ?? '0'),
viewers: hostViewers,
event: 'raid',
timestamp: Date.now(),
});
eventEmitter.emit('raid', data);
alerts.trigger({
eventId: eventData?.id ?? null,
event: 'raid',
name: userName,
amount: hostViewers,
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/events/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const subscription = async (username: string , subInfo: ChatSubInfo, user
profileImageUrl: profileImageUrl ? profileImageUrl : user.profileImageUrl,
});

eventlist.add({
const eventData = await eventlist.add({
event: 'sub',
tier: String(tier),
userId: String(userstate.userId),
Expand All @@ -68,6 +68,7 @@ export const subscription = async (username: string , subInfo: ChatSubInfo, user
userName: username, method: subInfo.isPrime ? 'Twitch Prime' : '', subCumulativeMonths: amount, tier: String(tier),
});
alerts.trigger({
eventId: eventData?.id ?? null,
event: 'sub',
name: username,
amount: 0,
Expand Down
56 changes: 56 additions & 0 deletions src/helpers/queryFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const evalComparatorMap = new Map(Object.entries({
lt: '<',
'lt-eq': '<=',
eq: '==',
neq: '!=',
gt: '>',
'gt-eq': '>=',
}));
const evalOperatorMap = new Map(Object.entries({
and: '&&',
'and not': '&& !(',
or: '||',
'or not': '|| !(',
}));

export function itemsToEvalPart (items: any[], operator: string): string {
let output = '';
for (let i = 0; i < items.length; i++) {
const item = items[i];
if (item !== null && typeof item.items === 'undefined') {
if (i > 0) {
output += ` ${evalOperatorMap.get(operator)} `;
}

if (['pr'].includes(item.comparator)) {
output += `${item.type}.length > 0`;
} else if (item.typeof === 'string' || item.typeof === 'service' || item.typeof === 'reward') {
if (['includes'].includes(item.comparator)) {
output += `[${item.value.split(',').map((o: string) => `'${o.trim()}'`).join(', ')}].includes(${item.type})`;
} else if (['co'].includes(item.comparator)) {
output += `${item.type}.includes('${item.value}')`;
} else {
output += `${item.type} ${evalComparatorMap.get(item.comparator)} '${item.value}'`;
}
} else if (item.typeof === 'tier') {
// we need to set Prime as value 0
const value = item.value === 'Prime' ? 0 : item.value;
output += `${item.type} ${evalComparatorMap.get(item.comparator)} ${value}`;
} else if (['is-even', 'is-odd'].includes(item.comparator)) {
output += `${item.type} % 2 === ${item.comparator === 'is-even' ? 0 : 1}`;
} else {
output += `${item.type} ${evalComparatorMap.get(item.comparator)} ${item.value}`;
}
if (i > 0 && operator.includes('not')) {
output += `) `; // ending ')' after 'not' operator
}
} else if (item !== null && item.items.length > 0) {
if (i > 0) {
output += `${evalOperatorMap.get(operator)?.replace('(', '')}`; // we need to replace '(' as it is already part of new group
}

output += '(' + itemsToEvalPart(item.items, item.operator) + ')';
}
}
return output;
}
22 changes: 12 additions & 10 deletions src/integrations/donatello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import users from '../users.js';

import { AppDataSource } from '~/database.js';
import { isStreamOnline, stats } from '~/helpers/api/index.js';
import { MINUTE } from '~/helpers/constants.js';
import exchange from '~/helpers/currency/exchange.js';
import { mainCurrency } from '~/helpers/currency/index.js';
import rates from '~/helpers/currency/rates.js';
Expand All @@ -18,7 +19,6 @@ import { triggerInterfaceOnTip } from '~/helpers/interface/triggers.js';
import {
error, tip,
} from '~/helpers/log.js';
import { MINUTE } from '~/helpers/constants.js';

type DonatelloResponse = {
content: {
Expand Down Expand Up @@ -118,7 +118,7 @@ class Donatello extends Integration {
const user = await users.getUserByUsername(username);
tip(`${username.toLowerCase()}${user.userId ? '#' + user.userId : ''}, amount: ${amount.toFixed(2)}${data.currency}, message: ${data.message}`);

eventlist.add({
const eventData = await eventlist.add({
event: 'tip',
amount: amount,
currency: data.currency,
Expand All @@ -138,6 +138,7 @@ class Donatello extends Integration {
});

alerts.trigger({
eventId: eventData?.id ?? null,
event: 'tip',
service: 'donationalerts',
name: username.toLowerCase(),
Expand All @@ -163,7 +164,16 @@ class Donatello extends Integration {
}
} else {
tip(`${username}#__anonymous__, amount: ${Number(amount).toFixed(2)}${data.currency}, message: ${data.message}`);
const eventData = await eventlist.add({
event: 'tip',
amount: amount,
currency: data.currency,
userId: `${username}#__anonymous__`,
message: data.message,
timestamp,
});
alerts.trigger({
eventId: eventData?.id ?? null,
event: 'tip',
name: username,
amount: Number(amount.toFixed(2)),
Expand All @@ -172,14 +182,6 @@ class Donatello extends Integration {
monthsName: '',
message: data.message,
});
eventlist.add({
event: 'tip',
amount: amount,
currency: data.currency,
userId: `${username}#__anonymous__`,
message: data.message,
timestamp,
});
}
triggerInterfaceOnTip({
userName: username.toLowerCase(),
Expand Down
20 changes: 11 additions & 9 deletions src/integrations/donationalerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class Donationalerts extends Integration {
const user = await users.getUserByUsername(data.username);
tip(`${data.username.toLowerCase()}${user.userId ? '#' + user.userId : ''}, amount: ${Number(data.amount).toFixed(2)}${data.currency}, message: ${data.message}`);

eventlist.add({
const eventData = await eventlist.add({
event: 'tip',
amount: data.amount,
currency: data.currency,
Expand All @@ -239,6 +239,7 @@ class Donationalerts extends Integration {
});

alerts.trigger({
eventId: eventData?.id ?? null,
event: 'tip',
service: 'donationalerts',
name: data.username.toLowerCase(),
Expand All @@ -261,7 +262,16 @@ class Donationalerts extends Integration {
AppDataSource.getRepository(UserTip).save(newTip);
} else {
tip(`anonymous#__anonymous__, amount: ${Number(data.amount).toFixed(2)}${data.currency}, message: ${data.message}`);
const eventData = await eventlist.add({
event: 'tip',
amount: data.amount,
currency: data.currency,
userId: `anonymous#__anonymous__`,
message: data.message,
timestamp,
});
alerts.trigger({
eventId: eventData?.id ?? null,
event: 'tip',
name: 'anonymous',
amount: Number(data.amount.toFixed(2)),
Expand All @@ -270,14 +280,6 @@ class Donationalerts extends Integration {
monthsName: '',
message: data.message,
});
eventlist.add({
event: 'tip',
amount: data.amount,
currency: data.currency,
userId: `anonymous#__anonymous__`,
message: data.message,
timestamp,
});
}
triggerInterfaceOnTip({
userName: isAnonymous ? 'anonymous' : data.username.toLowerCase(),
Expand Down
3 changes: 2 additions & 1 deletion src/integrations/kofi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Kofi extends Integration {
tip(`${data.from_name.toLowerCase()}#${userId}, amount: ${Number(data.amount).toFixed(2)}${data.currency}, message: ${data.message}`);
}

eventlist.add({
const eventData = await eventlist.add({
event: 'tip',
amount: Number(data.amount),
currency: data.currency,
Expand All @@ -92,6 +92,7 @@ class Kofi extends Integration {
message: data.message,
});
alerts.trigger({
eventId: eventData?.id ?? null,
event: 'tip',
service: 'kofi',
name: data.from_name,
Expand Down
Loading