properties.popularComments = Comments.find({postId: post._id}, {sort: {score: -1}, limit: 2, transform: function (comment) { // get comment author var user = Meteor.users.findOne(comment.userId); // add properties to comment comment.body = Telescope.utils.trimHTML(comment.htmlBody, 20); comment.authorProfileUrl = Users.getProfileUrl(user, true); comment.authorAvatarUrl = Avatar.getUrl(user); try { HTTP.get(comment.authorAvatarUrl); } catch (error) { comment.authorAvatarUrl = false; } return comment; }}).fetch();
postsArray.forEach(function (post, index) { // add post title to email subject (don't add a coma for the first post) if(index > 0) subject += ', '; subject += post.title; // get author of the current post var postUser = Meteor.users.findOne(post.userId); // the naked post object as stored in the database is missing a few properties, so let's add them var properties = _.extend(post, { authorName: post.getAuthorName(), postLink: Posts.getLink(post, true), profileUrl: Users.getProfileUrl(postUser, true), postPageLink: Posts.getPageUrl(post, true), date: moment(post.postedAt).format("MMMM D YYYY"), authorAvatarUrl: Avatar.getUrl(postUser) }); // if post author's avatar returns an error, remove it from properties object try { HTTP.get(post.authorAvatarUrl); } catch (error) { post.authorAvatarUrl = false; } // trim the body and remove any HTML tags if (post.body) { properties.body = Telescope.utils.trimHTML(post.htmlBody, 20); } // if post has comments if (post.commentCount > 0) { // get the two highest-scoring comments properties.popularComments = Comments.find({postId: post._id}, {sort: {score: -1}, limit: 2, transform: function (comment) { // get comment author var user = Meteor.users.findOne(comment.userId); // add properties to comment comment.body = Telescope.utils.trimHTML(comment.htmlBody, 20); comment.authorProfileUrl = Users.getProfileUrl(user, true); comment.authorAvatarUrl = Avatar.getUrl(user); try { HTTP.get(comment.authorAvatarUrl); } catch (error) { comment.authorAvatarUrl = false; } return comment; }}).fetch(); } // if post has an URL, at the link domain as a property if(post.url) { properties.domain = Telescope.utils.getDomain(post.url); } // if post has a thumbnail, add the thumbnail URL as a property if (properties.thumbnailUrl) { properties.thumbnailUrl = Telescope.utils.addHttp(properties.thumbnailUrl); } // generate post item HTML and add it to the postsHTML string postsHTML += Telescope.email.getTemplate('postItem')(properties); });