Skip to content

Commit

Permalink
Merge pull request clarkio#39 from PatPat1567/enhancement/1-sub-only
Browse files Browse the repository at this point in the history
Sub Only Mode Command. Closes clarkio#1. Must complete after PR#35
  • Loading branch information
michaeljolley authored May 21, 2019
2 parents ff1895b + a04d19d commit cba4e09
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Broadcaster can use `!theme sub` or `!theme !sub` to activate or deactivate sub only mode

---

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ These commands will either activate or deactivate follower only mode.
```
`Note: Follower only mode will be turned off on extension activation/start up`

#### Subscriber Only mode

These commands will either activate or deactivate subscriber only mode.

```
!theme sub
!theme !sub
```
`Note: Subscriber only mode will be turned off on extension activation/start up`


## How to connect to Twitch

Expand Down
43 changes: 43 additions & 0 deletions src/commands/Themer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class Themer {

private _originalTheme: string | undefined;
private _followerOnly: boolean = false;
private _subOnly: boolean = false;
private _availableThemes: Array<ITheme> = [];
private _listRecipients: Array<IListRecipient> = [];
private _authService: AuthenticationService;
Expand Down Expand Up @@ -42,6 +43,11 @@ export class Themer {
*/
this._followerOnly = this._state.get('followerOnly', false);

/**
* Initialize sub only flag
*/
this._subOnly = this._state.get('subOnly', false);

/**
* Rehydrate the banned users from the extensions global state
*/
Expand Down Expand Up @@ -106,6 +112,12 @@ export class Themer {
case '!follower':
await this.followerOnly(twitchUserName, false);
break;
case 'sub':
await this.subOnly(twitchUserName, true);
break;
case '!sub':
await this.subOnly(twitchUserName, false);
break;
default:
await this.changeTheme(twitchUser, param);
break;
Expand All @@ -131,6 +143,7 @@ export class Themer {

this._state.update('bannedUsers', bannedUsers);
this._state.update('followerOnly', this._followerOnly);
this._state.update('subOnly', this._subOnly);
}

/**
Expand Down Expand Up @@ -191,6 +204,21 @@ export class Themer {
this._followerOnly ? console.log('Follower Only mode has been activated.') : console.log('Follower Only mode has been deactivated.');
}
}

/**
* Activates follower only mode
* @param twitchUser - The user requesting the follower mode change
* @param activate - Set follower only mode
*/
private async subOnly(twitchUser: string | undefined, activate: boolean)
{
if (twitchUser !== undefined &&
twitchUser.toLowerCase() === Constants.chatClientUserName.toLowerCase()) {
this._subOnly = activate;
this.updateState();
this._subOnly ? console.log('Sub Only mode has been activated.') : console.log('Sub Only mode has been deactivated.');
}
}

/**
* Send a whisper to the requesting user with a list of available themes
Expand Down Expand Up @@ -304,6 +332,21 @@ export class Themer {
} else {
break following;
}

subscriber:
if (this._subOnly) {
if (twitchUserName.toLocaleLowerCase() === Constants.chatClientUserName.toLocaleLowerCase()) {
// broadcaster cannot subscribe to their own stream if they are not an affiate or partner.
break subscriber;
} else if (twitchUser && twitchUser["subscriber"]) {
break subscriber;
} else {
console.log (`${twitchUserName} is not a subscriber.`);
return;
}
} else {
break subscriber;
}

/** Ensure the user hasn't been banned before changing the theme */
if (twitchUserName) {
Expand Down
68 changes: 67 additions & 1 deletion src/test/themer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ suite('Themer Tests', function () {
};
const stateValues: { [key: string]: any } = {
'bannedUsers': [],
'followerOnly': false
'followerOnly': false,
'subOnly': false
};
fakeWorkspaceConfiguration = {
get(section: string) {
Expand Down Expand Up @@ -63,6 +64,7 @@ suite('Themer Tests', function () {
setup(function () {
fakeState.update('bannedUsers', []);
fakeState.update('followerOnly', false);
fakeState.update('subOnly', false);
fakeWorkspaceConfiguration.update('workbench.colorTheme', 'Visual Studio Dark');
fakeChatClient = new ChatClient(fakeState);
fakeThemer = new Themer(fakeChatClient, fakeState);
Expand Down Expand Up @@ -305,4 +307,68 @@ suite('Themer Tests', function () {
}
});
});

test('Themer should go to subscriber only mode if user is the logged in user', function(done) {
const twitchUser: Userstate = { 'display-name': Constants.chatClientUserName };
fakeState.update('subOnly', false);

fakeThemer.handleCommands(twitchUser, '!theme', `sub`)
.then(() => {
try {
fakeState.get('subOnly')!.should.be.true;
done();
}
catch (error) {
done(error);
}
});
});

test('Themer should not go to subscriber only mode if user is not the logged in user', function(done) {
const twitchUser: Userstate = { 'display-name': 'goofey' };
fakeState.update('subOnly', false);

fakeThemer.handleCommands(twitchUser, '!theme', `sub`)
.then(() => {
try {
fakeState.get('subOnly')!.should.be.false;
done();
}
catch (error) {
done(error);
}
});
});

test('Themer should leave subscriber only mode if user is the logged in user', function(done) {
const twitchUser: Userstate = { 'display-name': Constants.chatClientUserName };
fakeState.update('subOnly', true);

fakeThemer.handleCommands(twitchUser, '!theme', `!sub`)
.then(() => {
try {
fakeState.get('subOnly')!.should.be.false;
done();
}
catch (error) {
done(error);
}
});
});

test('Themer should not leave subscriber only mode if user is not the logged in user', function(done) {
const twitchUser: Userstate = { 'display-name': 'goofey' };
fakeState.update('subOnly', true);

fakeThemer.handleCommands(twitchUser, '!theme', `!sub`)
.then(() => {
try {
fakeState.get('subOnly')!.should.be.true;
done();
}
catch (error) {
done(error);
}
});
});
});

0 comments on commit cba4e09

Please sign in to comment.