Created
March 18, 2015 21:22
-
-
Save zxqx/2a1662ab97a97bc77b11 to your computer and use it in GitHub Desktop.
ContentStore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import extend from 'extend'; | |
import urlUtil from 'url'; | |
import _ from 'underscore'; | |
import debug from 'debug'; | |
// Models | |
import PostList from '../models/PostList.js'; | |
import InfluencerList from '../models/InfluencerList.js'; | |
import ContentList from '../models/ContentList.js'; | |
// Configs | |
import {BASE_URL} from './content.config.js'; | |
import {ORIGINAL_CONTENT_PARAMS} from './content.config.js'; | |
import {SHARED_CONTENT_PARAMS} from './content.config.js'; | |
import {REBROADCAST_CONTENT_PARAMS} from './content.config.js'; | |
import {TOP_POST_PARAMS} from './content.config.js'; | |
import {TOP_INFLUENCERS_PARAMS} from './content.config.js'; | |
import {CATEGORIES} from './content.config.js'; | |
export default class ContentStore | |
{ | |
/** | |
* Fetch application content via transport layer | |
* Set up content as domain model objects | |
* @param {AppConfig} appConfig | |
* @param {HttpTransport} transport | |
*/ | |
constructor(appConfig, transport) | |
{ | |
this.appConfig = appConfig; | |
this.transport = transport; | |
this.baseUrl = _getUrl(BASE_URL, appConfig.community); | |
} | |
/** | |
* Get top post data and instantiate PostList model | |
* @return {Promise<array<object>>} | |
*/ | |
async getTopPost() | |
{ | |
debug('getting top post'); | |
var communityParams = this.appConfig.communityParams; | |
var params = extend({}, TOP_POST_PARAMS, communityParams); | |
var postList = await this.transport.get(this.baseUrl, params); | |
debug('got top post'); | |
return new PostList(postList.body.data); | |
} | |
/** | |
* Get top influencers data and instantiate InfluencerList model | |
* @return {Promise<array<object>>} | |
*/ | |
async getTopInfluencers() | |
{ | |
debug('getting top influencers'); | |
var communityParams = this.appConfig.communityParams; | |
var params = extend({}, TOP_INFLUENCERS_PARAMS, communityParams); | |
var influencerList = await this.transport.get(this.baseUrl, params); | |
debug('got top influencers'); | |
// Filter by unique influencers | |
var influencers = _.uniq(influencerList.body.data, x => x.actor.id); | |
// Ensure list is 10 at max | |
influencers = influencers.slice(0, 10); | |
return new InfluencerList(influencers); | |
} | |
/** | |
* Get top streaming content given a category | |
* @param {string} category | |
* @return {Promise<array<object>>} | |
*/ | |
async getTopStreamingContent(category) | |
{ | |
if (category === CATEGORIES.ORIGINAL_CONTENT) { | |
return await this.getTopStreamingOriginalContent(); | |
} | |
if (category === CATEGORIES.SHARED_CONTENT) { | |
return await this.getTopStreamingSharedContent(); | |
} | |
if (category === CATEGORIES.REBROADCAST_CONTENT) { | |
return await this.getTopStreamingRebroadcastContent(); | |
} | |
} | |
/** | |
* Get top original content data and instantiate ContentList collection | |
* @return {Promise<array<object>>} | |
*/ | |
async getTopStreamingOriginalContent() | |
{ | |
debug('getting top streaming original content'); | |
var communityParams = this.appConfig.communityParams; | |
var params = extend({}, ORIGINAL_CONTENT_PARAMS, communityParams); | |
var originalContentList = await this.transport.get(this.baseUrl, params); | |
debug('got top streaming original content'); | |
var originalContent = originalContentList.body.data.sort((a, b) => { | |
return (b.extensions.rebroadcast_count + b.extensions.like_count) - (a.extensions.rebroadcast_count + a.extensions.like_count); | |
}); | |
return new ContentList(originalContent, CATEGORIES.ORIGINAL_CONTENT); | |
} | |
/** | |
* Get top shared content data and instantiate ContentList collection | |
* @return {Promise} | |
*/ | |
async getTopStreamingSharedContent() | |
{ | |
debug('getting top streaming shared content'); | |
var communityParams = this.appConfig.communityParams; | |
var params = extend({}, SHARED_CONTENT_PARAMS, communityParams); | |
var sharedContentList = await this.transport.get(this.baseUrl, params); | |
debug('got top streaming shared content'); | |
var sharedContent = sharedContentList.body.data.sort((a, b) => { | |
return b.extensions.rebroadcasts.count - a.extensions.rebroadcasts.count; | |
}); | |
return new ContentList(sharedContent, CATEGORIES.SHARED_CONTENT); | |
} | |
/** | |
* Get top rebroadcast content data and instantiate ContentList collection | |
* @return {Promise} | |
*/ | |
async getTopStreamingRebroadcastContent() | |
{ | |
debug('getting top streaming rebroadcast content'); | |
var communityParams = this.appConfig.communityParams; | |
var params = extend({}, REBROADCAST_CONTENT_PARAMS, communityParams); | |
var rebroadcastContentList = await this.transport.get(this.baseUrl, params); | |
debug('got top streaming rebroadcast content'); | |
var rebroadcastContent = rebroadcastContentList.body.data.sort((a, b) => { | |
return b.extensions.rebroadcast_count - a.extensions.rebroadcast_count; | |
}); | |
return new ContentList(rebroadcastContent, CATEGORIES.REBROADCAST_CONTENT); | |
} | |
} | |
/** | |
* @private | |
* @param {string} baseUrl | |
* @param {string} community | |
*/ | |
function _getUrl(baseUrl, community) | |
{ | |
return baseUrl + '/' + community + '/sample'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment