-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Follow Method and database updates #2
base: newsfeed
Are you sure you want to change the base?
Conversation
@@ -99,6 +99,19 @@ export const getActions = ({ user, directActions, hideAdminControls }) => { | |||
} | |||
}; | |||
|
|||
|
|||
const hasAlreadyFollowed = (username) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we name it / make a generic "canFollow" method? Which can be later extended to support other use cases (if any)?
let userObject = Users.findOneByUsername(username); | ||
if ('followers' in userObject) { | ||
const followersObject = userObject.followers; | ||
const { _id } = Users.findOneByUsername(Meteor.user().username); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can directly use Meteor.userId()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meteor.userId()
is the best approach here, but just for your knowledge you could have used Meteor.user()._id
since Meteor.user()
will return the entire user's object, so on that case you are requesting the entire object twice.
And, if you want just one field from a query, please always pass that information to the query to not transfer unnecessary data from db to rocket.chat:
Users.findOneByUsername(Meteor.user().username, {fields: {_id: 1}});
followersObject[_id] = ''; | ||
Users.update(userObject._id, { $set: { followers: followersObject } }); | ||
} else { | ||
const followersObject = new Object(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not validate at server startup and create this object there for every user to maintain consistency across all?
// Update following keys | ||
userObject = Users.findOneByUsername(Meteor.user().username); | ||
if ('following' in userObject) { | ||
const followingObject = userObject.following; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the number of Following/Followers increase, it would increase in size of each user object considerably. As the User
object is perhaps the most commonly fetched object for number of other purposes in all other areas of the application, we might want to structure our schema as described here. Thoughts? @bizzbyster @dtoth
Check this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kb0304 @fliptrail I am not an expert on the server side architecture but I tend to agree with Karan based on the link he sent. If you are still unsure we can try to get some inputs from others on the RC team. But in my opinion this seems like a good approach.
|
||
Meteor.methods({ | ||
followUser(username) { | ||
if (settings.get('Newsfeed_enabled')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, never put all your code inside an if, use the if first to stop the process:
if (!settings.get('Newsfeed_enabled')) {
return false;
}
It will make the code cleaner and more understandable.
const followersObject = userObject.followers; | ||
const { _id } = Users.findOneByUsername(Meteor.user().username); | ||
followersObject[_id] = ''; | ||
Users.update(userObject._id, { $set: { followers: followersObject } }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use a direct update?
Users.update(userObject._id, { $set: { `followers.${ _id }`: '' } });
let userObject = Users.findOneByUsername(username); | ||
if ('followers' in userObject) { | ||
const followersObject = userObject.followers; | ||
const { _id } = Users.findOneByUsername(Meteor.user().username); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meteor.userId()
is the best approach here, but just for your knowledge you could have used Meteor.user()._id
since Meteor.user()
will return the entire user's object, so on that case you are requesting the entire object twice.
And, if you want just one field from a query, please always pass that information to the query to not transfer unnecessary data from db to rocket.chat:
Users.findOneByUsername(Meteor.user().username, {fields: {_id: 1}});
// Update followers keys | ||
let userObject = Users.findOneByUsername(username); | ||
if ('followers' in userObject) { | ||
const followersObject = userObject.followers; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you could prevent the code duplication by deciding like:
const followersObject = userObject.followers || {};
No description provided.