group creation and deletion

v1.18.x
Julian Lam 12 years ago
parent f2b8813fb9
commit 85ac4aac94

@ -142,6 +142,30 @@
}
}
}
.groups {
li {
list-style-type: none;
margin-bottom: 1em;
padding: 1em;
.zebra;
h2 {
margin-top: 0;
font-size: 26px;
}
}
.members {
li {
display: inline-block;
img {
width: 32px;
}
}
}
}
}

@ -0,0 +1,70 @@
$(document).ready(function() {
var createEl = document.getElementById('create'),
createModal = $('#create-modal'),
createSubmitBtn = document.getElementById('create-modal-go'),
createNameEl = $('#create-group-name'),
detailsModal = $('#group-details-modal'),
listEl = $('#groups-list');
createEl.addEventListener('click', function() {
createModal.modal('show');
setTimeout(function() {
createNameEl.focus();
}, 250);
}, false);
createSubmitBtn.addEventListener('click', function() {
var submitObj = {
name: createNameEl.val(),
description: $('#create-group-desc').val()
},
errorEl = $('#create-modal-error'),
errorText;
socket.emit('api:groups.create', submitObj, function(err, data) {
if (err) {
switch(err) {
case 'group-exists':
errorText = '<strong>Please choose another name</strong><p>There seems to be a group with this name already.</p>';
break;
case 'name-too-short':
errorText = '<strong>Please specify a grou name</strong><p>A group name is required for administrative purposes.</p>';
break;
default:
errorText = '<strong>Uh-Oh</strong><p>There was a problem creating your group. Please try again later!</p>';
break;
}
errorEl.html(errorText).removeClass('hide');
} else {
createModal.modal('hide');
errorEl.addClass('hide');
createNameEl.val('');
ajaxify.go('admin/groups');
}
});
});
listEl.on('click', 'button[data-action]', function() {
var action = this.getAttribute('data-action'),
gid = $(this).parents('li[data-gid]').attr('data-gid');
switch(action) {
case 'delete':
bootbox.confirm('Are you sure you wish to delete this group?', function(confirm) {
if (confirm) {
socket.emit('api:groups.delete', gid, function(err, data) {
if (data === 'OK') ajaxify.go('admin/groups');
});
}
});
break;
case 'members':
socket.emit('api:groups.get', gid, function(err, groupObj) {
console.log(groupObj);
detailsModal.modal('show');
});
break;
}
});
});

@ -0,0 +1,100 @@
<h1>Groups</h1>
<hr />
<div class="groups">
<ul id="groups-list">
<!-- BEGIN groups -->
<li data-gid="{groups.gid}">
<div class="row">
<div class="col-lg-8">
<h2>{groups.name}</h2>
<p>{groups.description}</p>
<div class="btn-group">
<button class="btn btn-default" data-action="members">Members</button>
<button class="btn btn-danger" data-action="delete">Delete Group</button>
</div>
</div>
<div class="col-lg-4">
<ul class="pull-right members">
<!-- BEGIN members -->
<li title="{groups.members.username}"><img src="http://www.gravatar.com/avatar/e1565907855fffe8433c31671a49f177?size=128&default=identicon&rating=pg" /></li>
<!-- END members -->
</ul>
</div>
</div>
</li>
<!-- END groups -->
</ul>
<div class="text-center">
<button class="btn btn-primary" id="create">New Group</button>
</div>
</div>
<script type="text/javascript" src="{relative_path}/src/forum/admin/groups.js"></script>
<div class="modal fade" id="create-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">Create Group</h4>
</div>
<div class="modal-body">
<div class="alert alert-danger hide" id="create-modal-error"></div>
<form>
<div class="form-group">
<label for="group-name">Group Name</label>
<input type="text" class="form-control" id="create-group-name" placeholder="Group Name" />
</div>
<div class="form-group">
<label for="group-name">Description</label>
<input type="text" class="form-control" id="create-group-desc" placeholder="A short description about your group" />
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="create-modal-go">Create</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="group-details-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">Manage Group</h4>
</div>
<div class="modal-body">
<div class="alert alert-danger hide" id="create-modal-error"></div>
<form>
<div class="form-group">
<label for="group-name">Group Name</label>
<input type="text" class="form-control" id="create-group-name" placeholder="Group Name" />
</div>
<div class="form-group">
<label for="group-name">Description</label>
<input type="text" class="form-control" id="create-group-desc" placeholder="A short description about your group" />
</div>
<div class="form-group">
<label>Members</label>
<ul class="members" id="group-details-members"></ul>
</div>
<div class="form-group">
<label for="add-member">Add User to Group</label>
<input type="text" class="form-control" id="group-details-search" placeholder="Search Users" />
<ul class="members" id="group-details-search-results">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="create-modal-go">Create</button>
</div>
</div>
</div>
</div>

