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

Reader: Use the new style post normalization #5643

Merged
merged 3 commits into from
Jun 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions client/lib/feed-post-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ var assign = require( 'lodash/assign' ),
*/
var Dispatcher = require( 'dispatcher' ),
emitter = require( 'lib/mixins/emitter' ),
normalizer = require( 'lib/post-normalizer' ),
normalizationRules = require( './normalization-rules' ),
{ runFastRules, runSlowRules } = require( './normalization-rules' ),
FeedPostActionType = require( './constants' ).action,
FeedStreamActionType = require( 'lib/feed-stream-store/constants' ).action,
ReaderSiteBlockActionType = require( 'lib/reader-site-blocks/constants' ).action,
Expand Down Expand Up @@ -263,18 +262,11 @@ function normalizePost( feedId, postId, post ) {
return;
}

normalizer( post, normalizationRules.fastRules, function( err, normalizedPost ) {
if ( ! err ) {
setPost( postId, normalizedPost );

process.nextTick( function() {
normalizer( normalizedPost, normalizationRules.slowRules, function( fullErr, fullyNormalizedPost ) {
if ( ! fullErr ) {
setPost( postId, fullyNormalizedPost );
}
} );
} );
}
const normalizedPost = runFastRules( post );
setPost( postId, normalizedPost );

process.nextTick( () => {
runSlowRules( normalizedPost ).then( setPost.bind( null, postId ) );
} );
}

Expand All @@ -291,7 +283,7 @@ function markPostSeen( post ) {
if ( post.site_ID ) {
// they have a site ID, let's try to push a page view
const site = SiteStore.get( post.site_ID );
const isNotAdmin = ! ( site && site.getIn( [ 'capabilities', 'manage_options' ], false ) )
const isNotAdmin = ! ( site && site.getIn( [ 'capabilities', 'manage_options' ], false ) );
if ( site && site.get( 'state' ) === SiteState.COMPLETE ) {
if ( site.get( 'is_private' ) || isNotAdmin ) {
stats.pageViewForPost( site.get( 'ID' ), site.get( 'URL' ), post.ID, site.get( 'is_private' ) );
Expand Down
116 changes: 72 additions & 44 deletions client/lib/feed-post-store/normalization-rules.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
/**
* External Dependencies
*/
const find = require( 'lodash/find' ),
forEach = require( 'lodash/forEach' ),
url = require( 'url' ),
matches = require( 'lodash/matches' ),
toArray = require( 'lodash/toArray' );
import find from 'lodash/find';
import flow from 'lodash/flow';
import forEach from 'lodash/forEach';
import url from 'url';
import matches from 'lodash/matches';

/**
* Internal Dependencies
*/
const postNormalizer = require( 'lib/post-normalizer' ),
resizeImageUrl = require( 'lib/resize-image-url' ),
DISPLAY_TYPES = require( './display-types' );
import resizeImageUrl from 'lib/resize-image-url';
import DISPLAY_TYPES from './display-types';

/**
* Rules
*/
import createBetterExcerpt from 'lib/post-normalizer/rule-create-better-excerpt';
import detectEmbeds from 'lib/post-normalizer/rule-content-detect-embeds';
import detectPolls from 'lib/post-normalizer/rule-content-detect-polls';
import makeEmbedsSecure from 'lib/post-normalizer/rule-content-make-embeds-secure';
import removeStyles from 'lib/post-normalizer/rule-content-remove-styles';
import safeImages from 'lib/post-normalizer/rule-content-safe-images';
import wordCount from 'lib/post-normalizer/rule-content-word-count';
import { disableAutoPlayOnMedia, disableAutoPlayOnEmbeds} from 'lib/post-normalizer/rule-content-disable-autoplay';
import decodeEntities from 'lib/post-normalizer/rule-decode-entities';
import firstPassCanonicalImage from 'lib/post-normalizer/rule-first-pass-canonical-image';
import makeSiteIdSafeForApi from 'lib/post-normalizer/rule-make-site-id-safe-for-api';
import pickPrimaryTag from 'lib/post-normalizer/rule-pick-primary-tag';
import preventWidows from 'lib/post-normalizer/rule-prevent-widows';
import safeImageProperties from 'lib/post-normalizer/rule-safe-image-properties';
import stripHtml from 'lib/post-normalizer/rule-strip-html';
import withContentDom from 'lib/post-normalizer/rule-with-content-dom';
import keepValidImages from 'lib/post-normalizer/rule-keep-valid-images';
import pickCanonicalImage from 'lib/post-normalizer/rule-pick-canonical-image';
import waitForImagesToLoad from 'lib/post-normalizer/rule-wait-for-images-to-load';

/**
* Module vars
Expand All @@ -23,35 +45,6 @@ const READER_CONTENT_WIDTH = 720,
ONE_LINER_THRESHOLD = ( 20 * 10 ), // roughly 10 lines of words
DISCOVER_BLOG_ID = 53424024;

const fastPostNormalizationRules = [
postNormalizer.decodeEntities,
postNormalizer.stripHTML,
postNormalizer.preventWidows,
postNormalizer.makeSiteIDSafeForAPI,
postNormalizer.pickPrimaryTag,
postNormalizer.safeImageProperties( READER_CONTENT_WIDTH ),
postNormalizer.firstPassCanonicalImage,
postNormalizer.withContentDOM( [
postNormalizer.content.removeStyles,
postNormalizer.content.safeContentImages( READER_CONTENT_WIDTH ),
discoverFullBleedImages,
postNormalizer.content.makeEmbedsSecure,
postNormalizer.content.disableAutoPlayOnEmbeds,
postNormalizer.content.disableAutoPlayOnMedia,
postNormalizer.content.detectEmbeds,
postNormalizer.content.detectPolls,
postNormalizer.content.wordCountAndReadingTime
] ),
postNormalizer.createBetterExcerpt,
classifyPost
],
slowPostNormalizationRules = [
postNormalizer.waitForImagesToLoad,
postNormalizer.keepValidImages( 144, 72 ),
postNormalizer.pickCanonicalImage,
classifyPost
];

function discoverFullBleedImages( post, dom ) {
if ( post.site_ID === DISCOVER_BLOG_ID ) {
const images = dom.querySelectorAll( '.fullbleed img, img.fullbleed' );
Expand All @@ -68,9 +61,9 @@ function discoverFullBleedImages( post, dom ) {
/**
* Attempt to classify the post into a display type
* @param {object} post A post to classify
* @param {Function} callback A callback to invoke when we're done
* @return {object} The classified post
*/
function classifyPost( post, callback ) {
function classifyPost( post ) {
var displayType = DISPLAY_TYPES.UNCLASSIFIED,
canonicalImage = post.canonical_image,
canonicalAspect;
Expand Down Expand Up @@ -133,10 +126,45 @@ function classifyPost( post, callback ) {

post.display_type = displayType;

callback();
return post;
}

const fastPostNormalizationRules = flow( [
decodeEntities,
stripHtml,
preventWidows,
makeSiteIdSafeForApi,
pickPrimaryTag,
safeImageProperties( READER_CONTENT_WIDTH ),
firstPassCanonicalImage,
withContentDom( [
removeStyles,
safeImages( READER_CONTENT_WIDTH ),
discoverFullBleedImages,
makeEmbedsSecure,
disableAutoPlayOnEmbeds,
disableAutoPlayOnMedia,
detectEmbeds,
detectPolls,
wordCount
] ),
createBetterExcerpt,
classifyPost
] );

export function runFastRules( post ) {
post = Object.assign( {}, post );
fastPostNormalizationRules( post );
return post;
}

module.exports = {
fastRules: fastPostNormalizationRules,
slowRules: slowPostNormalizationRules
};
const slowSyncRules = flow( [
keepValidImages( 144, 72 ),
pickCanonicalImage,
classifyPost
] );

export function runSlowRules( post ) {
post = Object.assign( {}, post );
return waitForImagesToLoad( post ).then( slowSyncRules );
}
2 changes: 1 addition & 1 deletion client/lib/feed-post-store/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe( 'feed-post-store', function() {
expect( FeedPostStore.get( {
feedId: 1,
postId: 2
} ).title ).to.equal( 'chris & ben' );
} ).title ).to.equal( 'chris &\xA0ben' );
} );

it( 'should index a post by the site_ID and ID if it is internal', function() {
Expand Down
5 changes: 2 additions & 3 deletions client/lib/post-normalizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* External Dependencies
*/
var async = require( 'async' ),
debug = require( 'debug' )( 'calypso:post-normalizer' ),
cloneDeep = require( 'lodash/cloneDeep' );
debug = require( 'debug' )( 'calypso:post-normalizer' );
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -32,7 +31,7 @@ function normalizePost( post, transforms, callback ) {
return;
}

let normalizedPost = cloneDeep( post ),
let normalizedPost = Object.assign( {}, post ),
postDebug = debugForPost( post );

postDebug( 'running transforms' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export default function firstPassCanonicalImage( post ) {
};
}
}
return post;
}