Skip to content

Commit 563ca26

Browse files
committed
fix(twitch): fix setting up own app generator
1 parent de2248f commit 563ca26

3 files changed

Lines changed: 34 additions & 25 deletions

File tree

d.ts/src/helpers/socket.d.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,9 @@ import type { AlertInterface, EmitData } from '@entity/alert';
33
import type { BetsInterface } from '@entity/bets';
44
import type { CacheTitlesInterface } from '@entity/cacheTitles';
55
import type { ChecklistInterface } from '@entity/checklist';
6-
7-
import { AliasGroup, Alias } from '~/database/entity/alias';
8-
96
import type { CommandsCountInterface, CommandsGroupInterface, CommandsInterface } from '@entity/commands';
10-
11-
import { CacheGamesInterface } from '~/database/entity/cacheGames';
12-
137
import type { CooldownInterface } from '@entity/cooldown';
14-
15-
import { Plugin } from '~/database/entity/plugins';
16-
178
import type { EventInterface, Events } from '@entity/event';
18-
19-
import { MenuItem } from '~/helpers/panel';
20-
219
import type { EventListInterface } from '@entity/eventList';
2210
import type { GalleryInterface } from '@entity/gallery';
2311
import type { HighlightInterface } from '@entity/highlight';
@@ -47,6 +35,11 @@ import { FindConditions } from 'typeorm';
4735
import { QuickActions } from '../../../src/database/entity/dashboard';
4836
import { WidgetCustomInterface, WidgetSocialInterface } from '../../../src/database/entity/widget';
4937

38+
import { AliasGroup, Alias } from '~/database/entity/alias';
39+
import { CacheGamesInterface } from '~/database/entity/cacheGames';
40+
import { Plugin } from '~/database/entity/plugins';
41+
import { MenuItem } from '~/helpers/panel';
42+
5043
type Configuration = {
5144
[x:string]: Configuration | string;
5245
};
@@ -295,6 +288,7 @@ export type ClientToServerEventsWithNamespace = {
295288
'broadcaster': (cb: (error: Error | string | null, username: string) => void) => void,
296289
'twitch::revoke': (data: { accountType: 'bot' | 'broadcaster' }, cb: (err: Error | string | null) => void) => void,
297290
'twitch::token': (data: { accessToken: string, refreshToken: string, accountType: 'bot' | 'broadcaster' }, cb: (err: Error | string | null) => void) => void,
291+
'twitch::token::ownApp': (data: { accessToken: string, refreshToken: string, accountType: 'bot' | 'broadcaster', clientId: string, clientSecret: string }, cb: (err: Error | string | null) => void) => void,
298292
},
299293
'/core/socket': GenericEvents & {
300294
'purgeAllConnections': (cb: (error: Error | string | null) => void, socket?: Socket) => void,

src/services/twitch.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ import { dayjs, timezone } from '@sogebot/ui-helpers/dayjsHelper';
55
import { getTime } from '@sogebot/ui-helpers/getTime';
66
import { capitalize } from 'lodash';
77

8-
import {
9-
command, default_permission, example, persistent, settings,
10-
} from '../decorators';
11-
import { onChange, onLoad, onStartup, onStreamStart } from '../decorators/on';
12-
import Expects from '../expects';
13-
import emitter from '../helpers/interfaceEmitter';
14-
import { debug, error, info } from '../helpers/log';
15-
import users from '../users';
168
import Service from './_interface';
179
import { init as apiIntervalInit, stop as apiIntervalStop } from './twitch/api/interval';
1810
import { createClip } from './twitch/calls/createClip';
@@ -22,6 +14,14 @@ import EventSub from './twitch/eventsub';
2214
import PubSub from './twitch/pubsub';
2315
import { cleanErrors } from './twitch/token/refresh';
2416
import { cache, validate } from './twitch/token/validate';
17+
import {
18+
command, default_permission, example, persistent, settings,
19+
} from '../decorators';
20+
import { onChange, onLoad, onStartup, onStreamStart } from '../decorators/on';
21+
import Expects from '../expects';
22+
import emitter from '../helpers/interfaceEmitter';
23+
import { debug, error, info } from '../helpers/log';
24+
import users from '../users';
2525

2626
import { AppDataSource } from '~/database';
2727
import {
@@ -387,8 +387,23 @@ class Twitch extends Service {
387387
emitter.emit('set', '/services/twitch', `tokenService`, 'SogeBot Token Generator v2');
388388
emitter.emit('set', '/services/twitch', `${accountType}RefreshToken`, refreshToken);
389389
emitter.emit('set', '/services/twitch', `${accountType}AccessToken`, accessToken);
390-
await validate(accountType);
391-
cb(null);
390+
emitter.emit('set', '/services/twitch', `${accountType}TokenValid`, true);
391+
await validate(accountType, 0, true);
392+
setTimeout(async () => {
393+
cb(null);
394+
}, 1000);
395+
});
396+
adminEndpoint('/services/twitch', 'twitch::token::ownApp', async ({ accessToken, refreshToken, accountType, clientId, clientSecret }, cb) => {
397+
emitter.emit('set', '/services/twitch', `tokenService`, 'Own Twitch App');
398+
emitter.emit('set', '/services/twitch', `${accountType}AccessToken`, accessToken);
399+
emitter.emit('set', '/services/twitch', `${accountType}RefreshToken`, refreshToken);
400+
emitter.emit('set', '/services/twitch', `tokenServiceCustomClientId`, clientId);
401+
emitter.emit('set', '/services/twitch', `tokenServiceCustomClientSecret`, clientSecret);
402+
emitter.emit('set', '/services/twitch', `${accountType}TokenValid`, true);
403+
await validate(accountType, 0, true);
404+
setTimeout(async () => {
405+
cb(null);
406+
}, 1000);
392407
});
393408
}
394409

src/services/twitch/token/validate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import * as constants from '@sogebot/ui-helpers/constants';
22
import { Mutex } from 'async-mutex';
33
import axios from 'axios';
44

5+
import { refresh } from './refresh';
56
import { setStatus } from '../../../helpers/parser';
67
import { tmiEmitter } from '../../../helpers/tmi';
78
import client from '../api/client';
8-
import { refresh } from './refresh';
99

1010
import emitter from '~/helpers/interfaceEmitter';
1111
import {
@@ -49,7 +49,7 @@ const mutex = new Mutex();
4949
"user_id": "<authorized user ID>"
5050
}
5151
*/
52-
export const validate = async (type: 'bot' | 'broadcaster', retry = 0): Promise < boolean > => {
52+
export const validate = async (type: 'bot' | 'broadcaster', retry = 0, force = false): Promise < boolean > => {
5353
const release = await mutex.acquire();
5454
try {
5555
debug('oauth.validate', `Validation: ${type} - ${retry} retries`);
@@ -62,7 +62,7 @@ export const validate = async (type: 'bot' | 'broadcaster', retry = 0): Promise
6262
}
6363

6464
let token: string | null;
65-
if (expirationDate[type] - Date.now() > 5 * constants.MINUTE && expirationDate[type] !== -1) {
65+
if (!force && expirationDate[type] - Date.now() > 5 * constants.MINUTE && expirationDate[type] !== -1) {
6666
debug('oauth.validate', `Skipping refresh token for ${type}, expiration time: ${new Date(expirationDate[type]).toISOString()}`);
6767
return true;
6868
} else {

0 commit comments

Comments
 (0)