Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/src/bundles/vault/vault-create-tasks.js
/** | /** | ||||
* Copyright (C) 2018-2019 The Software Heritage developers | * Copyright (C) 2018-2019 The Software Heritage developers | ||||
* See the AUTHORS file at the top-level directory of this distribution | * See the AUTHORS file at the top-level directory of this distribution | ||||
* License: GNU Affero General Public License version 3, or any later version | * License: GNU Affero General Public License version 3, or any later version | ||||
* See top-level LICENSE file for more information | * See top-level LICENSE file for more information | ||||
*/ | */ | ||||
import {handleFetchError, csrfPost} from 'utils/functions'; | import {handleFetchError, csrfPost} from 'utils/functions'; | ||||
export function vaultRequest(objectType, objectId) { | |||||
let vaultUrl; | |||||
if (objectType === 'directory') { | |||||
vaultUrl = Urls.api_1_vault_cook_directory(objectId); | |||||
} else { | |||||
vaultUrl = Urls.api_1_vault_cook_revision_gitfast(objectId); | |||||
} | |||||
// check if object has already been cooked | |||||
fetch(vaultUrl) | |||||
.then(response => response.json()) | |||||
.then(data => { | |||||
anlambert: In order to check if an object has already been cooked, you need to make a call to the [[ https… | |||||
Done Inline ActionsI thought when a new cooking task is being created it's added to the local storage. That's why I was checking if the cooking task is already there. I've created that function to refactor some repeated parts of the code but It might not be a good idea to add it in this Diff. So, I'll change it to make the API call instead of checking the cooking task existence in the local storage. shisus: I thought when a new cooking task is being created it's added to the local storage. That's why… | |||||
// object needs to be cooked | |||||
if (data.exception === 'NotFoundExc') { | |||||
$(`#vault-cook-${objectType}-modal`).modal('show'); | |||||
// object has been cooked and is in the vault cache | |||||
} else if (data.status === 'done') { | |||||
$(`#vault-fetch-${objectType}-modal`).modal('show'); | |||||
} | |||||
}); | |||||
} | |||||
function addVaultCookingTask(cookingTask) { | function addVaultCookingTask(cookingTask) { | ||||
let vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | let vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | ||||
if (!vaultCookingTasks) { | if (!vaultCookingTasks) { | ||||
vaultCookingTasks = []; | vaultCookingTasks = []; | ||||
} | } | ||||
if (vaultCookingTasks.find(val => { | if (vaultCookingTasks.find(val => { | ||||
return val.object_type === cookingTask.object_type && | return val.object_type === cookingTask.object_type && | ||||
val.object_id === cookingTask.object_id; | val.object_id === cookingTask.object_id; | ||||
}) === undefined) { | }) === undefined) { | ||||
let cookingUrl; | let cookingUrl; | ||||
if (cookingTask.object_type === 'directory') { | if (cookingTask.object_type === 'directory') { | ||||
Not Done Inline ActionsThis will not work as the fetch response needs to be read and converted to JSON. The correct syntax is: fetch(vaultUrl) .then(response => response.json()) .then(data => { }) anlambert: This will not work as the fetch response needs to be read and converted to JSON. The correct… | |||||
Done Inline ActionsFixed shisus: Fixed | |||||
cookingUrl = Urls.api_1_vault_cook_directory(cookingTask.object_id); | cookingUrl = Urls.api_1_vault_cook_directory(cookingTask.object_id); | ||||
} else { | } else { | ||||
cookingUrl = Urls.api_1_vault_cook_revision_gitfast(cookingTask.object_id); | cookingUrl = Urls.api_1_vault_cook_revision_gitfast(cookingTask.object_id); | ||||
} | } | ||||
Not Done Inline ActionsHere, you must redirect to the URL contained in the fetch_url field from the fetched JSON response. anlambert: Here, you must redirect to the URL contained in the `fetch_url` field from the fetched JSON… | |||||
Done Inline ActionsFixed shisus: Fixed | |||||
if (cookingTask.email) { | if (cookingTask.email) { | ||||
cookingUrl += '?email=' + cookingTask.email; | cookingUrl += '?email=' + cookingTask.email; | ||||
} | } | ||||
csrfPost(cookingUrl) | csrfPost(cookingUrl) | ||||
.then(handleFetchError) | .then(handleFetchError) | ||||
.then(() => { | .then(() => { | ||||
vaultCookingTasks.push(cookingTask); | vaultCookingTasks.push(cookingTask); | ||||
localStorage.setItem('swh-vault-cooking-tasks', JSON.stringify(vaultCookingTasks)); | localStorage.setItem('swh-vault-cooking-tasks', JSON.stringify(vaultCookingTasks)); | ||||
Show All 26 Lines | if (!email || validateEmail(email)) { | ||||
}; | }; | ||||
addVaultCookingTask(cookingTask); | addVaultCookingTask(cookingTask); | ||||
} else { | } else { | ||||
$('#invalid-email-modal').modal('show'); | $('#invalid-email-modal').modal('show'); | ||||
} | } | ||||
} | } | ||||
export function fetchDirectoryArchive(directoryId) { | |||||
$('#vault-fetch-directory-modal').modal('hide'); | |||||
const vaultUrl = Urls.api_1_vault_cook_directory(directoryId); | |||||
fetch(vaultUrl) | |||||
.then(response => response.json()) | |||||
.then(data => { | |||||
swh.vault.fetchCookedObject(data.fetch_url); | |||||
}); | |||||
} | |||||
export function cookRevisionArchive(revisionId) { | export function cookRevisionArchive(revisionId) { | ||||
let email = $('#swh-vault-revision-email').val().trim(); | let email = $('#swh-vault-revision-email').val().trim(); | ||||
if (!email || validateEmail(email)) { | if (!email || validateEmail(email)) { | ||||
let cookingTask = { | let cookingTask = { | ||||
'object_type': 'revision', | 'object_type': 'revision', | ||||
'object_id': revisionId, | 'object_id': revisionId, | ||||
'email': email, | 'email': email, | ||||
'status': 'new' | 'status': 'new' | ||||
}; | }; | ||||
addVaultCookingTask(cookingTask); | addVaultCookingTask(cookingTask); | ||||
} else { | } else { | ||||
$('#invalid-email-modal').modal('show'); | $('#invalid-email-modal').modal('show'); | ||||
} | } | ||||
} | } | ||||
export function fetchRevisionArchive(revisionId) { | |||||
$('#vault-fetch-directory-modal').modal('hide'); | |||||
const vaultUrl = Urls.api_1_vault_cook_revision_gitfast(revisionId); | |||||
fetch(vaultUrl) | |||||
.then(response => response.json()) | |||||
.then(data => { | |||||
swh.vault.fetchCookedObject(data.fetch_url); | |||||
}); | |||||
} |
In order to check if an object has already been cooked, you need to make a call to the vault/directory or the vault/revision/gitfast endpoints of the Software Heritage public API.
The local storage is only used to track the cooking progress of objects not available in the vault cache.