summaryrefslogtreecommitdiff
path: root/src/server/api/stream
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-06-18 14:43:56 +0900
committerGitHub <noreply@github.com>2018-06-18 14:43:56 +0900
commit5d3943ffa8cb4090c5c111397e266d255cc2212b (patch)
tree46f692fb21005bdc89499ea012c2436e8a2e90c0 /src/server/api/stream
parentNew translations ja.yml (Spanish) (diff)
parentyatta (diff)
downloadmisskey-5d3943ffa8cb4090c5c111397e266d255cc2212b.tar.gz
misskey-5d3943ffa8cb4090c5c111397e266d255cc2212b.tar.bz2
misskey-5d3943ffa8cb4090c5c111397e266d255cc2212b.zip
Merge branch 'master' into l10n_master
Diffstat (limited to 'src/server/api/stream')
-rw-r--r--src/server/api/stream/home.ts10
-rw-r--r--src/server/api/stream/local-timeline.ts8
-rw-r--r--src/server/api/stream/notes-stats.ts35
-rw-r--r--src/server/api/stream/requests.ts19
-rw-r--r--src/server/api/stream/reversi-game.ts (renamed from src/server/api/stream/othello-game.ts)78
-rw-r--r--src/server/api/stream/reversi.ts (renamed from src/server/api/stream/othello.ts)8
-rw-r--r--src/server/api/stream/server-stats.ts35
-rw-r--r--src/server/api/stream/server.ts19
8 files changed, 130 insertions, 82 deletions
diff --git a/src/server/api/stream/home.ts b/src/server/api/stream/home.ts
index 54fde2d776..d9b8f7fb96 100644
--- a/src/server/api/stream/home.ts
+++ b/src/server/api/stream/home.ts
@@ -4,7 +4,7 @@ import * as debug from 'debug';
import User, { IUser } from '../../../models/user';
import Mute from '../../../models/mute';
-import { pack as packNote } from '../../../models/note';
+import { pack as packNote, pack } from '../../../models/note';
import readNotification from '../common/read-notification';
import call from '../call';
import { IApp } from '../../../models/app';
@@ -48,6 +48,14 @@ export default async function(
}
//#endregion
+ // Renoteなら再pack
+ if (x.type == 'note' && x.body.renoteId != null) {
+ x.body.renote = await pack(x.body.renoteId, user, {
+ detail: true
+ });
+ data = JSON.stringify(x);
+ }
+
connection.send(data);
} catch (e) {
connection.send(data);
diff --git a/src/server/api/stream/local-timeline.ts b/src/server/api/stream/local-timeline.ts
index a790ba878b..8f6a445be0 100644
--- a/src/server/api/stream/local-timeline.ts
+++ b/src/server/api/stream/local-timeline.ts
@@ -3,6 +3,7 @@ import * as redis from 'redis';
import { IUser } from '../../../models/user';
import Mute from '../../../models/mute';
+import { pack } from '../../../models/note';
export default async function(
request: websocket.request,
@@ -31,6 +32,13 @@ export default async function(
}
//#endregion
+ // Renoteなら再pack
+ if (note.renoteId != null) {
+ note.renote = await pack(note.renoteId, user, {
+ detail: true
+ });
+ }
+
connection.send(JSON.stringify({
type: 'note',
body: note
diff --git a/src/server/api/stream/notes-stats.ts b/src/server/api/stream/notes-stats.ts
new file mode 100644
index 0000000000..ab00620018
--- /dev/null
+++ b/src/server/api/stream/notes-stats.ts
@@ -0,0 +1,35 @@
+import * as websocket from 'websocket';
+import Xev from 'xev';
+
+const ev = new Xev();
+
+export default function(request: websocket.request, connection: websocket.connection): void {
+ const onStats = (stats: any) => {
+ connection.send(JSON.stringify({
+ type: 'stats',
+ body: stats
+ }));
+ };
+
+ connection.on('message', async data => {
+ const msg = JSON.parse(data.utf8Data);
+
+ switch (msg.type) {
+ case 'requestLog':
+ ev.once('notesStatsLog:' + msg.id, statsLog => {
+ connection.send(JSON.stringify({
+ type: 'statsLog',
+ body: statsLog
+ }));
+ });
+ ev.emit('requestNotesStatsLog', msg.id);
+ break;
+ }
+ });
+
+ ev.addListener('notesStats', onStats);
+
+ connection.on('close', () => {
+ ev.removeListener('notesStats', onStats);
+ });
+}
diff --git a/src/server/api/stream/requests.ts b/src/server/api/stream/requests.ts
deleted file mode 100644
index d7bb5e6c5c..0000000000
--- a/src/server/api/stream/requests.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import * as websocket from 'websocket';
-import Xev from 'xev';
-
-const ev = new Xev();
-
-export default function(request: websocket.request, connection: websocket.connection): void {
- const onRequest = request => {
- connection.send(JSON.stringify({
- type: 'request',
- body: request
- }));
- };
-
- ev.addListener('request', onRequest);
-
- connection.on('close', () => {
- ev.removeListener('request', onRequest);
- });
-}
diff --git a/src/server/api/stream/othello-game.ts b/src/server/api/stream/reversi-game.ts
index 841e542610..ea8a9741d2 100644
--- a/src/server/api/stream/othello-game.ts
+++ b/src/server/api/stream/reversi-game.ts
@@ -1,10 +1,10 @@
import * as websocket from 'websocket';
import * as redis from 'redis';
import * as CRC32 from 'crc-32';
-import OthelloGame, { pack } from '../../../models/othello-game';
-import { publishOthelloGameStream } from '../../../publishers/stream';
-import Othello from '../../../othello/core';
-import * as maps from '../../../othello/maps';
+import ReversiGame, { pack } from '../../../models/reversi-game';
+import { publishReversiGameStream } from '../../../publishers/stream';
+import Reversi from '../../../reversi/core';
+import * as maps from '../../../reversi/maps';
import { ParsedUrlQuery } from 'querystring';
export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user?: any): void {
@@ -12,7 +12,7 @@ export default function(request: websocket.request, connection: websocket.connec
const gameId = q.game;
// Subscribe game stream
- subscriber.subscribe(`misskey:othello-game-stream:${gameId}`);
+ subscriber.subscribe(`misskey:reversi-game-stream:${gameId}`);
subscriber.on('message', (_, data) => {
connection.send(data);
});
@@ -61,25 +61,25 @@ export default function(request: websocket.request, connection: websocket.connec
}
});
- async function updateSettings(settings) {
- const game = await OthelloGame.findOne({ _id: gameId });
+ async function updateSettings(settings: any) {
+ const game = await ReversiGame.findOne({ _id: gameId });
if (game.isStarted) return;
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
if (game.user1Id.equals(user._id) && game.user1Accepted) return;
if (game.user2Id.equals(user._id) && game.user2Accepted) return;
- await OthelloGame.update({ _id: gameId }, {
+ await ReversiGame.update({ _id: gameId }, {
$set: {
settings
}
});
- publishOthelloGameStream(gameId, 'update-settings', settings);
+ publishReversiGameStream(gameId, 'update-settings', settings);
}
- async function initForm(form) {
- const game = await OthelloGame.findOne({ _id: gameId });
+ async function initForm(form: any) {
+ const game = await ReversiGame.findOne({ _id: gameId });
if (game.isStarted) return;
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
@@ -90,25 +90,25 @@ export default function(request: websocket.request, connection: websocket.connec
form2: form
};
- await OthelloGame.update({ _id: gameId }, {
+ await ReversiGame.update({ _id: gameId }, {
$set: set
});
- publishOthelloGameStream(gameId, 'init-form', {
+ publishReversiGameStream(gameId, 'init-form', {
userId: user._id,
form
});
}
- async function updateForm(id, value) {
- const game = await OthelloGame.findOne({ _id: gameId });
+ async function updateForm(id: string, value: any) {
+ const game = await ReversiGame.findOne({ _id: gameId });
if (game.isStarted) return;
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
const form = game.user1Id.equals(user._id) ? game.form2 : game.form1;
- const item = form.find(i => i.id == id);
+ const item = form.find((i: any) => i.id == id);
if (item == null) return;
@@ -120,53 +120,53 @@ export default function(request: websocket.request, connection: websocket.connec
form1: form
};
- await OthelloGame.update({ _id: gameId }, {
+ await ReversiGame.update({ _id: gameId }, {
$set: set
});
- publishOthelloGameStream(gameId, 'update-form', {
+ publishReversiGameStream(gameId, 'update-form', {
userId: user._id,
id,
value
});
}
- async function message(message) {
+ async function message(message: any) {
message.id = Math.random();
- publishOthelloGameStream(gameId, 'message', {
+ publishReversiGameStream(gameId, 'message', {
userId: user._id,
message
});
}
async function accept(accept: boolean) {
- const game = await OthelloGame.findOne({ _id: gameId });
+ const game = await ReversiGame.findOne({ _id: gameId });
if (game.isStarted) return;
let bothAccepted = false;
if (game.user1Id.equals(user._id)) {
- await OthelloGame.update({ _id: gameId }, {
+ await ReversiGame.update({ _id: gameId }, {
$set: {
user1Accepted: accept
}
});
- publishOthelloGameStream(gameId, 'change-accepts', {
+ publishReversiGameStream(gameId, 'change-accepts', {
user1: accept,
user2: game.user2Accepted
});
if (accept && game.user2Accepted) bothAccepted = true;
} else if (game.user2Id.equals(user._id)) {
- await OthelloGame.update({ _id: gameId }, {
+ await ReversiGame.update({ _id: gameId }, {
$set: {
user2Accepted: accept
}
});
- publishOthelloGameStream(gameId, 'change-accepts', {
+ publishReversiGameStream(gameId, 'change-accepts', {
user1: game.user1Accepted,
user2: accept
});
@@ -179,7 +179,7 @@ export default function(request: websocket.request, connection: websocket.connec
if (bothAccepted) {
// 3秒後、まだacceptされていたらゲーム開始
setTimeout(async () => {
- const freshGame = await OthelloGame.findOne({ _id: gameId });
+ const freshGame = await ReversiGame.findOne({ _id: gameId });
if (freshGame == null || freshGame.isStarted || freshGame.isEnded) return;
if (!freshGame.user1Accepted || !freshGame.user2Accepted) return;
@@ -198,7 +198,7 @@ export default function(request: websocket.request, connection: websocket.connec
const map = freshGame.settings.map != null ? freshGame.settings.map : getRandomMap();
- await OthelloGame.update({ _id: gameId }, {
+ await ReversiGame.update({ _id: gameId }, {
$set: {
startedAt: new Date(),
isStarted: true,
@@ -208,7 +208,7 @@ export default function(request: websocket.request, connection: websocket.connec
});
//#region 盤面に最初から石がないなどして始まった瞬間に勝敗が決定する場合があるのでその処理
- const o = new Othello(map, {
+ const o = new Reversi(map, {
isLlotheo: freshGame.settings.isLlotheo,
canPutEverywhere: freshGame.settings.canPutEverywhere,
loopedBoard: freshGame.settings.loopedBoard
@@ -224,7 +224,7 @@ export default function(request: websocket.request, connection: websocket.connec
winner = null;
}
- await OthelloGame.update({
+ await ReversiGame.update({
_id: gameId
}, {
$set: {
@@ -233,27 +233,27 @@ export default function(request: websocket.request, connection: websocket.connec
}
});
- publishOthelloGameStream(gameId, 'ended', {
+ publishReversiGameStream(gameId, 'ended', {
winnerId: winner,
game: await pack(gameId, user)
});
}
//#endregion
- publishOthelloGameStream(gameId, 'started', await pack(gameId, user));
+ publishReversiGameStream(gameId, 'started', await pack(gameId, user));
}, 3000);
}
}
// 石を打つ
- async function set(pos) {
- const game = await OthelloGame.findOne({ _id: gameId });
+ async function set(pos: number) {
+ const game = await ReversiGame.findOne({ _id: gameId });
if (!game.isStarted) return;
if (game.isEnded) return;
if (!game.user1Id.equals(user._id) && !game.user2Id.equals(user._id)) return;
- const o = new Othello(game.settings.map, {
+ const o = new Reversi(game.settings.map, {
isLlotheo: game.settings.isLlotheo,
canPutEverywhere: game.settings.canPutEverywhere,
loopedBoard: game.settings.loopedBoard
@@ -290,7 +290,7 @@ export default function(request: websocket.request, connection: websocket.connec
const crc32 = CRC32.str(game.logs.map(x => x.pos.toString()).join('') + pos.toString());
- await OthelloGame.update({
+ await ReversiGame.update({
_id: gameId
}, {
$set: {
@@ -303,20 +303,20 @@ export default function(request: websocket.request, connection: websocket.connec
}
});
- publishOthelloGameStream(gameId, 'set', Object.assign(log, {
+ publishReversiGameStream(gameId, 'set', Object.assign(log, {
next: o.turn
}));
if (o.isEnded) {
- publishOthelloGameStream(gameId, 'ended', {
+ publishReversiGameStream(gameId, 'ended', {
winnerId: winner,
game: await pack(gameId, user)
});
}
}
- async function check(crc32) {
- const game = await OthelloGame.findOne({ _id: gameId });
+ async function check(crc32: string) {
+ const game = await ReversiGame.findOne({ _id: gameId });
if (!game.isStarted) return;
diff --git a/src/server/api/stream/othello.ts b/src/server/api/stream/reversi.ts
index fa62b05836..35c6167364 100644
--- a/src/server/api/stream/othello.ts
+++ b/src/server/api/stream/reversi.ts
@@ -1,12 +1,12 @@
import * as mongo from 'mongodb';
import * as websocket from 'websocket';
import * as redis from 'redis';
-import Matching, { pack } from '../../../models/othello-matching';
+import Matching, { pack } from '../../../models/reversi-matching';
import publishUserStream from '../../../publishers/stream';
export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void {
- // Subscribe othello stream
- subscriber.subscribe(`misskey:othello-stream:${user._id}`);
+ // Subscribe reversi stream
+ subscriber.subscribe(`misskey:reversi-stream:${user._id}`);
subscriber.on('message', (_, data) => {
connection.send(data);
});
@@ -22,7 +22,7 @@ export default function(request: websocket.request, connection: websocket.connec
childId: new mongo.ObjectID(msg.id)
});
if (matching == null) return;
- publishUserStream(matching.childId, 'othello_invited', await pack(matching, matching.childId));
+ publishUserStream(matching.childId, 'reversi_invited', await pack(matching, matching.childId));
break;
}
});
diff --git a/src/server/api/stream/server-stats.ts b/src/server/api/stream/server-stats.ts
new file mode 100644
index 0000000000..2a058de6c3
--- /dev/null
+++ b/src/server/api/stream/server-stats.ts
@@ -0,0 +1,35 @@
+import * as websocket from 'websocket';
+import Xev from 'xev';
+
+const ev = new Xev();
+
+export default function(request: websocket.request, connection: websocket.connection): void {
+ const onStats = (stats: any) => {
+ connection.send(JSON.stringify({
+ type: 'stats',
+ body: stats
+ }));
+ };
+
+ connection.on('message', async data => {
+ const msg = JSON.parse(data.utf8Data);
+
+ switch (msg.type) {
+ case 'requestLog':
+ ev.once('serverStatsLog:' + msg.id, statsLog => {
+ connection.send(JSON.stringify({
+ type: 'statsLog',
+ body: statsLog
+ }));
+ });
+ ev.emit('requestServerStatsLog', msg.id);
+ break;
+ }
+ });
+
+ ev.addListener('serverStats', onStats);
+
+ connection.on('close', () => {
+ ev.removeListener('serverStats', onStats);
+ });
+}
diff --git a/src/server/api/stream/server.ts b/src/server/api/stream/server.ts
deleted file mode 100644
index 4ca2ad1b10..0000000000
--- a/src/server/api/stream/server.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import * as websocket from 'websocket';
-import Xev from 'xev';
-
-const ev = new Xev();
-
-export default function(request: websocket.request, connection: websocket.connection): void {
- const onStats = stats => {
- connection.send(JSON.stringify({
- type: 'stats',
- body: stats
- }));
- };
-
- ev.addListener('stats', onStats);
-
- connection.on('close', () => {
- ev.removeListener('stats', onStats);
- });
-}