Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

slack_import: Move slack import file upload logic to separate file.

Aman Agrawal8e27424996ec
diff --git a/templates/zerver/slack_import.html b/templates/zerver/slack_import.html
index f0043054be0..7ea81341aef 100644
--- a/templates/zerver/slack_import.html
+++ b/templates/zerver/slack_import.html
@@ -1,5 +1,5 @@
{% extends "zerver/portico_signup.html" %}
-{% set entrypoint = "register" %}
+{% set entrypoint = "slack-import" %}
{% block title %}
<title>{{ _("Import from Slack") }} | Zulip</title>
diff --git a/web/src/portico/signup.ts b/web/src/portico/signup.ts
index b00a5343478..8762b757b4f 100644
--- a/web/src/portico/signup.ts
+++ b/web/src/portico/signup.ts
@@ -1,8 +1,3 @@
-import {Uppy} from "@uppy/core";
-import DragDrop from "@uppy/drag-drop";
-import Tus from "@uppy/tus";
-import "@uppy/core/css/style.min.css";
-import "@uppy/drag-drop/css/style.min.css";
import $ from "jquery";
import _ from "lodash";
import assert from "minimalistic-assert";
@@ -405,106 +400,6 @@ $(() => {
}) as EventListener);
}
- if ($("#slack-import-drag-and-drop").length > 0) {
- const key = $<HTMLInputElement>("#auth_key_for_file_upload").val();
- const uppy = new Uppy({
- autoProceed: true,
- restrictions: {
- maxNumberOfFiles: 1,
- minNumberOfFiles: 1,
- allowedFileTypes: [".zip", "application/zip"],
- },
- meta: {
- key,
- },
- locale: {
- strings: {
- youCanOnlyUploadFileTypes: $t({
- defaultMessage: "Upload your Slack export zip file.",
- }),
- },
- // Copied from
- // https://github.com/transloadit/uppy/blob/d1a3345263b3421a06389aa2e84c66e894b3f29d/packages/%40uppy/utils/src/Translator.ts#L122
- // since we don't want to override the default function.
- // Defining pluralize is required by typescript.
- pluralize(n: number): 0 | 1 {
- if (n === 1) {
- return 0;
- }
- return 1;
- },
- },
- });
- uppy.use(DragDrop, {
- target: "#slack-import-drag-and-drop",
- locale: {
- strings: {
- // Override the default text for the drag and drop area.
- dropHereOr: $t({
- defaultMessage:
- "Drag and drop your Slack export file here, or click to browse.",
- }),
- // Required by typescript to define this.
- browse: $t({
- defaultMessage: "Browse",
- }),
- },
- },
- });
- uppy.use(Tus, {
- endpoint: "/api/v1/tus/",
- // Allow user to upload the same file multiple times.
- removeFingerprintOnSuccess: true,
- });
- uppy.on("restriction-failed", (_file, error) => {
- $("#slack-import-file-upload-error").text(error.message);
- });
- uppy.on("upload-error", (_file, error) => {
- $("#slack-import-file-upload-error").text(error.message);
- });
- uppy.on("upload-success", (file, _response) => {
- assert(file !== undefined);
- $("#slack-import-start-upload-wrapper").removeClass("hidden");
- $("#slack-import-uploaded-file-name").text(file.name);
- $("#slack-import-file-upload-error").text("");
- $("#realm-creation-form-slack-import .register-button").prop("disabled", false);
- });
- // Reset uppy state to allow user replace existing uploaded file.
- uppy.on("complete", () => {
- uppy.clear();
- });
- }
-
- if ($("#slack-import-poll-status").length > 0) {
- const key = $<HTMLInputElement>("#auth_key_for_polling").val();
- const pollInterval = 2000; // Poll every 2 seconds
-
- let poll_id: ReturnType<typeof setTimeout> | undefined;
- function checkImportStatus(): void {
- $.get(`/json/realm/import/status/${key}`, {}, (response) => {
- const {status, redirect} = z
- .object({status: z.string(), redirect: z.optional(z.string())})
- .parse(response);
- $("#slack-import-poll-status").text(status);
- if (poll_id && redirect !== undefined) {
- clearInterval(poll_id);
- window.location.assign(redirect);
- }
- });
- }
-
- // Start polling
- poll_id = setInterval(checkImportStatus, pollInterval);
- }
-
- $("#cancel-slack-import").on("click", () => {
- $("#cancel-slack-import-form").trigger("submit");
- });
-
- $("#slack-access-token").on("input", () => {
- $("#update-slack-access-token").show();
- });
-
if ($("a#deactivated-org-auto-redirect").length > 0) {
// Update the href of the deactivated organization auto-redirect link
// to include the current URL hash, if it exists.
diff --git a/web/src/portico/slack-import.ts b/web/src/portico/slack-import.ts
new file mode 100644
index 00000000000..d47efeaf546
--- /dev/null
+++ b/web/src/portico/slack-import.ts
@@ -0,0 +1,110 @@
+import {Uppy} from "@uppy/core";
+import DragDrop from "@uppy/drag-drop";
+import Tus from "@uppy/tus";
+import $ from "jquery";
+import assert from "minimalistic-assert";
+import * as z from "zod/mini";
+
+import {$t} from "../i18n.ts";
+
+$(() => {
+ if ($("#slack-import-drag-and-drop").length > 0) {
+ const key = $<HTMLInputElement>("#auth_key_for_file_upload").val();
+ const uppy = new Uppy({
+ autoProceed: true,
+ restrictions: {
+ maxNumberOfFiles: 1,
+ minNumberOfFiles: 1,
+ allowedFileTypes: [".zip", "application/zip"],
+ },
+ meta: {
+ key,
+ },
+ locale: {
+ strings: {
+ youCanOnlyUploadFileTypes: $t({
+ defaultMessage: "Upload your Slack export zip file.",
+ }),
+ },
+ // Copied from
+ // https://github.com/transloadit/uppy/blob/d1a3345263b3421a06389aa2e84c66e894b3f29d/packages/%40uppy/utils/src/Translator.ts#L122
+ // since we don't want to override the default function.
+ // Defining pluralize is required by typescript.
+ pluralize(n: number): 0 | 1 {
+ if (n === 1) {
+ return 0;
+ }
+ return 1;
+ },
+ },
+ });
+ uppy.use(DragDrop, {
+ target: "#slack-import-drag-and-drop",
+ locale: {
+ strings: {
+ // Override the default text for the drag and drop area.
+ dropHereOr: $t({
+ defaultMessage:
+ "Drag and drop your Slack export file here, or click to browse.",
+ }),
+ // Required by typescript to define this.
+ browse: $t({
+ defaultMessage: "Browse",
+ }),
+ },
+ },
+ });
+ uppy.use(Tus, {
+ endpoint: "/api/v1/tus/",
+ // Allow user to upload the same file multiple times.
+ removeFingerprintOnSuccess: true,
+ });
+ uppy.on("restriction-failed", (_file, error) => {
+ $("#slack-import-file-upload-error").text(error.message);
+ });
+ uppy.on("upload-error", (_file, error) => {
+ $("#slack-import-file-upload-error").text(error.message);
+ });
+ uppy.on("upload-success", (file, _response) => {
+ assert(file !== undefined);
+ $("#slack-import-start-upload-wrapper").removeClass("hidden");
+ $("#slack-import-uploaded-file-name").text(file.name);
+ $("#slack-import-file-upload-error").text("");
+ $("#realm-creation-form-slack-import .register-button").prop("disabled", false);
+ });
+ // Reset uppy state to allow user replace existing uploaded file.
+ uppy.on("complete", () => {
+ uppy.clear();
+ });
+ }
+
+ if ($("#slack-import-poll-status").length > 0) {
+ const key = $<HTMLInputElement>("#auth_key_for_polling").val();
+ const pollInterval = 2000; // Poll every 2 seconds
+
+ let poll_id: ReturnType<typeof setTimeout> | undefined;
+ function checkImportStatus(): void {
+ $.get(`/json/realm/import/status/${key}`, {}, (response) => {
+ const {status, redirect} = z
+ .object({status: z.string(), redirect: z.optional(z.string())})
+ .parse(response);
+ $("#slack-import-poll-status").text(status);
+ if (poll_id && redirect !== undefined) {
+ clearInterval(poll_id);
+ window.location.assign(redirect);
+ }
+ });
+ }
+
+ // Start polling
+ poll_id = setInterval(checkImportStatus, pollInterval);
+ }
+
+ $("#cancel-slack-import").on("click", () => {
+ $("#cancel-slack-import-form").trigger("submit");
+ });
+
+ $("#slack-access-token").on("input", () => {
+ $("#update-slack-access-token").show();
+ });
+});
diff --git a/web/webpack.assets.json b/web/webpack.assets.json
index 85d131672e7..a3c590886f0 100644
--- a/web/webpack.assets.json
+++ b/web/webpack.assets.json
@@ -97,6 +97,14 @@
],
"signup": ["./src/bundles/portico.ts", "jquery-validation", "./src/portico/signup.ts"],
"register": ["./src/bundles/portico.ts", "jquery-validation", "./src/portico/signup.ts"],
+ "slack-import": [
+ "./src/bundles/portico.ts",
+ "jquery-validation",
+ "./src/portico/signup.ts",
+ "@uppy/core/css/style.min.css",
+ "@uppy/drag-drop/css/style.min.css",
+ "./src/portico/slack-import.ts"
+ ],
"redirect-to-post": [
"./third/bootstrap/css/bootstrap.portico.css",
"./src/bundles/common.ts",