@ -74,6 +74,7 @@
<li class='active'><a href='{relative_path}/admin/index'><i class='icon-home'></i> Home</a></li>
<li class=''><a href='{relative_path}/admin/categories/active'><i class='icon-folder-close-alt'></i> Categories</a></li>
<li class=''><a href='{relative_path}/admin/users/latest'><i class='icon-user'></i> Users</a></li>
<li class=""><a href="{relative_path}/admin/groups"><i class="icon-group"></i> Groups</a></li>
<li class=''><a href='{relative_path}/admin/topics'><i class='icon-book'></i> Topics</a></li>
<li class=''><a href='{relative_path}/admin/themes'><i class='icon-th'></i> Themes</a></li>
<li class=''><a href='{relative_path}/admin/plugins'><i class='icon-code-fork'></i> Plugins</a></li>

@ -13,6 +13,7 @@
"admin/facebook[^]*": "admin/facebook",
"admin/gplus[^]*": "admin/gplus",
"admin/motd/?$": "admin/motd",
"admin/groups/?$": "admin/groups",
"install/?$": "install/mail",
"install/mail/?": "install/mail",
"install/social/?": "install/social",

@ -1,12 +1,19 @@
var async = require('async'),
User = require('./user'),
Groups = {
list: function(callback) {
list: function(options, callback) {
RDB.hvals('group:gid', function(err, gids) {
if (gids.length > 0) {
async.each(gids, function(gid, next) {
Groups.get(gid, next);
}, callback);
async.map(gids, function(gid, next) {
Groups.get(gid, {
expand: options.expand
}, next);
}, function(err, groups) {
callback(err, groups.filter(function(group) {
if (group.deleted === '1') return false;
else return true;
}));
});
} else callback(null, []);
});
},
@ -33,10 +40,10 @@ var async = require('async'),
}, function(err, results) {
if (err) return callback(err);
results[0].count = results[1].length;
results[0].users = results[1];
results.base.count = results.users.length;
results.base.members = results.users;
callback(err, results[0]);
callback(err, results.base);
});
},
getGidFromName: function(name, callback) {
@ -49,26 +56,28 @@ var async = require('async'),
RDB.hexists('group:gid', name, callback);
},
create: function(name, description, callback) {
if (name.length === 0) return callback(new Error('name-too-short'));
Groups.exists(name, function(err, exists) {
if (!exists) {
RDB.incr('next_gid', function(err, gid) {
RDB.multi()
.hset('group:gid', name, gid)
.hset('gid:' + gid, {
.hmset('gid:' + gid, {
gid: gid,
name: name,
description: description,
deleted: '0'
})
.exec(function(err) {
callback(err, gid);
Groups.get(gid, {}, callback);
});
});
}
} else callback(new Error('group-exists'))
});
},
destroy: function(gid, callback) {
RDB.hset('gid:' + gid, deleted, '1', callback);
RDB.hset('gid:' + gid, 'deleted', '1', callback);
},
join: function(gid, uid, callback) {
RDB.sadd('gid:' + gid + ':members', uid, callback);

@ -1,5 +1,6 @@
var user = require('./../user.js'),
Groups = require('../groups'),
topics = require('./../topics.js'),
RDB = require('./../redis.js'),
pkg = require('./../../package.json'),
@ -27,7 +28,7 @@ var user = require('./../user.js'),
(function() {
var routes = [
'categories/active', 'categories/disabled', 'users', 'topics', 'settings', 'themes',
'twitter', 'facebook', 'gplus', 'redis', 'motd',
'twitter', 'facebook', 'gplus', 'redis', 'motd', 'groups',
'users/latest', 'users/sort-posts', 'users/sort-reputation',
'users/search', 'plugins'
];
@ -189,6 +190,15 @@ var user = require('./../user.js'),
res.json(200, {});
});
app.get('/api/admin/groups', function(req, res) {
Groups.list({
expand: true
}, function(err, groups) {
res.json(200, {
groups: groups
});
});
});
};

@ -3,6 +3,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
cookie = require('cookie'),
express = require('express'),
user = require('./user.js'),
Groups = require('./groups'),
posts = require('./posts.js'),
favourites = require('./favourites.js'),
utils = require('../public/src/utils.js'),
@ -817,6 +818,30 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
callback(title, numNotifications);
});
});
/*
GROUPS
*/
socket.on('api:groups.create', function(data, callback) {
Groups.create(data.name, data.description, function(err, groupObj) {
callback(err ? err.message : null, groupObj || undefined);
});
});
socket.on('api:groups.delete', function(gid, callback) {
Groups.destroy(gid, function(err) {
callback(err ? err.message : null, err ? null : 'OK');
});
});
socket.on('api:groups.get', function(gid, callback) {
Groups.get(gid, {
expand: true
}, function(err, groupObj) {
callback(err ? err.message : null, groupObj || undefined);
});
});
});
}(SocketIO));

Loading…
Cancel
Save