Skip to content

Swik 1440 improve slide links in notifications #1055

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion components/Deck/ActivityFeedPanel/ActivityItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {connectToStores} from 'fluxible-addons-react';
import cheerio from 'cheerio';
import DeckTreeStore from '../../../stores/DeckTreeStore';
import Util from '../../common/Util';
import {getLanguageName} from '../../../common';

class ActivityItem extends React.Component {
handleLike() {
Expand Down Expand Up @@ -70,7 +71,7 @@ class ActivityItem extends React.Component {
{node.author ? node.author.displayName || node.author.username : 'unknown'}
</a> {'translated '} {nodeRef} {' to '}
{/*<a href={'/slideview/' + node.translation_info.content_id}>{node.translation_info.language}</a>*/}
<a href={viewPath}>{node.translation_info.language}</a>
{getLanguageName(node.translation_info.language)}
<br/>
{DateDiv}
</div>
Expand Down
2 changes: 2 additions & 0 deletions components/Deck/ActivityFeedPanel/util/ActivityFeedUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class ActivityFeedUtil {
let msPerYear = msPerDay * 365;

let elapsed = nowTime - thenTime;
if (elapsed < 0)
elapsed = 0;

if (elapsed < msPerMinute) {
return Math.round(elapsed/1000) + ' seconds ago';
Expand Down
14 changes: 12 additions & 2 deletions components/User/UserNotificationsPanel/UserNotificationsItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import PropTypes from 'prop-types';
import React from 'react';
import {formatDate} from '../../Deck/ActivityFeedPanel/util/ActivityFeedUtil';
import {List, Icon, Button} from 'semantic-ui-react';
import { isEmpty } from '../../../common.js';
import classNames from 'classnames/bind';
import cheerio from 'cheerio';
import readUserNotification from '../../../actions/user/notifications/readUserNotification';
import deleteUserNotification from '../../../actions/user/notifications/deleteUserNotification';
import {getLanguageName} from '../../../common';

class UserNotificationsItem extends React.Component {
handleMarkAsRead(notification) {
Expand Down Expand Up @@ -41,7 +43,15 @@ class UserNotificationsItem extends React.Component {
notification.user_id = undefined;
}

let viewPath = ((notification.content_kind === 'slide') ? '/slideview/' : '/deck/') + notification.content_id;
let viewPath = '';
if (!isEmpty(notification.content_root_id) && !isEmpty(notification.path)) {
viewPath = notification.path;
} else if (notification.content_kind === 'deck') {
viewPath = '/deck/' + notification.content_id;
} else {
viewPath = '/slideview/' + notification.content_id;
}

const cheerioContentName = (notification.content_name !== undefined) ? cheerio.load(notification.content_name).text() : '';
if (notification.content_kind === 'group')
viewPath = '/user/' + this.props.username + '/groups/overview';
Expand All @@ -55,7 +65,7 @@ class UserNotificationsItem extends React.Component {
</a> {'translated ' + notification.content_kind + ' '}
<a href={viewPath}>{cheerioContentName}</a>{' to '}
{/*<a href={'/slideview/' + notification.translation_info.content_id}>{notification.translation_info.language}</a>*/}
<a href={viewPath}>{notification.translation_info.language}</a>
{getLanguageName(notification.translation_info.language)}
</div>
);
break;
Expand Down
112 changes: 110 additions & 2 deletions services/notifications.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Microservices} from '../configs/microservices';
import rp from 'request-promise';
import Util from '../components/common/Util';
const log = require('../configs/log').log;

export default {
Expand All @@ -15,10 +16,40 @@ export default {
}

if (resource === 'notifications.list'){

rp.get({uri: Microservices.notification.uri + '/notifications/' + uid + '?metaonly=false'}).then((res) => {
let notifications = JSON.parse(res).items;
callback(null, {notifications: notifications});

//create path for slides and subdecks- GET DECK DATA for content_root_id FROM DECK SERVICE
let deckTreePromises = [];
for(let notification of notifications){
if (notification.content_root_id && notification.content_id.split('-')[0] !== notification.content_root_id.split('-')[0]) {
deckTreePromises.push(
rp.get({
uri: `${Microservices.deck.uri}/decktree/${notification.content_root_id}`,
json: true
})
);
}
}

Promise.all(deckTreePromises).then( (data) => {
let deckTrees = data;
for (let i = 0; i < deckTrees.length; i++) {
let deckTree = makePathForTree(deckTrees[i], []);
let deckId = deckTree.id;
let flatTree = flattenTree(deckTree);
for (let j = 0; j < notifications.length; j++) {
if (notifications[j].content_root_id && notifications[j].content_root_id.split('-')[0] === deckId.split('-')[0]) {
notifications[j].path = getPath(notifications[j].content_id, notifications[j].content_kind, deckId, flatTree);
}
}
}

callback(null, {notifications: notifications});
}).catch( (err) => {
console.log(err);
callback(null, {notifications: []});
});
}).catch((err) => {
console.log(err);
callback(null, {notifications: []});
Expand Down Expand Up @@ -116,3 +147,80 @@ export default {
// create: (req, resource, params, body, config, callback) => {},

};

//return the position of the node in the deck
function getPath(sid, type, deckId, flatTree){
let path = '';
for (let i = 0; i < flatTree.length; i++) {
if (flatTree[i].type === type && flatTree[i].id === sid) {
path = flatTree[i].path;
let nodeSelector = {id: deckId, stype: type, sid: sid, spath: path};
let nodeURL = Util.makeNodeURL(nodeSelector, 'deck', 'view', undefined, undefined, true);

return nodeURL;
}
}
return path;
}

//flat tree is used to avoid complex recursive functions on tree
//it is a trade off: updating the tree needs this to be synchronized
//this also propagates themes from decks to slide children
function flattenTree(deckTree, theme) {
if (!theme) theme = deckTree.theme;

let list = [];
list.push({
id: deckTree.id,
title: deckTree.title,
type: deckTree.type,
path: deckTree.path,
language: deckTree.language,
theme: theme
});

if (deckTree.type === 'deck') {
deckTree.children.forEach((item) => {
let theme = item.theme;
if (item.type === 'slide') theme = deckTree.theme;

list = list.concat(flattenTree(item, theme));
});
}
return list;
}

//deckTree: original deckTree from service without path
//path: array of binary id:position
function makePathForTree(deckTree, path) {
let nodePath = makeSelectorPathString(path);
let newTree = {
id: deckTree.id,
title: deckTree.title,
type: deckTree.type,
path: nodePath,
theme: deckTree.theme,
language: deckTree.language,
selected: false,
editable: false,
onAction: 0
};
if (deckTree.type === 'deck') {
newTree.children = [];
newTree.expanded = true;
deckTree.children.forEach((item, index) => {
newTree.children.push(makePathForTree(item, path.concat([[item.id, index + 1]])) );
});
}
return newTree;
}

//parses the nodePath and builds a selector path for navigation
function makeSelectorPathString(nodePath) {
let out = [], slectorPath = '';
nodePath.forEach((element) => {
out.push(element.join(':'));
});
slectorPath = out.join(';');
return slectorPath;
}