From 067447e3b1e1f407d08821eadf798d6d138f175b Mon Sep 17 00:00:00 2001 From: Brian Clark Date: Thu, 11 Apr 2019 20:37:28 -0400 Subject: [PATCH] Remove Highlights from Users Banned in Chat (#99) * working solution * fix tests * bump version for next release --- package.json | 2 +- src/constants.ts | 8 ++++++-- src/extension.ts | 40 ++++++++++++++++++++++++++++--------- src/highlighter.ts | 5 +++++ src/twitchChatClient.ts | 32 ++++++++++++++++++++++++++--- src/twitchLanguageServer.ts | 20 ++++++++++++++++--- 6 files changed, 89 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 969def9..81d3e2d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "twitch-highlighter", "displayName": "Twitch Highlighter", "description": "Allow your Twitch viewers to help in spotting bugs, typos, etc. by sending a command in chat that will highlight the line of code they want you to check.", - "version": "0.1.5", + "version": "0.2.1", "preview": true, "publisher": "clarkio", "engines": { diff --git a/src/constants.ts b/src/constants.ts index 5b3d792..77006ad 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -11,7 +11,7 @@ export enum Settings { 'joinMessage' = 'joinMessage', 'leaveMessage' = 'leaveMessage', 'unhighlightOnDisconnect' = 'unhighlightOnDisconnect', - 'showHighlightsInActivityBar' = 'showHighlightsInActivityBar', + 'showHighlightsInActivityBar' = 'showHighlightsInActivityBar' } export enum Commands { @@ -27,5 +27,9 @@ export enum Commands { 'removeTwitchPassword' = 'twitchHighlighter.removeTwitchPassword', 'refreshTreeView' = 'twitchHighlighter.refreshTreeView', 'gotoHighlight' = 'twitchHighlighter.gotoHighlight', - 'removeHighlight' = 'twitchHighlighter.removeHighlight', + 'removeHighlight' = 'twitchHighlighter.removeHighlight' +} + +export enum InternalCommands { + 'removeBannedHighlights' = 'twitchHighlighter.removeBannedHighlights' } diff --git a/src/extension.ts b/src/extension.ts index 3977cab..e222d68 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -35,6 +35,7 @@ export function activate(context: vscode.ExtensionContext) { twitchChatClient.onHighlight = highlight; twitchChatClient.onUnhighlight = unhighlight; + twitchChatClient.onBannedUser = handleBannedUser; twitchChatClient.onConnected = () => setConnectionStatus(true); twitchChatClient.onConnecting = () => setConnectionStatus(false, true); twitchChatClient.onDisconnected = () => { @@ -134,6 +135,14 @@ export function activate(context: vscode.ExtensionContext) { }); } + /** + * This function handles removing any highlights that were created from a user that was banned in chat + * @param bannedUserName name of the user that was banned in the chat + */ + function handleBannedUser(bannedUserName: string) { + removeHighlight(bannedUserName); + } + async function setTwitchTokenHandler(): Promise { const value = await vscode.window.showInputBox({ prompt: @@ -370,7 +379,7 @@ function highlight( range, hoverMessage: `From @${twitchUser === 'self' ? 'You' : twitchUser}${ comment !== undefined ? `: ${comment}` : '' - }` + }` }; addHighlight( @@ -479,20 +488,33 @@ function addHighlight( twitchHighlighterTreeView.refresh(); } +function removeHighlight(username: string): void; +function removeHighlight(lineNumber: number, fileName: string, deferRefresh?: boolean): void; function removeHighlight( - lineNumber: number, - fileName: string, + searchQuery: string | number, + fileName?: string, deferRefresh?: boolean ) { - const existingHighlight = findHighlighter(fileName); - if (!existingHighlight) { - console.warn(`Highlight not found so can't unhighlight the line from file`); - return; + if (isNaN(Number(searchQuery))) { + const username = searchQuery as string; + highlighters.forEach(highlighter => highlighter.removeDecorations(username)); + } + // the searchQuery is a number (lineNumber) + else { + if (!fileName) { return; } // the fileName should always be truthy, but tslint generates warnings. + + const existingHighlight = findHighlighter(fileName); + if (!existingHighlight) { + console.warn(`Highlight not found so can't unhighlight the line from file`); + return; + } + + const lineNumber = searchQuery as number; + existingHighlight.removeDecoration(lineNumber); } - existingHighlight.removeDecoration(lineNumber); - triggerUpdateDecorations(); if (!deferRefresh) { + triggerUpdateDecorations(); twitchHighlighterTreeView.refresh(); } } diff --git a/src/highlighter.ts b/src/highlighter.ts index c02b287..d0a6204 100644 --- a/src/highlighter.ts +++ b/src/highlighter.ts @@ -38,6 +38,11 @@ export class Highlighter { } return this.highlights; } + + removeDecorations(username: string): Highlight[] { + this.highlights = this.highlights.filter(highlight => highlight.twitchUser !== username); + return this.highlights; + } } export class Highlight { diff --git a/src/twitchChatClient.ts b/src/twitchChatClient.ts index 6c662c3..18ef410 100644 --- a/src/twitchChatClient.ts +++ b/src/twitchChatClient.ts @@ -6,7 +6,7 @@ import { } from 'vscode-languageclient'; import { workspace, window, Disposable } from 'vscode'; import CredentialManager from './credentialManager'; -import { extSuffix, Settings, Commands } from './constants'; +import { extSuffix, Settings, Commands, InternalCommands } from './constants'; export class TwitchChatClient { private readonly _languageClient: LanguageClient; @@ -39,7 +39,13 @@ export class TwitchChatClient { * this.onHighlight(params.twitchUser, params.startLine, params.endLine, params.fileName, params.comments); */ - public onHighlight?: (twitchUser: string, startLine: number, endLine: number, fileName?: string, comments?: string) => void; + public onHighlight?: ( + twitchUser: string, + startLine: number, + endLine: number, + fileName?: string, + comments?: string + ) => void; /** * Called when an unhighlight request is made from chat * @param lineNumber The line number to unhighlight, the entire highlight is removed if the lineNumber exists in the highlight range. @@ -59,6 +65,11 @@ export class TwitchChatClient { */ public onDisconnected?: () => void; + /** + * Called when a user is banned in chat + */ + public onBannedUser?: (bannedUserName: string) => void; + /** * Retrieves the connection status. */ @@ -202,7 +213,13 @@ export class TwitchChatClient { this._languageClient.onNotification(Commands.highlight, (params: any) => { console.log('highlight requested.', params); if (this.onHighlight) { - this.onHighlight(params.twitchUser, params.startLine, params.endLine, params.fileName, params.comment); + this.onHighlight( + params.twitchUser, + params.startLine, + params.endLine, + params.fileName, + params.comment + ); } }); @@ -212,6 +229,15 @@ export class TwitchChatClient { this.onUnhighlight(params.endLine, params.fileName); } }); + + this._languageClient.onNotification( + InternalCommands.removeBannedHighlights, + (bannedUserName: string) => { + if (this.onBannedUser) { + this.onBannedUser(bannedUserName); + } + } + ); } private getServerOptions(serverModule: string): ServerOptions { diff --git a/src/twitchLanguageServer.ts b/src/twitchLanguageServer.ts index 1b9b8dc..ff71830 100644 --- a/src/twitchLanguageServer.ts +++ b/src/twitchLanguageServer.ts @@ -7,7 +7,7 @@ import { InitializedParams, TextDocumentSyncKind } from 'vscode-languageserver/lib/main'; -import { Commands } from './constants'; +import { Commands, InternalCommands } from './constants'; import * as tmi from 'twitch-js'; @@ -61,6 +61,7 @@ connection.onRequest(Commands.startChat, params => { .then(() => { ttvChatClient.on('join', onTtvChatJoin); ttvChatClient.on('chat', onTtvChatMessage); + ttvChatClient.on('ban', onTtvBanUser); return; }) .catch((error: any) => { @@ -81,8 +82,19 @@ function onTtvChatMessage(channel: string, user: any, message: string) { parseMessage(userName, message); } -export function parseMessage(userName: string, message: string) { +function onTtvBanUser( + channel: string, + userName: string, + reason: string, + userstate: any +) { + connection.sendNotification( + InternalCommands.removeBannedHighlights, + userName.toLocaleLowerCase() + ); +} +export function parseMessage(userName: string, message: string) { /** * Regex pattern to verify the command is a highlight command * groups the different sections of the command. @@ -111,7 +123,9 @@ export function parseMessage(userName: string, message: string) { const commandPattern = /\!(?:line|highlight) (?:((?:[\w]+)?\.?[\w]*) )?(\!)?(\d+)(?:-{1}(\d+))?(?: ((?:[\w]+)?\.[\w]{1,}))?(?: (.+))?/; const cmdopts = commandPattern.exec(message); - if (!cmdopts) { return; } + if (!cmdopts) { + return; + } const fileName: string = cmdopts[1] || cmdopts[5]; const highlight: boolean = cmdopts[2] === undefined;