From 0610acbf6eef11a9a85cc35851150cb3cc072785 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 20 Mar 2017 13:54:59 +0900 Subject: [PATCH] #302 --- package.json | 2 +- src/api/endpoints/posts/polls/vote.ts | 7 ++- src/api/endpoints/posts/reactions/create.ts | 5 ++- src/api/event.ts | 6 +++ src/api/stream/home.ts | 40 ++++++++++++++++- src/api/streaming.ts | 4 -- src/web/app/common/scripts/stream.js | 4 ++ src/web/app/common/tags/poll.tag | 15 ++++--- src/web/app/desktop/tags/timeline-post.tag | 44 +++++++++++++------ src/web/app/mobile/tags/timeline-post.tag | 48 +++++++++++++++------ 10 files changed, 134 insertions(+), 41 deletions(-) diff --git a/package.json b/package.json index 3a6faac574..ac7760c6de 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "misskey", "author": "syuilo ", - "version": "0.0.1340", + "version": "0.0.1395", "license": "MIT", "description": "A miniblog-based SNS", "bugs": "https://github.com/syuilo/misskey/issues", diff --git a/src/api/endpoints/posts/polls/vote.ts b/src/api/endpoints/posts/polls/vote.ts index 6e71d1816f..d359d7d2c3 100644 --- a/src/api/endpoints/posts/polls/vote.ts +++ b/src/api/endpoints/posts/polls/vote.ts @@ -5,6 +5,7 @@ import $ from 'cafy'; import Vote from '../../../models/poll-vote'; import Post from '../../../models/post'; import notify from '../../../common/notify'; +import { publishPostStream } from '../../../event'; /** * Vote poll of a post @@ -62,11 +63,13 @@ module.exports = (params, user) => new Promise(async (res, rej) => { const inc = {}; inc[`poll.choices.${findWithAttr(post.poll.choices, 'id', choice)}.votes`] = 1; - // Increment likes count - Post.update({ _id: post._id }, { + // Increment votes count + await Post.update({ _id: post._id }, { $inc: inc }); + publishPostStream(post._id, 'poll_voted'); + // Notify notify(post.user_id, user._id, 'poll_vote', { post_id: post._id, diff --git a/src/api/endpoints/posts/reactions/create.ts b/src/api/endpoints/posts/reactions/create.ts index de4df5fbe1..5425eaea02 100644 --- a/src/api/endpoints/posts/reactions/create.ts +++ b/src/api/endpoints/posts/reactions/create.ts @@ -5,6 +5,7 @@ import $ from 'cafy'; import Reaction from '../../../models/post-reaction'; import Post from '../../../models/post'; import notify from '../../../common/notify'; +import { publishPostStream } from '../../../event'; /** * React to a post @@ -69,10 +70,12 @@ module.exports = (params, user) => new Promise(async (res, rej) => { inc['reaction_counts.' + reaction] = 1; // Increment reactions count - Post.update({ _id: post._id }, { + await Post.update({ _id: post._id }, { $inc: inc }); + publishPostStream(post._id, 'reacted'); + // Notify notify(post.user_id, user._id, 'reaction', { post_id: post._id, diff --git a/src/api/event.ts b/src/api/event.ts index 24541ee26e..39dc809bdc 100644 --- a/src/api/event.ts +++ b/src/api/event.ts @@ -25,6 +25,10 @@ class MisskeyEvent { this.publish(`user-stream:${userId}`, type, typeof value === 'undefined' ? null : value); } + public publishPostStream(postId: ID, type: string, value?: any): void { + this.publish(`post-stream:${postId}`, type, typeof value === 'undefined' ? null : value); + } + public publishMessagingStream(userId: ID, otherpartyId: ID, type: string, value?: any): void { this.publish(`messaging-stream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value); } @@ -34,4 +38,6 @@ const ev = new MisskeyEvent(); export default ev.publishUserStream.bind(ev); +export const publishPostStream = ev.publishPostStream.bind(ev); + export const publishMessagingStream = ev.publishMessagingStream.bind(ev); diff --git a/src/api/stream/home.ts b/src/api/stream/home.ts index 975bea4c60..80bced8ac8 100644 --- a/src/api/stream/home.ts +++ b/src/api/stream/home.ts @@ -1,10 +1,46 @@ import * as websocket from 'websocket'; import * as redis from 'redis'; +import * as debug from 'debug'; + +import serializePost from '../serializers/post'; + +const log = debug('misskey'); export default function homeStream(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient, user: any): void { // Subscribe Home stream channel subscriber.subscribe(`misskey:user-stream:${user._id}`); - subscriber.on('message', (_, data) => { - connection.send(data); + + subscriber.on('message', async (channel, data) => { + switch (channel.split(':')[1]) { + case 'user-stream': + connection.send(data); + break; + case 'post-stream': + const postId = channel.split(':')[2]; + log(`RECEIVED: ${postId} ${data} by @${user.username}`); + const post = await serializePost(postId, user, { + detail: true + }); + connection.send(JSON.stringify({ + type: 'post-updated', + body: { + post: post + } + })); + break; + } + }); + + connection.on('message', data => { + const msg = JSON.parse(data.utf8Data); + + switch (msg.type) { + case 'capture': + if (!msg.id) return; + const postId = msg.id; + log(`CAPTURE: ${postId} by @${user.username}`); + subscriber.subscribe(`misskey:post-stream:${postId}`); + break; + } }); } diff --git a/src/api/streaming.ts b/src/api/streaming.ts index 17db59fd0a..e1d79481d3 100644 --- a/src/api/streaming.ts +++ b/src/api/streaming.ts @@ -62,10 +62,6 @@ function authenticate(connection: websocket.connection, token: string): Promise< const user = await User .findOne({ token: token - }, { - fields: { - _id: true - } }); resolve(user); diff --git a/src/web/app/common/scripts/stream.js b/src/web/app/common/scripts/stream.js index d6e6bf8aa5..cbdde8d2f2 100644 --- a/src/web/app/common/scripts/stream.js +++ b/src/web/app/common/scripts/stream.js @@ -44,6 +44,10 @@ class Connection { } } + send(message) { + this.socket.send(JSON.stringify(message)); + } + close() { this.socket.removeEventListener('open', this.onOpen); this.socket.removeEventListener('message', this.onMessage); diff --git a/src/web/app/common/tags/poll.tag b/src/web/app/common/tags/poll.tag index ce6460bce9..ce006e5e02 100644 --- a/src/web/app/common/tags/poll.tag +++ b/src/web/app/common/tags/poll.tag @@ -70,11 +70,16 @@