The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/app/api/server/lib/rooms.js

156 lines
3.8 KiB

import { hasPermissionAsync, hasAtLeastOnePermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { Rooms } from '../../../models/server/raw';
import { Subscriptions } from '../../../models/server';
import { adminFields } from '../../../../lib/rooms/adminFields';
export async function findAdminRooms({ uid, filter, types = [], pagination: { offset, count, sort } }) {
if (!(await hasPermissionAsync(uid, 'view-room-administration'))) {
throw new Error('error-not-authorized');
}
const name = filter && filter.trim();
const discussion = types && types.includes('discussions');
const includeTeams = types && types.includes('teams');
const showOnlyTeams = types.length === 1 && types.includes('teams');
const typesToRemove = ['discussions', 'teams'];
const showTypes = Array.isArray(types) ? types.filter((type) => !typesToRemove.includes(type)) : [];
const options = {
fields: adminFields,
sort: sort || { default: -1, name: 1 },
skip: offset,
limit: count,
};
let cursor;
if (name && showTypes.length) {
cursor = Rooms.findByNameContainingAndTypes(name, showTypes, discussion, includeTeams, showOnlyTeams, options);
} else if (showTypes.length) {
cursor = Rooms.findByTypes(showTypes, discussion, includeTeams, showOnlyTeams, options);
} else {
cursor = Rooms.findByNameContaining(name, discussion, includeTeams, showOnlyTeams, options);
}
const total = await cursor.count();
const rooms = await cursor.toArray();
return {
rooms,
count: rooms.length,
offset,
total,
};
}
export async function findAdminRoom({ uid, rid }) {
if (!(await hasPermissionAsync(uid, 'view-room-administration'))) {
throw new Error('error-not-authorized');
}
return Rooms.findOneById(rid, { fields: adminFields });
}
export async function findChannelAndPrivateAutocomplete({ uid, selector }) {
const options = {
fields: {
_id: 1,
fname: 1,
name: 1,
t: 1,
avatarETag: 1,
},
limit: 10,
sort: {
name: 1,
},
};
const userRoomsIds = Subscriptions.cachedFindByUserId(uid, { fields: { rid: 1 } })
.fetch()
.map((item) => item.rid);
const rooms = await Rooms.findRoomsWithoutDiscussionsByRoomIds(selector.name, userRoomsIds, options).toArray();
return {
items: rooms,
};
}
export async function findAdminRoomsAutocomplete({ uid, selector }) {
if (!(await hasAtLeastOnePermissionAsync(uid, ['view-room-administration', 'can-audit']))) {
throw new Error('error-not-authorized');
}
const options = {
fields: {
_id: 1,
fname: 1,
name: 1,
t: 1,
avatarETag: 1,
},
limit: 10,
sort: {
name: 1,
},
};
const rooms = await Rooms.findRoomsByNameOrFnameStarting(selector.name, options).toArray();
return {
items: rooms,
};
}
export async function findChannelAndPrivateAutocompleteWithPagination({ uid, selector, pagination: { offset, count, sort } }) {
const userRoomsIds = Subscriptions.cachedFindByUserId(uid, { fields: { rid: 1 } })
.fetch()
.map((item) => item.rid);
const options = {
fields: {
_id: 1,
fname: 1,
name: 1,
t: 1,
avatarETag: 1,
},
sort: sort || { name: 1 },
skip: offset,
limit: count,
};
const cursor = await Rooms.findRoomsWithoutDiscussionsByRoomIds(selector.name, userRoomsIds, options);
const total = await cursor.count();
const rooms = await cursor.toArray();
return {
items: rooms,
total,
};
}
export async function findRoomsAvailableForTeams({ uid, name }) {
const options = {
fields: {
_id: 1,
fname: 1,
name: 1,
t: 1,
avatarETag: 1,
},
limit: 10,
sort: {
name: 1,
},
};
const userRooms = Subscriptions.findByUserIdAndRoles(uid, ['owner'], { fields: { rid: 1 } })
.fetch()
.map((item) => item.rid);
const rooms = await Rooms.findChannelAndGroupListWithoutTeamsByNameStartingByOwner(uid, name, userRooms, options).toArray();
return {
items: rooms,
};
}