🔀 Merge branch 'main' into release/v1.0.5

This commit is contained in:
Eric Doughty-Papassideris
2024-11-25 13:30:27 +01:00
15 changed files with 228 additions and 30 deletions
@@ -15,7 +15,6 @@ import { convert } from "html-to-text";
import path from "path";
import { existsSync } from "fs";
import axios from "axios";
import short, { Translator } from "short-uuid";
import { joinURL } from "../../../../utils/urls";
export default class EmailPusherClass
@@ -43,6 +42,7 @@ export default class EmailPusherClass
});
this.interface = this.configuration.get<string>("email_interface", "");
this.platformUrl = this.configuration.get<string>("platform_url", "");
this.debug = this.configuration.get<boolean>("debug", false);
if (this.interface === "smtp") {
const useTLS = this.configuration.get<string>("smtp_tls", "false") == "true";
const smtpConfig: SMTPClientConfigType = {
@@ -60,7 +60,6 @@ export default class EmailPusherClass
this.apiUrl = this.configuration.get<string>("endpoint", "");
this.apiKey = this.configuration.get<string>("api_key", "");
this.sender = this.configuration.get<string>("sender", "");
this.debug = this.configuration.get<boolean>("debug", false);
}
return this;
@@ -81,20 +80,10 @@ export default class EmailPusherClass
): Promise<EmailBuilderRenderedResult> {
try {
language = ["en", "fr"].find(l => language.toLocaleLowerCase().includes(l)) || "en";
const translator: Translator = short();
const templatePath = path.join(__dirname, "templates", language, `${template}.eta`);
const subjectPath = path.join(__dirname, "templates", language, `${template}.subject.eta`);
const encodedCompanyId = translator.fromUUID(data.notifications[0].item.company_id);
const encodedItemId = translator.fromUUID(data.notifications[0].item.id);
const previewType = data.notifications[0].item.is_directory ? "d" : "preview";
const encodedUrl = joinURL([
this.platformUrl,
"client",
encodedCompanyId,
"v/shared_with_me",
previewType,
encodedItemId,
]);
const urlComponents = data.notifications[0].urlComponents;
const encodedUrl = joinURL([this.platformUrl, ...urlComponents]);
if (!existsSync(templatePath)) {
throw Error(`template not found: ${templatePath}`);
@@ -118,7 +107,7 @@ export default class EmailPusherClass
return { html, text, subject };
} catch (error) {
this.logger.error(`Failure when building email template: ${error}`);
this.logger.error({ error }, `Failure when building email template: ${error}`);
}
}
@@ -150,7 +139,7 @@ export default class EmailPusherClass
};
if (this.debug) {
this.logger.info("EMAIL::SENT ", { emailObject });
this.logger.info({ emailObject }, "EMAIL::SENT");
} else {
if (this.interface === "smtp") {
try {
@@ -9,6 +9,7 @@ export type EmailBuilderDataPayload = {
notifications?: {
type: string;
item: DriveFile;
urlComponents: string[];
}[];
};
@@ -13,8 +13,8 @@ export class AVServiceImpl implements TdriveServiceProvider, Initializable {
version: "1";
av: NodeClam = null;
logger: TdriveLogger = getLogger("Antivirus Service");
avEnabled = getConfigOrDefault("drive.featureAntivirus", false);
private MAX_FILE_SIZE = getConfigOrDefault("av.maxFileSize", 26214400); // 25 MB
avEnabled: boolean = getConfigOrDefault<boolean>("drive.featureAntivirus", false);
private MAX_FILE_SIZE: number = getConfigOrDefault<number>("av.maxFileSize", 26214400); // 25 MB
async init(): Promise<this> {
try {
@@ -23,11 +23,11 @@ export class AVServiceImpl implements TdriveServiceProvider, Initializable {
removeInfected: false, // Do not remove infected files
quarantineInfected: false, // Do not quarantine, just alert
scanLog: null, // No log file for this test
debugMode: getConfigOrDefault("av.debugMode", false), // Enable debug messages
debugMode: getConfigOrDefault<boolean>("av.debugMode", false), // Enable debug messages
clamdscan: {
host: getConfigOrDefault("av.host", "localhost"), // IP of the server
port: getConfigOrDefault("av.port", 3310) as number, // ClamAV server port
timeout: getConfigOrDefault("av.timeout", 2000), // Timeout for scans
host: getConfigOrDefault<string>("av.host", "localhost"), // IP of the server
port: getConfigOrDefault<number>("av.port", 3310) as number, // ClamAV server port
timeout: getConfigOrDefault<number>("av.timeout", 2000), // Timeout for scans
localFallback: true, // Use local clamscan if needed
},
});
@@ -7,6 +7,8 @@ import { DocumentsProcessor } from "./extract-keywords";
import Repository from "../../../../core/platform/services/database/services/orm/repository/repository";
import { DriveFile, TYPE } from "../../entities/drive-file";
import { DocumentsFinishedProcess } from "./save-keywords";
import { generateEncodedUrlComponents } from "../../utils";
export class DocumentsEngine implements Initializable {
private documentRepository: Repository<DriveFile>;
@@ -24,6 +26,7 @@ export class DocumentsEngine implements Initializable {
logger.error(`Error dispatching document event. Unknown event type: ${event}`);
return; // Early return on unknown event type
}
const urlComponents = generateEncodedUrlComponents(e, receiver.id);
try {
const { html, text, subject } = await globalResolver.platformServices.emailPusher.build(
@@ -37,10 +40,12 @@ export class DocumentsEngine implements Initializable {
{
type: event,
item: e.item,
urlComponents,
},
],
},
);
logger.info(`Sending email notification to ${receiver.email_canonical}`);
await globalResolver.platformServices.emailPusher.send(receiver.email_canonical, {
subject,
@@ -30,6 +30,7 @@ import {
DriveFileAccessLevel,
DriveItemDetails,
DriveTdriveTab,
NotificationActionType,
RootType,
SearchDocumentsOptions,
TrashType,
@@ -73,9 +74,9 @@ export class DocumentsService {
userRepository: Repository<User>;
ROOT: RootType = "root";
TRASH: TrashType = "trash";
quotaEnabled: boolean = getConfigOrDefault("drive.featureUserQuota", false);
defaultQuota: number = getConfigOrDefault("drive.defaultUserQuota", 0);
manageAccessEnabled: boolean = getConfigOrDefault("drive.featureManageAccess", false);
quotaEnabled: boolean = getConfigOrDefault<boolean>("drive.featureUserQuota", false);
defaultQuota: number = getConfigOrDefault<number>("drive.defaultUserQuota", 0);
manageAccessEnabled: boolean = getConfigOrDefault<boolean>("drive.featureManageAccess", false);
logger: TdriveLogger = getLogger("Documents Service");
async init(): Promise<this> {
@@ -457,6 +458,7 @@ export class DocumentsService {
gr.services.documents.engine.notifyDocumentShared({
context,
item: driveItem,
type: NotificationActionType.UPDATE,
notificationEmitter: context.user.id,
notificationReceiver: parentItem.creator,
});
@@ -628,6 +630,7 @@ export class DocumentsService {
gr.services.documents.engine.notifyDocumentShared({
context,
item,
type: NotificationActionType.DIRECT,
notificationEmitter: context.user.id,
notificationReceiver: sharedWith[0].id,
});
@@ -1004,6 +1007,7 @@ export class DocumentsService {
gr.services.documents.engine.notifyDocumentVersionUpdated({
context,
item,
type: NotificationActionType.UPDATE,
notificationEmitter: context.user.id,
notificationReceiver: item.creator,
});
@@ -1761,6 +1765,7 @@ export class DocumentsService {
await gr.services.documents.engine.notifyDocumentAVScanAlert({
context,
item,
type: NotificationActionType.DIRECT,
notificationEmitter: context.user.id,
notificationReceiver: context.user.id,
});
@@ -125,9 +125,15 @@ export const eventToTemplateMap: Record<string, any> = {
[DocumentEvents.DOCUMENT_SAHRED]: "notification-document-shared",
};
export enum NotificationActionType {
DIRECT = "direct",
UPDATE = "update",
}
export type NotificationPayloadType = {
context: CompanyExecutionContext;
item: DriveFile;
type: NotificationActionType;
notificationEmitter: string;
notificationReceiver: string;
};
@@ -21,10 +21,13 @@ import { checkAccess, generateAccessToken } from "./services/access-check";
import {
CompanyExecutionContext,
DriveExecutionContext,
NotificationActionType,
NotificationPayloadType,
RootType,
SharedWithMeType,
TrashType,
} from "./types";
import short, { Translator } from "short-uuid";
const ROOT: RootType = "root";
const TRASH: TrashType = "trash";
@@ -673,3 +676,43 @@ export const getKeywordsOfFile = async (
}
return extractKeywords(content_strings);
};
/**
* Generate encodedUrl for email notification
*/
export const generateEncodedUrlComponents = (e: NotificationPayloadType, receiver: string) => {
const translator: Translator = short();
const encodedCompanyId = translator.fromUUID(e.item.company_id);
const clientPath = ["client", encodedCompanyId, "v"];
const isPersonalScope = e.item.scope === "personal";
const isDirectory = e.item.is_directory;
const itemId = isDirectory ? e.item.id : e.item.parent_id;
// Determine the scope and base view
let view: string;
switch (e.type) {
case NotificationActionType.UPDATE:
view = isPersonalScope ? `user_${receiver}` : "root";
break;
case NotificationActionType.DIRECT:
view = "shared_with_me";
break;
default:
throw new Error(`Unexpected NotificationActionType value: ${e.type}`);
}
// Build URL components
const urlComponents = [...clientPath, view];
// Add directory and itemId if applicable
if (e.type === NotificationActionType.UPDATE || isDirectory) {
urlComponents.push("d", itemId);
}
// To highlight the file in the document browser when the user clicks on the notification
if (!isDirectory) {
urlComponents.push("preview", e.item.id);
}
return urlComponents;
};
@@ -136,7 +136,7 @@ class GlobalResolver {
};
// AV service is optional
if (getConfigOrDefault("drive.featureAntivirus", false))
if (getConfigOrDefault<boolean>("drive.featureAntivirus", false))
this.services.av = await new AVServiceImpl().init();
Object.keys(this.services).forEach((key: keyof TdriveServices) => {
+9 -2
View File
@@ -1,5 +1,12 @@
import config from "config";
export const getConfigOrDefault = (key: string, defaultValue: any) => {
return config.has(key) ? config.get(key) : defaultValue;
export const getConfigOrDefault = <T>(key: string, defaultValue: T): T => {
const value = config.has(key) ? config.get(key) : defaultValue;
// Handle specific cases for boolean
if (typeof defaultValue === "boolean") {
return (value === "true" || value === true) as T;
}
return value as T;
};
@@ -0,0 +1,118 @@
import { expect, jest, describe, beforeEach, test } from "@jest/globals";
import { generateEncodedUrlComponents } from "../../../../../src/services/documents/utils";
import {
NotificationActionType,
NotificationPayloadType,
} from "../../../../../src/services/documents/types";
// Mock short-uuid
jest.mock("short-uuid", () => {
const mockTranslator = {
fromUUID: jest.fn((uuid: string) => `short-${uuid}`),
};
return jest.fn(() => mockTranslator);
});
describe("generateEncodedUrlComponents", () => {
const receiver = "receiver-id";
const mockItem = (overrides: Partial<NotificationPayloadType["item"]>) => ({
company_id: "company-uuid",
id: "item-id",
parent_id: "parent-id",
scope: "personal",
is_directory: false,
is_in_trash: false,
...overrides,
});
const mockPayload = (
type: NotificationActionType,
overrides: Partial<NotificationPayloadType> = {},
) => ({
type,
item: mockItem({}),
notificationEmitter: "user_emitter-id",
notificationReceiver: "user_receiver-id",
context: {},
...overrides,
});
beforeEach(() => {
jest.clearAllMocks();
});
describe("DIRECT action: user shares a directory or a file with another user", () => {
test("should link to shared with me view for a shared directory", () => {
const payload = mockPayload(NotificationActionType.DIRECT, {
item: mockItem({ is_directory: true }) as any,
});
const expectedResult = [
"client",
"short-company-uuid",
"v",
"shared_with_me",
"d",
"item-id",
];
const result = generateEncodedUrlComponents(payload as NotificationPayloadType, receiver);
expect(result).toEqual(expectedResult);
});
test("should link to shared with me and preview for a shared file", () => {
const payload = mockPayload(NotificationActionType.DIRECT);
const expectedResult = [
"client",
"short-company-uuid",
"v",
"shared_with_me",
"preview",
"item-id",
];
const result = generateEncodedUrlComponents(payload as NotificationPayloadType, receiver);
expect(result).toEqual(expectedResult);
});
});
describe("UPDATE action: user updates a direcotry or file shared with them, on twake drive or through public link", () => {
test("should link to my drive view for a directory update", () => {
const payload = mockPayload(NotificationActionType.UPDATE, {
item: mockItem({ is_directory: true }) as any,
});
const expectedResult = [
"client",
"short-company-uuid",
"v",
"user_receiver-id",
"d",
"item-id",
];
const result = generateEncodedUrlComponents(payload as NotificationPayloadType, receiver);
expect(result).toEqual(expectedResult);
});
test("should link to my drive and preview for a file update", () => {
const payload = mockPayload(NotificationActionType.UPDATE);
const expectedResult = [
"client",
"short-company-uuid",
"v",
"user_receiver-id",
"d",
"parent-id",
"preview",
"item-id",
];
const result = generateEncodedUrlComponents(payload as NotificationPayloadType, receiver);
expect(result).toEqual(expectedResult);
});
});
});
@@ -16,6 +16,8 @@ export const {
OOCONNECTOR_HEALTH_SECRET,
} = process.env;
export const isProductionEnv = (NODE_ENV ?? '').toLocaleLowerCase().trim() === 'production';
const secs = 1000,
mins = 60 * secs;
@@ -1,5 +1,7 @@
import { Logger } from 'tslog';
import { isProductionEnv } from '../config';
export default new Logger({
name: 'twake-onlyoffice-plugin',
type: isProductionEnv ? 'json' : 'pretty',
});
@@ -79,7 +79,6 @@ class RouterServices extends Observable {
UUIDsToTranslate: Readonly<string[]> = [
'companyId',
'itemId',
'workspaceId',
'channelId',
'messageId',
@@ -78,7 +78,7 @@ export default memo(
: 'member';
setTdriveTabToken(tdriveTabContextToken || null);
const [ filter ] = useRecoilState(SharedWithMeFilterState);
const { viewId, dirId } = useRouteState();
const { viewId, dirId, itemId } = useRouteState();
const [sortLabel] = useRecoilState(DriveItemSort)
const [parentId, _setParentId] = useRecoilState(
DriveCurrentFolderAtom({
@@ -259,6 +259,26 @@ export default memo(
};
}, [parentId, loading]);
// Scroll to item in view
const scrollTillItemInView = itemId && itemId?.length > 0;
const scrollItemId = itemId || '';
useEffect(() => {
const itemInChildren = children.find(item => item.id === scrollItemId);
if (!loading && scrollTillItemInView && !itemInChildren) {
scrollViwer.current?.scrollTo(0, scrollViwer.current?.scrollHeight);
} else {
if (!loading && itemInChildren) {
// scroll to preview item using id for current preview routes
const element = document.getElementById(`DR-${scrollItemId}`);
element?.scrollIntoView({ behavior: 'smooth', block: 'center' });
// set it as checked to indicate it is in view
setChecked({ [scrollItemId]: true });
}
}
}, [loading, children]);
return (
<>
{viewId == 'shared-with-me' ? (
@@ -54,6 +54,7 @@ export const DocumentRow = ({
: 'hover:bg-zinc-500 hover:bg-opacity-10 ') +
(className || '')
}
id={`DR-${item.id}`}
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
onClick={e => {