Skip to content

Commit

Permalink
📁 Implement #2766 (#2767)
Browse files Browse the repository at this point in the history
* Fix wrong feedback when no secret is given for apps

* Temporary fix for onlyoffice on canary

* Fix edit with feature
  • Loading branch information
RomaricMourgues authored Mar 1, 2023
1 parent 8577d43 commit f39bad5
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export class ApplicationsApiController {
request: FastifyRequest<{ Body: ApplicationLoginRequest }>,
): Promise<ResourceGetResponse<ApplicationLoginResponse>> {
const context = getExecutionContext(request);

if (!request.body.id || !request.body.secret) {
throw CrudException.forbidden("Application not found");
}

const app = await gr.services.applications.marketplaceApps.get(
{
id: request.body.id,
Expand All @@ -33,7 +38,7 @@ export class ApplicationsApiController {
throw CrudException.forbidden("Application not found");
}

if (app.api.private_key !== request.body.secret) {
if (!app.api.private_key || app.api.private_key !== request.body.secret) {
throw CrudException.forbidden("Secret key is not valid");
}

Expand Down
57 changes: 57 additions & 0 deletions twake/frontend/src/app/features/applications/hooks/temp-fix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Application } from '../types/application';

export const replaceOnlyOfficeForCanary = (applications: Application[]) => {
return applications.map(application => {
if (
application?.identity?.code === 'only_office' &&
!application.display?.twake?.files?.editor?.preview_url?.includes('plugins.twake.app')
) {
return {
...application,
display: {
twake: {
version: 1,
files: {
editor: {
preview_url: 'https://plugins.twake.app/plugins/onlyoffice/?preview=1',
edition_url: 'https://plugins.twake.app/plugins/onlyoffice/',
empty_files: [
{
url: 'https://plugins.twake.app/plugins/onlyoffice/assets/empty.docx',
filename: 'Untitled.docx',
name: 'ONLYOFFICE Word Document',
},
{
url: 'https://plugins.twake.app/plugins/onlyoffice/assets/empty.xlsx',
filename: 'Untitled.xlsx',
name: 'ONLYOFFICE Excel Document',
},
{
url: 'https://plugins.twake.app/plugins/onlyoffice/assets/empty.pptx',
filename: 'Untitled.pptx',
name: 'ONLYOFFICE PowerPoint Document',
},
],
extensions: [
'xlsx',
'pptx',
'docx',
'xls',
'ppt',
'doc',
'odt',
'ods',
'odp',
'txt',
'html',
'csv',
],
},
},
},
},
};
}
return application;
});
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useRef, useState } from 'react';
import { Application } from 'app/features/applications/types/application';
import ApplicationsAPIClient from '../api/applications-api-client';
import { replaceOnlyOfficeForCanary } from './temp-fix';

export function useApplications() {
const [loading, setLoading] = useState<boolean>(true);
Expand All @@ -20,7 +21,7 @@ export function useApplications() {
}, []);

return {
applications,
applications: applications && replaceOnlyOfficeForCanary(applications),
loading,
search,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LoadingState } from 'app/features/global/state/atoms/Loading';
import useRouterWorkspace from 'app/features/router/hooks/use-router-workspace';
import { useGlobalEffect } from 'app/features/global/hooks/use-global-effect';
import useRouterCompany from 'app/features/router/hooks/use-router-company';
import { replaceOnlyOfficeForCanary } from './temp-fix';

const logger = Logger.getLogger('useApplications');
/**
Expand Down Expand Up @@ -92,7 +93,7 @@ export function useCompanyApplications(companyId = '') {
const isInstalled = (applicationId: string) => (get(applicationId) ? true : false);

return {
applications,
applications: replaceOnlyOfficeForCanary(applications),
get,
loading,
add,
Expand Down
2 changes: 1 addition & 1 deletion twake/frontend/src/app/features/viewer/hooks/use-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FileUploadService from 'app/features/files/services/file-upload-service';
import { LoadingState } from 'app/features/global/state/atoms/Loading';

export const FileViewerState = atom<{
file: null | { company_id?: string; message_id?: string; id?: string };
file: null | { company_id?: string; message_id?: string; id?: string; drive_id?: string };
details?: MessageFileDetails;
loading: boolean;
}>({
Expand Down
9 changes: 4 additions & 5 deletions twake/frontend/src/app/views/applications/viewer/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import VideoControls from './videos/controls';
import PdfControls from './pdf/controls';
import ArchiveControls from './archive/controls';
import CodeControls from './code/controls';
import OtherControls from './other/controls';

type PropsType = {
type: string;
}
};

export default ({ type }: PropsType) => {

if (!type) {
return <></>;
}
Expand All @@ -34,10 +34,9 @@ export default ({ type }: PropsType) => {
return <ArchiveControls />;
}

/* Uncomment after https://github.com/linagora/Twake/issues/2453 is done
if (type) {
return <OtherControls name={name} />;
}*/
return <OtherControls />;
}

return <></>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,15 @@ export default (): React.ReactElement => {
switch (type) {
case 'image':
return <ImageDisplay loading={loading} setLoading={setLoading} download={download} />;

case 'video':
case 'audio':
return <VideoDisplay download={download} />;

case 'code':
return <CodeDisplay download={download} name={name} />;

case 'archive':
return <ArchiveDisplay download={download} name={name} />;

case 'pdf':
return <PdfDisplay download={download} name={name} />;

default:
return <OtherDisplay download={download} name={name} id={id} />;
}
Expand Down
29 changes: 17 additions & 12 deletions twake/frontend/src/app/views/applications/viewer/other/controls.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Button } from 'app/atoms/button/button';
import LockedOnlyOfficePopup from 'app/components/locked-features-components/locked-only-office-popup/locked-only-office-popup';
import MenuManager from 'components/menus/menus-manager';
import ModalManager from 'app/components/modal/modal-manager';
import Languages from 'app/features/global/services/languages-service';
import { useDrivePreview } from 'app/features/drive/hooks/use-drive-preview';
import FeatureTogglesService, {
FeatureNames,
} from 'app/features/global/services/feature-toggles-service';
import Languages from 'app/features/global/services/languages-service';
import MenuManager from 'components/menus/menus-manager';
import { useEditors } from './editors-service';
import useRouterCompany from 'app/features/router/hooks/use-router-company';
import AccessRightsService from 'app/features/workspace-members/services/workspace-members-access-rights-service';

export default (props: { name: string }) => {
const extension = props.name.split('.').pop();
export default (props: {}) => {
const { status } = useDrivePreview();
const extension = status?.item?.name?.split('.').pop();
const { candidates, openFile } = useEditors(extension || '');

const companyId = useRouterCompany();
const previewOnly = AccessRightsService.getCompanyLevel(companyId) !== 'guest';

if (previewOnly) {
if (!['manage', 'write'].includes(status?.details?.access || '') || !status?.details) {
return <></>;
}

Expand All @@ -30,7 +27,11 @@ export default (props: { name: string }) => {
theme="dark"
onClick={() => {
if (FeatureTogglesService.isActiveFeatureName(FeatureNames.EDIT_FILES)) {
openFile(candidates[0]);
openFile(
candidates[0].app,
status.details?.versions?.[0]?.id || '',
status.details?.item.id || '',
);
} else {
ModalManager.open(
<LockedOnlyOfficePopup />,
Expand Down Expand Up @@ -60,7 +61,11 @@ export default (props: { name: string }) => {
type: 'menu',
text: editor?.app?.identity?.name,
onClick: () => {
openFile(editor);
openFile(
editor.app,
status.details?.versions?.[0]?.id || '',
status.details?.item.id || '',
);
},
};
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,29 @@ export const useEditors = (
}
});

const openFile = (app: any) => {
const openFile = (app: any, fileId: string, driveId: string) => {
if (app.url && app.is_url_file) {
window.open(app.url);
return;
}
const documentId = ''; //TODO

window.open(getFileUrl(app.display?.twake?.files?.editor?.edition_url, documentId));
window.open(getFileUrl(app.display?.twake?.files?.editor?.edition_url, fileId, driveId));
};

const getPreviewUrl = (documentId: string): string => {
return getFileUrl(preview_candidate?.[0]?.url as string, documentId);
};

const getFileUrl = (url: string, file_id: string): string => {
const getFileUrl = (url: string, file_id: string, drive_id?: string): string => {
const jwt = jwtStorageService.getJWT();

if (!url) return '';

return `${url}${url.indexOf('?') > 0 ? '&' : '?'}token=${jwt}&workspace_id=${
workspace?.id
}&company_id=${workspace?.company_id}&file_id=${file_id}`;
}&company_id=${workspace?.company_id}&file_file_id=${file_id}${
drive_id ? `&drive_id=${drive_id}` : ''
}`;
};

return { candidates: editor_candidate, openFile, getPreviewUrl };
Expand Down

0 comments on commit f39bad5

Please sign in to comment.