Skip to content

Commit

Permalink
Merge pull request #304 from graasp/303/signInSignOutActions
Browse files Browse the repository at this point in the history
feat: add signin and signout actions, refactor dashboard
  • Loading branch information
juancarlosfarah authored Aug 3, 2020
2 parents 040ac55 + 0ed6a96 commit 40270d2
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 120 deletions.
7 changes: 7 additions & 0 deletions public/app/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ const VISIBILITIES = {
PRIVATE: 'private',
};

const DEFAULT_FORMAT = 'v1';
const ACTIONS_VERBS = {
LOGOUT: 'logout',
};

module.exports = {
DEFAULT_LOGGING_LEVEL,
DEFAULT_PROTOCOL,
Expand All @@ -103,4 +108,6 @@ module.exports = {
DEFAULT_USER_MODE,
MAX_RECENT_SPACES,
VISIBILITIES,
DEFAULT_FORMAT,
ACTIONS_VERBS,
};
4 changes: 3 additions & 1 deletion public/app/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const fsPromises = fs.promises;

const SPACES_COLLECTION = 'spaces';
const ACTIONS_COLLECTION = 'actions';
const USER_COLLECTION = 'user';
const USERS_COLLECTION = 'users';
const APP_INSTANCE_RESOURCES_COLLECTION = 'appInstanceResources';
const CLASSROOMS_COLLECTION = 'classrooms';
Expand Down Expand Up @@ -37,7 +38,7 @@ const bootstrapDatabase = (dbPath = DATABASE_PATH) => {
db.defaults({
[SPACES_COLLECTION]: [],
[USERS_COLLECTION]: [],
user: { lang: DEFAULT_LANG },
[USER_COLLECTION]: { lang: DEFAULT_LANG },
[ACTIONS_COLLECTION]: [],
[APP_INSTANCE_RESOURCES_COLLECTION]: [],
[CLASSROOMS_COLLECTION]: [],
Expand All @@ -47,6 +48,7 @@ const bootstrapDatabase = (dbPath = DATABASE_PATH) => {

module.exports = {
SPACES_COLLECTION,
USER_COLLECTION,
USERS_COLLECTION,
APP_INSTANCE_RESOURCES_COLLECTION,
ACTIONS_COLLECTION,
Expand Down
2 changes: 2 additions & 0 deletions public/app/listeners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const getSpaceInClassroom = require('./getSpaceInClassroom');
const loadSpaceInClassroom = require('./loadSpaceInClassroom');
const setActionAccessibility = require('./setActionAccessibility');
const setActionsAsEnabled = require('./setActionsAsEnabled');
const windowAllClosed = require('./windowAllClosed');

module.exports = {
loadSpace,
Expand Down Expand Up @@ -101,4 +102,5 @@ module.exports = {
loadSpaceInClassroom,
setActionAccessibility,
setActionsAsEnabled,
windowAllClosed,
};
21 changes: 12 additions & 9 deletions public/app/listeners/postAction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ObjectId = require('bson-objectid');
const { POST_ACTION_CHANNEL } = require('../config/channels');
const logger = require('../logger');
const { DEFAULT_FORMAT } = require('../config/config');

const postAction = (mainWindow, db) => (event, payload = {}) => {
try {
Expand All @@ -9,7 +10,7 @@ const postAction = (mainWindow, db) => (event, payload = {}) => {
appInstanceId,
spaceId,
subSpaceId,
format,
format = DEFAULT_FORMAT,
verb,
data,
geolocation,
Expand All @@ -21,9 +22,9 @@ const postAction = (mainWindow, db) => (event, payload = {}) => {

// prepare the action that we will create
const actionToWrite = {
spaceId,
subSpaceId,
appInstanceId,
...(spaceId ? { spaceId } : {}),
...(subSpaceId ? { subSpaceId } : {}),
...(appInstanceId ? { appInstanceId } : {}),
createdAt: now,
updatedAt: now,
data,
Expand All @@ -40,11 +41,13 @@ const postAction = (mainWindow, db) => (event, payload = {}) => {
.push(actionToWrite)
.write();

// send back the resource
mainWindow.webContents.send(
`${POST_ACTION_CHANNEL}_${appInstanceId}`,
actionToWrite
);
// send back the action to the app
if (appInstanceId) {
mainWindow.webContents.send(
`${POST_ACTION_CHANNEL}_${appInstanceId}`,
actionToWrite
);
}
} catch (e) {
logger.error(e);
mainWindow.webContents.send(POST_ACTION_CHANNEL, null);
Expand Down
28 changes: 28 additions & 0 deletions public/app/listeners/windowAllClosed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const { app } = require('electron');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const postAction = require('./postAction');
const { USER_COLLECTION } = require('../db');
const { DATABASE_PATH, ACTIONS_VERBS } = require('../config/config');

const windowAllClosed = mainWindow => {
const adapter = new FileSync(DATABASE_PATH);
const db = low(adapter);
const user = db.get(USER_COLLECTION).value();

// post sign out action if user is still connected when quitting the app
if (user) {
const { id: userId, username, anonymous, geolocation } = user;
postAction(mainWindow, db)(null, {
userId,
verb: ACTIONS_VERBS.LOGOUT,
data: { id: userId, username, anonymous },
geolocation,
});
}

app.quit();
};

module.exports = windowAllClosed;
5 changes: 2 additions & 3 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const {
loadSpaceInClassroom,
setActionAccessibility,
setActionsAsEnabled,
windowAllClosed,
} = require('./app/listeners');
const isMac = require('./app/utils/isMac');

Expand Down Expand Up @@ -601,9 +602,7 @@ app.on('ready', async () => {
ipcMain.on(SYNC_SPACE_CHANNEL, syncSpace(mainWindow, db));
});

app.on('window-all-closed', () => {
app.quit();
});
app.on('window-all-closed', () => windowAllClosed(mainWindow));

app.on('activate', () => {
if (mainWindow === null) {
Expand Down
76 changes: 54 additions & 22 deletions src/actions/action.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,76 @@
import { POST_ACTION_SUCCEEDED } from '../types';
import { POST_ACTION_CHANNEL } from '../config/channels';
import {
DEFAULT_ACTIONS_AS_ENABLED,
DEFAULT_GEOLOCATION_ENABLED,
} from '../config/constants';

const postAction = async (
{
userId,
userId: actionUserId,
appInstanceId,
spaceId,
subSpaceId,
format,
data,
verb,
geolocation,
visibility,
} = {},
user,
callback
) => () => {
try {
window.ipcRenderer.send(POST_ACTION_CHANNEL, {
userId,
appInstanceId,
spaceId,
subSpaceId,
format,
data,
verb,
const mutableUser = user.toJS();
const {
id,
settings: {
actionsEnabled = DEFAULT_ACTIONS_AS_ENABLED,
geolocationEnabled = DEFAULT_GEOLOCATION_ENABLED,
},
geolocation,
visibility,
});
} = mutableUser;

window.ipcRenderer.once(
`${POST_ACTION_CHANNEL}_${appInstanceId}`,
async (event, response) => {
callback({
// have to include the appInstanceId to avoid broadcasting
appInstanceId,
type: POST_ACTION_SUCCEEDED,
payload: response,
});
if (actionsEnabled) {
// get user id
let userId = actionUserId;
if (!actionUserId) {
userId = id;
}
);

// add geolocation to action if enabled
let geolocationLatLong = null;
if (geolocationEnabled) {
const { latitude, longitude } = geolocation;
geolocationLatLong = { ll: [latitude, longitude] };
}

window.ipcRenderer.send(POST_ACTION_CHANNEL, {
userId,
appInstanceId,
spaceId,
subSpaceId,
format,
data,
verb,
geolocation: geolocationLatLong,
visibility,
});

// if action from app, wait in app channel
if (appInstanceId) {
window.ipcRenderer.once(
`${POST_ACTION_CHANNEL}_${appInstanceId}`,
async (event, response) => {
callback({
// have to include the appInstanceId to avoid broadcasting
appInstanceId,
type: POST_ACTION_SUCCEEDED,
payload: response,
});
}
);
}
}
} catch (err) {
// do nothing
}
Expand Down
17 changes: 16 additions & 1 deletion src/actions/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
} from '../config/channels';
import { createFlag } from './common';
import { ERROR_GENERAL } from '../config/errors';
import { postAction } from './action';
import { ACTION_VERBS } from '../config/constants';

const flagSigningIn = createFlag(FLAG_SIGNING_IN);
const flagSigningOut = createFlag(FLAG_SIGNING_OUT);
Expand All @@ -37,6 +39,7 @@ const signIn = async ({ username, anonymous }) => async dispatch => {
type: SIGN_IN_SUCCEEDED,
payload: user,
});
// action is sent in sign in redirection
}
dispatch(flagSigningIn(false));
});
Expand All @@ -46,7 +49,7 @@ const signIn = async ({ username, anonymous }) => async dispatch => {
}
};

const signOut = () => dispatch => {
const signOut = user => dispatch => {
try {
dispatch(flagSigningOut(true));
window.ipcRenderer.send(SIGN_OUT_CHANNEL);
Expand All @@ -58,6 +61,18 @@ const signOut = () => dispatch => {
type: SIGN_OUT_SUCCEEDED,
payload: response,
});
const username = user.get('username');
const id = user.get('id');
const anonymous = user.get('anonymous');
dispatch(
postAction(
{
verb: ACTION_VERBS.LOGOUT,
data: { username, id, anonymous },
},
user
)
);
}
dispatch(flagSigningOut(false));
});
Expand Down
11 changes: 9 additions & 2 deletions src/components/common/MainMenu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Component } from 'react';
import { Map } from 'immutable';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router';
import { connect } from 'react-redux';
Expand Down Expand Up @@ -58,6 +59,11 @@ export class MainMenu extends Component {
location: PropTypes.shape({ pathname: PropTypes.string.isRequired })
.isRequired,
userMode: PropTypes.oneOf(Object.values(USER_MODES)).isRequired,
user: PropTypes.instanceOf(Map),
};

static defaultProps = {
user: Map(),
};

handleClick = path => {
Expand All @@ -74,12 +80,12 @@ export class MainMenu extends Component {

handleSignOut() {
// pathname inside location matches the path in url
const { location: { pathname } = {}, dispatchSignOut } = this.props;
const { location: { pathname } = {}, dispatchSignOut, user } = this.props;
if (pathname) {
sessionStorage.setItem('redirect', pathname);
}

dispatchSignOut();
dispatchSignOut(user);
}

renderSignOut() {
Expand Down Expand Up @@ -342,6 +348,7 @@ const mapStateToProps = ({ authentication }) => ({
developerMode: authentication.getIn(['user', 'settings', 'developerMode']),
userMode: authentication.getIn(['user', 'settings', 'userMode']),
activity: Boolean(authentication.getIn(['current', 'activity']).size),
user: authentication.get('user'),
});
const mapDispatchToProps = {
dispatchSignOut: signOut,
Expand Down
Loading

0 comments on commit 40270d2

Please sign in to comment.