summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorha-dai <contact@haradai.net>2017-11-02 13:58:47 +0900
committerha-dai <contact@haradai.net>2017-11-02 13:58:47 +0900
commit4e83106853e1da2ff7f5b3dabe23c3791c25f289 (patch)
tree1fb73d608bfe65ad9274477fc2b99822d36bf76f /src/api
parentMerge branch 'master' of https://github.com/syuilo/misskey (diff)
parentRefactor (diff)
downloadmisskey-4e83106853e1da2ff7f5b3dabe23c3791c25f289.tar.gz
misskey-4e83106853e1da2ff7f5b3dabe23c3791c25f289.tar.bz2
misskey-4e83106853e1da2ff7f5b3dabe23c3791c25f289.zip
Merge branch 'master' of https://github.com/syuilo/misskey
Diffstat (limited to 'src/api')
-rw-r--r--src/api/common/read-notification.ts52
-rw-r--r--src/api/endpoints.ts37
-rw-r--r--src/api/endpoints/aggregation/posts.ts4
-rw-r--r--src/api/endpoints/aggregation/posts/reply.ts2
-rw-r--r--src/api/endpoints/aggregation/users/activity.ts4
-rw-r--r--src/api/endpoints/aggregation/users/post.ts4
-rw-r--r--src/api/endpoints/channels.ts59
-rw-r--r--src/api/endpoints/channels/create.ts39
-rw-r--r--src/api/endpoints/channels/posts.ts79
-rw-r--r--src/api/endpoints/channels/show.ts31
-rw-r--r--src/api/endpoints/channels/unwatch.ts60
-rw-r--r--src/api/endpoints/channels/watch.ts58
-rw-r--r--src/api/endpoints/i/notifications.ts14
-rw-r--r--src/api/endpoints/notifications/get_unread_count.ts23
-rw-r--r--src/api/endpoints/notifications/mark_as_read.ts47
-rw-r--r--src/api/endpoints/notifications/mark_as_read_all.ts32
-rw-r--r--src/api/endpoints/posts.ts2
-rw-r--r--src/api/endpoints/posts/context.ts8
-rw-r--r--src/api/endpoints/posts/create.ts162
-rw-r--r--src/api/endpoints/posts/replies.ts2
-rw-r--r--src/api/endpoints/posts/timeline.ts37
-rw-r--r--src/api/endpoints/posts/trend.ts2
-rw-r--r--src/api/endpoints/users/get_frequently_replied_users.ts6
-rw-r--r--src/api/endpoints/users/posts.ts2
-rw-r--r--src/api/event.ts6
-rw-r--r--src/api/models/channel-watching.ts3
-rw-r--r--src/api/models/channel.ts14
-rw-r--r--src/api/models/notification.ts5
-rw-r--r--src/api/models/post.ts3
-rw-r--r--src/api/serializers/channel.ts66
-rw-r--r--src/api/serializers/post.ts12
-rw-r--r--src/api/stream/channel.ts12
-rw-r--r--src/api/stream/home.ts6
-rw-r--r--src/api/streaming.ts22
34 files changed, 772 insertions, 143 deletions
diff --git a/src/api/common/read-notification.ts b/src/api/common/read-notification.ts
new file mode 100644
index 0000000000..3009cc5d08
--- /dev/null
+++ b/src/api/common/read-notification.ts
@@ -0,0 +1,52 @@
+import * as mongo from 'mongodb';
+import { default as Notification, INotification } from '../models/notification';
+import publishUserStream from '../event';
+
+/**
+ * Mark as read notification(s)
+ */
+export default (
+ user: string | mongo.ObjectID,
+ message: string | string[] | INotification | INotification[] | mongo.ObjectID | mongo.ObjectID[]
+) => new Promise<any>(async (resolve, reject) => {
+
+ const userId = mongo.ObjectID.prototype.isPrototypeOf(user)
+ ? user
+ : new mongo.ObjectID(user);
+
+ const ids: mongo.ObjectID[] = Array.isArray(message)
+ ? mongo.ObjectID.prototype.isPrototypeOf(message[0])
+ ? (message as mongo.ObjectID[])
+ : typeof message[0] === 'string'
+ ? (message as string[]).map(m => new mongo.ObjectID(m))
+ : (message as INotification[]).map(m => m._id)
+ : mongo.ObjectID.prototype.isPrototypeOf(message)
+ ? [(message as mongo.ObjectID)]
+ : typeof message === 'string'
+ ? [new mongo.ObjectID(message)]
+ : [(message as INotification)._id];
+
+ // Update documents
+ await Notification.update({
+ _id: { $in: ids },
+ is_read: false
+ }, {
+ $set: {
+ is_read: true
+ }
+ }, {
+ multi: true
+ });
+
+ // Calc count of my unread notifications
+ const count = await Notification
+ .count({
+ notifiee_id: userId,
+ is_read: false
+ });
+
+ if (count == 0) {
+ // 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行
+ publishUserStream(userId, 'read_all_notifications');
+ }
+});
diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts
index f05762340c..afefce39e5 100644
--- a/src/api/endpoints.ts
+++ b/src/api/endpoints.ts
@@ -196,17 +196,17 @@ const endpoints: Endpoint[] = [
kind: 'notification-read'
},
{
- name: 'notifications/delete',
+ name: 'notifications/get_unread_count',
withCredential: true,
- kind: 'notification-write'
+ kind: 'notification-read'
},
{
- name: 'notifications/delete_all',
+ name: 'notifications/delete',
withCredential: true,
kind: 'notification-write'
},
{
- name: 'notifications/mark_as_read',
+ name: 'notifications/delete_all',
withCredential: true,
kind: 'notification-write'
},
@@ -474,8 +474,33 @@ const endpoints: Endpoint[] = [
name: 'messaging/messages/create',
withCredential: true,
kind: 'messaging-write'
- }
-
+ },
+ {
+ name: 'channels/create',
+ withCredential: true,
+ limit: {
+ duration: ms('1hour'),
+ max: 3,
+ minInterval: ms('10seconds')
+ }
+ },
+ {
+ name: 'channels/show'
+ },
+ {
+ name: 'channels/posts'
+ },
+ {
+ name: 'channels/watch',
+ withCredential: true
+ },
+ {
+ name: 'channels/unwatch',
+ withCredential: true
+ },
+ {
+ name: 'channels'
+ },
];
export default endpoints;
diff --git a/src/api/endpoints/aggregation/posts.ts b/src/api/endpoints/aggregation/posts.ts
index 48ee225129..9d8bccbdb2 100644
--- a/src/api/endpoints/aggregation/posts.ts
+++ b/src/api/endpoints/aggregation/posts.ts
@@ -19,7 +19,7 @@ module.exports = params => new Promise(async (res, rej) => {
.aggregate([
{ $project: {
repost_id: '$repost_id',
- reply_to_id: '$reply_to_id',
+ reply_id: '$reply_id',
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
@@ -34,7 +34,7 @@ module.exports = params => new Promise(async (res, rej) => {
then: 'repost',
else: {
$cond: {
- if: { $ne: ['$reply_to_id', null] },
+ if: { $ne: ['$reply_id', null] },
then: 'reply',
else: 'post'
}
diff --git a/src/api/endpoints/aggregation/posts/reply.ts b/src/api/endpoints/aggregation/posts/reply.ts
index 02a60c8969..b114c34e1e 100644
--- a/src/api/endpoints/aggregation/posts/reply.ts
+++ b/src/api/endpoints/aggregation/posts/reply.ts
@@ -26,7 +26,7 @@ module.exports = (params) => new Promise(async (res, rej) => {
const datas = await Post
.aggregate([
- { $match: { reply_to: post._id } },
+ { $match: { reply: post._id } },
{ $project: {
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
diff --git a/src/api/endpoints/aggregation/users/activity.ts b/src/api/endpoints/aggregation/users/activity.ts
index 5a3e78c441..102a71d7cb 100644
--- a/src/api/endpoints/aggregation/users/activity.ts
+++ b/src/api/endpoints/aggregation/users/activity.ts
@@ -40,7 +40,7 @@ module.exports = (params) => new Promise(async (res, rej) => {
{ $match: { user_id: user._id } },
{ $project: {
repost_id: '$repost_id',
- reply_to_id: '$reply_to_id',
+ reply_id: '$reply_id',
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
@@ -55,7 +55,7 @@ module.exports = (params) => new Promise(async (res, rej) => {
then: 'repost',
else: {
$cond: {
- if: { $ne: ['$reply_to_id', null] },
+ if: { $ne: ['$reply_id', null] },
then: 'reply',
else: 'post'
}
diff --git a/src/api/endpoints/aggregation/users/post.ts b/src/api/endpoints/aggregation/users/post.ts
index c964815a0c..c6a75eee39 100644
--- a/src/api/endpoints/aggregation/users/post.ts
+++ b/src/api/endpoints/aggregation/users/post.ts
@@ -34,7 +34,7 @@ module.exports = (params) => new Promise(async (res, rej) => {
{ $match: { user_id: user._id } },
{ $project: {
repost_id: '$repost_id',
- reply_to_id: '$reply_to_id',
+ reply_id: '$reply_id',
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
@@ -49,7 +49,7 @@ module.exports = (params) => new Promise(async (res, rej) => {
then: 'repost',
else: {
$cond: {
- if: { $ne: ['$reply_to_id', null] },
+ if: { $ne: ['$reply_id', null] },
then: 'reply',
else: 'post'
}
diff --git a/src/api/endpoints/channels.ts b/src/api/endpoints/channels.ts
new file mode 100644
index 0000000000..e10c943896
--- /dev/null
+++ b/src/api/endpoints/channels.ts
@@ -0,0 +1,59 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Channel from '../models/channel';
+import serialize from '../serializers/channel';
+
+/**
+ * Get all channels
+ *
+ * @param {any} params
+ * @param {any} me
+ * @return {Promise<any>}
+ */
+module.exports = (params, me) => new Promise(async (res, rej) => {
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'since_id' parameter
+ const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
+ if (sinceIdErr) return rej('invalid since_id param');
+
+ // Get 'max_id' parameter
+ const [maxId, maxIdErr] = $(params.max_id).optional.id().$;
+ if (maxIdErr) return rej('invalid max_id param');
+
+ // Check if both of since_id and max_id is specified
+ if (sinceId && maxId) {
+ return rej('cannot set since_id and max_id');
+ }
+
+ // Construct query
+ const sort = {
+ _id: -1
+ };
+ const query = {} as any;
+ if (sinceId) {
+ sort._id = 1;
+ query._id = {
+ $gt: sinceId
+ };
+ } else if (maxId) {
+ query._id = {
+ $lt: maxId
+ };
+ }
+
+ // Issue query
+ const channels = await Channel
+ .find(query, {
+ limit: limit,
+ sort: sort
+ });
+
+ // Serialize
+ res(await Promise.all(channels.map(async channel =>
+ await serialize(channel, me))));
+});
diff --git a/src/api/endpoints/channels/create.ts b/src/api/endpoints/channels/create.ts
new file mode 100644
index 0000000000..a8d7c29dc1
--- /dev/null
+++ b/src/api/endpoints/channels/create.ts
@@ -0,0 +1,39 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Channel from '../../models/channel';
+import Watching from '../../models/channel-watching';
+import serialize from '../../serializers/channel';
+
+/**
+ * Create a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'title' parameter
+ const [title, titleErr] = $(params.title).string().range(1, 100).$;
+ if (titleErr) return rej('invalid title param');
+
+ // Create a channel
+ const channel = await Channel.insert({
+ created_at: new Date(),
+ user_id: user._id,
+ title: title,
+ index: 0,
+ watching_count: 1
+ });
+
+ // Response
+ res(await serialize(channel));
+
+ // Create Watching
+ await Watching.insert({
+ created_at: new Date(),
+ user_id: user._id,
+ channel_id: channel._id
+ });
+});
diff --git a/src/api/endpoints/channels/posts.ts b/src/api/endpoints/channels/posts.ts
new file mode 100644
index 0000000000..fa91fb93ee
--- /dev/null
+++ b/src/api/endpoints/channels/posts.ts
@@ -0,0 +1,79 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import { default as Channel, IChannel } from '../../models/channel';
+import { default as Post, IPost } from '../../models/post';
+import serialize from '../../serializers/post';
+
+/**
+ * Show a posts of a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'limit' parameter
+ const [limit = 1000, limitErr] = $(params.limit).optional.number().range(1, 1000).$;
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'since_id' parameter
+ const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
+ if (sinceIdErr) return rej('invalid since_id param');
+
+ // Get 'max_id' parameter
+ const [maxId, maxIdErr] = $(params.max_id).optional.id().$;
+ if (maxIdErr) return rej('invalid max_id param');
+
+ // Check if both of since_id and max_id is specified
+ if (sinceId && maxId) {
+ return rej('cannot set since_id and max_id');
+ }
+
+ // Get 'channel_id' parameter
+ const [channelId, channelIdErr] = $(params.channel_id).id().$;
+ if (channelIdErr) return rej('invalid channel_id param');
+
+ // Fetch channel
+ const channel: IChannel = await Channel.findOne({
+ _id: channelId
+ });
+
+ if (channel === null) {
+ return rej('channel not found');
+ }
+
+ //#region Construct query
+ const sort = {
+ _id: -1
+ };
+
+ const query = {
+ channel_id: channel._id
+ } as any;
+
+ if (sinceId) {
+ sort._id = 1;
+ query._id = {
+ $gt: sinceId
+ };
+ } else if (maxId) {
+ query._id = {
+ $lt: maxId
+ };
+ }
+ //#endregion Construct query
+
+ // Issue query
+ const posts = await Post
+ .find(query, {
+ limit: limit,
+ sort: sort
+ });
+
+ // Serialize
+ res(await Promise.all(posts.map(async (post) =>
+ await serialize(post, user)
+ )));
+});
diff --git a/src/api/endpoints/channels/show.ts b/src/api/endpoints/channels/show.ts
new file mode 100644
index 0000000000..8861e54594
--- /dev/null
+++ b/src/api/endpoints/channels/show.ts
@@ -0,0 +1,31 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import { default as Channel, IChannel } from '../../models/channel';
+import serialize from '../../serializers/channel';
+
+/**
+ * Show a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'channel_id' parameter
+ const [channelId, channelIdErr] = $(params.channel_id).id().$;
+ if (channelIdErr) return rej('invalid channel_id param');
+
+ // Fetch channel
+ const channel: IChannel = await Channel.findOne({
+ _id: channelId
+ });
+
+ if (channel === null) {
+ return rej('channel not found');
+ }
+
+ // Serialize
+ res(await serialize(channel, user));
+});
diff --git a/src/api/endpoints/channels/unwatch.ts b/src/api/endpoints/channels/unwatch.ts
new file mode 100644
index 0000000000..19d3be118a
--- /dev/null
+++ b/src/api/endpoints/channels/unwatch.ts
@@ -0,0 +1,60 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Channel from '../../models/channel';
+import Watching from '../../models/channel-watching';
+
+/**
+ * Unwatch a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'channel_id' parameter
+ const [channelId, channelIdErr] = $(params.channel_id).id().$;
+ if (channelIdErr) return rej('invalid channel_id param');
+
+ //#region Fetch channel
+ const channel = await Channel.findOne({
+ _id: channelId
+ });
+
+ if (channel === null) {
+ return rej('channel not found');
+ }
+ //#endregion
+
+ //#region Check whether not watching
+ const exist = await Watching.findOne({
+ user_id: user._id,
+ channel_id: channel._id,
+ deleted_at: { $exists: false }
+ });
+
+ if (exist === null) {
+ return rej('already not watching');
+ }
+ //#endregion
+
+ // Delete watching
+ await Watching.update({
+ _id: exist._id
+ }, {
+ $set: {
+ deleted_at: new Date()
+ }
+ });
+
+ // Send response
+ res();
+
+ // Decrement watching count
+ Channel.update(channel._id, {
+ $inc: {
+ watching_count: -1
+ }
+ });
+});
diff --git a/src/api/endpoints/channels/watch.ts b/src/api/endpoints/channels/watch.ts
new file mode 100644
index 0000000000..030e0dd411
--- /dev/null
+++ b/src/api/endpoints/channels/watch.ts
@@ -0,0 +1,58 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Channel from '../../models/channel';
+import Watching from '../../models/channel-watching';
+
+/**
+ * Watch a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'channel_id' parameter
+ const [channelId, channelIdErr] = $(params.channel_id).id().$;
+ if (channelIdErr) return rej('invalid channel_id param');
+
+ //#region Fetch channel
+ const channel = await Channel.findOne({
+ _id: channelId
+ });
+
+ if (channel === null) {
+ return rej('channel not found');
+ }
+ //#endregion
+
+ //#region Check whether already watching
+ const exist = await Watching.findOne({
+ user_id: user._id,
+ channel_id: channel._id,
+ deleted_at: { $exists: false }
+ });
+
+ if (exist !== null) {
+ return rej('already watching');
+ }
+ //#endregion
+
+ // Create Watching
+ await Watching.insert({
+ created_at: new Date(),
+ user_id: user._id,
+ channel_id: channel._id
+ });
+
+ // Send response
+ res();
+
+ // Increment watching count
+ Channel.update(channel._id, {
+ $inc: {
+ watching_count: 1
+ }
+ });
+});
diff --git a/src/api/endpoints/i/notifications.ts b/src/api/endpoints/i/notifications.ts
index 5575fb7412..607e0768a4 100644
--- a/src/api/endpoints/i/notifications.ts
+++ b/src/api/endpoints/i/notifications.ts
@@ -5,6 +5,7 @@ import $ from 'cafy';
import Notification from '../../models/notification';
import serialize from '../../serializers/notification';
import getFriends from '../../common/get-friends';
+import read from '../../common/read-notification';
/**
* Get notifications
@@ -91,17 +92,6 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Mark as read all
if (notifications.length > 0 && markAsRead) {
- const ids = notifications
- .filter(x => x.is_read == false)
- .map(x => x._id);
-
- // Update documents
- await Notification.update({
- _id: { $in: ids }
- }, {
- $set: { is_read: true }
- }, {
- multi: true
- });
+ read(user._id, notifications);
}
});
diff --git a/src/api/endpoints/notifications/get_unread_count.ts b/src/api/endpoints/notifications/get_unread_count.ts
new file mode 100644
index 0000000000..9514e78713
--- /dev/null
+++ b/src/api/endpoints/notifications/get_unread_count.ts
@@ -0,0 +1,23 @@
+/**
+ * Module dependencies
+ */
+import Notification from '../../models/notification';
+
+/**
+ * Get count of unread notifications
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ const count = await Notification
+ .count({
+ notifiee_id: user._id,
+ is_read: false
+ });
+
+ res({
+ count: count
+ });
+});
diff --git a/src/api/endpoints/notifications/mark_as_read.ts b/src/api/endpoints/notifications/mark_as_read.ts
deleted file mode 100644
index 5cce33e850..0000000000
--- a/src/api/endpoints/notifications/mark_as_read.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- * Module dependencies
- */
-import $ from 'cafy';
-import Notification from '../../models/notification';
-import serialize from '../../serializers/notification';
-import event from '../../event';
-
-/**
- * Mark as read a notification
- *
- * @param {any} params
- * @param {any} user
- * @return {Promise<any>}
- */
-module.exports = (params, user) => new Promise(async (res, rej) => {
- const [notificationId, notificationIdErr] = $(params.notification_id).id().$;
- if (notificationIdErr) return rej('invalid notification_id param');
-
- // Get notification
- const notification = await Notification
- .findOne({
- _id: notificationId,
- i: user._id
- });
-
- if (notification === null) {
- return rej('notification-not-found');
- }
-
- // Update
- notification.is_read = true;
- Notification.update({ _id: notification._id }, {
- $set: {
- is_read: true
- }
- });
-
- // Response
- res();
-
- // Serialize
- const notificationObj = await serialize(notification);
-
- // Publish read_notification event
- event(user._id, 'read_notification', notificationObj);
-});
diff --git a/src/api/endpoints/notifications/mark_as_read_all.ts b/src/api/endpoints/notifications/mark_as_read_all.ts
new file mode 100644
index 0000000000..3550e344c4
--- /dev/null
+++ b/src/api/endpoints/notifications/mark_as_read_all.ts
@@ -0,0 +1,32 @@
+/**
+ * Module dependencies
+ */
+import Notification from '../../models/notification';
+import event from '../../event';
+
+/**
+ * Mark as read all notifications
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Update documents
+ await Notification.update({
+ notifiee_id: user._id,
+ is_read: false
+ }, {
+ $set: {
+ is_read: true
+ }
+ }, {
+ multi: true
+ });
+
+ // Response
+ res();
+
+ // 全ての通知を読みましたよというイベントを発行
+ event(user._id, 'read_all_notifications');
+});
diff --git a/src/api/endpoints/posts.ts b/src/api/endpoints/posts.ts
index 23b9bd0b66..f6efcc108d 100644
--- a/src/api/endpoints/posts.ts
+++ b/src/api/endpoints/posts.ts
@@ -62,7 +62,7 @@ module.exports = (params) => new Promise(async (res, rej) => {
}
if (reply != undefined) {
- query.reply_to_id = reply ? { $exists: true, $ne: null } : null;
+ query.reply_id = reply ? { $exists: true, $ne: null } : null;
}
if (repost != undefined) {
diff --git a/src/api/endpoints/posts/context.ts b/src/api/endpoints/posts/context.ts
index cd5f15f481..bad59a6bee 100644
--- a/src/api/endpoints/posts/context.ts
+++ b/src/api/endpoints/posts/context.ts
@@ -49,13 +49,13 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return;
}
- if (p.reply_to_id) {
- await get(p.reply_to_id);
+ if (p.reply_id) {
+ await get(p.reply_id);
}
}
- if (post.reply_to_id) {
- await get(post.reply_to_id);
+ if (post.reply_id) {
+ await get(post.reply_id);
}
// Serialize
diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts
index 805dba7f83..f982b9ee93 100644
--- a/src/api/endpoints/posts/create.ts
+++ b/src/api/endpoints/posts/create.ts
@@ -4,16 +4,17 @@
import $ from 'cafy';
import deepEqual = require('deep-equal');
import parse from '../../common/text';
-import Post from '../../models/post';
-import { isValidText } from '../../models/post';
+import { default as Post, IPost, isValidText } from '../../models/post';
import { default as User, IUser } from '../../models/user';
+import { default as Channel, IChannel } from '../../models/channel';
import Following from '../../models/following';
import DriveFile from '../../models/drive-file';
import Watching from '../../models/post-watching';
+import ChannelWatching from '../../models/channel-watching';
import serialize from '../../serializers/post';
import notify from '../../common/notify';
import watch from '../../common/watch-post';
-import event from '../../event';
+import { default as event, publishChannelStream } from '../../event';
import config from '../../../conf';
/**
@@ -62,7 +63,8 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
const [repostId, repostIdErr] = $(params.repost_id).optional.id().$;
if (repostIdErr) return rej('invalid repost_id');
- let repost = null;
+ let repost: IPost = null;
+ let isQuote = false;
if (repostId !== undefined) {
// Fetch repost to post
repost = await Post.findOne({
@@ -84,43 +86,86 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
}
});
+ isQuote = text != null || files != null;
+
// 直近と同じRepost対象かつ引用じゃなかったらエラー
if (latestPost &&
latestPost.repost_id &&
latestPost.repost_id.equals(repost._id) &&
- text === undefined && files === null) {
+ !isQuote) {
return rej('cannot repost same post that already reposted in your latest post');
}
// 直近がRepost対象かつ引用じゃなかったらエラー
if (latestPost &&
latestPost._id.equals(repost._id) &&
- text === undefined && files === null) {
+ !isQuote) {
return rej('cannot repost your latest post');
}
}
- // Get 'in_reply_to_post_id' parameter
- const [inReplyToPostId, inReplyToPostIdErr] = $(params.reply_to_id).optional.id().$;
- if (inReplyToPostIdErr) return rej('invalid in_reply_to_post_id');
+ // Get 'reply_id' parameter
+ const [replyId, replyIdErr] = $(params.reply_id).optional.id().$;
+ if (replyIdErr) return rej('invalid reply_id');
- let inReplyToPost = null;
- if (inReplyToPostId !== undefined) {
+ let reply: IPost = null;
+ if (replyId !== undefined) {
// Fetch reply
- inReplyToPost = await Post.findOne({
- _id: inReplyToPostId
+ reply = await Post.findOne({
+ _id: replyId
});
- if (inReplyToPost === null) {
+ if (reply === null) {
return rej('in reply to post is not found');
}
// 返信対象が引用でないRepostだったらエラー
- if (inReplyToPost.repost_id && !inReplyToPost.text && !inReplyToPost.media_ids) {
+ if (reply.repost_id && !reply.text && !reply.media_ids) {
return rej('cannot reply to repost');
}
}
+ // Get 'channel_id' parameter
+ const [channelId, channelIdErr] = $(params.channel_id).optional.id().$;
+ if (channelIdErr) return rej('invalid channel_id');
+
+ let channel: IChannel = null;
+ if (channelId !== undefined) {
+ // Fetch channel
+ channel = await Channel.findOne({
+ _id: channelId
+ });
+
+ if (channel === null) {
+ return rej('channel not found');
+ }
+
+ // 返信対象の投稿がこのチャンネルじゃなかったらダメ
+ if (reply && !channelId.equals(reply.channel_id)) {
+ return rej('チャンネル内部からチャンネル外部の投稿に返信することはできません');
+ }
+
+ // Repost対象の投稿がこのチャンネルじゃなかったらダメ
+ if (repost && !channelId.equals(repost.channel_id)) {
+ return rej('チャンネル内部からチャンネル外部の投稿をRepostすることはできません');
+ }
+
+ // 引用ではないRepostはダメ
+ if (repost && !isQuote) {
+ return rej('チャンネル内部では引用ではないRepostをすることはできません');
+ }
+ } else {
+ // 返信対象の投稿がチャンネルへの投稿だったらダメ
+ if (reply && reply.channel_id != null) {
+ return rej('チャンネル外部からチャンネル内部の投稿に返信することはできません');
+ }
+
+ // Repost対象の投稿がチャンネルへの投稿だったらダメ
+ if (repost && repost.channel_id != null) {
+ return rej('チャンネル外部からチャンネル内部の投稿をRepostすることはできません');
+ }
+ }
+
// Get 'poll' parameter
const [poll, pollErr] = $(params.poll).optional.strict.object()
.have('choices', $().array('string')
@@ -148,15 +193,15 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
if (user.latest_post) {
if (deepEqual({
text: user.latest_post.text,
- reply: user.latest_post.reply_to_id ? user.latest_post.reply_to_id.toString() : null,
+ reply: user.latest_post.reply_id ? user.latest_post.reply_id.toString() : null,
repost: user.latest_post.repost_id ? user.latest_post.repost_id.toString() : null,
media_ids: (user.latest_post.media_ids || []).map(id => id.toString())
}, {
- text: text,
- reply: inReplyToPost ? inReplyToPost._id.toString() : null,
- repost: repost ? repost._id.toString() : null,
- media_ids: (files || []).map(file => file._id.toString())
- })) {
+ text: text,
+ reply: reply ? reply._id.toString() : null,
+ repost: repost ? repost._id.toString() : null,
+ media_ids: (files || []).map(file => file._id.toString())
+ })) {
return rej('duplicate');
}
}
@@ -164,8 +209,10 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// 投稿を作成
const post = await Post.insert({
created_at: new Date(),
+ channel_id: channel ? channel._id : undefined,
+ index: channel ? channel.index + 1 : undefined,
media_ids: files ? files.map(file => file._id) : undefined,
- reply_to_id: inReplyToPost ? inReplyToPost._id : undefined,
+ reply_id: reply ? reply._id : undefined,
repost_id: repost ? repost._id : undefined,
poll: poll,
text: text,
@@ -179,8 +226,7 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// Reponse
res(postObj);
- // -----------------------------------------------------------
- // Post processes
+ //#region Post processes
User.update({ _id: user._id }, {
$set: {
@@ -203,23 +249,51 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
}
}
- // Publish event to myself's stream
- event(user._id, 'post', postObj);
+ // タイムラインへの投稿
+ if (!channel) {
+ // Publish event to myself's stream
+ event(user._id, 'post', postObj);
- // Fetch all followers
- const followers = await Following
- .find({
- followee_id: user._id,
+ // Fetch all followers
+ const followers = await Following
+ .find({
+ followee_id: user._id,
+ // 削除されたドキュメントは除く
+ deleted_at: { $exists: false }
+ }, {
+ follower_id: true,
+ _id: false
+ });
+
+ // Publish event to followers stream
+ followers.forEach(following =>
+ event(following.follower_id, 'post', postObj));
+ }
+
+ // チャンネルへの投稿
+ if (channel) {
+ // Increment channel index(posts count)
+ Channel.update({ _id: channel._id }, {
+ $inc: {
+ index: 1
+ }
+ });
+
+ // Publish event to channel
+ publishChannelStream(channel._id, 'post', postObj);
+
+ // Get channel watchers
+ const watches = await ChannelWatching.find({
+ channel_id: channel._id,
// 削除されたドキュメントは除く
deleted_at: { $exists: false }
- }, {
- follower_id: true,
- _id: false
});
- // Publish event to followers stream
- followers.forEach(following =>
- event(following.follower_id, 'post', postObj));
+ // チャンネルの視聴者(のタイムライン)に配信
+ watches.forEach(w => {
+ event(w.user_id, 'post', postObj);
+ });
+ }
// Increment my posts count
User.update({ _id: user._id }, {
@@ -229,23 +303,23 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
});
// If has in reply to post
- if (inReplyToPost) {
+ if (reply) {
// Increment replies count
- Post.update({ _id: inReplyToPost._id }, {
+ Post.update({ _id: reply._id }, {
$inc: {
replies_count: 1
}
});
// 自分自身へのリプライでない限りは通知を作成
- notify(inReplyToPost.user_id, user._id, 'reply', {
+ notify(reply.user_id, user._id, 'reply', {
post_id: post._id
});
// Fetch watchers
Watching
.find({
- post_id: inReplyToPost._id,
+ post_id: reply._id,
user_id: { $ne: user._id },
// 削除されたドキュメントは除く
deleted_at: { $exists: false }
@@ -265,10 +339,10 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// この投稿をWatchする
// TODO: ユーザーが「返信したときに自動でWatchする」設定を
// オフにしていた場合はしない
- watch(user._id, inReplyToPost);
+ watch(user._id, reply);
// Add mention
- addMention(inReplyToPost.user_id, 'reply');
+ addMention(reply.user_id, 'reply');
}
// If it is repost
@@ -369,7 +443,7 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
if (mentionee == null) return;
// 既に言及されたユーザーに対する返信や引用repostの場合も無視
- if (inReplyToPost && inReplyToPost.user_id.equals(mentionee._id)) return;
+ if (reply && reply.user_id.equals(mentionee._id)) return;
if (repost && repost.user_id.equals(mentionee._id)) return;
// Add mention
@@ -406,4 +480,6 @@ module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
}
});
}
+
+ //#endregion
});
diff --git a/src/api/endpoints/posts/replies.ts b/src/api/endpoints/posts/replies.ts
index 89f4d99841..3fd6a46769 100644
--- a/src/api/endpoints/posts/replies.ts
+++ b/src/api/endpoints/posts/replies.ts
@@ -40,7 +40,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Issue query
const replies = await Post
- .find({ reply_to_id: post._id }, {
+ .find({ reply_id: post._id }, {
limit: limit,
skip: offset,
sort: {
diff --git a/src/api/endpoints/posts/timeline.ts b/src/api/endpoints/posts/timeline.ts
index 314e992344..aa5aff5ba5 100644
--- a/src/api/endpoints/posts/timeline.ts
+++ b/src/api/endpoints/posts/timeline.ts
@@ -3,6 +3,7 @@
*/
import $ from 'cafy';
import Post from '../../models/post';
+import ChannelWatching from '../../models/channel-watching';
import getFriends from '../../common/get-friends';
import serialize from '../../serializers/post';
@@ -32,18 +33,43 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
return rej('cannot set since_id and max_id');
}
- // ID list of the user $self and other users who the user follows
+ // ID list of the user itself and other users who the user follows
const followingIds = await getFriends(user._id);
- // Construct query
+ // Watchしているチャンネルを取得
+ const watches = await ChannelWatching.find({
+ user_id: user._id,
+ // 削除されたドキュメントは除く
+ deleted_at: { $exists: false }
+ });
+
+ //#region Construct query
const sort = {
_id: -1
};
+
const query = {
- user_id: {
- $in: followingIds
- }
+ $or: [{
+ // フォローしている人のタイムラインへの投稿
+ user_id: {
+ $in: followingIds
+ },
+ // 「タイムラインへの」投稿に限定するためにチャンネルが指定されていないもののみに限る
+ $or: [{
+ channel_id: {
+ $exists: false
+ }
+ }, {
+ channel_id: null
+ }]
+ }, {
+ // Watchしているチャンネルへの投稿
+ channel_id: {
+ $in: watches.map(w => w.channel_id)
+ }
+ }]
} as any;
+
if (sinceId) {
sort._id = 1;
query._id = {
@@ -54,6 +80,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
$lt: maxId
};
}
+ //#endregion
// Issue query
const timeline = await Post
diff --git a/src/api/endpoints/posts/trend.ts b/src/api/endpoints/posts/trend.ts
index 3277206d26..64a195dff1 100644
--- a/src/api/endpoints/posts/trend.ts
+++ b/src/api/endpoints/posts/trend.ts
@@ -48,7 +48,7 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
} as any;
if (reply != undefined) {
- query.reply_to_id = reply ? { $exists: true, $ne: null } : null;
+ query.reply_id = reply ? { $exists: true, $ne: null } : null;
}
if (repost != undefined) {
diff --git a/src/api/endpoints/users/get_frequently_replied_users.ts b/src/api/endpoints/users/get_frequently_replied_users.ts
index 2e0e2e40a7..bb0f3b4cea 100644
--- a/src/api/endpoints/users/get_frequently_replied_users.ts
+++ b/src/api/endpoints/users/get_frequently_replied_users.ts
@@ -27,7 +27,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
// Fetch recent posts
const recentPosts = await Post.find({
user_id: user._id,
- reply_to_id: {
+ reply_id: {
$exists: true,
$ne: null
}
@@ -38,7 +38,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
limit: 1000,
fields: {
_id: false,
- reply_to_id: true
+ reply_id: true
}
});
@@ -49,7 +49,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
const replyTargetPosts = await Post.find({
_id: {
- $in: recentPosts.map(p => p.reply_to_id)
+ $in: recentPosts.map(p => p.reply_id)
},
user_id: {
$ne: user._id
diff --git a/src/api/endpoints/users/posts.ts b/src/api/endpoints/users/posts.ts
index e37b660773..d8204b8b80 100644
--- a/src/api/endpoints/users/posts.ts
+++ b/src/api/endpoints/users/posts.ts
@@ -85,7 +85,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
}
if (!includeReplies) {
- query.reply_to_id = null;
+ query.reply_id = null;
}
if (withMedia) {
diff --git a/src/api/event.ts b/src/api/event.ts
index 9613a9f7cc..909b0d2556 100644
--- a/src/api/event.ts
+++ b/src/api/event.ts
@@ -25,6 +25,10 @@ class MisskeyEvent {
this.publish(`messaging-stream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value);
}
+ public publishChannelStream(channelId: ID, type: string, value?: any): void {
+ this.publish(`channel-stream:${channelId}`, type, typeof value === 'undefined' ? null : value);
+ }
+
private publish(channel: string, type: string, value?: any): void {
const message = value == null ?
{ type: type } :
@@ -41,3 +45,5 @@ export default ev.publishUserStream.bind(ev);
export const publishPostStream = ev.publishPostStream.bind(ev);
export const publishMessagingStream = ev.publishMessagingStream.bind(ev);
+
+export const publishChannelStream = ev.publishChannelStream.bind(ev);
diff --git a/src/api/models/channel-watching.ts b/src/api/models/channel-watching.ts
new file mode 100644
index 0000000000..6184ae408d
--- /dev/null
+++ b/src/api/models/channel-watching.ts
@@ -0,0 +1,3 @@
+import db from '../../db/mongodb';
+
+export default db.get('channel_watching') as any; // fuck type definition
diff --git a/src/api/models/channel.ts b/src/api/models/channel.ts
new file mode 100644
index 0000000000..c80e84dbc8
--- /dev/null
+++ b/src/api/models/channel.ts
@@ -0,0 +1,14 @@
+import * as mongo from 'mongodb';
+import db from '../../db/mongodb';
+
+const collection = db.get('channels');
+
+export default collection as any; // fuck type definition
+
+export type IChannel = {
+ _id: mongo.ObjectID;
+ created_at: Date;
+ title: string;
+ user_id: mongo.ObjectID;
+ index: number;
+};
diff --git a/src/api/models/notification.ts b/src/api/models/notification.ts
index 1c1f429a0d..1065e8baaa 100644
--- a/src/api/models/notification.ts
+++ b/src/api/models/notification.ts
@@ -1,3 +1,8 @@
+import * as mongo from 'mongodb';
import db from '../../db/mongodb';
export default db.get('notifications') as any; // fuck type definition
+
+export interface INotification {
+ _id: mongo.ObjectID;
+}
diff --git a/src/api/models/post.ts b/src/api/models/post.ts
index 8b9f7f5ef6..7584ce182d 100644
--- a/src/api/models/post.ts
+++ b/src/api/models/post.ts
@@ -10,9 +10,10 @@ export function isValidText(text: string): boolean {
export type IPost = {
_id: mongo.ObjectID;
+ channel_id: mongo.ObjectID;
created_at: Date;
media_ids: mongo.ObjectID[];
- reply_to_id: mongo.ObjectID;
+ reply_id: mongo.ObjectID;
repost_id: mongo.ObjectID;
poll: {}; // todo
text: string;
diff --git a/src/api/serializers/channel.ts b/src/api/serializers/channel.ts
new file mode 100644
index 0000000000..3cba39aa16
--- /dev/null
+++ b/src/api/serializers/channel.ts
@@ -0,0 +1,66 @@
+/**
+ * Module dependencies
+ */
+import * as mongo from 'mongodb';
+import deepcopy = require('deepcopy');
+import { IUser } from '../models/user';
+import { default as Channel, IChannel } from '../models/channel';
+import Watching from '../models/channel-watching';
+
+/**
+ * Serialize a channel
+ *
+ * @param channel target
+ * @param me? serializee
+ * @return response
+ */
+export default (
+ channel: string | mongo.ObjectID | IChannel,
+ me?: string | mongo.ObjectID | IUser
+) => new Promise<any>(async (resolve, reject) => {
+
+ let _channel: any;
+
+ // Populate the channel if 'channel' is ID
+ if (mongo.ObjectID.prototype.isPrototypeOf(channel)) {
+ _channel = await Channel.findOne({
+ _id: channel
+ });
+ } else if (typeof channel === 'string') {
+ _channel = await Channel.findOne({
+ _id: new mongo.ObjectID(channel)
+ });
+ } else {
+ _channel = deepcopy(channel);
+ }
+
+ // Rename _id to id
+ _channel.id = _channel._id;
+ delete _channel._id;
+
+ // Remove needless properties
+ delete _channel.user_id;
+
+ // Me
+ const meId: mongo.ObjectID = me
+ ? mongo.ObjectID.prototype.isPrototypeOf(me)
+ ? me as mongo.ObjectID
+ : typeof me === 'string'
+ ? new mongo.ObjectID(me)
+ : (me as IUser)._id
+ : null;
+
+ if (me) {
+ //#region Watchしているかどうか
+ const watch = await Watching.findOne({
+ user_id: meId,
+ channel_id: _channel.id,
+ deleted_at: { $exists: false }
+ });
+
+ _channel.is_watching = watch !== null;
+ //#endregion
+ }
+
+ resolve(_channel);
+});
diff --git a/src/api/serializers/post.ts b/src/api/serializers/post.ts
index df917a8595..7c3690ef79 100644
--- a/src/api/serializers/post.ts
+++ b/src/api/serializers/post.ts
@@ -8,6 +8,7 @@ import Reaction from '../models/post-reaction';
import { IUser } from '../models/user';
import Vote from '../models/poll-vote';
import serializeApp from './app';
+import serializeChannel from './channel';
import serializeUser from './user';
import serializeDriveFile from './drive-file';
import parse from '../common/text';
@@ -76,8 +77,13 @@ const self = (
_post.app = await serializeApp(_post.app_id);
}
+ // Populate channel
+ if (_post.channel_id) {
+ _post.channel = await serializeChannel(_post.channel_id);
+ }
+
+ // Populate media
if (_post.media_ids) {
- // Populate media
_post.media = await Promise.all(_post.media_ids.map(async fileId =>
await serializeDriveFile(fileId)
));
@@ -117,9 +123,9 @@ const self = (
});
_post.next = next ? next._id : null;
- if (_post.reply_to_id) {
+ if (_post.reply_id) {
// Populate reply to post
- _post.reply_to = await self(_post.reply_to_id, meId, {
+ _post.reply = await self(_post.reply_id, meId, {
detail: false
});
}
diff --git a/src/api/stream/channel.ts b/src/api/stream/channel.ts
new file mode 100644
index 0000000000..d67d77cbf4
--- /dev/null
+++ b/src/api/stream/channel.ts
@@ -0,0 +1,12 @@
+import * as websocket from 'websocket';
+import * as redis from 'redis';
+
+export default function(request: websocket.request, connection: websocket.connection, subscriber: redis.RedisClient): void {
+ const channel = request.resourceURL.query.channel;
+
+ // Subscribe channel stream
+ subscriber.subscribe(`misskey:channel-stream:${channel}`);
+ subscriber.on('message', (_, data) => {
+ connection.send(data);
+ });
+}
diff --git a/src/api/stream/home.ts b/src/api/stream/home.ts
index d5fe01c261..7c8f3bfec8 100644
--- a/src/api/stream/home.ts
+++ b/src/api/stream/home.ts
@@ -4,6 +4,7 @@ import * as debug from 'debug';
import User from '../models/user';
import serializePost from '../serializers/post';
+import readNotification from '../common/read-notification';
const log = debug('misskey');
@@ -45,6 +46,11 @@ export default function homeStream(request: websocket.request, connection: webso
});
break;
+ case 'read_notification':
+ if (!msg.id) return;
+ readNotification(user._id, msg.id);
+ break;
+
case 'capture':
if (!msg.id) return;
const postId = msg.id;
diff --git a/src/api/streaming.ts b/src/api/streaming.ts
index db600013b9..0e512fb210 100644
--- a/src/api/streaming.ts
+++ b/src/api/streaming.ts
@@ -9,6 +9,7 @@ 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(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 :