Skip to content

Commit

Permalink
Remove Highlights from Users Banned in Chat (#99)
Browse files Browse the repository at this point in the history
* working solution

* fix tests

* bump version for next release
  • Loading branch information
clarkio authored Apr 12, 2019
1 parent 49402dc commit 067447e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
8 changes: 6 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export enum Settings {
'joinMessage' = 'joinMessage',
'leaveMessage' = 'leaveMessage',
'unhighlightOnDisconnect' = 'unhighlightOnDisconnect',
'showHighlightsInActivityBar' = 'showHighlightsInActivityBar',
'showHighlightsInActivityBar' = 'showHighlightsInActivityBar'
}

export enum Commands {
Expand All @@ -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'
}
40 changes: 31 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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<boolean> {
const value = await vscode.window.showInputBox({
prompt:
Expand Down Expand Up @@ -370,7 +379,7 @@ function highlight(
range,
hoverMessage: `From @${twitchUser === 'self' ? 'You' : twitchUser}${
comment !== undefined ? `: ${comment}` : ''
}`
}`
};

addHighlight(
Expand Down Expand Up @@ -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();
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 29 additions & 3 deletions src/twitchChatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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
);
}
});

Expand All @@ -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 {
Expand Down
20 changes: 17 additions & 3 deletions src/twitchLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) => {
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 067447e

Please sign in to comment.