summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/posts
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/api/endpoints/posts')
-rw-r--r--src/server/api/endpoints/posts/create.ts88
-rw-r--r--src/server/api/endpoints/posts/reactions/create.ts88
2 files changed, 18 insertions, 158 deletions
diff --git a/src/server/api/endpoints/posts/create.ts b/src/server/api/endpoints/posts/create.ts
index 47897626f1..003a892bc0 100644
--- a/src/server/api/endpoints/posts/create.ts
+++ b/src/server/api/endpoints/posts/create.ts
@@ -3,17 +3,12 @@
*/
import $ from 'cafy';
import deepEqual = require('deep-equal');
-import parseAcct from '../../../../acct/parse';
-import renderAcct from '../../../../acct/render';
-import config from '../../../../config';
-import html from '../../../../text/html';
-import parse from '../../../../text/parse';
-import Post, { IPost, isValidText, isValidCw } from '../../../../models/post';
-import User, { ILocalUser } from '../../../../models/user';
+import Post, { IPost, isValidText, isValidCw, pack } from '../../../../models/post';
+import { ILocalUser } from '../../../../models/user';
import Channel, { IChannel } from '../../../../models/channel';
import DriveFile from '../../../../models/drive-file';
-import create from '../../../../post/create';
-import distribute from '../../../../post/distribute';
+import create from '../../../../services/post/create';
+import { IApp } from '../../../../models/app';
/**
* Create a post
@@ -23,7 +18,7 @@ import distribute from '../../../../post/distribute';
* @param {any} app
* @return {Promise<any>}
*/
-module.exports = (params, user: ILocalUser, app) => new Promise(async (res, rej) => {
+module.exports = (params, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => {
// Get 'visibility' parameter
const [visibility = 'public', visibilityErr] = $(params.visibility).optional.string().or(['public', 'unlisted', 'private', 'direct']).$;
if (visibilityErr) return rej('invalid visibility');
@@ -231,85 +226,26 @@ module.exports = (params, user: ILocalUser, app) => new Promise(async (res, rej)
}
}
- let tokens = null;
- if (text) {
- // Analyze
- tokens = parse(text);
-
- // Extract hashtags
- const hashtags = tokens
- .filter(t => t.type == 'hashtag')
- .map(t => t.hashtag);
-
- hashtags.forEach(tag => {
- if (tags.indexOf(tag) == -1) {
- tags.push(tag);
- }
- });
- }
-
- let atMentions = [];
-
- // If has text content
- if (text) {
- /*
- // Extract a hashtags
- const hashtags = tokens
- .filter(t => t.type == 'hashtag')
- .map(t => t.hashtag)
- // Drop dupulicates
- .filter((v, i, s) => s.indexOf(v) == i);
-
- // ハッシュタグをデータベースに登録
- registerHashtags(user, hashtags);
- */
- // Extract an '@' mentions
- atMentions = tokens
- .filter(t => t.type == 'mention')
- .map(renderAcct)
- // Drop dupulicates
- .filter((v, i, s) => s.indexOf(v) == i)
- // Fetch mentioned user
- // SELECT _id
- .map(mention => User.findOne(parseAcct(mention), { _id: true }));
- }
-
// 投稿を作成
- const post = await create({
+ const post = await create(user, {
createdAt: new Date(),
- channelId: channel ? channel._id : undefined,
- index: channel ? channel.index + 1 : undefined,
- mediaIds: files ? files.map(file => file._id) : [],
+ media: files,
poll: poll,
text: text,
- textHtml: tokens === null ? null : html(tokens),
+ reply,
+ repost,
cw: cw,
tags: tags,
- userId: user._id,
- appId: app ? app._id : null,
+ app: app,
viaMobile: viaMobile,
visibility,
geo
- }, reply, repost, await Promise.all(atMentions));
+ });
- const postObj = await distribute(user, post.mentions, post);
+ const postObj = await pack(post, user);
// Reponse
res({
createdPost: postObj
});
-
- // Register to search database
- if (post.text && config.elasticsearch.enable) {
- const es = require('../../../db/elasticsearch');
-
- es.index({
- index: 'misskey',
- type: 'post',
- id: post._id.toString(),
- body: {
- text: post.text
- }
- });
- }
});
diff --git a/src/server/api/endpoints/posts/reactions/create.ts b/src/server/api/endpoints/posts/reactions/create.ts
index f1b0c7dd29..71fa6a2955 100644
--- a/src/server/api/endpoints/posts/reactions/create.ts
+++ b/src/server/api/endpoints/posts/reactions/create.ts
@@ -3,20 +3,11 @@
*/
import $ from 'cafy';
import Reaction from '../../../../../models/post-reaction';
-import Post, { pack as packPost } from '../../../../../models/post';
-import { pack as packUser } from '../../../../../models/user';
-import Watching from '../../../../../models/post-watching';
-import watch from '../../../../../post/watch';
-import { publishPostStream } from '../../../../../publishers/stream';
-import notify from '../../../../../publishers/notify';
-import pushSw from '../../../../../publishers/push-sw';
+import Post from '../../../../../models/post';
+import create from '../../../../../services/post/reaction/create';
/**
* React to a post
- *
- * @param {any} params
- * @param {any} user
- * @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'postId' parameter
@@ -46,78 +37,11 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
return rej('post not found');
}
- // Myself
- if (post.userId.equals(user._id)) {
- return rej('cannot react to my post');
+ try {
+ await create(user, post, reaction);
+ } catch (e) {
+ rej(e);
}
- // if already reacted
- const exist = await Reaction.findOne({
- postId: post._id,
- userId: user._id,
- deletedAt: { $exists: false }
- });
-
- if (exist !== null) {
- return rej('already reacted');
- }
-
- // Create reaction
- await Reaction.insert({
- createdAt: new Date(),
- postId: post._id,
- userId: user._id,
- reaction: reaction
- });
-
- // Send response
res();
-
- const inc = {};
- inc[`reactionCounts.${reaction}`] = 1;
-
- // Increment reactions count
- await Post.update({ _id: post._id }, {
- $inc: inc
- });
-
- publishPostStream(post._id, 'reacted');
-
- // Notify
- notify(post.userId, user._id, 'reaction', {
- postId: post._id,
- reaction: reaction
- });
-
- pushSw(post.userId, 'reaction', {
- user: await packUser(user, post.userId),
- post: await packPost(post, post.userId),
- reaction: reaction
- });
-
- // Fetch watchers
- Watching
- .find({
- postId: post._id,
- userId: { $ne: user._id },
- // 削除されたドキュメントは除く
- deletedAt: { $exists: false }
- }, {
- fields: {
- userId: true
- }
- })
- .then(watchers => {
- watchers.forEach(watcher => {
- notify(watcher.userId, user._id, 'reaction', {
- postId: post._id,
- reaction: reaction
- });
- });
- });
-
- // この投稿をWatchする
- if (user.account.settings.autoWatch !== false) {
- watch(user._id, post);
- }
});