summaryrefslogtreecommitdiff
path: root/src/api/streaming.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/streaming.ts')
-rw-r--r--src/api/streaming.ts42
1 files changed, 23 insertions, 19 deletions
diff --git a/src/api/streaming.ts b/src/api/streaming.ts
index c71132100c..0e512fb210 100644
--- a/src/api/streaming.ts
+++ b/src/api/streaming.ts
@@ -2,13 +2,14 @@ import * as http from 'http';
import * as websocket from 'websocket';
import * as redis from 'redis';
import config from '../conf';
-import User from './models/user';
+import { default as User, IUser } from './models/user';
import AccessToken from './models/access-token';
import isNativeToken from './common/is-native-token';
import homeStream from './stream/home';
import messagingStream from './stream/messaging';
import serverStream from './stream/server';
+import channelStream from './stream/channel';
module.exports = (server: http.Server) => {
/**
@@ -26,14 +27,6 @@ module.exports = (server: http.Server) => {
return;
}
- const user = await authenticate(connection, request.resourceURL.query.i);
-
- if (user == null) {
- connection.send('authentication-failed');
- connection.close();
- return;
- }
-
// Connect to Redis
const subscriber = redis.createClient(
config.redis.port, config.redis.host);
@@ -43,6 +36,19 @@ module.exports = (server: http.Server) => {
subscriber.quit();
});
+ if (request.resourceURL.pathname === '/channel') {
+ channelStream(request, connection, subscriber);
+ return;
+ }
+
+ const user = await authenticate(request.resourceURL.query.i);
+
+ if (user == null) {
+ connection.send('authentication-failed');
+ connection.close();
+ return;
+ }
+
const channel =
request.resourceURL.pathname === '/' ? homeStream :
request.resourceURL.pathname === '/messaging' ? messagingStream :
@@ -56,7 +62,11 @@ module.exports = (server: http.Server) => {
});
};
-function authenticate(connection: websocket.connection, token: string): Promise<any> {
+/**
+ * 接続してきたユーザーを取得します
+ * @param token 送信されてきたトークン
+ */
+function authenticate(token: string): Promise<IUser> {
if (token == null) {
return Promise.resolve(null);
}
@@ -64,8 +74,7 @@ function authenticate(connection: websocket.connection, token: string): Promise<
return new Promise(async (resolve, reject) => {
if (isNativeToken(token)) {
// Fetch user
- // SELECT _id
- const user = await User
+ const user: IUser = await User
.findOne({
token: token
});
@@ -81,13 +90,8 @@ function authenticate(connection: websocket.connection, token: string): Promise<
}
// Fetch user
- // SELECT _id
- const user = await User
- .findOne({ _id: accessToken.user_id }, {
- fields: {
- _id: true
- }
- });
+ const user: IUser = await User
+ .findOne({ _id: accessToken.user_id });
resolve(user);
}