From e7415dd42bf656a24d70c49776ff7c84a1838f9e Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 30 Aug 2017 17:31:39 +0900 Subject: [PATCH] Implement #746 --- CHANGELOG.md | 4 + locales/en.yml | 4 + locales/ja.yml | 4 + src/api/endpoints.ts | 4 + src/api/endpoints/i/pin.ts | 44 ++++++++ src/api/serializers/user.ts | 41 ++++--- src/web/app/common/tags/index.js | 1 + src/web/app/common/tags/post-menu.tag | 134 +++++++++++++++++++++++ src/web/app/desktop/tags/post-detail.tag | 23 ++-- src/web/app/desktop/tags/timeline.tag | 21 ++-- src/web/app/mobile/tags/page/post.tag | 14 ++- src/web/app/mobile/tags/post-detail.tag | 54 +++++---- src/web/app/mobile/tags/timeline.tag | 23 +++- src/web/app/mobile/tags/user.tag | 4 + 14 files changed, 315 insertions(+), 60 deletions(-) create mode 100644 src/api/endpoints/i/pin.ts create mode 100644 src/web/app/common/tags/post-menu.tag diff --git a/CHANGELOG.md b/CHANGELOG.md index c64a8ea04..2669bfee7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ChangeLog (Release Notes) ========================= 主に notable な changes を書いていきます +unreleased +---------- +* New: 投稿のピン留め (#746) + 2508 (2017/08/30) ----------------- * New: モバイル版のユーザーページのアクティビティチャートを変更 diff --git a/locales/en.yml b/locales/en.yml index 29a6a764d..15d278c37 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -77,6 +77,10 @@ common: show-result: "Show result" voted: "Voted" + mk-post-menu: + pin: "Pin" + pinned: "Pinned" + mk-reaction-picker: choose-reaction: "Pick your reaction" diff --git a/locales/ja.yml b/locales/ja.yml index 21a07640d..7ed4262f1 100644 --- a/locales/ja.yml +++ b/locales/ja.yml @@ -77,6 +77,10 @@ common: show-result: "結果を見る" voted: "投票済み" + mk-post-menu: + pin: "ピン留め" + pinned: "ピン留めしました" + mk-reaction-picker: choose-reaction: "リアクションを選択" diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts index c6661533e..e5be68c09 100644 --- a/src/api/endpoints.ts +++ b/src/api/endpoints.ts @@ -167,6 +167,10 @@ const endpoints: Endpoint[] = [ name: 'i/regenerate_token', withCredential: true }, + { + name: 'i/pin', + kind: 'account-write' + }, { name: 'i/appdata/get', withCredential: true diff --git a/src/api/endpoints/i/pin.ts b/src/api/endpoints/i/pin.ts new file mode 100644 index 000000000..a94950d22 --- /dev/null +++ b/src/api/endpoints/i/pin.ts @@ -0,0 +1,44 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import User from '../../models/user'; +import Post from '../../models/post'; +import serialize from '../../serializers/user'; + +/** + * Pin post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = async (params, user) => new Promise(async (res, rej) => { + // Get 'post_id' parameter + const [postId, postIdErr] = $(params.post_id).id().$; + if (postIdErr) return rej('invalid post_id param'); + + // Fetch pinee + const post = await Post.findOne({ + _id: postId, + user_id: user._id + }); + + if (post === null) { + return rej('post not found'); + } + + await User.update(user._id, { + $set: { + pinned_post_id: post._id + } + }); + + // Serialize + const iObj = await serialize(user, user, { + detail: true + }); + + // Send response + res(iObj); +}); diff --git a/src/api/serializers/user.ts b/src/api/serializers/user.ts index bdbc74958..c9189d903 100644 --- a/src/api/serializers/user.ts +++ b/src/api/serializers/user.ts @@ -4,6 +4,7 @@ import * as mongo from 'mongodb'; import deepcopy = require('deepcopy'); import User from '../models/user'; +import serializePost from './post'; import Following from '../models/following'; import getFriends from '../common/get-friends'; import config from '../../conf'; @@ -116,24 +117,32 @@ export default ( _user.is_followed = follow2 !== null; } - if (me && !me.equals(_user.id) && opts.detail) { - const myFollowingIds = await getFriends(me); + if (opts.detail) { + if (_user.pinned_post_id) { + _user.pinned_post = await serializePost(_user.pinned_post_id, me, { + detail: true + }); + } - // Get following you know count - const followingYouKnowCount = await Following.count({ - followee_id: { $in: myFollowingIds }, - follower_id: _user.id, - deleted_at: { $exists: false } - }); - _user.following_you_know_count = followingYouKnowCount; + if (me && !me.equals(_user.id)) { + const myFollowingIds = await getFriends(me); - // Get followers you know count - const followersYouKnowCount = await Following.count({ - followee_id: _user.id, - follower_id: { $in: myFollowingIds }, - deleted_at: { $exists: false } - }); - _user.followers_you_know_count = followersYouKnowCount; + // Get following you know count + const followingYouKnowCount = await Following.count({ + followee_id: { $in: myFollowingIds }, + follower_id: _user.id, + deleted_at: { $exists: false } + }); + _user.following_you_know_count = followingYouKnowCount; + + // Get followers you know count + const followersYouKnowCount = await Following.count({ + followee_id: _user.id, + follower_id: { $in: myFollowingIds }, + deleted_at: { $exists: false } + }); + _user.followers_you_know_count = followersYouKnowCount; + } } resolve(_user); diff --git a/src/web/app/common/tags/index.js b/src/web/app/common/tags/index.js index dd6ba75d7..6e6081da9 100644 --- a/src/web/app/common/tags/index.js +++ b/src/web/app/common/tags/index.js @@ -28,3 +28,4 @@ require('./reaction-picker.tag'); require('./reactions-viewer.tag'); require('./reaction-icon.tag'); require('./weekly-activity-chart.tag'); +require('./post-menu.tag'); diff --git a/src/web/app/common/tags/post-menu.tag b/src/web/app/common/tags/post-menu.tag new file mode 100644 index 000000000..33895212b --- /dev/null +++ b/src/web/app/common/tags/post-menu.tag @@ -0,0 +1,134 @@ + +
+
+ +
+ + +
diff --git a/src/web/app/desktop/tags/post-detail.tag b/src/web/app/desktop/tags/post-detail.tag index 7a90dccf3..58343482d 100644 --- a/src/web/app/desktop/tags/post-detail.tag +++ b/src/web/app/desktop/tags/post-detail.tag @@ -43,16 +43,18 @@
- - - + -
@@ -315,6 +317,13 @@ }); }; + this.menu = () => { + riot.mount(document.body.appendChild(document.createElement('mk-post-menu')), { + source: this.refs.menuButton, + post: this.p + }); + }; + this.loadContext = () => { this.contextFetching = true; diff --git a/src/web/app/desktop/tags/timeline.tag b/src/web/app/desktop/tags/timeline.tag index bce27cd7f..cd7ac7d20 100644 --- a/src/web/app/desktop/tags/timeline.tag +++ b/src/web/app/desktop/tags/timeline.tag @@ -128,16 +128,16 @@
- - - - - - + -
-
+
@@ -64,19 +66,14 @@ :scope display block overflow hidden - margin 8px auto + margin 0 auto padding 0 - max-width 500px - width calc(100% - 16px) + width 100% text-align left background #fff border-radius 8px box-shadow 0 0 0 1px rgba(0, 0, 0, 0.2) - @media (min-width 500px) - margin 16px auto - width calc(100% - 32px) - > .fetching padding 64px 0 @@ -269,6 +266,7 @@ this.mixin('api'); + this.compact = this.opts.compact; this.post = this.opts.post; this.isRepost = this.post.repost != null; this.p = this.isRepost ? this.post.repost : this.post; @@ -299,14 +297,16 @@ } // Get replies - this.api('posts/replies', { - post_id: this.p.id, - limit: 8 - }).then(replies => { - this.update({ - replies: replies + if (!this.compact) { + this.api('posts/replies', { + post_id: this.p.id, + limit: 8 + }).then(replies => { + this.update({ + replies: replies + }); }); - }); + } }); this.reply = () => { @@ -332,6 +332,14 @@ }); }; + this.menu = () => { + riot.mount(document.body.appendChild(document.createElement('mk-post-menu')), { + source: this.refs.menuButton, + post: this.p, + compact: true + }); + }; + this.loadContext = () => { this.contextFetching = true; diff --git a/src/web/app/mobile/tags/timeline.tag b/src/web/app/mobile/tags/timeline.tag index 43470d197..d8df8b266 100644 --- a/src/web/app/mobile/tags/timeline.tag +++ b/src/web/app/mobile/tags/timeline.tag @@ -181,14 +181,17 @@
- - - +
@@ -558,6 +561,14 @@ compact: true }); }; + + this.menu = () => { + riot.mount(document.body.appendChild(document.createElement('mk-post-menu')), { + source: this.refs.menuButton, + post: this.p, + compact: true + }); + }; diff --git a/src/web/app/mobile/tags/user.tag b/src/web/app/mobile/tags/user.tag index d85e3b51f..0fe4055cf 100644 --- a/src/web/app/mobile/tags/user.tag +++ b/src/web/app/mobile/tags/user.tag @@ -215,6 +215,7 @@ +

%i18n:mobile.tags.mk-user-overview.recent-posts%

@@ -240,6 +241,9 @@ max-width 600px margin 0 auto + > mk-post-detail + margin 0 0 8px 0 + > section background #eee border-radius 8px