Skip to content
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

Open
wants to merge 13 commits into
base: newsfeed
Choose a base branch
from
Open

Follow Method and database updates #2

wants to merge 13 commits into from

Conversation

fliptrail
Copy link
Owner

No description provided.

app/newsfeed/server/deinitialize.js Show resolved Hide resolved
@@ -99,6 +99,19 @@ export const getActions = ({ user, directActions, hideAdminControls }) => {
}
};


const hasAlreadyFollowed = (username) => {
Copy link
Collaborator

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);
Copy link
Collaborator

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()

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();
Copy link
Collaborator

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;
Copy link
Collaborator

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

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')) {

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 } });

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);

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;

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 || {};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants