Skip to content

Commit

Permalink
Fix relay condition & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HitomaruKonpaku committed Sep 28, 2024
1 parent cabf832 commit 72f6b09
Show file tree
Hide file tree
Showing 13 changed files with 6,736 additions and 3,313 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
dist/
submodule/

.tmp/
6 changes: 4 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
"env": {
"node": true,
"browser": true,
"es2021": true
"es2021": true,
"jest": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
"@typescript-eslint",
"jest"
],
"extends": [
"eslint:recommended",
Expand Down
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
"skipFiles": [
"<node_internals>/**"
],
},
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/jest/bin/jest.js",
"--runInBand"
],
"outputCapture": "std",
}
]
}
47 changes: 4 additions & 43 deletions apps/back-end/src/module/youtube/base/base-action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Track, TrackService } from '@app/track'
import { UserFilter, UserFilterRepository, UserFilterType, UserSourceType } from '@app/user'
import { YoutubeChatAction, YoutubeChatActionJobData, YoutubeChatUtil } from '@app/youtube'
import { ModuleRef } from '@nestjs/core'
import { NumberUtil } from '@shared/util/number.util'
import { Logger } from '@shared/logger/logger'
import { bold, inlineCode } from 'discord.js'
import {
AddBannerAction,
Expand All @@ -18,7 +18,6 @@ import {
MembershipGiftRedemptionAction,
stringify,
} from 'masterchat'
import { Logger } from '../../../../../../shared/logger/logger'
import { YoutubeChatHandlerUtil } from '../util/youtube-chat-handler.util'

export type HandlerAction = AddBannerAction
Expand Down Expand Up @@ -110,47 +109,7 @@ export abstract class BaseActionHandler<T1 extends HandlerAction, T2 extends Pro

protected async handleTrack(track: Track) {
const action = this.getProcessAction()

if (!this.data.video.isLive && !track.allowReplay) {
return
}

if (this.data.video.isLive && action.timestamp) {
const age = Date.now() - NumberUtil.fromDate(action.timestamp)
const maxAge = (NumberUtil.parse(process.env.YOUTUBE_ACTION_MAX_AGE) || 3600) * 1000
if (age > maxAge) {
return
}
}

if (this.data.video.isMembersOnly && !track.allowMemberChat) {
return
}

if (!this.data.video.isMembersOnly && !track.allowPublicChat) {
return
}

if (this.authorId === this.hostId) {
if (this.authorId === track.filterId) {
return
}
if (this.authorId === track.sourceId && track.filterId) {
return
}
}

const message = stringify(action.message) || ''

if (this.userFilter?.type === UserFilterType.ALLOW) {
// ignore
} else if (this.authorId === track.filterId) {
if (track.filterKeywords?.length) {
if (!track.filterKeywords.some((v) => message.toLowerCase().includes(v.toLowerCase()))) {
return
}
}
} else if (this.hostId !== track.sourceId) {
if (!YoutubeChatHandlerUtil.canRelay(this.data, action, track, this.userFilter)) {
return
}

Expand All @@ -161,6 +120,8 @@ export abstract class BaseActionHandler<T1 extends HandlerAction, T2 extends Pro
icons.join(' '),
name,
].filter((v) => v).join(' ')

const message = stringify(action.message) || ''
const displayMessage = message
? `${bold(inlineCode(message))}`
: ''
Expand Down
40 changes: 40 additions & 0 deletions apps/back-end/src/module/youtube/util/youtube-chat-handler.util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
import { Track } from '@app/track'
import { UserFilter } from '@app/user'
import {
YoutubeChannelUtil,
YoutubeChatActionJobData,
YoutubeChatMetadata,
YoutubeChatUtil,
YoutubeVideoUtil,
} from '@app/youtube'
import { NumberUtil } from '@shared/util/number.util'
import { hideLinkEmbed, hyperlink, spoiler } from 'discord.js'
import {
AddBannerAction,
AddChatItemAction,
AddSuperChatItemAction,
stringify,
} from 'masterchat'
import { TrackHandlerUtil } from '../../../util/track-handler.util'
import { ProcessAction } from '../base/base-action-handler'

export class YoutubeChatHandlerUtil {
public static canRelay(
data: YoutubeChatMetadata,
action: ProcessAction,
track: Track,
userFilter?: Pick<UserFilter, 'type'>,
): boolean {
if (!data.video.isLive && !track.allowReplay) {
return false
}

if (data.video.isLive && action.timestamp) {
const age = Date.now() - NumberUtil.fromDate(action.timestamp)
const maxAge = (NumberUtil.parse(process.env.YOUTUBE_ACTION_MAX_AGE) || 3600) * 1000
if (age > maxAge) {
return false
}
}

if (data.video.isMembersOnly && !track.allowMemberChat) {
return false
}

if (!data.video.isMembersOnly && !track.allowPublicChat) {
return false
}

const message = stringify(action.message) || ''
if (!TrackHandlerUtil.canRelay(track, action.authorChannelId, data.channel.id, message, userFilter)) {
return false
}

return true
}

public static getSrcHyperlink(data: YoutubeChatActionJobData) {
const s = spoiler(
hyperlink(
Expand Down
48 changes: 48 additions & 0 deletions apps/back-end/src/util/track-handler.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Track } from '@app/track'
import { UserFilter, UserFilterType } from '@app/user'

export class TrackHandlerUtil {
public static canRelay(
track: Pick<Track, 'sourceId' | 'filterId' | 'filterKeywords'>,
authorId: string,
hostId: string,
message: string,
userFilter?: Pick<UserFilter, 'type'>,
) {
// eslint-disable-next-line no-param-reassign
message = message || ''

if (authorId === hostId) {
if (authorId === track.filterId) {
return false
}

if (authorId === track.sourceId && track.filterId) {
return false
}
}

if (userFilter) {
return userFilter.type === UserFilterType.ALLOW
}

if (authorId !== track.sourceId && authorId !== track.filterId) {
return false
}

if (authorId === track.filterId) {
if (hostId !== track.sourceId && track.sourceId) {
return false
}

if (
track.filterKeywords?.length
&& !track.filterKeywords.some((keyword) => message.toLowerCase().includes(keyword.toLowerCase()))
) {
return false
}
}

return true
}
}
2 changes: 2 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ RUN npm install --no-progress

COPY . .

RUN npm test

#
FROM builder AS dashboard
RUN nest build dashboard
Expand Down
20 changes: 20 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable @typescript-eslint/no-var-requires */

const { pathsToModuleNameMapper } = require('ts-jest')
const { compilerOptions } = require('./tsconfig')

module.exports = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'node',
moduleFileExtensions: ['js', 'json', 'ts'],
moduleNameMapper: {
...pathsToModuleNameMapper(compilerOptions.paths),
'^masterchat$': '<rootDir>/submodule/masterchat/src',
},
modulePaths: ['<rootDir>'],
testRegex: '.*\\.spec\\.ts$',
transform: { '^.+\\.(t|j)s$': 'ts-jest' },
coverageDirectory: '../coverage',
collectCoverageFrom: ['**/*.(t|j)s'],
}
Loading

0 comments on commit 72f6b09

Please sign in to comment.