This commit is contained in:
Belen Curcio
2016-11-29 13:57:14 -03:00
28 changed files with 228 additions and 185 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ const session_opts = {
},
store: new RedisStore({
ttl: 1800,
client: redis,
client: redis.createClient(),
})
};
+5 -3
View File
@@ -1,6 +1,8 @@
const redis = require('./redis');
const cache = module.exports = {};
const cache = module.exports = {
client: redis.createClient()
};
/**
* This collects a key that may either be an array or a string and creates a
@@ -51,7 +53,7 @@ cache.wrap = (key, expiry, work) => {
* @return {Promise}
*/
cache.get = (key) => new Promise((resolve, reject) => {
redis.get(keyfunc(key), (err, reply) => {
cache.client.get(keyfunc(key), (err, reply) => {
if (err) {
return reject(err);
}
@@ -87,7 +89,7 @@ cache.set = (key, value, expiry) => new Promise((resolve, reject) => {
// Serialize the value as JSON.
let reply = JSON.stringify(value);
redis.set(keyfunc(key), reply, 'EX', expiry, (err) => {
cache.client.set(keyfunc(key), reply, 'EX', expiry, (err) => {
if (err) {
return reject(err);
}
+15 -1
View File
@@ -1,5 +1,5 @@
import * as actions from '../constants/auth';
import {base, handleResp, getInit} from '../helpers/response';
import {base, handleResp, getInit} from '../../../coral-framework/helpers/response';
// Check Login
@@ -17,3 +17,17 @@ export const checkLogin = () => dispatch => {
})
.catch(error => dispatch(checkLoginFailure(error)));
};
// LogOut Actions
const logOutRequest = () => ({type: actions.LOGOUT_REQUEST});
const logOutSuccess = () => ({type: actions.LOGOUT_SUCCESS});
const logOutFailure = () => ({type: actions.LOGOUT_FAILURE});
export const logout = () => dispatch => {
dispatch(logOutRequest());
fetch(`${base}/auth`, getInit('DELETE'))
.then(handleResp)
.then(() => dispatch(logOutSuccess()))
.catch(error => dispatch(logOutFailure(error)));
};
+1 -1
View File
@@ -9,7 +9,7 @@ import {
SET_ROLE
} from '../constants/community';
import {base, getInit, handleResp} from '../helpers/response';
import {base, getInit, handleResp} from '../../../coral-framework/helpers/response';
export const fetchCommenters = (query = {}) => dispatch => {
dispatch(requestFetchCommenters());
+1 -1
View File
@@ -1,4 +1,4 @@
import {base, handleResp, getInit} from '../helpers/response';
import {base, handleResp, getInit} from '../../../coral-framework/helpers/response';
export const SETTINGS_LOADING = 'SETTINGS_LOADING';
export const SETTINGS_RECEIVED = 'SETTINGS_RECEIVED';
@@ -0,0 +1,12 @@
.layout {
max-width: 800px;
margin: 0 auto;
}
.layout h1 {
font-size: 40px;
}
.layout img {
width: 100%;
}
@@ -0,0 +1,13 @@
import React from 'react';
import {Layout} from 'react-mdl';
import styles from './FullLoading.css';
import {CoralLogo} from 'coral-ui';
export const FullLoading = () => (
<Layout fixedDrawer>
<div className={styles.layout} >
<h1>Loading</h1>
<CoralLogo />
</div>
</Layout>
);
@@ -1,6 +1,5 @@
.header {
background: #505050;
overflow: hidden;
}
.header > div {
@@ -14,8 +13,35 @@
background: #232323;
}
.version {
.rightPanel {
position: absolute;
right: 0;
width: 50px;
width: 170px;
}
.rightPanel ul {
list-style: none;
line-height: 38px;
}
.rightPanel li {
display: inline-block;
float: right;
margin-left: 15px;
}
.rightPanel .settings {
vertical-align: middle;
border-radius: 3px;
border: solid 1px #9e9e9e;
line-height: 10px;
}
.rightPanel .settings > div {
position: relative;
}
.rightPanel .settings:hover {
background: rgba(158, 158, 158, 0.69);
cursor: pointer;
}
+22 -7
View File
@@ -1,21 +1,36 @@
import React from 'react';
import {Navigation, Header} from 'react-mdl';
import {Navigation, Header, IconButton, MenuItem, Menu} from 'react-mdl';
import {Link, IndexLink} from 'react-router';
import styles from './Header.css';
import I18n from 'coral-framework/modules/i18n/i18n';
import translations from '../../translations.json';
import {Logo} from './Logo';
export default () => (
export default ({handleLogout}) => (
<Header className={styles.header}>
<Logo />
<Navigation>
<IndexLink className={styles.navLink} to="/admin" activeClassName={styles.active}>{lang.t('configure.moderate')}</IndexLink>
<Link className={styles.navLink} to="/admin/community" activeClassName={styles.active}>{lang.t('configure.community')}</Link>
<Link className={styles.navLink} to="/admin/configure" activeClassName={styles.active}>{lang.t('configure.configure')}</Link>
<IndexLink className={styles.navLink} to="/admin"
activeClassName={styles.active}>{lang.t('configure.moderate')}</IndexLink>
<Link className={styles.navLink} to="/admin/community"
activeClassName={styles.active}>{lang.t('configure.community')}</Link>
<Link className={styles.navLink} to="/admin/configure"
activeClassName={styles.active}>{lang.t('configure.configure')}</Link>
</Navigation>
<div className={styles.version}>
{`v${process.env.VERSION}`}
<div className={styles.rightPanel}>
<ul>
<li className={styles.settings}>
<div>
<IconButton name="settings" id="menu-settings"/>
<Menu target="menu-settings" align="right">
<MenuItem onClick={handleLogout}>Sign Out</MenuItem>
</Menu>
</div>
</li>
<li>
{`v${process.env.VERSION}`}
</li>
</ul>
</div>
</Header>
);
@@ -4,9 +4,9 @@ import Header from './Header';
import Drawer from './Drawer';
import styles from './Layout.css';
export const Layout = ({children}) => (
export const Layout = ({children, ...props}) => (
<LayoutMDL fixedDrawer>
<Header />
<Header {...props}/>
<Drawer />
<div className={styles.layout} >
{children}
@@ -1,7 +1,9 @@
.logo h1 {
color: #272727;
font-size: 20px;
padding: 0 30px;
margin: 0;
line-height: 60px;
padding: 0 20px;
}
.logo span {
@@ -13,6 +15,7 @@
.logo {
background: #E5E5E5;
height: 100%;
}
@@ -1,37 +1,31 @@
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {Layout} from '../components/ui/Layout';
import {checkLogin} from '../actions/auth';
import {NotFound} from '../components/NotFound';
import {checkLogin, logout} from '../actions/auth';
import {FullLoading} from '../components/FullLoading';
import {PermissionRequired} from '../components/PermissionRequired';
class LayoutContainer extends Component {
componentWillMount () {
this.props.checkLogin();
const {checkLogin} = this.props;
checkLogin();
}
render () {
const {isAdmin, loggedIn} = this.props.auth;
if (!loggedIn) {
return <NotFound />;
}
if (!isAdmin && loggedIn) {
return <PermissionRequired />;
}
return <Layout {...this.props} />;
const {isAdmin, loggedIn, loadingUser} = this.props.auth;
if (loadingUser) { return <FullLoading />; }
if (!isAdmin) { return <PermissionRequired />; }
if (isAdmin && loggedIn) { return <Layout {...this.props} />; }
return <FullLoading />;
}
}
LayoutContainer.propTypes = {};
const mapStateToProps = state => ({
auth: state.auth.toJS()
});
const mapDispatchToProps = dispatch => ({
checkLogin: () => dispatch(checkLogin()),
handleLogout: () => dispatch(logout())
});
export default connect(
+7 -1
View File
@@ -9,19 +9,25 @@ const initialState = Map({
export default function auth (state = initialState, action) {
switch (action.type) {
case actions.CHECK_LOGIN_REQUEST:
return state
.set('loadingUser', true);
case actions.CHECK_LOGIN_FAILURE:
return state
.set('loggedIn', false)
.set('loadingUser', false)
.set('user', null);
case actions.CHECK_LOGIN_SUCCESS:
return state
.set('loggedIn', true)
.set('loadingUser', false)
.set('isAdmin', action.isAdmin)
.set('user', action.user);
case actions.LOGOUT_SUCCESS:
return state
.set('loggedIn', false)
.set('user', null);
.set('user', null)
.set('isAdmin', false);
default :
return state;
}
@@ -1,4 +1,4 @@
import {base, handleResp, getInit} from '../helpers/response';
import {base, handleResp, getInit} from '../../../coral-framework/helpers/response';
/**
* The adapter is a redux middleware that interecepts the actions that need
+1 -3
View File
@@ -2,9 +2,7 @@ import React from 'react';
import {render} from 'react-dom';
import CommentStream from './CommentStream';
import {Provider} from 'react-redux';
import {fetchConfig, store} from '../../coral-framework';
store.dispatch(fetchConfig());
import {store} from '../../coral-framework';
render(
<Provider store={store}>
-35
View File
@@ -1,35 +0,0 @@
import {fromJS} from 'immutable';
/**
* Action name constants
*/
export const FETCH_CONFIG_REQUEST = 'FETCH_CONFIG_REQUEST';
export const FETCH_CONFIG_FAILED = 'FETCH_CONFIG_FAILED';
export const FETCH_CONFIG_SUCCESS = 'FETCH_CONFIG_SUCCESS';
/**
* Action creators
*/
export function fetchConfig () {
return (dispatch) => {
dispatch({type: FETCH_CONFIG_REQUEST});
return fetch('/api/v1/settings')
.then(
response => {
return response.ok ? response.json()
: Promise.reject(`${response.status} ${response.statusText}`);
}
)
.then((json) => {
return dispatch({type: FETCH_CONFIG_SUCCESS, config: fromJS(json)});
})
.catch((error) => {
dispatch({type: FETCH_CONFIG_FAILED, error});
});
};
}
+28 -30
View File
@@ -1,3 +1,5 @@
import {getInit, base, handleResp} from '../../coral-framework/helpers/response';
import {fromJS} from 'immutable';
/* Item Actions */
/**
@@ -6,28 +8,9 @@
export const ADD_ITEM = 'ADD_ITEM';
export const UPDATE_ITEM = 'UPDATE_ITEM';
export const UPDATE_SETTINGS = 'UPDATE_SETTINGS';
export const APPEND_ITEM_ARRAY = 'APPEND_ITEM_ARRAY';
const getInit = (method, body) => {
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
const init = {method, headers};
if (body) {
init.body = JSON.stringify(body);
}
return init;
};
const responseHandler = response => {
if (response.status === 204) {
return;
}
return response.ok ? response.json() : Promise.reject(`${response.status} ${response.statusText}`);
};
/**
* Action creators
*/
@@ -61,6 +44,7 @@ export const addItem = (item, item_type) => {
* id - the id of the item to be posted
* property - the property to be updated
* value - the value that the property should be set to
* item_type - the type of the item being updated (users, comments, etc)
*
*/
export const updateItem = (id, property, value, item_type) => {
@@ -73,6 +57,18 @@ export const updateItem = (id, property, value, item_type) => {
};
};
/*
* Appends data to an array in an item in the local store without posting it to the server
* Useful for adding a recently posted reply to a comment, etc.
*
* @params
* id - the id of the item to be posted
* property - the property to be updated (should be an array)
* value - the value that should be added to the array
* add_to_front - boolean that defines whether value is added at the beginning (unshift) or end (push)
* item_type - the type of the item being updated (users, comments, etc)
*
*/
export const appendItemArray = (id, property, value, add_to_front, item_type) => {
return {
type: APPEND_ITEM_ARRAY,
@@ -99,8 +95,8 @@ export const appendItemArray = (id, property, value, add_to_front, item_type) =>
*/
export function getStream (assetUrl) {
return (dispatch) => {
return fetch(`/api/v1/stream?asset_url=${encodeURIComponent(assetUrl)}`)
.then(responseHandler)
return fetch(`${base}/stream?asset_url=${encodeURIComponent(assetUrl)}`)
.then(handleResp)
.then((json) => {
/* Add items to the store */
@@ -110,6 +106,8 @@ export function getStream (assetUrl) {
action.id = `${action.action_type}_${action.item_id}`;
dispatch(addItem(action, 'actions'));
});
} else if (type === 'settings') {
dispatch({type: UPDATE_SETTINGS, config: fromJS(json[type])});
} else {
json[type].forEach(item => {
dispatch(addItem(item, type));
@@ -168,8 +166,8 @@ export function getStream (assetUrl) {
export function getItemsArray (ids) {
return (dispatch) => {
return fetch(`/v1/item/${ids}`, getInit('GET'))
.then(responseHandler)
return fetch(`${base}/item/${ids}`, getInit('GET'))
.then(handleResp)
.then((json) => {
for (let i = 0; i < json.items.length; i++) {
dispatch(addItem(json.items[i]));
@@ -198,8 +196,8 @@ export function postItem (item, type, id) {
if (id) {
item.id = id;
}
return fetch(`/api/v1/${type}`, getInit('POST', item))
.then(responseHandler)
return fetch(`${base}/${type}`, getInit('POST', item))
.then(handleResp)
.then((json) => {
dispatch(addItem({...item, id:json.id}, type));
return json.id;
@@ -229,8 +227,8 @@ export function postAction (item_id, action_type, user_id, item_type) {
user_id
};
return fetch(`/api/v1/${item_type}/${item_id}/actions`, getInit('POST', action))
.then(responseHandler);
return fetch(`${base}/${item_type}/${item_id}/actions`, getInit('POST', action))
.then(handleResp);
};
}
@@ -251,7 +249,7 @@ export function postAction (item_id, action_type, user_id, item_type) {
export function deleteAction (action_id) {
return () => {
return fetch(`/api/v1/actions/${action_id}`, {method: 'DELETE'})
.then(responseHandler);
return fetch(`${base}/actions/${action_id}`, {method: 'DELETE'})
.then(handleResp);
};
}
+2 -2
View File
@@ -3,10 +3,10 @@ export const base = '/api/v1';
export const getInit = (method, body) => {
let init = {
method,
headers: new Headers({
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}),
},
credentials: 'same-origin'
};
-2
View File
@@ -1,6 +1,5 @@
import Notification from './modules/notification/Notification';
import store from './store';
import {fetchConfig} from './actions/config';
import * as itemActions from './actions/items';
import I18n from './modules/i18n/i18n';
import * as notificationActions from './actions/notification';
@@ -9,7 +8,6 @@ import * as authActions from './actions/auth';
export {
Notification,
store,
fetchConfig,
itemActions,
I18n,
notificationActions,
+4 -9
View File
@@ -1,7 +1,7 @@
/* @flow */
import {Map} from 'immutable';
import * as actions from '../actions/config';
import * as actions from '../actions/items';
const initialState = Map({
features: Map({})
@@ -9,15 +9,10 @@ const initialState = Map({
export default (state = initialState, action) => {
switch(action.type) {
case actions.FETCH_CONFIG_REQUEST:
return state.set('loading', true);
case actions.FETCH_CONFIG_FAILED:
return state.set('loading', false);
// Override config if worked
case actions.FETCH_CONFIG_SUCCESS:
return action.config.set('loading', false);
// Override config if worked
case actions.UPDATE_SETTINGS:
return action.config;
default:
return state;
+11
View File
@@ -0,0 +1,11 @@
const kue = require('kue');
const redis = require('./redis');
module.exports = {
queue: kue.createQueue({
redis: {
createClientFactory: () => redis.createClient()
}
}),
kue
};
+33 -29
View File
@@ -2,38 +2,42 @@ const redis = require('redis');
const debug = require('debug')('talk:redis');
const url = process.env.TALK_REDIS_URL || 'redis://localhost';
const client = redis.createClient(url, {
retry_strategy: function(options) {
if (options.error && options.error.code === 'ECONNREFUSED') {
module.exports = {
createClient() {
let client = redis.createClient(url, {
retry_strategy: function(options) {
if (options.error && options.error.code === 'ECONNREFUSED') {
// End reconnecting on a specific error and flush all commands with a individual error
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting on a specific error and flush all commands with a individual error
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout and flush all commands with a individual error
return new Error('Retry time exhausted');
}
// End reconnecting after a specific timeout and flush all commands with a individual error
return new Error('Retry time exhausted');
}
if (options.times_connected > 10) {
if (options.times_connected > 10) {
// End reconnecting with built in error
return undefined;
}
// End reconnecting with built in error
return undefined;
}
// reconnect after
return Math.max(options.attempt * 100, 3000);
// reconnect after
return Math.max(options.attempt * 100, 3000);
}
});
client.ping((err) => {
if (err) {
console.error('Can\'t ping the redis server!');
throw err;
}
debug('connection established');
});
return client;
}
});
client.ping((err) => {
if (err) {
console.error('Can\'t ping the redis server!');
throw err;
}
debug('connection established');
});
module.exports = client;
};
+1 -1
View File
@@ -15,6 +15,6 @@ router.use('/stream', require('./stream'));
router.use('/user', require('./user'));
// Bind the kue handler to the /kue path.
router.use('/kue', authorization.needed('admin'), require('kue').app);
router.use('/kue', authorization.needed('admin'), require('../../kue').kue.app);
module.exports = router;
+1 -3
View File
@@ -1,6 +1,5 @@
const express = require('express');
const Setting = require('../../../models/setting');
const _ = require('lodash');
const router = express.Router();
@@ -8,8 +7,7 @@ router.get('/', (req, res, next) => {
Setting
.getSettings()
.then(settings => {
const whitelist = ['moderation'];
res.json(_.pick(settings, whitelist));
res.json(settings);
})
.catch(next);
});
+14 -24
View File
@@ -14,7 +14,6 @@ router.get('/', (req, res, next) => {
// Get the asset_id for this url (or create it if it doesn't exist)
Promise.all([
// Find or create the asset by url.
Asset.findOrCreateByUrl(decodeURIComponent(req.query.asset_url))
@@ -30,29 +29,18 @@ router.get('/', (req, res, next) => {
// Get the moderation setting from the settings.
Setting.getModerationSetting()
])
.then(([asset, {moderation}]) => {
.then(([asset, settings]) => {
// Get the sitewide moderation setting and return the appropriate comments
let comments;
if (moderation === 'post') {
if (settings.moderation === 'pre') {
comments = Comment.findAcceptedByAssetId(asset.id);
} else {
// Defaults to 'pre' moderation.
comments = Comment.findAcceptedAndNewByAssetId(asset.id);
}
return Promise.all([
// This is the promised component... Fetch the comments based on the
// moderation settings.
comments,
// Send back the reference to the asset.
asset
]);
return Promise.all([comments, asset, settings]);
})
// Get all the users and actions for those comments.
.then(([comments, asset]) => {
.then(([comments, asset, settings]) => {
// Get the user id's from the author id's as a unique array that gets
// sorted.
@@ -82,21 +70,23 @@ router.get('/', (req, res, next) => {
// It's comments...
comments,
// All the users/authors of those comments...
// The users who wrote those comments
users,
// And all actions about the asset, comments, and users.
actions
// The actions on the above items
actions,
// And the relevant settings
settings
]);
})
.then(([asset, comments, users, actions]) => {
// Send back the payload containing all this data.
.then(([asset, comments, users, actions, settings]) => {
res.json({
assets: [asset],
comments,
users,
actions
actions,
settings
});
})
.catch(error => {
+4 -5
View File
@@ -1,5 +1,4 @@
const kue = require('kue');
const queue = kue.createQueue();
const kue = require('../kue');
const debug = require('debug')('talk:services:scraper');
const Asset = require('../models/asset');
const JOB_NAME = 'scraper';
@@ -19,7 +18,7 @@ const scraper = {
return new Promise((resolve, reject) => {
debug(`Creating job for Asset[${asset.id}]`);
let job = queue
let job = kue.queue
.create(JOB_NAME, {
title: `Scrape for asset ${asset.id}`,
asset_id: asset.id
@@ -72,7 +71,7 @@ const scraper = {
debug(`Now processing ${JOB_NAME} jobs`);
// Process jobs with the processJob function.
queue.process(JOB_NAME, (job, done) => {
kue.queue.process(JOB_NAME, (job, done) => {
debug(`Starting on Job[${job.id}] for Asset[${job.data.asset_id}]`);
@@ -123,7 +122,7 @@ const scraper = {
// Shutdown and give the queue 5 seconds to shutdown before we start
// killing jobs.
queue.shutdown(5000, (err) => {
kue.queue.shutdown(5000, (err) => {
if (err) {
return reject(err);
}
@@ -127,6 +127,7 @@ describe('itemActions', () => {
'Accept': 'application/json',
'Content-Type':'application/json'
},
credentials: 'same-origin',
body: JSON.stringify(item.data)
}
);
+5 -4
View File
@@ -89,10 +89,11 @@ describe('api/stream: routes', () => {
.query({'asset_url': 'http://test.com'})
.then(res => {
expect(res).to.have.status(200);
expect(res.body.assets.length).to.equal(1);
expect(res.body.comments.length).to.equal(2);
expect(res.body.users.length).to.equal(2);
expect(res.body.actions.length).to.equal(1);
expect(res.body.assets[0]).to.have.property('url');
expect(res.body.comments[0]).to.have.property('body');
expect(res.body.users[0]).to.have.property('displayName');
expect(res.body.actions[0]).to.have.property('action_type');
expect(res.body.settings).to.have.property('moderation');
});
});
});