[Server] Some performance improvements

This commit is contained in:
syuilo 2017-02-22 13:08:33 +09:00
parent e52965a923
commit 1f88c4d40c
14 changed files with 74 additions and 8 deletions

View File

@ -25,6 +25,10 @@ module.exports = (params) =>
// Lookup user // Lookup user
const user = await User.findOne({ const user = await User.findOne({
_id: new mongo.ObjectID(userId) _id: new mongo.ObjectID(userId)
}, {
fields: {
_id: true
}
}); });
if (user === null) { if (user === null) {

View File

@ -25,6 +25,10 @@ module.exports = (params) =>
// Lookup user // Lookup user
const user = await User.findOne({ const user = await User.findOne({
_id: new mongo.ObjectID(userId) _id: new mongo.ObjectID(userId)
}, {
fields: {
_id: true
}
}); });
if (user === null) { if (user === null) {

View File

@ -25,6 +25,10 @@ module.exports = (params) =>
// Lookup user // Lookup user
const user = await User.findOne({ const user = await User.findOne({
_id: new mongo.ObjectID(userId) _id: new mongo.ObjectID(userId)
}, {
fields: {
_id: true
}
}); });
if (user === null) { if (user === null) {

View File

@ -25,6 +25,10 @@ module.exports = (params) =>
// Lookup user // Lookup user
const user = await User.findOne({ const user = await User.findOne({
_id: new mongo.ObjectID(userId) _id: new mongo.ObjectID(userId)
}, {
fields: {
_id: true
}
}); });
if (user === null) { if (user === null) {

View File

@ -41,6 +41,11 @@ module.exports = (params, user) =>
// Get followee // Get followee
const followee = await User.findOne({ const followee = await User.findOne({
_id: new mongo.ObjectID(userId) _id: new mongo.ObjectID(userId)
}, {
fields: {
data: false,
profile: false
}
}); });
if (followee === null) { if (followee === null) {

View File

@ -40,6 +40,11 @@ module.exports = (params, user) =>
// Get followee // Get followee
const followee = await User.findOne({ const followee = await User.findOne({
_id: new mongo.ObjectID(userId) _id: new mongo.ObjectID(userId)
}, {
fields: {
data: false,
profile: false
}
}); });
if (followee === null) { if (followee === null) {

View File

@ -25,6 +25,10 @@ module.exports = (params, user) =>
if (recipient !== undefined && recipient !== null) { if (recipient !== undefined && recipient !== null) {
recipient = await User.findOne({ recipient = await User.findOne({
_id: new mongo.ObjectID(recipient) _id: new mongo.ObjectID(recipient)
}, {
fields: {
_id: true
}
}); });
if (recipient === null) { if (recipient === null) {

View File

@ -33,6 +33,10 @@ module.exports = (params, user) =>
if (recipient !== undefined && recipient !== null) { if (recipient !== undefined && recipient !== null) {
recipient = await User.findOne({ recipient = await User.findOne({
_id: new mongo.ObjectID(recipient) _id: new mongo.ObjectID(recipient)
}, {
fields: {
_id: true
}
}); });
if (recipient === null) { if (recipient === null) {

View File

@ -47,6 +47,10 @@ module.exports = (params, me) =>
// Lookup user // Lookup user
const user = await User.findOne({ const user = await User.findOne({
_id: new mongo.ObjectID(userId) _id: new mongo.ObjectID(userId)
}, {
fields: {
_id: true
}
}); });
if (user === null) { if (user === null) {

View File

@ -47,6 +47,10 @@ module.exports = (params, me) =>
// Lookup user // Lookup user
const user = await User.findOne({ const user = await User.findOne({
_id: new mongo.ObjectID(userId) _id: new mongo.ObjectID(userId)
}, {
fields: {
_id: true
}
}); });
if (user === null) { if (user === null) {

View File

@ -67,10 +67,16 @@ module.exports = (params, me) =>
return rej('cannot set since_id and max_id'); return rej('cannot set since_id and max_id');
} }
const q = userId != null
? { _id: new mongo.ObjectID(userId) }
: { username_lower: username.toLowerCase() } ;
// Lookup user // Lookup user
const user = userId !== null const user = await User.findOne(q, {
? await User.findOne({ _id: new mongo.ObjectID(userId) }) fields: {
: await User.findOne({ username_lower: username.toLowerCase() }); _id: true
}
});
if (user === null) { if (user === null) {
return rej('user not found'); return rej('user not found');

View File

@ -38,10 +38,16 @@ module.exports = (params, me) =>
return rej('incorrect user_id'); return rej('incorrect user_id');
} }
const q = userId != null
? { _id: new mongo.ObjectID(userId) }
: { username_lower: username.toLowerCase() } ;
// Lookup user // Lookup user
const user = userId !== null const user = await User.findOne(q, {
? await User.findOne({ _id: new mongo.ObjectID(userId) }) fields: {
: await User.findOne({ username_lower: username.toLowerCase() }); data: false
}
});
if (user === null) { if (user === null) {
return rej('user not found'); return rej('user not found');

View File

@ -15,6 +15,11 @@ export default async (req: express.Request, res: express.Response) => {
// Fetch user // Fetch user
const user = await User.findOne({ const user = await User.findOne({
username_lower: username.toLowerCase() username_lower: username.toLowerCase()
}, {
fields: {
data: false,
profile: false
}
}); });
if (user === null) { if (user === null) {

View File

@ -34,15 +34,22 @@ export default (
let _user: any; let _user: any;
const fields = opts.detail ? {
data: false
} : {
data: false,
profile: false
};
// Populate the user if 'user' is ID // Populate the user if 'user' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(user)) { if (mongo.ObjectID.prototype.isPrototypeOf(user)) {
_user = await User.findOne({ _user = await User.findOne({
_id: user _id: user
}); }, { fields });
} else if (typeof user === 'string') { } else if (typeof user === 'string') {
_user = await User.findOne({ _user = await User.findOne({
_id: new mongo.ObjectID(user) _id: new mongo.ObjectID(user)
}); }, { fields });
} else { } else {
_user = deepcopy(user); _user = deepcopy(user);
} }