Redis必須に
This commit is contained in:
		
							parent
							
								
									186d7bbfd9
								
							
						
					
					
						commit
						e369031a28
					
				
					 12 changed files with 48 additions and 111 deletions
				
			
		| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
import * as redis from 'redis';
 | 
			
		||||
import config from '../config';
 | 
			
		||||
 | 
			
		||||
export default config.redis ? redis.createClient(
 | 
			
		||||
export default redis.createClient(
 | 
			
		||||
	config.redis.port,
 | 
			
		||||
	config.redis.host,
 | 
			
		||||
	{
 | 
			
		||||
| 
						 | 
				
			
			@ -9,4 +9,4 @@ export default config.redis ? redis.createClient(
 | 
			
		|||
		prefix: config.redis.prefix,
 | 
			
		||||
		db: config.redis.db || 0
 | 
			
		||||
	}
 | 
			
		||||
) : null;
 | 
			
		||||
);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,12 +8,6 @@ import Logger from '../../services/logger';
 | 
			
		|||
const logger = new Logger('limiter');
 | 
			
		||||
 | 
			
		||||
export default (endpoint: IEndpoint, user: User) => new Promise((ok, reject) => {
 | 
			
		||||
	// Redisがインストールされてない場合は常に許可
 | 
			
		||||
	if (limiterDB == null) {
 | 
			
		||||
		ok();
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	const limitation = endpoint.meta.limit!;
 | 
			
		||||
 | 
			
		||||
	const key = limitation.hasOwnProperty('key')
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -83,8 +83,6 @@ async function getOAuth2() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
router.get('/connect/discord', async ctx => {
 | 
			
		||||
	if (redis == null) return;
 | 
			
		||||
 | 
			
		||||
	if (!compareOrigin(ctx)) {
 | 
			
		||||
		ctx.throw(400, 'invalid origin');
 | 
			
		||||
		return;
 | 
			
		||||
| 
						 | 
				
			
			@ -110,8 +108,6 @@ router.get('/connect/discord', async ctx => {
 | 
			
		|||
});
 | 
			
		||||
 | 
			
		||||
router.get('/signin/discord', async ctx => {
 | 
			
		||||
	if (redis == null) return;
 | 
			
		||||
 | 
			
		||||
	const sessid = uuid();
 | 
			
		||||
 | 
			
		||||
	const params = {
 | 
			
		||||
| 
						 | 
				
			
			@ -138,8 +134,6 @@ router.get('/signin/discord', async ctx => {
 | 
			
		|||
});
 | 
			
		||||
 | 
			
		||||
router.get('/dc/cb', async ctx => {
 | 
			
		||||
	if (redis == null) return;
 | 
			
		||||
 | 
			
		||||
	const userToken = getUserToken(ctx);
 | 
			
		||||
 | 
			
		||||
	const oauth2 = await getOAuth2();
 | 
			
		||||
| 
						 | 
				
			
			@ -160,7 +154,7 @@ router.get('/dc/cb', async ctx => {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		const { redirect_uri, state } = await new Promise<any>((res, rej) => {
 | 
			
		||||
			redis!.get(sessid, async (_, state) => {
 | 
			
		||||
			redis.get(sessid, async (_, state) => {
 | 
			
		||||
				res(JSON.parse(state));
 | 
			
		||||
			});
 | 
			
		||||
		});
 | 
			
		||||
| 
						 | 
				
			
			@ -241,7 +235,7 @@ router.get('/dc/cb', async ctx => {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		const { redirect_uri, state } = await new Promise<any>((res, rej) => {
 | 
			
		||||
			redis!.get(userToken, async (_, state) => {
 | 
			
		||||
			redis.get(userToken, async (_, state) => {
 | 
			
		||||
				res(JSON.parse(state));
 | 
			
		||||
			});
 | 
			
		||||
		});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -80,8 +80,6 @@ async function getOath2() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
router.get('/connect/github', async ctx => {
 | 
			
		||||
	if (redis == null) return;
 | 
			
		||||
 | 
			
		||||
	if (!compareOrigin(ctx)) {
 | 
			
		||||
		ctx.throw(400, 'invalid origin');
 | 
			
		||||
		return;
 | 
			
		||||
| 
						 | 
				
			
			@ -106,8 +104,6 @@ router.get('/connect/github', async ctx => {
 | 
			
		|||
});
 | 
			
		||||
 | 
			
		||||
router.get('/signin/github', async ctx => {
 | 
			
		||||
	if (redis == null) return;
 | 
			
		||||
 | 
			
		||||
	const sessid = uuid();
 | 
			
		||||
 | 
			
		||||
	const params = {
 | 
			
		||||
| 
						 | 
				
			
			@ -133,8 +129,6 @@ router.get('/signin/github', async ctx => {
 | 
			
		|||
});
 | 
			
		||||
 | 
			
		||||
router.get('/gh/cb', async ctx => {
 | 
			
		||||
	if (redis == null) return;
 | 
			
		||||
 | 
			
		||||
	const userToken = getUserToken(ctx);
 | 
			
		||||
 | 
			
		||||
	const oauth2 = await getOath2();
 | 
			
		||||
| 
						 | 
				
			
			@ -155,7 +149,7 @@ router.get('/gh/cb', async ctx => {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		const { redirect_uri, state } = await new Promise<any>((res, rej) => {
 | 
			
		||||
			redis!.get(sessid, async (_, state) => {
 | 
			
		||||
			redis.get(sessid, async (_, state) => {
 | 
			
		||||
				res(JSON.parse(state));
 | 
			
		||||
			});
 | 
			
		||||
		});
 | 
			
		||||
| 
						 | 
				
			
			@ -222,7 +216,7 @@ router.get('/gh/cb', async ctx => {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		const { redirect_uri, state } = await new Promise<any>((res, rej) => {
 | 
			
		||||
			redis!.get(userToken, async (_, state) => {
 | 
			
		||||
			redis.get(userToken, async (_, state) => {
 | 
			
		||||
				res(JSON.parse(state));
 | 
			
		||||
			});
 | 
			
		||||
		});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -79,8 +79,6 @@ async function getTwAuth() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
router.get('/connect/twitter', async ctx => {
 | 
			
		||||
	if (redis == null) return;
 | 
			
		||||
 | 
			
		||||
	if (!compareOrigin(ctx)) {
 | 
			
		||||
		ctx.throw(400, 'invalid origin');
 | 
			
		||||
		return;
 | 
			
		||||
| 
						 | 
				
			
			@ -99,8 +97,6 @@ router.get('/connect/twitter', async ctx => {
 | 
			
		|||
});
 | 
			
		||||
 | 
			
		||||
router.get('/signin/twitter', async ctx => {
 | 
			
		||||
	if (redis == null) return;
 | 
			
		||||
 | 
			
		||||
	const twAuth = await getTwAuth();
 | 
			
		||||
	const twCtx = await twAuth!.begin();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -122,8 +118,6 @@ router.get('/signin/twitter', async ctx => {
 | 
			
		|||
});
 | 
			
		||||
 | 
			
		||||
router.get('/tw/cb', async ctx => {
 | 
			
		||||
	if (redis == null) return;
 | 
			
		||||
 | 
			
		||||
	const userToken = getUserToken(ctx);
 | 
			
		||||
 | 
			
		||||
	const twAuth = await getTwAuth();
 | 
			
		||||
| 
						 | 
				
			
			@ -137,7 +131,7 @@ router.get('/tw/cb', async ctx => {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		const get = new Promise<any>((res, rej) => {
 | 
			
		||||
			redis!.get(sessid, async (_, twCtx) => {
 | 
			
		||||
			redis.get(sessid, async (_, twCtx) => {
 | 
			
		||||
				res(twCtx);
 | 
			
		||||
			});
 | 
			
		||||
		});
 | 
			
		||||
| 
						 | 
				
			
			@ -170,7 +164,7 @@ router.get('/tw/cb', async ctx => {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		const get = new Promise<any>((res, rej) => {
 | 
			
		||||
			redis!.get(userToken, async (_, twCtx) => {
 | 
			
		||||
			redis.get(userToken, async (_, twCtx) => {
 | 
			
		||||
				res(twCtx);
 | 
			
		||||
			});
 | 
			
		||||
		});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,6 @@
 | 
			
		|||
import * as http from 'http';
 | 
			
		||||
import * as websocket from 'websocket';
 | 
			
		||||
import * as redis from 'redis';
 | 
			
		||||
import Xev from 'xev';
 | 
			
		||||
 | 
			
		||||
import MainStreamConnection from './stream';
 | 
			
		||||
import { ParsedUrlQuery } from 'querystring';
 | 
			
		||||
| 
						 | 
				
			
			@ -23,28 +22,24 @@ module.exports = (server: http.Server) => {
 | 
			
		|||
 | 
			
		||||
		let ev: EventEmitter;
 | 
			
		||||
 | 
			
		||||
		if (config.redis) {
 | 
			
		||||
			// Connect to Redis
 | 
			
		||||
			const subscriber = redis.createClient(
 | 
			
		||||
				config.redis.port, config.redis.host);
 | 
			
		||||
		// Connect to Redis
 | 
			
		||||
		const subscriber = redis.createClient(
 | 
			
		||||
			config.redis.port, config.redis.host);
 | 
			
		||||
 | 
			
		||||
			subscriber.subscribe('misskey');
 | 
			
		||||
		subscriber.subscribe('misskey');
 | 
			
		||||
 | 
			
		||||
			ev = new EventEmitter();
 | 
			
		||||
		ev = new EventEmitter();
 | 
			
		||||
 | 
			
		||||
			subscriber.on('message', async (_, data) => {
 | 
			
		||||
				const obj = JSON.parse(data);
 | 
			
		||||
		subscriber.on('message', async (_, data) => {
 | 
			
		||||
			const obj = JSON.parse(data);
 | 
			
		||||
 | 
			
		||||
				ev.emit(obj.channel, obj.message);
 | 
			
		||||
			});
 | 
			
		||||
			ev.emit(obj.channel, obj.message);
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
			connection.once('close', () => {
 | 
			
		||||
				subscriber.unsubscribe();
 | 
			
		||||
				subscriber.quit();
 | 
			
		||||
			});
 | 
			
		||||
		} else {
 | 
			
		||||
			ev = new Xev();
 | 
			
		||||
		}
 | 
			
		||||
		connection.once('close', () => {
 | 
			
		||||
			subscriber.unsubscribe();
 | 
			
		||||
			subscriber.quit();
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		const main = new MainStreamConnection(connection, ev, user, app);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,33 +1,19 @@
 | 
			
		|||
import redis from '../db/redis';
 | 
			
		||||
import Xev from 'xev';
 | 
			
		||||
import { User } from '../models/entities/user';
 | 
			
		||||
import { Note } from '../models/entities/note';
 | 
			
		||||
import { UserList } from '../models/entities/user-list';
 | 
			
		||||
import { ReversiGame } from '../models/entities/games/reversi/game';
 | 
			
		||||
 | 
			
		||||
class Publisher {
 | 
			
		||||
	private ev: Xev | null = null;
 | 
			
		||||
 | 
			
		||||
	constructor() {
 | 
			
		||||
		// Redisがインストールされてないときはプロセス間通信を使う
 | 
			
		||||
		if (redis == null) {
 | 
			
		||||
			this.ev = new Xev();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private publish = (channel: string, type: string | null, value?: any): void => {
 | 
			
		||||
		const message = type == null ? value : value == null ?
 | 
			
		||||
			{ type: type, body: null } :
 | 
			
		||||
			{ type: type, body: value };
 | 
			
		||||
 | 
			
		||||
		if (this.ev) {
 | 
			
		||||
			this.ev.emit(channel, message);
 | 
			
		||||
		} else {
 | 
			
		||||
			redis!.publish('misskey', JSON.stringify({
 | 
			
		||||
				channel: channel,
 | 
			
		||||
				message: message
 | 
			
		||||
			}));
 | 
			
		||||
		}
 | 
			
		||||
		redis.publish('misskey', JSON.stringify({
 | 
			
		||||
			channel: channel,
 | 
			
		||||
			message: message
 | 
			
		||||
		}));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public publishMainStream = (userId: User['id'], type: string, value?: any): void => {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue