Commit b26711e2 authored by Luc Didry's avatar Luc Didry

Fix #51, not cleaned, but working

parent 95208529
Pipeline #44278 passed with stage
in 3 minutes and 7 seconds
......@@ -940,7 +940,32 @@ module.exports = (function () {
'lastname', 'email');
}
);
res.send({ value: data });
data.bookmarks = { groups: {}, pads: {} }
group.getBookmarkedGroupsByUser(u, function (err, bookmarks) {
if (err) {
return res.status(404).send({
error: err.message
});
}
data.bookmarks.groups = ld.transform(bookmarks,
function (memo, val, key) {
memo[key] = ld.omit(val, 'password');
}
);
pad.getBookmarkedPadsByUser(u, function (err, bookmarks) {
if (err) {
return res.status(404).send({
error: err.message
});
}
data.bookmarks.pads = ld.transform(bookmarks,
function (memo, val, key) {
memo[key] = ld.omit(val, 'password');
}
);
res.send({ value: data });
});
});
});
}
catch (e) {
......
......@@ -43,6 +43,7 @@ module.exports = (function () {
users: m.prop([]),
admins: m.prop([]),
tags: m.prop([]),
bookmarks: m.prop({ groups: m.prop({}), pads: m.prop({}) }),
tmp: m.prop({ groups: m.prop({}), pads: m.prop({}) })
});
};
......@@ -72,6 +73,7 @@ module.exports = (function () {
function (resp) {
model.groups(resp.value.groups);
model.pads(resp.value.pads);
model.bookmarks(resp.value.bookmarks);
var u = auth.userInfo();
resp.value.users[u._id] = u;
model.users(resp.value.users);
......
......@@ -36,7 +36,7 @@ module.exports = (function () {
var auth = require('../auth.js');
var u = auth.userInfo;
var layout = require('./layout.js');
var groupMark = require('./group.js').mark;
var groupMark = require('./group-mark.js');
var padMark = require('./pad-mark.js');
var model = require('../model/group.js');
......@@ -74,8 +74,8 @@ module.exports = (function () {
.value();
};
c.bookmarks = {
groups: items(model.groups(), uMarks.groups),
pads: items(model.pads(), uMarks.pads)
groups: items(model.bookmarks().groups, uMarks.groups),
pads: items(model.bookmarks().pads, uMarks.pads)
};
};
......@@ -86,9 +86,9 @@ module.exports = (function () {
* bookmark.
*/
c.unmark = function (itemId, type) {
c.unmark = function (item, type) {
var action = (type === 'groups') ? groupMark : padMark;
action(itemId, c.computeBookmarks);
action(item, c.computeBookmarks);
};
// Bootstrapping
......@@ -126,7 +126,7 @@ module.exports = (function () {
return m('li', [
m('button.btn.btn-link.btn-lg', {
title: conf.LANG.GROUP.UNMARK,
onclick: ld.partial(c.unmark, item._id, type)
onclick: ld.partial(c.unmark, item, type)
}, [ m('i.glyphicon glyphicon-star') ]),
m('a', { href: route, config: m.route }, item.name)
]);
......
/**
* # Group bookmarking module
*
* ## License
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* ## Description
*
* Short module for group bookmark and unmark
*/
module.exports = (function () {
'use strict';
// Dependencies
var m = require('mithril');
var ld = require('lodash');
var auth = require('../auth.js');
var conf = require('../configuration.js');
var notif = require('../widgets/notification.js');
var model = require('../model/group.js');
/**
* ## Main function
*
* Used for authentication enforcement and confirmation before removal. In all
* cases, redirection to parent group view. An optional `successFn` can be
* given, called with no argument after successfull operation.
*/
return function (group, successFn) {
var gid = group._id;
var user = auth.userInfo();
if (ld.includes(user.bookmarks.groups, gid)) {
ld.pull(user.bookmarks.groups, gid);
} else {
user.bookmarks.groups.push(gid);
}
if (typeof(model.bookmarks().groups[group._id]) !== 'undefined') {
delete model.bookmarks().groups[group._id];
} else {
model.bookmarks().groups[group._id] = group;
}
m.request({
url: conf.URLS.USERMARK,
method: 'POST',
data: {
type: 'groups',
key: gid,
auth_token: auth.token()
}
}).then(function () {
notif.success({ body: conf.LANG.GROUP.MARK_SUCCESS });
if (successFn) { successFn(); }
}, function (err) {
return notif.error({ body: ld.result(conf.LANG, err.error) });
});
};
}).call(this);
......@@ -39,7 +39,9 @@ module.exports = (function () {
var padMark = require('./pad-mark.js');
var padShare = require('./pad-share.js');
var ready = require('../helpers/ready.js');
var groupMark = require('./group-mark.js');
var u = auth.userInfo;
var group = {};
/**
......@@ -422,7 +424,22 @@ module.exports = (function () {
*/
view.main = function (c) {
var h2Elements = [ m('span', conf.LANG.GROUP.GROUP + ' ' + c.group.name) ];
var isBookmarked = (ld.includes(u().bookmarks.groups, c.group._id));
var h2Elements = [ m('span', [
m('button.btn.btn-link.btn-lg', {
onclick: function (e) {
e.preventDefault();
groupMark(c.group)
},
title: (isBookmarked ? conf.LANG.GROUP.UNMARK : conf.LANG.GROUP.BOOKMARK)
}, [
m('i',
{ class: 'glyphicon glyphicon-star' +
(isBookmarked ? '' : '-empty') })
]
),
conf.LANG.GROUP.GROUP + ' ' + c.group.name
])];
var shareBtn = '';
if (c.group.visibility !== 'restricted') {
shareBtn = m('button.btn.btn-default', {
......
......@@ -38,42 +38,10 @@ module.exports = (function () {
var layout = require('./layout.js');
var model = require('../model/group.js');
var padShare = require('./pad-share.js');
var groupMark = require('./group-mark.js');
var group = {};
/**
* ### mark
*
* `mark` public function takes a group object and adds or removes it from the
* bookmarks of the current user. It also can have a `successFn` function that
* is called after success.
*/
group.mark = function (gid, successFn) {
var user = u();
var errfn = function (err) {
return notif.error({ body: ld.result(conf.LANG, err.error) });
};
if (ld.includes(user.bookmarks.groups, gid)) {
ld.pull(user.bookmarks.groups, gid);
} else {
user.bookmarks.groups.push(gid);
}
m.request({
url: conf.URLS.USERMARK,
method: 'POST',
data: {
type: 'groups',
key: gid,
auth_token: auth.token()
}
}).then(function () {
notif.success({ body: conf.LANG.GROUP.MARK_SUCCESS });
if (successFn) { successFn(); }
}, errfn);
};
/**
* ## Controller
*
......@@ -403,7 +371,7 @@ module.exports = (function () {
m('th', [
m('p.pull-right', actions),
m('a.btn.btn-link.btn-lg', {
onclick: group.mark.bind(c, g._id, c.computeGroups),
onclick: groupMark.bind(c, g, c.computeGroups),
href: '/mypads',
config: m.route,
title: (isBookmarked ? GROUP.UNMARK : GROUP.BOOKMARK)
......
......@@ -33,6 +33,7 @@ module.exports = (function () {
var auth = require('../auth.js');
var conf = require('../configuration.js');
var notif = require('../widgets/notification.js');
var model = require('../model/group.js');
/**
* ## Main function
......@@ -42,13 +43,19 @@ module.exports = (function () {
* given, called with no argument after successfull operation.
*/
return function (pid, successFn) {
return function (pad, successFn) {
var pid = pad._id
var user = auth.userInfo();
if (ld.includes(user.bookmarks.pads, pid)) {
ld.pull(user.bookmarks.pads, pid);
} else {
user.bookmarks.pads.push(pid);
}
if (typeof(model.bookmarks().pads[pad._id]) !== 'undefined') {
delete model.bookmarks().pads[pad._id];
} else {
model.bookmarks().pads[pad._id] = pad;
}
m.request({
url: conf.URLS.USERMARK,
method: 'POST',
......
......@@ -248,11 +248,11 @@ module.exports = (function () {
]),
m('h2', [
(function () {
if (!c.isGuest) {
if (auth.isAuthenticated()) {
var isBookmarked = ld.includes(c.bookmarks, c.pad._id);
return m('button.btn.btn-link.btn-lg', {
title: (isBookmarked ? GROUP.UNMARK : GROUP.BOOKMARK),
onclick: function () { padMark(c.pad._id); }
onclick: function () { padMark(c.pad); }
}, [
m('i',
{ class: 'glyphicon glyphicon-star' +
......
......@@ -149,6 +149,27 @@ module.exports = (function () {
);
};
group.getBookmarkedGroupsByUser = function (user, callback) {
if (!ld.isObject(user) || !ld.isArray(user.bookmarks.groups)) {
throw new TypeError('BACKEND.ERROR.TYPE.USER_INVALID');
}
if (!ld.isFunction(callback)) {
throw new TypeError('BACKEND.ERROR.TYPE.CALLBACK_FN');
}
storage.fn.getKeys(
ld.map(user.bookmarks.groups, function (g) { return GPREFIX + g; }),
function (err, groups) {
if (err) { return callback(err); }
groups = ld.reduce(groups, function (memo, val, key) {
key = key.substr(GPREFIX.length);
memo[key] = val;
return memo;
}, {});
callback(null, groups);
}
);
};
/**
* ### set
*
......
......@@ -304,6 +304,27 @@ module.exports = (function () {
});
};
pad.getBookmarkedPadsByUser = function(user, callback) {
if (!ld.isObject(user) || !ld.isArray(user.bookmarks.pads)) {
throw new TypeError('BACKEND.ERROR.TYPE.USER_INVALID');
}
if (!ld.isFunction(callback)) {
throw new TypeError('BACKEND.ERROR.TYPE.CALLBACK_FN');
}
storage.fn.getKeys(
ld.map(user.bookmarks.pads, function (p) { return PPREFIX + p; }),
function (err, pads) {
if (err) { return callback(err); }
pads = ld.reduce(pads, function (memo, val, key) {
key = key.substr(PPREFIX.length);
memo[key] = val;
return memo;
}, {});
callback(null, pads);
}
);
};
/**
* ## Helpers functions
*
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment