diff --git a/scripts/api/user.ts b/scripts/api/user.ts index 7f35352f55..1f2eff49ed 100644 --- a/scripts/api/user.ts +++ b/scripts/api/user.ts @@ -6,6 +6,12 @@ function hasPrivilege(privilege: string): boolean { return privileges.userHasPrivileges({[privilege]: 1}); } +function getCurrentUserId(): string { + const session = ng.get('session'); + + return session?.identity?._id; +} + function isLoggedIn() { const session = ng.get('session'); @@ -15,4 +21,5 @@ function isLoggedIn() { export const user = { hasPrivilege, isLoggedIn, + getCurrentUserId, }; diff --git a/scripts/core/get-superdesk-api-implementation.tsx b/scripts/core/get-superdesk-api-implementation.tsx index cb644addfa..87d36bf93b 100644 --- a/scripts/core/get-superdesk-api-implementation.tsx +++ b/scripts/core/get-superdesk-api-implementation.tsx @@ -476,7 +476,7 @@ export function getSuperdeskApiImplementation( getToken: () => session.token, getCurrentUser: () => session.getIdentity(), getSessionId: () => session.sessionId, - getCurrentUserId: () => session.identity._id, + getCurrentUserId: () => sdApi.user.getCurrentUserId(), }, browser: { location: { diff --git a/scripts/core/ui/components/content-create-dropdown/more-templates.tsx b/scripts/core/ui/components/content-create-dropdown/more-templates.tsx index 1bb577b506..1a62d63ca3 100644 --- a/scripts/core/ui/components/content-create-dropdown/more-templates.tsx +++ b/scripts/core/ui/components/content-create-dropdown/more-templates.tsx @@ -6,6 +6,7 @@ import {IRestApiResponse, ITemplate} from 'superdesk-api'; import {httpRequestJsonLocal} from 'core/helpers/network'; import {DropdownOption} from './dropdown-option'; import {nameof} from 'core/helpers/typescript-helpers'; +import {sdApi} from 'api'; interface IProps { onSelect(template: ITemplate): void; @@ -28,19 +29,37 @@ export class MoreTemplates extends React.PureComponent { } fetchData(pageToFetch: number, pageSize: number, abortSignal?: AbortSignal): Promise> { + const templateDesks = nameof('template_desks'); + const currentDeskId = sdApi.desks.getCurrentDeskId(); + + const deskCriteria: any = [ + {[templateDesks]: {$exists: false}}, + {[templateDesks]: []}, + ]; + + if (currentDeskId != null) { + deskCriteria.push({[templateDesks]: {$in: [currentDeskId]}}); + } + + const criteria = {$or: [{$or: deskCriteria}, {user: sdApi.user.getCurrentUserId()}]}; + const templateName = nameof('template_name'); + const where = {$and: [criteria]}; + + if (this.state.searchString.length < 1) { + where[templateName] = { + $regex: this.state.searchString, + $options: '-i', + }; + } + return httpRequestJsonLocal>({ method: 'GET', path: '/content_templates', urlParams: { max_results: pageSize, page: pageToFetch, - sort: nameof('template_name'), - where: this.state.searchString.length < 1 ? undefined : { - [nameof('template_name')]: { - $regex: this.state.searchString, - $options: '-i', - }, - }, + sort: templateName, + where: where, }, abortSignal, });