Commit 38dd85da authored by Eugen Rochko's avatar Eugen Rochko

Adding notifications column

parent da2ef4d6
export const ALERT_SHOW = 'ALERT_SHOW';
export const ALERT_DISMISS = 'ALERT_DISMISS';
export const ALERT_CLEAR = 'ALERT_CLEAR';
export function dismissAlert(alert) {
return {
type: ALERT_DISMISS,
alert
};
};
export function clearAlert() {
return {
type: ALERT_CLEAR
};
};
export function showAlert(title, message) {
return {
type: ALERT_SHOW,
title,
message
};
};
export const NOTIFICATION_SHOW = 'NOTIFICATION_SHOW';
export const NOTIFICATION_DISMISS = 'NOTIFICATION_DISMISS';
export const NOTIFICATION_CLEAR = 'NOTIFICATION_CLEAR';
import api, { getLinks } from '../api'
import Immutable from 'immutable';
export function dismissNotification(notification) {
import { fetchRelationships } from './accounts';
export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
export const NOTIFICATIONS_REFRESH_REQUEST = 'NOTIFICATIONS_REFRESH_REQUEST';
export const NOTIFICATIONS_REFRESH_SUCCESS = 'NOTIFICATIONS_REFRESH_SUCCESS';
export const NOTIFICATIONS_REFRESH_FAIL = 'NOTIFICATIONS_REFRESH_FAIL';
export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
export const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL';
const fetchRelatedRelationships = (dispatch, notifications) => {
const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
if (accountIds > 0) {
dispatch(fetchRelationships(accountIds));
}
};
export function updateNotifications(notification) {
return dispatch => {
dispatch({
type: NOTIFICATIONS_UPDATE,
notification,
account: notification.account,
status: notification.status
});
fetchRelatedRelationships(dispatch, [notification]);
};
};
export function refreshNotifications() {
return (dispatch, getState) => {
dispatch(refreshNotificationsRequest());
const params = {};
const ids = getState().getIn(['notifications', 'items']);
if (ids.size > 0) {
params.since_id = ids.first().get('id');
}
api(getState).get('/api/v1/notifications', { params }).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(refreshNotificationsSuccess(response.data, next ? next.uri : null));
fetchRelatedRelationships(dispatch, response.data);
}).catch(error => {
dispatch(refreshNotificationsFail(error));
});
};
};
export function refreshNotificationsRequest() {
return {
type: NOTIFICATIONS_REFRESH_REQUEST
};
};
export function refreshNotificationsSuccess(notifications, next) {
return {
type: NOTIFICATIONS_REFRESH_SUCCESS,
notifications,
accounts: notifications.map(item => item.account),
statuses: notifications.map(item => item.status),
next
};
};
export function refreshNotificationsFail(error) {
return {
type: NOTIFICATIONS_REFRESH_FAIL,
error
};
};
export function expandNotifications() {
return (dispatch, getState) => {
const url = getState().getIn(['notifications', 'next'], null);
if (url === null) {
return;
}
dispatch(expandNotificationsRequest());
api(getState).get(url).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
fetchRelatedRelationships(dispatch, response.data);
}).catch(error => {
dispatch(expandNotificationsFail(error));
});
};
};
export function expandNotificationsRequest() {
return {
type: NOTIFICATION_DISMISS,
notification: notification
type: NOTIFICATIONS_EXPAND_REQUEST
};
};
export function clearNotifications() {
export function expandNotificationsSuccess(notifications, next) {
return {
type: NOTIFICATION_CLEAR
type: NOTIFICATIONS_EXPAND_SUCCESS,
notifications,
accounts: notifications.map(item => item.account),
statuses: notifications.map(item => item.status),
next
};
};
export function showNotification(title, message) {
export function expandNotificationsFail(error) {
return {
type: NOTIFICATION_SHOW,
title: title,
message: message
type: NOTIFICATIONS_EXPAND_FAIL,
error
};
};
......@@ -2,7 +2,13 @@ import axios from 'axios';
import LinkHeader from 'http-link-header';
export const getLinks = response => {
return LinkHeader.parse(response.headers.link);
const value = response.headers.link;
if (!value) {
return { refs: [] };
}
return LinkHeader.parse(value);
};
export default getState => axios.create({
......
import PureRenderMixin from 'react-addons-pure-render-mixin';
import ImmutablePropTypes from 'react-immutable-proptypes';
import Avatar from '../../../components/avatar';
import DisplayName from '../../../components/display_name';
import Avatar from './avatar';
import DisplayName from './display_name';
import { Link } from 'react-router';
import IconButton from '../../../components/icon_button';
import IconButton from './icon_button';
import { defineMessages, injectIntl } from 'react-intl';
const messages = defineMessages({
......
......@@ -11,6 +11,15 @@ import { FormattedMessage } from 'react-intl';
import emojify from '../emoji';
import escapeTextContentForBrowser from 'react/lib/escapeTextContentForBrowser';
const outerStyle = {
padding: '8px 10px',
paddingLeft: '68px',
position: 'relative',
minHeight: '48px',
borderBottom: '1px solid #363c4b',
cursor: 'default'
};
const Status = React.createClass({
contextTypes: {
......@@ -26,7 +35,7 @@ const Status = React.createClass({
onDelete: React.PropTypes.func,
onOpenMedia: React.PropTypes.func,
me: React.PropTypes.number,
now: React.PropTypes.any
muted: React.PropTypes.bool
},
mixins: [PureRenderMixin],
......@@ -81,14 +90,14 @@ const Status = React.createClass({
}
return (
<div style={{ padding: '8px 10px', paddingLeft: '68px', position: 'relative', minHeight: '48px', borderBottom: '1px solid #363c4b', cursor: 'default' }}>
<div className={this.props.muted ? 'muted' : ''} style={outerStyle}>
<div style={{ fontSize: '15px' }}>
<div style={{ float: 'right', fontSize: '14px' }}>
<a href={status.get('url')} className='status__relative-time' style={{ color: '#616b86' }} target='_blank' rel='noopener'><RelativeTimestamp timestamp={status.get('created_at')} now={now} /></a>
</div>
<a onClick={this.handleAccountClick.bind(this, status.getIn(['account', 'id']))} href={status.getIn(['account', 'url'])} className='status__display-name' style={{ display: 'block', maxWidth: '100%', paddingRight: '25px', color: '#616b86' }}>
<div style={{ position: 'absolute', left: '10px', top: '10px', width: '48px', height: '48px' }}>
<div className='status__avatar' style={{ position: 'absolute', left: '10px', top: '10px', width: '48px', height: '48px' }}>
<Avatar src={status.getIn(['account', 'avatar'])} size={48} />
</div>
......
import { connect } from 'react-redux';
import { makeGetAccount } from '../../../selectors';
import Account from '../components/account';
import { connect } from 'react-redux';
import { makeGetAccount } from '../selectors';
import Account from '../components/account';
import {
followAccount,
unfollowAccount
} from '../../../actions/accounts';
} from '../actions/accounts';
const makeMapStateToProps = () => {
const getAccount = makeGetAccount();
......
......@@ -6,6 +6,7 @@ import {
deleteFromTimelines,
refreshTimeline
} from '../actions/timelines';
import { updateNotifications } from '../actions/notifications';
import { setAccessToken } from '../actions/meta';
import { setAccountSelf } from '../actions/accounts';
import PureRenderMixin from 'react-addons-pure-render-mixin';
......@@ -32,6 +33,7 @@ import Following from '../features/following';
import Reblogs from '../features/reblogs';
import Favourites from '../features/favourites';
import HashtagTimeline from '../features/hashtag_timeline';
import Notifications from '../features/notifications';
import { IntlProvider, addLocaleData } from 'react-intl';
import en from 'react-intl/locale-data/en';
import de from 'react-intl/locale-data/de';
......@@ -75,11 +77,18 @@ const Mastodon = React.createClass({
return store.dispatch(refreshTimeline('home', true));
case 'block':
return store.dispatch(refreshTimeline('mentions', true));
case 'notification':
return store.dispatch(updateNotifications(JSON.parse(data.message)));
}
}
});
}
// Desktop notifications
if (Notification.permission === 'default') {
Notification.requestPermission();
}
},
componentWillUnmount () {
......@@ -103,6 +112,8 @@ const Mastodon = React.createClass({
<Route path='timelines/public' component={PublicTimeline} />
<Route path='timelines/tag/:id' component={HashtagTimeline} />
<Route path='notifications' component={Notifications} />
<Route path='statuses/new' component={Compose} />
<Route path='statuses/:statusId' component={Status} />
<Route path='statuses/:statusId/reblogs' component={Reblogs} />
......
import PureRenderMixin from 'react-addons-pure-render-mixin';
import ImmutablePropTypes from 'react-immutable-proptypes';
import AccountContainer from '../../followers/containers/account_container';
import AccountContainer from '../../../containers/account_container';
import { FormattedMessage } from 'react-intl';
const outerStyle = {
......
......@@ -4,7 +4,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
import LoadingIndicator from '../../components/loading_indicator';
import { fetchFavourites } from '../../actions/interactions';
import { ScrollContainer } from 'react-router-scroll';
import AccountContainer from '../followers/containers/account_container';
import AccountContainer from '../../containers/account_container';
import Column from '../ui/components/column';
import ColumnBackButton from '../../components/column_back_button';
......
......@@ -7,7 +7,7 @@ import {
expandFollowers
} from '../../actions/accounts';
import { ScrollContainer } from 'react-router-scroll';
import AccountContainer from './containers/account_container';
import AccountContainer from '../../containers/account_container';
const mapStateToProps = (state, props) => ({
accountIds: state.getIn(['user_lists', 'followers', Number(props.params.accountId), 'items'])
......
......@@ -7,7 +7,7 @@ import {
expandFollowing
} from '../../actions/accounts';
import { ScrollContainer } from 'react-router-scroll';
import AccountContainer from '../followers/containers/account_container';
import AccountContainer from '../../containers/account_container';
const mapStateToProps = (state, props) => ({
accountIds: state.getIn(['user_lists', 'following', Number(props.params.accountId), 'items'])
......
import PureRenderMixin from 'react-addons-pure-render-mixin';
import ImmutablePropTypes from 'react-immutable-proptypes';
import StatusContainer from '../../../containers/status_container';
import AccountContainer from '../../../containers/account_container';
import { FormattedMessage } from 'react-intl';
import { Link } from 'react-router';
const messageStyle = {
padding: '8px 10px',
paddingBottom: '0',
cursor: 'default',
color: '#d9e1e8',
fontSize: '15px'
};
const linkStyle = {
fontWeight: '500'
};
const Notification = React.createClass({
propTypes: {
notification: ImmutablePropTypes.map.isRequired
},
mixins: [PureRenderMixin],
renderFollow (account, link) {
return (
<div className='notification'>
<div style={messageStyle}><i className='fa fa-fw fa-user-plus' style={{ color: '#2b90d9' }} /> <FormattedMessage id='notification.follow' defaultMessage='{name} followed you' values={{ name: link }} /></div>
<AccountContainer id={account.get('id')} withNote={false} />
</div>
);
},
renderMention (notification) {
return <StatusContainer id={notification.get('status')} />;
},
renderFavourite (notification, link) {
return (
<div className='notification'>
<div style={messageStyle}><i className='fa fa-fw fa-star' style={{ color: '#ca8f04' }} /> <FormattedMessage id='notification.favourite' defaultMessage='{name} favourited your status' values={{ name: link }} /></div>
<StatusContainer id={notification.get('status')} muted={true} />
</div>
);
},
renderReblog (notification, link) {
return (
<div className='notification'>
<div style={messageStyle}><i className='fa fa-fw fa-retweet' style={{ color: '#2b90d9' }} /> <FormattedMessage id='notification.reblog' defaultMessage='{name} reblogged your status' values={{ name: link }} /></div>
<StatusContainer id={notification.get('status')} muted={true} />
</div>
);
},
render () {
const { notification } = this.props;
const account = notification.get('account');
const displayName = account.get('display_name').length > 0 ? account.get('display_name') : account.get('username');
const link = <Link className='notification__display-name' style={linkStyle} to={`/accounts/${account.get('id')}`}>{displayName}</Link>;
switch(notification.get('type')) {
case 'follow':
return this.renderFollow(account, link);
case 'mention':
return this.renderMention(notification);
case 'favourite':
return this.renderFavourite(notification, link);
case 'reblog':
return this.renderReblog(notification, link);
}
}
});
export default Notification;
import { connect } from 'react-redux';
import { makeGetNotification } from '../../../selectors';
import Notification from '../components/notification';
const makeMapStateToProps = () => {
const getNotification = makeGetNotification();
const mapStateToProps = (state, props) => ({
notification: getNotification(state, props.notification, props.accountId)
});
return mapStateToProps;
};
export default connect(makeMapStateToProps)(Notification);
import { connect } from 'react-redux';
import PureRenderMixin from 'react-addons-pure-render-mixin';
import ImmutablePropTypes from 'react-immutable-proptypes';
import Column from '../ui/components/column';
import {
refreshNotifications,
expandNotifications
} from '../../actions/notifications';
import NotificationContainer from './containers/notification_container';
import { ScrollContainer } from 'react-router-scroll';
import { defineMessages, injectIntl } from 'react-intl';
const messages = defineMessages({
title: { id: 'column.notifications', defaultMessage: 'Notifications' }
});
const mapStateToProps = state => ({
notifications: state.getIn(['notifications', 'items'])
});
const Notifications = React.createClass({
propTypes: {
notifications: ImmutablePropTypes.list.isRequired,
dispatch: React.PropTypes.func.isRequired
},
mixins: [PureRenderMixin],
componentWillMount () {
const { dispatch } = this.props;
dispatch(refreshNotifications());
},
handleScroll (e) {
const { scrollTop, scrollHeight, clientHeight } = e.target;
if (scrollTop === scrollHeight - clientHeight) {
this.props.dispatch(expandNotifications());
}
},
render () {
const { intl, notifications } = this.props;
return (
<Column icon='bell' heading={intl.formatMessage(messages.title)}>
<ScrollContainer scrollKey='notifications'>
<div className='scrollable' onScroll={this.handleScroll}>
<div>
{notifications.map(item => <NotificationContainer key={item.get('id')} notification={item} accountId={item.get('account')} />)}
</div>
</div>
</ScrollContainer>
</Column>
);
}
});
export default connect(mapStateToProps)(injectIntl(Notifications));
......@@ -4,7 +4,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
import LoadingIndicator from '../../components/loading_indicator';
import { fetchReblogs } from '../../actions/interactions';
import { ScrollContainer } from 'react-router-scroll';
import AccountContainer from '../followers/containers/account_container';
import AccountContainer from '../../containers/account_container';
import Column from '../ui/components/column';
import ColumnBackButton from '../../components/column_back_button';
......
import { connect } from 'react-redux';
import { NotificationStack } from 'react-notification';
import { connect } from 'react-redux';
import { NotificationStack } from 'react-notification';
import {
dismissNotification,
clearNotifications
} from '../../../actions/notifications';
import { getNotifications } from '../../../selectors';
dismissAlert,
clearAlerts
} from '../../../actions/alerts';
import { getAlerts } from '../../../selectors';
const mapStateToProps = (state, props) => ({
notifications: getNotifications(state)
notifications: getAlerts(state)
});
const mapDispatchToProps = (dispatch) => {
return {
onDismiss: notifiction => {
dispatch(dismissNotification(notifiction));
onDismiss: alert => {
dispatch(dismissAlert(alert));
}
};
};
......
......@@ -26,10 +26,12 @@ const en = {
"column.home": "Home",
"column.mentions": "Erwähnungen",
"column.public": "Gesamtes Bekanntes Netz",
"column.notifications": "Mitteilungen",
"tabs_bar.compose": "Schreiben",
"tabs_bar.home": "Home",
"tabs_bar.mentions": "Erwähnungen",
"tabs_bar.public": "Gesamtes Netz",
"tabs_bar.notifications": "Mitteilungen",
"compose_form.placeholder": "Worüber möchstest du schreiben?",
"compose_form.publish": "Veröffentlichen",
"navigation_bar.settings": "Einstellungen",
......@@ -42,7 +44,11 @@ const en = {
"suggestions_box.who_to_follow": "Wem folgen",
"suggestions_box.refresh": "Aktualisieren",
"upload_button.label": "Media-Datei anfügen",
"upload_form.undo": "Entfernen"
"upload_form.undo": "Entfernen",
"notification.follow": "{name} folgt dir",
"notification.favourite": "{name} favorisierte deinen Status",
"notification.reblog": "{name} teilte deinen Status",
"notification.mention": "{name} erwähnte dich"
};
export default en;
......@@ -27,10 +27,12 @@ const en = {
"column.home": "Home",
"column.mentions": "Mentions",
"column.public": "Public",
"column.notifications": "Notifications",
"tabs_bar.compose": "Compose",
"tabs_bar.home": "Home",
"tabs_bar.mentions": "Mentions",