Commit 88a28850 authored by Eric Eastwood's avatar Eric Eastwood

Add groupService.deleteGroup() and util script to delete community

See gitlab-org/gitter/webapp!1166

Adjust `groupService.findByUri()` and `groupService.findById()` to NOT be lean by default (mimics `troupeService`)
parent 894da27b
# 19.5.0 - *upcoming*
Developer facing:
- Add `scripts/utils/delete-group.js` util script to delete a group/community, https://gitlab.com/gitlab-org/gitter/webapp/merge_requests/1166
# 19.4.0 - 2018-6-11
- Revoke desktop app v2, v3 to prevent token leaks, https://gitlab.com/gitlab-org/gitter/webapp/merge_requests/1180
......
......@@ -21,9 +21,8 @@ var groupFavouritesCore = require('./group-favourites-core');
/**
* Find a group given an id
*/
function findById(groupId) {
return Group.findById(groupId)
.lean()
function findById(groupId, opts = {}) {
return Group.findById(groupId, {}, { lean: opts.lean || false })
.exec();
}
......@@ -34,10 +33,17 @@ function findByIds(ids) {
/**
* Find a group given a URI
*/
function findByUri(uri) {
function findByUri(uri, opts = {}) {
assert(uri, 'uri required');
return Group.findOne({ lcUri: uri.toLowerCase() })
.lean()
return Group.findOne({ lcUri: uri.toLowerCase() }, {}, { lean: opts.lean || false })
.exec();
}
// Find all rooms in a given group
function findRoomsInGroup(groupId) {
return Troupe.find({
groupId: groupId
})
.exec();
}
......@@ -272,12 +278,34 @@ function updateFavourite(userId, groupId, favouritePosition) {
});
}
function deleteGroup(group) {
assert(group, 'group is required');
// Avoid the circular dependency with the rooms module using `gitter-web-groups`
// This will cause an empty object import in the rooms module
var roomService = require('gitter-web-rooms');
return findRoomsInGroup(group.get('id'))
.then((rooms) => {
return rooms.reduce((promiseChain, room) => {
return promiseChain.then(() => {
return roomService.deleteRoom(room);
});
}, Promise.resolve());
})
.then(() => {
return group.remove();
});
}
module.exports = {
findByUri: Promise.method(findByUri),
findById: Promise.method(findById),
findByIds: findByIds,
findRoomsInGroup: findRoomsInGroup,
createGroup: Promise.method(createGroup),
deleteGroup: deleteGroup,
findRoomsIdForGroup: Promise.method(findRoomsIdForGroup),
setAvatarForGroup: setAvatarForGroup,
setForumForGroup: setForumForGroup,
......
......@@ -185,7 +185,7 @@ describe('group-service', function() {
});
it('should find a group', function() {
return groupService.findById(fixture.group1._id)
return groupService.findById(fixture.group1._id, { lean: true })
.then(function(group) {
assert.strictEqual(group.name, fixture.group1.name);
assert.strictEqual(group.uri, fixture.group1.uri);
......@@ -320,7 +320,38 @@ describe('group-service', function() {
});
});
});
});
describe('findRoomsInGroup', () => {
var fixture = fixtureLoader.setup({
group1: {},
troupe1: { group: 'group1', security: 'PUBLIC' },
troupe2: { group: 'group1', security: 'PUBLIC' },
troupe3: { group: 'group1', security: 'PRIVATE' },
troupe4: { group: 'group1', security: 'PRIVATE' },
});
it('should find rooms in group', () => {
return groupService.findRoomsInGroup(fixture.group1.get('id'))
.then(function(results) {
assert.strictEqual(results.length, 4);
});
});
});
describe('deleteGroup', () => {
var fixture = fixtureLoader.setup({
group1: {}
});
it('should delete group', () => {
return groupService.deleteGroup(fixture.group1)
.then(function() {
return groupService.findById(fixture.group1.get('id'));
})
.then((groupResult) => {
assert.strictEqual(groupResult, null);
});
});
});
})
......@@ -88,7 +88,7 @@ function findContextForUri(user, uri, options) {
if (uri.indexOf('/') < 0 && resolvedTroupe.groupId) {
debug('Redirecting on ORG room permission denied');
return groupService.findById(resolvedTroupe.groupId)
return groupService.findById(resolvedTroupe.groupId, { lean: true })
.then(function(group) {
if (group && group.homeUri) {
var err = new StatusError(301);
......@@ -201,7 +201,7 @@ function findContextForGroup(user, uri, options) {
if (!uri) throw new StatusError(400, 'uri required');
return groupService.findByUri(uri)
return groupService.findByUri(uri, { lean: true })
.then(function (group) {
if (!group) throw new StatusError(404);
......
......@@ -47,7 +47,7 @@ function resolveFromLookup(uriLookup, userId) {
if (uriLookup.groupId) {
/* The uri is for a group */
return groupService.findById(uriLookup.groupId)
return groupService.findById(uriLookup.groupId, { lean: true })
.then(function(group) {
if (!group) return null;
......
'use strict';
var assert = require('assert');
var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
var uriResolver = require('../lib/uri-resolver');
describe('uri-resolver', function() {
var fixture = fixtureLoader.setup({
user1: {},
group1: {},
troupe1: { group: 'group1', security: 'PUBLIC' },
});
describe('uriResolver', () => {
it('should resolve user', () => {
return uriResolver(fixture.user1.get('id'), fixture.user1.get('username'))
.then(function(result) {
assert.strictEqual(result.user.username, fixture.user1.get('username'));
});
});
it('should resolve room', () => {
return uriResolver(fixture.user1.get('id'), fixture.troupe1.get('uri'))
.then(function(result) {
assert.strictEqual(result.room.uri, fixture.troupe1.get('uri'));
});
});
it('should resolve group', () => {
return uriResolver(fixture.user1.get('id'), fixture.group1.get('uri'))
.then(function(result) {
assert.strictEqual(result.group.uri, fixture.group1.get('uri'));
});
});
});
});
......@@ -21,7 +21,7 @@ var ForumWithPolicyService = require('../../server/services/forum-with-policy-se
function getGroupWithPolicyService(username, groupUri) {
return Promise.join(
userService.findByUsername(username),
groupService.findByUri(groupUri))
groupService.findByUri(groupUri), { lean: true })
.bind({})
.spread(function(user, group) {
if (!user) throw new StatusError(404, 'User not found.');
......@@ -40,7 +40,7 @@ function getGroupWithPolicyService(username, groupUri) {
function getForumWithPolicyService(username, groupUri) {
return Promise.join(
userService.findByUsername(username),
groupService.findByUri(groupUri))
groupService.findByUri(groupUri), { lean: true })
.bind({})
.spread(function(user, group) {
if (!user) throw new StatusError(404, 'User not found.');
......@@ -71,7 +71,7 @@ function getUser(username) {
}
function getForum(uri) {
return groupService.findByUri(uri)
return groupService.findByUri(uri, { lean: true })
.then(function(group) {
if (!group) throw new StatusError(404, 'Group not found.');
if (!group.forumId) throw new StatusError(404, 'The group has no forum.');
......
......@@ -51,7 +51,7 @@ Importer.prototype = {
if (user) return user;
return userService.findByUsername('suprememoocow');
}),
groupService.findByUri(groupUri))
groupService.findByUri(groupUri, { lean: true }))
.bind({})
.spread(function(user, group) {
if (!user) throw new StatusError(404, 'User not found.');
......
#!/usr/bin/env node
/*jslint node: true */
'use strict';
const Promise = require('bluebird');
const shutdown = require('shutdown');
const readline = require('readline');
const groupService = require('gitter-web-groups');
require('../../server/event-listeners').install();
var opts = require('yargs')
.option('uri', {
alias: 'u',
required: true,
description: 'URI of group to remove',
string: true
})
.help('help')
.alias('help', 'h')
.argv;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
groupService.findByUri(opts.uri)
.then((group) => {
if (!group) {
throw new Error(`Group with URI ${opts.uri} does not exist`);
}
return new Promise(function(resolve, reject) {
rl.question(`Are you sure you want to delete the ${group.uri} group and the rooms in it? (yes/no)`, function(answer) {
rl.close();
console.log(`Answered: ${answer}`);
if(answer === 'yes') {
resolve(group);
} else {
reject(new Error('Answered no'));
}
});
});
})
.then(function(group) {
return groupService.deleteGroup(group);
})
.then(function() {
console.log('Group and rooms deleted!');
shutdown.shutdownGracefully();
})
.catch(function(err) {
console.error(err);
shutdown.shutdownGracefully(1);
})
.done();
......@@ -47,7 +47,7 @@ var opts = yargs
function getParams() {
var params = { type: opts.type };
if (opts.type === 'avatar') {
return groupService.findByUri(opts.group_uri)
return groupService.findByUri(opts.group_uri, { lean: true })
.then(function(group) {
if (!group) throw new StatusError(404, 'Group not found.')
params.group_id = group._id.toString();
......
......@@ -87,7 +87,7 @@ function handleUploadToGroup(transloadit, metadata) {
var group, user, policy;
return Promise.join(
groupService.findById(metadata.group_id),
groupService.findById(metadata.group_id, { lean: true }),
userService.findById(metadata.user_id))
.spread(function(_group, _user) {
group = _group;
......
......@@ -164,7 +164,7 @@ module.exports = {
return Promise.all(promises)
.then(function() {
// reload the group so we get the updated forumId, etc.
return groupService.findById(group._id);
return groupService.findById(group._id, { lean: true });
})
.then(function(group) {
var strategy = new restSerializer.GroupStrategy({
......@@ -200,7 +200,7 @@ module.exports = {
.then(function(access) {
if (!access) return null;
return groupService.findById(id);
return groupService.findById(id, { lean: true });
});
},
......
......@@ -11,7 +11,7 @@ module.exports = {
if (!req.user) return [];
var userId = req.user._id;
return groupService.findByUri(req.params.orgName)
return groupService.findByUri(req.params.orgName, { lean: true })
.then(function(group) {
if (!group) return [];
......
......@@ -69,7 +69,7 @@ module.exports = {
.then(function(access) {
if (!access) return null;
return groupService.findById(id);
return groupService.findById(id, { lean: true });
});
},
......
......@@ -19,7 +19,7 @@ var getLeftMenuViewData = require('./get-left-menu-view-data');
var generateUserThemeSnapshot = require('../../snapshots/user-theme-snapshot');
function getLeftMenuForumGroupInfo(leftMenuGroupId) {
return groupService.findById(leftMenuGroupId)
return groupService.findById(leftMenuGroupId, { lean: true })
.then(function(group) {
var forumId = group && group.forumId;
......
......@@ -36,7 +36,7 @@ function renderForum(req, res, next, options) {
return Promise.props({
context: contextGenerator.generateBasicContext(req),
group: groupService.findByUri(groupUri),
group: groupService.findByUri(groupUri, { lean: true }),
userThemeSnapshot: generateUserThemeSnapshot(req),
})
.bind({
......@@ -128,7 +128,7 @@ function renderTopic(req, res, next) {
return Promise.props({
context: contextGenerator.generateBasicContext(req),
group: groupService.findByUri(groupUri),
group: groupService.findByUri(groupUri, { lean: true }),
userThemeSnapshot: generateUserThemeSnapshot(req)
})
.bind({
......
......@@ -86,7 +86,7 @@ describe('integration tests #slow', function() {
})
.then(function() {
// load the group again so we can see if the url changed
return groupService.findById(fixture.group1.id);
return groupService.findById(fixture.group1.id, { lean: true });
})
.then(function(group) {
assert.strictEqual(group.avatarUrl, transloaditUrl);
......
......@@ -345,7 +345,7 @@ describe('group-with-policy-service #slow', function() {
this.forum = forum;
// load the group again so we can see if it actually set the group's forum
return groupService.findById(fixture.group1._id);
return groupService.findById(fixture.group1._id, { lean: true });
})
.then(function(group) {
// did it set the group's forumId?
......@@ -357,7 +357,7 @@ describe('group-with-policy-service #slow', function() {
return group2WithPolicyService.createForum()
.then(function() {
// reload the group so we get the updated group.forumId
return groupService.findById(fixture.group2._id);
return groupService.findById(fixture.group2._id, { lean: true });
})
.then(function(group) {
var updatedGroupWithPolicyService = new GroupWithPolicyService(group, fixture.user1, isAdminPolicy);
......
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