Skip to content

Commit

Permalink
📁 Drive fix QA issues (#2764)
Browse files Browse the repository at this point in the history
* Partial fixes on #2763

* Fix preview with OnlyOffice

* Fix access level and onlyoffice edition
  • Loading branch information
RomaricMourgues authored Feb 28, 2023
1 parent 3de6bc0 commit 28ed51f
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 93 deletions.
49 changes: 24 additions & 25 deletions twake/backend/node/src/services/documents/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,35 +378,34 @@ export const getAccessLevel = async (
if (itemToken === publicToken) return itemLevel;
}

//From there a user must be logged in
if (!context.user.id) return "none";

const accessEntities = item.access_info.entities || [];
const otherLevels = [];

//Users
const matchingUser = accessEntities.find(a => a.type === "user" && a.id === context.user?.id);
if (matchingUser) return matchingUser.level;

//Channels
if (context.twake_tab_token) {
try {
const [channelId] = context.twake_tab_token.split("+"); //First item will be the channel id
const matchingChannel = accessEntities.find(
a => a.type === "channel" && a.id === channelId,
);
if (matchingChannel) return matchingChannel.level;
} catch (e) {
console.log(e);
//From there a user must be logged in
if (context?.user?.id) {
//Users
const matchingUser = accessEntities.find(a => a.type === "user" && a.id === context.user?.id);
if (matchingUser) return matchingUser.level;

//Channels
if (context.twake_tab_token) {
try {
const [channelId] = context.twake_tab_token.split("+"); //First item will be the channel id
const matchingChannel = accessEntities.find(
a => a.type === "channel" && a.id === channelId,
);
if (matchingChannel) return matchingChannel.level;
} catch (e) {
console.log(e);
}
}
}

const otherLevels = [];

//Companies
const matchingCompany = accessEntities.find(
a => a.type === "company" && a.id === context.company.id,
);
if (matchingCompany) otherLevels.push(matchingCompany.level);
//Companies
const matchingCompany = accessEntities.find(
a => a.type === "company" && a.id === context.company.id,
);
if (matchingCompany) otherLevels.push(matchingCompany.level);
}

//Parent folder
const maxParentFolderLevel =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { useRealtimeRoom } from 'app/features/global/hooks/use-realtime';
import { Application } from 'app/features/applications/types/application';
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';

const logger = Logger.getLogger('useApplications');
/**
Expand All @@ -21,8 +23,8 @@ const logger = Logger.getLogger('useApplications');
* @returns
*/
export function useCompanyApplications(companyId = '') {
const { company } = useCurrentCompany();
companyId = companyId || company?.id || '';
const routerCompanyId = useRouterCompany();
companyId = companyId || routerCompanyId || '';
const workspaceId = useRouterWorkspace();

const [applications, setApplications] = useRecoilState(CompanyApplicationsStateFamily(companyId));
Expand All @@ -42,12 +44,16 @@ export function useCompanyApplications(companyId = '') {
onChangeCompanyApplications(companyId, applications);
}, [applications]);

useEffect(() => {
refresh();
}, [workspaceId]);
useGlobalEffect(
'useCompanyApplications',
() => {
refresh();
},
[workspaceId],
);

const get = (applicationId: string) => {
return applications.find(a => a.id === applicationId);
const get = (applicationId: string): Application | null => {
return applications.find(a => a.id === applicationId) || null;
};

const remove = async (applicationId: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@ export const CompanyApplicationsStateFamily = atomFamily<Application[], string>(

export const fetchCompanyApplications = selectorFamily<Application[], string>({
key: 'fetchCompanyApplications',
get: companyId => async () => {
logger.debug('fetchCompanyApplications', companyId);
return (await CompanyApplicationsAPIClient.list(companyId)) || [];
},
get: companyId => () => [],
});
6 changes: 5 additions & 1 deletion twake/frontend/src/app/features/auth/jwt-storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ class JWTStorage {
}

authenticateCall(callback?: () => void) {
if (this.isAccessExpired() && LoginService.currentUserId) {
if (
this.isAccessExpired() &&
LoginService.currentUserId &&
!document.location.pathname.includes('/shared/')
) {
this.logger.debug('authenticateCall: Updating user because the access token expired');
this.renew()
.then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ export const useDrivePreviewDisplayData = () => {
const name =
status.details?.item.last_version_cache.file_metadata.name || status.details?.item.name || '';
const extension = name.split('.').pop();
const type = fileUploadApiClient.mimeToType(status.details?.item.last_version_cache.file_metadata.mime || '', extension);
const type = fileUploadApiClient.mimeToType(
status.details?.item.last_version_cache.file_metadata.mime || '',
extension,
);
const id = status.details?.item.last_version_cache.file_metadata.external_id || '';
const download = fileUploadService.getDownloadRoute({
companyId: status.item?.company_id || '',
fileId: status.details?.item.last_version_cache.file_metadata.external_id || ''
fileId: status.details?.item.last_version_cache.file_metadata.external_id || '',
});

return { download, id, name, type, extension }
return { download, id, name, type, extension };
};
85 changes: 44 additions & 41 deletions twake/frontend/src/app/features/drive/hooks/use-drive-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,51 +110,54 @@ export const useDriveUpload = () => {
}
};

const uploadFromUrl =
(url: string, name: string, context: { companyId: string; parentId: string }) => () => {
const request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'blob';
request.onload = function () {
try {
const file = new File([request.response], name);
FileUploadService.upload([file], {
context: {
companyId: context.companyId,
parentId: context.parentId,
},
callback: (file, context) => {
if (file) {
create(
{
company_id: context.companyId,
workspace_id: 'drive', //We don't set workspace ID for now
parent_id: context.parentId,
const uploadFromUrl = (
url: string,
name: string,
context: { companyId: string; parentId: string },
) => {
const request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'blob';
request.onload = function () {
try {
const file = new File([request.response], name);
FileUploadService.upload([file], {
context: {
companyId: context.companyId,
parentId: context.parentId,
},
callback: (file, context) => {
if (file) {
create(
{
company_id: context.companyId,
workspace_id: 'drive', //We don't set workspace ID for now
parent_id: context.parentId,
name: file.metadata?.name,
size: file.upload_data?.size,
},
{
provider: 'internal',
application_id: '',
file_metadata: {
name: file.metadata?.name,
size: file.upload_data?.size,
mime: file.metadata?.mime,
thumbnails: file?.thumbnails,
source: 'internal',
external_id: file.id,
},
{
provider: 'internal',
application_id: '',
file_metadata: {
name: file.metadata?.name,
size: file.upload_data?.size,
mime: file.metadata?.mime,
thumbnails: file?.thumbnails,
source: 'internal',
external_id: file.id,
},
},
);
}
},
});
} catch (e) {
ToasterService.error('Error while creating an empty file.');
}
};
request.send();
},
);
}
},
});
} catch (e) {
ToasterService.error('Error while creating an empty file.');
}
};
request.send();
};

return { uploadTree, uploadFromUrl, uploadVersion };
};
6 changes: 4 additions & 2 deletions twake/frontend/src/app/views/applications/drive/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ export default memo(
({
initialParentId,
twakeTabContextToken,
inPublicSharing,
}: {
initialParentId?: string;
twakeTabContextToken?: string;
inPublicSharing?: boolean;
}) => {
const companyId = useRouterCompany();
setTwakeTabToken(twakeTabContextToken || null);
Expand Down Expand Up @@ -68,7 +70,7 @@ export default memo(
useEffect(() => {
setChecked({});
refresh(parentId);
refresh('trash');
if (!inPublicSharing) refresh('trash');
}, [parentId, refresh]);

const openItemModal = useCallback(() => {
Expand Down Expand Up @@ -131,7 +133,7 @@ export default memo(
(loading && !children?.length ? 'opacity-50 ' : '')
}
>
{document.location.origin.includes('canary') && (
{document.location.origin.includes('canary') && access !== 'read' && (
<div className="bg-linear-purple w-full hidden sm:block px-4 py-2 rounded-md">
<Base className=" !text-white">
Welcome to the next version of Twake Drive.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default (props: {
const inputElement = useRef<HTMLInputElement>(null);

useEffect(() => {
props.onChange(users);
if (users.length) props.onChange(users);
}, [users]);

return (
Expand Down
12 changes: 10 additions & 2 deletions twake/frontend/src/app/views/applications/drive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,36 @@ export type EmbedContext = {
export default ({
initialParentId,
context,
inPublicSharing,
}: {
initialParentId?: string;
context?: EmbedContext;
inPublicSharing?: boolean;
}) => {
return (
<>
<SelectorModal />
<Drive initialParentId={initialParentId} context={context} />
<Drive
initialParentId={initialParentId}
context={context}
inPublicSharing={inPublicSharing}
/>
</>
);
};

const Drive = ({
initialParentId,
context,
inPublicSharing,
}: {
initialParentId?: string;
context?: EmbedContext;
inPublicSharing?: boolean;
}) => {
if (context?.tabId) {
return <TwakeTabConfiguration context={context} />;
}

return <Browser initialParentId={initialParentId} />;
return <Browser initialParentId={initialParentId} inPublicSharing={inPublicSharing} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import A from 'app/atoms/link';
import { Modal, ModalContent } from 'app/atoms/modal';
import { Base } from 'app/atoms/text';
import { useApplications } from 'app/features/applications/hooks/use-applications';
import { useCompanyApplications } from 'app/features/applications/hooks/use-company-applications';
import { Application } from 'app/features/applications/types/application';
import { ReactNode } from 'react';
import { atom, useRecoilState } from 'recoil';
Expand Down Expand Up @@ -34,7 +35,7 @@ export const CreateModal = ({
addFromUrl: (url: string, name: string) => void;
}) => {
const [state, setState] = useRecoilState(CreateModalAtom);
const { applications } = useApplications();
const { applications } = useCompanyApplications();

return (
<Modal
Expand Down Expand Up @@ -114,7 +115,7 @@ export const CreateModal = ({
avatar={app.app.identity?.icon}
/>
}
text={`${app.emptyFile.name} (${app.app.identity?.name})`}
text={`${app.emptyFile.name}`}
onClick={() => addFromUrl(app.emptyFile.url, app.emptyFile.name)}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { AccessLevel } from './common';
export const InternalAccessManager = ({ id, disabled }: { id: string; disabled: boolean }) => {
const { item, loading, update } = useDriveItem(id);

console.log(item?.access_info.entities);

const userEntities = item?.access_info.entities.filter(a => a.type === 'user') || [];
const folderEntity = item?.access_info.entities.filter(a => a.type === 'folder')?.[0] || {
type: 'folder',
Expand Down
2 changes: 1 addition & 1 deletion twake/frontend/src/app/views/applications/drive/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default () => {
</div>
</div>
<div className="main-view public ">
<Drive initialParentId={documentId} />
<Drive initialParentId={documentId} inPublicSharing />
</div>
<MenusBodyLayer />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default (): React.ReactElement => {
const { isOpen } = useDrivePreviewModal();
const { loading, setLoading } = useDrivePreviewLoading();

if (!download || !isOpen) {
if (!download || !isOpen || !id) {
return <></>;
}

Expand Down
11 changes: 10 additions & 1 deletion twake/frontend/src/app/views/client/main-view/MainView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,13 @@ export const MainContentWrapper = () => {
return <MainContent />;
};

export default MainView;
export default ({ className }: PropsType) => {
//This is a hack because main view is displayed before we detect the current "channel" is in fact an application
const channelId = useRouterChannel();
const { applications } = useCompanyApplications();
const isChannelMember = useIsChannelMember(channelId);

if (applications.length === 0 && !isChannelMember) return <></>;

return <MainView className={className} />;
};

0 comments on commit 28ed51f

Please sign in to comment.