From 47269a1782d56ae75d4cf210ad7eac1e3fb94a64 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 25 Mar 2017 15:56:26 +0900 Subject: [PATCH] [API] Check post dupulication --- package.json | 2 ++ src/api/endpoints/posts/create.ts | 21 +++++++++++++++++++++ src/api/serializers/user.ts | 3 +++ 3 files changed, 26 insertions(+) diff --git a/package.json b/package.json index a4283c15b..8948ce0bf 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "@types/compression": "0.0.33", "@types/cors": "2.8.1", "@types/debug": "0.0.29", + "@types/deep-equal": "1.0.0", "@types/elasticsearch": "5.0.13", "@types/escape-html": "0.0.19", "@types/event-stream": "3.3.31", @@ -78,6 +79,7 @@ "crypto": "0.0.3", "css-loader": "0.27.3", "debug": "2.6.3", + "deep-equal": "1.0.1", "deepcopy": "0.6.3", "download": "5.0.3", "elasticsearch": "12.1.3", diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts index 60b359268..9ece143fa 100644 --- a/src/api/endpoints/posts/create.ts +++ b/src/api/endpoints/posts/create.ts @@ -2,6 +2,7 @@ * Module dependencies */ import $ from 'cafy'; +import deepEqual = require('deep-equal'); import parse from '../../common/text'; import Post from '../../models/post'; import { isValidText } from '../../models/post'; @@ -142,6 +143,20 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { return rej('text, media_ids, repost_id or poll is required'); } + // 直近の投稿と重複してたらエラー + // TODO: 直近の投稿が一日前くらいなら重複とは見なさない + if (user.latest_post) { + if (deepEqual({ + text: user.latest_post.text, + media_ids: (user.latest_post.media_ids || []).map(id => id.toString()) + }, { + text: text, + media_ids: (files || []).map(file => file._id.toString()) + })) { + return rej('duplicate'); + } + } + // 投稿を作成 const post = await Post.insert({ created_at: new Date(), @@ -163,6 +178,12 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { //-------------------------------- // Post processes + User.update({ _id: user._id }, { + $set: { + latest_post: post + } + }); + let mentions = []; function addMention(mentionee, type) { diff --git a/src/api/serializers/user.ts b/src/api/serializers/user.ts index d367dc865..bdbc74958 100644 --- a/src/api/serializers/user.ts +++ b/src/api/serializers/user.ts @@ -65,6 +65,9 @@ export default ( _user.id = _user._id; delete _user._id; + // Remove needless properties + delete _user.lates_post; + // Remove private properties delete _user.password; delete _user.token;