summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-08-30 17:31:39 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-08-30 17:31:39 +0900
commite7415dd42bf656a24d70c49776ff7c84a1838f9e (patch)
tree517b326e16cdacda245661b2bfb15585a4bfa09d /src/api
parentMerge branch 'master' of https://github.com/syuilo/misskey (diff)
downloadsharkey-e7415dd42bf656a24d70c49776ff7c84a1838f9e.tar.gz
sharkey-e7415dd42bf656a24d70c49776ff7c84a1838f9e.tar.bz2
sharkey-e7415dd42bf656a24d70c49776ff7c84a1838f9e.zip
Implement #746
Diffstat (limited to 'src/api')
-rw-r--r--src/api/endpoints.ts4
-rw-r--r--src/api/endpoints/i/pin.ts44
-rw-r--r--src/api/serializers/user.ts41
3 files changed, 73 insertions, 16 deletions
diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts
index c6661533e8..e5be68c096 100644
--- a/src/api/endpoints.ts
+++ b/src/api/endpoints.ts
@@ -168,6 +168,10 @@ const endpoints: Endpoint[] = [
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 0000000000..a94950d22b
--- /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<any>}
+ */
+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 bdbc749589..c9189d9034 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);