Skip to content

Commit

Permalink
fix(integrations): start properly integrations on startup (#2272)
Browse files Browse the repository at this point in the history
Fixes #2269
  • Loading branch information
sogehige committed Jun 18, 2019
1 parent 43f4a2b commit 5adb56f
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 40 deletions.
7 changes: 6 additions & 1 deletion src/bot/integrations/donationalerts.ts
Expand Up @@ -19,12 +19,17 @@ class Donationalerts extends Integration {
super();

if (isMainThread) {
setTimeout(() => {
this.isEnabled().then(value => {
this.onStateChange('enabled', value);
});
}, 10000);
setInterval(() => this.connect(), constants.HOUR); // restart socket each hour
}
}

@onChange('enabled')
onStateChange (key: string, val: string) {
onStateChange (key: string, val: boolean) {
if (val) {this.connect();}
else {this.disconnect();}
}
Expand Down
10 changes: 9 additions & 1 deletion src/bot/integrations/phillipsHue.ts
Expand Up @@ -45,6 +45,14 @@ class PhillipsHue extends Integration {
constructor () {
super();

if (isMainThread) {
setTimeout(() => {
this.isEnabled().then(value => {
this.onStateChange('enabled', value);
});
}, 10000);
}

setInterval(() => {
if (!this.isEnabled()) {return;}
for (let index = 0, length = this.states.length; index < length; index++) {
Expand Down Expand Up @@ -86,7 +94,7 @@ class PhillipsHue extends Integration {
}

@onChange('enabled')
onStateChange (key: string, value: string) {
onStateChange (key: string, value: boolean) {
if (value) {
if (this.host.length === 0 || this.user.length === 0) {return;}

Expand Down
81 changes: 45 additions & 36 deletions src/bot/integrations/spotify.ts
Expand Up @@ -35,18 +35,17 @@ class Spotify extends Integration {
originalUri: string | null = null;
skipToNextSong: boolean = false;
state: any = null;

@shared()
accessToken: string | null = null;
@shared()
refreshToken: string | null = null;
@shared()
userId: string | null = null;
@shared()
playlistId: string | null = null;
@shared()
currentSong: string = JSON.stringify({});

@settings()
_accessToken: string | null = null;
@settings()
_refreshToken: string | null = null;
@settings()
songRequests: boolean = true;
@settings()
Expand All @@ -71,7 +70,7 @@ class Spotify extends Integration {
@ui({ type: 'text-input', readOnly: true })
username: string = '';
@settings('connection')
@ui({ type: 'check-list', current: '_authenticatedScopes' })
@ui({ type: 'check-list', current: 'authenticatedScopes' })
scopes: string[] = [
'user-read-currently-playing',
'user-read-private',
Expand All @@ -83,29 +82,29 @@ class Spotify extends Integration {
'playlist-read-private',
'user-modify-playback-state' ];
@settings('connection')
_authenticatedScopes: string[] = [];
@ui({ ignore: true })
authenticatedScopes: string[] = [];

@ui({
type: 'button-socket',
on: '/integrations/spotify',
class: 'btn btn-primary btn-block',
text: 'integrations.spotify.settings.authorize',
if: function () { return this.username.length === 0; },
if: () => global.integrations.spotify.username.length === 0,
emit: 'authorize'
})
}, 'connection')
authorizeBtn: null = null;

@ui({
type: 'button-socket',
on: '/integrations/spotify',
if: function () { return this.username.length > 0; },
if: () => global.integrations.spotify.username.length > 0,
emit: 'revoke',
class: 'btn btn-primary btn-block',
text: 'integrations.spotify.settings.revoke'
})
}, 'connection')
revokeBtn: null = null;


constructor () {
super();

Expand All @@ -116,6 +115,11 @@ class Spotify extends Integration {
this.timeouts.ICurrentSong = global.setTimeout(() => this.ICurrentSong(), 10000);
this.timeouts.getMe = global.setTimeout(() => this.getMe(), 10000);
setInterval(() => this.sendSongs(), 500);
setTimeout(() => {
this.isEnabled().then(value => {
this.onStateChange('enabled', value);
});
}, 10000);
}
}

Expand All @@ -125,12 +129,14 @@ class Spotify extends Integration {
}

@onChange('enabled')
onStateChange (key: string, value: string) {
onStateChange (key: string, value: boolean) {
this.currentSong = JSON.stringify({});
if (value) {
this.connect();
this.getMe();
} else {this.disconnect();}
} else {
this.disconnect();
}
}

@command('!spotify skip')
Expand All @@ -150,7 +156,7 @@ class Spotify extends Integration {
method: 'put',
url: 'https://api.spotify.com/v1/me/player/play',
headers: {
'Authorization': 'Bearer ' + this.accessToken,
'Authorization': 'Bearer ' + this._accessToken,
'Content-Type': 'application/json'
},
data: {
Expand All @@ -176,7 +182,7 @@ class Spotify extends Integration {
method: 'put',
url: 'https://api.spotify.com/v1/me/player/play',
headers: {
'Authorization': 'Bearer ' + this.accessToken,
'Authorization': 'Bearer ' + this._accessToken,
'Content-Type': 'application/json'
},
data: {
Expand All @@ -189,7 +195,7 @@ class Spotify extends Integration {
method: 'post',
url: 'https://api.spotify.com/v1/me/player/next',
headers: {
'Authorization': 'Bearer ' + this.accessToken
'Authorization': 'Bearer ' + this._accessToken
}
});
this.currentUris = null;
Expand Down Expand Up @@ -305,10 +311,10 @@ class Spotify extends Integration {
clearTimeout(this.timeouts['IRefreshToken']);

try {
if (!_.isNil(this.client) && this.refreshToken) {
if (!_.isNil(this.client) && this._refreshToken) {
let data = await this.client.refreshAccessToken();
this.client.setAccessToken(data.body['access_token']);
this.accessToken = data.body['access_token'];
this._accessToken = data.body['access_token'];
}
} catch (e) {
global.log.info(chalk.yellow('SPOTIFY: ') + 'Refreshing access token failed');
Expand Down Expand Up @@ -357,9 +363,9 @@ class Spotify extends Integration {
this.client.resetAccessToken();
this.client.resetRefreshToken();
this.userId = null;
this.accessToken = null;
this.refreshToken = null;
this._authenticatedScopes = [];
this._accessToken = null;
this._refreshToken = null;
this.authenticatedScopes = [];
this.username = '';
this.currentSong = JSON.stringify({});

Expand All @@ -377,8 +383,9 @@ class Spotify extends Integration {
} else {
try {
const authorizeURI = this.authorizeURI();
if (!authorizeURI) {cb('Integration must enabled to authorize');}
else {
if (!authorizeURI) {
cb('Integration must enabled to authorize');
} else {
cb(null, { do: 'redirect', opts: [authorizeURI] });
}
} catch (e) {
Expand All @@ -397,29 +404,29 @@ class Spotify extends Integration {
if (this.clientId.trim().length === 0) {error.push('clientId');}
if (this.clientSecret.trim().length === 0) {error.push('clientSecret');}
if (this.redirectURI.trim().length === 0) {error.push('redirectURI');}
if (error.length > 0) {throw new Error(error.join(', ') + 'missing');}
if (error.length > 0) {throw new Error(error.join(', ') + ' missing');}

this.client = new SpotifyWebApi({
clientId: this.clientId,
clientSecret: this.clientSecret,
redirectUri: this.redirectURI
});

if (this.accessToken && this.refreshToken) {
this.client.setAccessToken(this.accessToken);
this.client.setRefreshToken(this.refreshToken);
if (this._accessToken && this._refreshToken) {
this.client.setAccessToken(this._accessToken);
this.client.setRefreshToken(this._refreshToken);
}

try {
if (opts.token && !_.isNil(this.client)) {
this.client.authorizationCodeGrant(opts.token)
.then((data) => {
this._authenticatedScopes = data.body.scope.split(' ');
this.accessToken = data.body['access_token'];
this.refreshToken = data.body['refresh_token'];
this.authenticatedScopes = data.body.scope.split(' ');
this._accessToken = data.body['access_token'];
this._refreshToken = data.body['refresh_token'];

this.client.setAccessToken(this.accessToken);
this.client.setRefreshToken(this.refreshToken);
this.client.setAccessToken(this._accessToken);
this.client.setRefreshToken(this._refreshToken);
}, (err) => {
if (err) {global.log.info(chalk.yellow('SPOTIFY: ') + 'Getting of accessToken and refreshToken failed');}
});
Expand All @@ -440,7 +447,9 @@ class Spotify extends Integration {
}

authorizeURI () {
if (_.isNil(this.client)) {return null;}
if (_.isNil(this.client)) {
return null;
}
let state = crypto.createHash('md5').update(Math.random().toString()).digest('hex');
this.state = state;
return this.client.createAuthorizeURL(this.scopes, state);
Expand Down Expand Up @@ -475,7 +484,7 @@ class Spotify extends Integration {
method: 'get',
url: 'https://api.spotify.com/v1/tracks/' + id,
headers: {
'Authorization': 'Bearer ' + this.accessToken
'Authorization': 'Bearer ' + this._accessToken
}
});
let track = response.data;
Expand All @@ -494,7 +503,7 @@ class Spotify extends Integration {
method: 'get',
url: 'https://api.spotify.com/v1/search?type=track&limit=1&q=' + encodeURI(spotifyId),
headers: {
'Authorization': 'Bearer ' + this.accessToken,
'Authorization': 'Bearer ' + this._accessToken,
'Content-Type': 'application/json'
}
});
Expand Down
15 changes: 14 additions & 1 deletion src/bot/integrations/streamlabs.ts
Expand Up @@ -5,6 +5,7 @@ import chalk from 'chalk';
import Integration from './_interface';
import { settings, ui } from '../decorators';
import { onChange } from '../decorators/on';
import { isMainThread } from 'worker_threads';

class Streamlabs extends Integration {
socket: SocketIOClient.Socket | null = null;
Expand All @@ -13,8 +14,20 @@ class Streamlabs extends Integration {
@ui({ type: 'text-input', secret: true })
socketToken: string = '';

constructor () {
super();

if (isMainThread) {
setTimeout(() => {
this.isEnabled().then(value => {
this.onStateChange('enabled', value);
});
}, 10000);
}
}

@onChange('enabled')
onStateChange (key: string, val: string) {
onStateChange (key: string, val: boolean) {
if (val) {this.connect();}
else {this.disconnect();}
}
Expand Down
7 changes: 6 additions & 1 deletion src/bot/integrations/twitter.ts
Expand Up @@ -39,6 +39,11 @@ class Twitter extends Integration {
setInterval(() => {
this.updateStreams();
}, 10000);
setTimeout(() => {
this.isEnabled().then(value => {
this.onStateChange('enabled', value);
});
}, 10000);
}
}

Expand Down Expand Up @@ -121,7 +126,7 @@ class Twitter extends Integration {
}

@onChange('enabled')
public onStateChange(key: string, value: string) {
public onStateChange(key: string, value: boolean) {
if (value) {
this.connect();
} else {
Expand Down
19 changes: 19 additions & 0 deletions tools/migrate.js
Expand Up @@ -296,6 +296,25 @@ let migration = {
'clips.shouldPlay': 'cClipsShouldPlay',
'clips.volume': 'cClipsVolume',
}
},
'integrations.settings': {
spotify: {
'_.accessToken': '_accessToken',
'_.refreshToken': '_refreshToken',
'output.format': 'format',
'output.playlistToPlay': 'playlistToPlay',
'output.continueOnPlaylistAfterRequest': 'continueOnPlaylistAfterRequest',
'connection.clientId': 'clientId',
'connection.clientSecret': 'clientSecret',
'connection.redirectURI': 'redirectURI',
'connection._authenticatedScopes': 'authenticatedScopes',
},
twitter: {
'token.consumerKey': 'consumerKey',
'token.consumerSecret': 'consumerSecret',
'token.accessToken': 'accessToken',
'token.secretToken': 'secretToken',
}
}
};

Expand Down

0 comments on commit 5adb56f

Please sign in to comment.