summaryrefslogtreecommitdiff
path: root/src/post
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-04-07 16:26:50 +0900
committerGitHub <noreply@github.com>2018-04-07 16:26:50 +0900
commit2547891f940a2872fcfb2b33cd33d4f7a42ca7bc (patch)
tree5cba4ae9cdfd63e7e1ef74a002a7b742183e8d3c /src/post
parentMerge pull request #1410 from akihikodaki/objec (diff)
parentRefactor (diff)
downloadmisskey-2547891f940a2872fcfb2b33cd33d4f7a42ca7bc.tar.gz
misskey-2547891f940a2872fcfb2b33cd33d4f7a42ca7bc.tar.bz2
misskey-2547891f940a2872fcfb2b33cd33d4f7a42ca7bc.zip
Merge pull request #1397 from syuilo/refactor
Refactor
Diffstat (limited to 'src/post')
-rw-r--r--src/post/create.ts40
-rw-r--r--src/post/distribute.ts274
-rw-r--r--src/post/watch.ts26
3 files changed, 0 insertions, 340 deletions
diff --git a/src/post/create.ts b/src/post/create.ts
deleted file mode 100644
index 4ad1503e0f..0000000000
--- a/src/post/create.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-import Post from '../models/post';
-
-export default async (post, reply, repost, mentions) => {
- post.mentions = [];
-
- function addMention(mentionee) {
- // Reject if already added
- if (post.mentions.some(x => x.equals(mentionee))) return;
-
- // Add mention
- post.mentions.push(mentionee);
- }
-
- if (reply) {
- // Add mention
- addMention(reply.userId);
- post.replyId = reply._id;
- post._reply = { userId: reply.userId };
- } else {
- post.replyId = null;
- post._reply = null;
- }
-
- if (repost) {
- if (post.text) {
- // Add mention
- addMention(repost.userId);
- }
-
- post.repostId = repost._id;
- post._repost = { userId: repost.userId };
- } else {
- post.repostId = null;
- post._repost = null;
- }
-
- await Promise.all(mentions.map(({ _id }) => addMention(_id)));
-
- return Post.insert(post);
-};
diff --git a/src/post/distribute.ts b/src/post/distribute.ts
deleted file mode 100644
index f748a620c0..0000000000
--- a/src/post/distribute.ts
+++ /dev/null
@@ -1,274 +0,0 @@
-import Channel from '../models/channel';
-import ChannelWatching from '../models/channel-watching';
-import Following from '../models/following';
-import Mute from '../models/mute';
-import Post, { pack } from '../models/post';
-import Watching from '../models/post-watching';
-import User, { isLocalUser } from '../models/user';
-import stream, { publishChannelStream } from '../publishers/stream';
-import notify from '../publishers/notify';
-import pushSw from '../publishers/push-sw';
-import { createHttp } from '../queue';
-import watch from './watch';
-
-export default async (user, mentions, post) => {
- const promisedPostObj = pack(post);
- const promises = [
- User.update({ _id: user._id }, {
- // Increment my posts count
- $inc: {
- postsCount: 1
- },
-
- $set: {
- latestPost: post._id
- }
- }),
- ] as Array<Promise<any>>;
-
- function addMention(promisedMentionee, reason) {
- // Publish event
- promises.push(promisedMentionee.then(mentionee => {
- if (user._id.equals(mentionee)) {
- return Promise.resolve();
- }
-
- return Promise.all([
- promisedPostObj,
- Mute.find({
- muterId: mentionee,
- deletedAt: { $exists: false }
- })
- ]).then(([postObj, mentioneeMutes]) => {
- const mentioneesMutedUserIds = mentioneeMutes.map(m => m.muteeId.toString());
- if (mentioneesMutedUserIds.indexOf(user._id.toString()) == -1) {
- stream(mentionee, reason, postObj);
- pushSw(mentionee, reason, postObj);
- }
- });
- }));
- }
-
- // タイムラインへの投稿
- if (!post.channelId) {
- promises.push(
- // Publish event to myself's stream
- promisedPostObj.then(postObj => {
- stream(post.userId, 'post', postObj);
- }),
-
- Promise.all([
- User.findOne({ _id: post.userId }),
-
- // Fetch all followers
- Following.aggregate([{
- $lookup: {
- from: 'users',
- localField: 'followerId',
- foreignField: '_id',
- as: 'follower'
- }
- }, {
- $match: {
- followeeId: post.userId
- }
- }], {
- _id: false
- })
- ]).then(([user, followers]) => Promise.all(followers.map(following => {
- if (isLocalUser(following.follower)) {
- // Publish event to followers stream
- return promisedPostObj.then(postObj => {
- stream(following.followerId, 'post', postObj);
- });
- }
-
- return new Promise((resolve, reject) => {
- createHttp({
- type: 'deliverPost',
- fromId: user._id,
- toId: following.followerId,
- postId: post._id
- }).save(error => {
- if (error) {
- reject(error);
- } else {
- resolve();
- }
- });
- });
- })))
- );
- }
-
- // チャンネルへの投稿
- if (post.channelId) {
- promises.push(
- // Increment channel index(posts count)
- Channel.update({ _id: post.channelId }, {
- $inc: {
- index: 1
- }
- }),
-
- // Publish event to channel
- promisedPostObj.then(postObj => {
- publishChannelStream(post.channelId, 'post', postObj);
- }),
-
- Promise.all([
- promisedPostObj,
-
- // Get channel watchers
- ChannelWatching.find({
- channelId: post.channelId,
- // 削除されたドキュメントは除く
- deletedAt: { $exists: false }
- })
- ]).then(([postObj, watches]) => {
- // チャンネルの視聴者(のタイムライン)に配信
- watches.forEach(w => {
- stream(w.userId, 'post', postObj);
- });
- })
- );
- }
-
- // If has in reply to post
- if (post.replyId) {
- promises.push(
- // Increment replies count
- Post.update({ _id: post.replyId }, {
- $inc: {
- repliesCount: 1
- }
- }),
-
- // 自分自身へのリプライでない限りは通知を作成
- promisedPostObj.then(({ reply }) => {
- return notify(reply.userId, user._id, 'reply', {
- postId: post._id
- });
- }),
-
- // Fetch watchers
- Watching
- .find({
- postId: post.replyId,
- userId: { $ne: user._id },
- // 削除されたドキュメントは除く
- deletedAt: { $exists: false }
- }, {
- fields: {
- userId: true
- }
- })
- .then(watchers => {
- watchers.forEach(watcher => {
- notify(watcher.userId, user._id, 'reply', {
- postId: post._id
- });
- });
- })
- );
-
- // Add mention
- addMention(promisedPostObj.then(({ reply }) => reply.userId), 'reply');
-
- // この投稿をWatchする
- if (user.account.settings.autoWatch !== false) {
- promises.push(promisedPostObj.then(({ reply }) => {
- return watch(user._id, reply);
- }));
- }
- }
-
- // If it is repost
- if (post.repostId) {
- const type = post.text ? 'quote' : 'repost';
-
- promises.push(
- promisedPostObj.then(({ repost }) => Promise.all([
- // Notify
- notify(repost.userId, user._id, type, {
- postId: post._id
- }),
-
- // この投稿をWatchする
- // TODO: ユーザーが「Repostしたときに自動でWatchする」設定を
- // オフにしていた場合はしない
- watch(user._id, repost)
- ])),
-
- // Fetch watchers
- Watching
- .find({
- postId: post.repostId,
- userId: { $ne: user._id },
- // 削除されたドキュメントは除く
- deletedAt: { $exists: false }
- }, {
- fields: {
- userId: true
- }
- })
- .then(watchers => {
- watchers.forEach(watcher => {
- notify(watcher.userId, user._id, type, {
- postId: post._id
- });
- });
- })
- );
-
- // If it is quote repost
- if (post.text) {
- // Add mention
- addMention(promisedPostObj.then(({ repost }) => repost.userId), 'quote');
- } else {
- promises.push(promisedPostObj.then(postObj => {
- // Publish event
- if (!user._id.equals(postObj.repost.userId)) {
- stream(postObj.repost.userId, 'repost', postObj);
- }
- }));
- }
-
- // 今までで同じ投稿をRepostしているか
- const existRepost = await Post.findOne({
- userId: user._id,
- repostId: post.repostId,
- _id: {
- $ne: post._id
- }
- });
-
- if (!existRepost) {
- // Update repostee status
- promises.push(Post.update({ _id: post.repostId }, {
- $inc: {
- repostCount: 1
- }
- }));
- }
- }
-
- // Resolve all mentions
- await promisedPostObj.then(({ reply, repost }) => Promise.all(mentions.map(async mention => {
- // 既に言及されたユーザーに対する返信や引用repostの場合も無視
- if (reply && reply.userId.equals(mention)) return;
- if (repost && repost.userId.equals(mention)) return;
-
- // Add mention
- addMention(mention, 'mention');
-
- // Create notification
- await notify(mention, user._id, 'mention', {
- postId: post._id
- });
- })));
-
- await Promise.all(promises);
-
- return promisedPostObj;
-};
diff --git a/src/post/watch.ts b/src/post/watch.ts
deleted file mode 100644
index 61ea444430..0000000000
--- a/src/post/watch.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-import * as mongodb from 'mongodb';
-import Watching from '../models/post-watching';
-
-export default async (me: mongodb.ObjectID, post: object) => {
- // 自分の投稿はwatchできない
- if (me.equals((post as any).userId)) {
- return;
- }
-
- // if watching now
- const exist = await Watching.findOne({
- postId: (post as any)._id,
- userId: me,
- deletedAt: { $exists: false }
- });
-
- if (exist !== null) {
- return;
- }
-
- await Watching.insert({
- createdAt: new Date(),
- postId: (post as any)._id,
- userId: me
- });
-};