misskey/src/api/serializers/user.ts

171 lines
3.6 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
2017-01-03 06:03:19 +09:00
import deepcopy = require('deepcopy');
2017-09-08 17:24:11 +09:00
import { default as User, IUser } from '../models/user';
2017-08-30 17:31:39 +09:00
import serializePost from './post';
2016-12-29 07:49:51 +09:00
import Following from '../models/following';
import getFriends from '../common/get-friends';
2017-01-17 09:17:52 +09:00
import config from '../../conf';
import rap from '@prezzemolo/rap';
2016-12-29 07:49:51 +09:00
/**
* Serialize a user
*
2017-09-08 17:24:11 +09:00
* @param user target
* @param me? serializee
* @param options? serialize options
* @return response
2016-12-29 07:49:51 +09:00
*/
export default (
2017-09-08 17:24:11 +09:00
user: string | mongo.ObjectID | IUser,
me?: string | mongo.ObjectID | IUser,
2016-12-29 07:49:51 +09:00
options?: {
2017-02-28 23:56:27 +09:00
detail?: boolean,
includeSecrets?: boolean
2016-12-29 07:49:51 +09:00
}
) => new Promise<any>(async (resolve, reject) => {
const opts = Object.assign({
detail: false,
includeSecrets: false
}, options);
let _user: any;
2017-02-22 13:08:33 +09:00
const fields = opts.detail ? {
data: false
} : {
data: false,
2017-09-09 04:39:23 +09:00
profile: false,
keywords: false,
domains: false
2017-02-22 13:08:33 +09:00
};
2016-12-29 07:49:51 +09:00
// Populate the user if 'user' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(user)) {
_user = await User.findOne({
_id: user
2017-02-22 13:08:33 +09:00
}, { fields });
2016-12-29 07:49:51 +09:00
} else if (typeof user === 'string') {
_user = await User.findOne({
_id: new mongo.ObjectID(user)
2017-02-22 13:08:33 +09:00
}, { fields });
2016-12-29 07:49:51 +09:00
} else {
_user = deepcopy(user);
}
// Me
2017-09-08 17:24:11 +09:00
const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me)
? me as mongo.ObjectID
: typeof me === 'string'
? new mongo.ObjectID(me)
: (me as IUser)._id
: null;
2016-12-29 07:49:51 +09:00
// Rename _id to id
_user.id = _user._id;
delete _user._id;
2017-03-25 15:56:26 +09:00
// Remove needless properties
delete _user.lates_post;
2016-12-29 07:49:51 +09:00
// Remove private properties
delete _user.password;
delete _user.token;
delete _user.username_lower;
2017-01-21 15:31:16 +09:00
if (_user.twitter) {
2017-01-21 17:25:12 +09:00
delete _user.twitter.access_token;
delete _user.twitter.access_token_secret;
2017-01-21 15:31:16 +09:00
}
2017-10-07 03:36:46 +09:00
delete _user.line;
2016-12-29 07:49:51 +09:00
// Visible via only the official client
if (!opts.includeSecrets) {
delete _user.data;
delete _user.email;
}
_user.avatar_url = _user.avatar_id != null
? `${config.drive_url}/${_user.avatar_id}`
: `${config.drive_url}/default-avatar.jpg`;
_user.banner_url = _user.banner_id != null
? `${config.drive_url}/${_user.banner_id}`
: null;
2017-09-08 17:24:11 +09:00
if (!meId || !meId.equals(_user.id) || !opts.detail) {
2016-12-29 07:49:51 +09:00
delete _user.avatar_id;
delete _user.banner_id;
delete _user.drive_capacity;
}
2017-09-08 17:24:11 +09:00
if (meId && !meId.equals(_user.id)) {
2016-12-29 07:49:51 +09:00
// If the user is following
_user.is_following = (async () => {
const follow = await Following.findOne({
follower_id: meId,
followee_id: _user.id,
deleted_at: { $exists: false }
});
return follow !== null;
})()
2016-12-29 07:49:51 +09:00
// If the user is followed
_user.is_followed = (async () => {
const follow2 = await Following.findOne({
follower_id: _user.id,
followee_id: meId,
deleted_at: { $exists: false }
});
return follow2 !== null;
})()
2016-12-29 07:49:51 +09:00
}
2017-08-30 17:31:39 +09:00
if (opts.detail) {
if (_user.pinned_post_id) {
2017-09-09 04:39:23 +09:00
// Populate pinned post
_user.pinned_post = serializePost(_user.pinned_post_id, meId, {
2017-08-30 17:31:39 +09:00
detail: true
});
}
2016-12-29 07:49:51 +09:00
2017-09-08 17:24:11 +09:00
if (meId && !meId.equals(_user.id)) {
const myFollowingIds = await getFriends(meId);
2017-08-30 17:31:39 +09:00
// Get following you know count
_user.following_you_know_count = Following.count({
2017-08-30 17:31:39 +09:00
followee_id: { $in: myFollowingIds },
follower_id: _user.id,
deleted_at: { $exists: false }
});
// Get followers you know count
_user.followers_you_know_count = Following.count({
2017-08-30 17:31:39 +09:00
followee_id: _user.id,
follower_id: { $in: myFollowingIds },
deleted_at: { $exists: false }
});
}
2016-12-29 07:49:51 +09:00
}
// resolve promises in _user object
_user = await rap(_user)
2016-12-29 07:49:51 +09:00
resolve(_user);
});
/*
function img(url) {
return {
thumbnail: {
large: `${url}`,
medium: '',
small: ''
}
};
}
*/