Changeset View
Changeset View
Standalone View
Standalone View
assets/src/bundles/vault/vault-ui.js
Show First 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | |||||
// called when the user wants to recook an archive | // called when the user wants to recook an archive | ||||
// for which the download link is not available anymore | // for which the download link is not available anymore | ||||
export async function recookObject() { | export async function recookObject() { | ||||
if (recookTask) { | if (recookTask) { | ||||
// stop cooking tasks status polling | // stop cooking tasks status polling | ||||
clearTimeout(checkVaultId); | clearTimeout(checkVaultId); | ||||
// build cook request url | // build cook request url | ||||
let cookingUrl; | let cookingUrl; | ||||
if (recookTask.object_type === 'directory') { | if (recookTask.bundle_type === 'flat') { | ||||
cookingUrl = Urls.api_1_vault_cook_directory(recookTask.object_id); | cookingUrl = Urls.api_1_vault_cook_flat(recookTask.swhid); | ||||
} else { | } else { | ||||
cookingUrl = Urls.api_1_vault_cook_revision_gitfast(recookTask.object_id); | cookingUrl = Urls.api_1_vault_cook_gitfast(recookTask.swhid); | ||||
} | } | ||||
if (recookTask.email) { | if (recookTask.email) { | ||||
cookingUrl += '?email=' + recookTask.email; | cookingUrl += '?email=' + recookTask.email; | ||||
} | } | ||||
try { | try { | ||||
// request archive cooking | // request archive cooking | ||||
const response = await csrfPost(cookingUrl); | const response = await csrfPost(cookingUrl); | ||||
handleFetchError(response); | handleFetchError(response); | ||||
// update task status | // update task status | ||||
recookTask.status = 'new'; | recookTask.status = 'new'; | ||||
const vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | const vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | ||||
for (let i = 0; i < vaultCookingTasks.length; ++i) { | for (let i = 0; i < vaultCookingTasks.length; ++i) { | ||||
if (vaultCookingTasks[i].object_id === recookTask.object_id) { | if (vaultCookingTasks[i].swhid === recookTask.swhid) { | ||||
vaultCookingTasks[i] = recookTask; | vaultCookingTasks[i] = recookTask; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// save updated tasks to local storage | // save updated tasks to local storage | ||||
localStorage.setItem('swh-vault-cooking-tasks', JSON.stringify(vaultCookingTasks)); | localStorage.setItem('swh-vault-cooking-tasks', JSON.stringify(vaultCookingTasks)); | ||||
// restart cooking tasks status polling | |||||
checkVaultCookingTasks(); | |||||
// hide recook archive modal | // hide recook archive modal | ||||
$('#vault-recook-object-modal').modal('hide'); | $('#vault-recook-object-modal').modal('hide'); | ||||
// restart cooking tasks status polling | |||||
await checkVaultCookingTasks(); | |||||
} catch (_) { | } catch (_) { | ||||
// something went wrong | // something went wrong | ||||
checkVaultCookingTasks(); | |||||
$('#vault-recook-object-modal').modal('hide'); | $('#vault-recook-object-modal').modal('hide'); | ||||
await checkVaultCookingTasks(); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
async function checkVaultCookingTasks() { | async function checkVaultCookingTasks() { | ||||
const vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | const vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | ||||
if (!vaultCookingTasks || vaultCookingTasks.length === 0) { | if (!vaultCookingTasks || vaultCookingTasks.length === 0) { | ||||
$('.swh-vault-table tbody tr').remove(); | $('.swh-vault-table tbody tr').remove(); | ||||
checkVaultId = setTimeout(checkVaultCookingTasks, pollingInterval); | checkVaultId = setTimeout(checkVaultCookingTasks, pollingInterval); | ||||
return; | return; | ||||
} | } | ||||
const cookingTaskRequests = []; | const cookingTaskRequests = []; | ||||
const tasks = {}; | const tasks = {}; | ||||
const currentObjectIds = []; | const currentObjectIds = []; | ||||
for (let i = 0; i < vaultCookingTasks.length; ++i) { | for (let i = 0; i < vaultCookingTasks.length; ++i) { | ||||
const cookingTask = vaultCookingTasks[i]; | const cookingTask = vaultCookingTasks[i]; | ||||
currentObjectIds.push(cookingTask.object_id); | |||||
tasks[cookingTask.object_id] = cookingTask; | if (typeof cookingTask.object_type !== 'undefined') { | ||||
let cookingUrl; | // Legacy cooking task, upgrade it to the new schema | ||||
if (cookingTask.object_type === 'directory') { | if (cookingTask.object_type === 'directory') { | ||||
cookingUrl = Urls.api_1_vault_cook_directory(cookingTask.object_id); | cookingTask.swhid = `swh:1:dir:${cookingTask.object_id}`; | ||||
cookingTask.bundle_type = 'flat'; | |||||
} else if (cookingTask.object_type === 'revision') { | |||||
cookingTask.swhid = `swh:1:rev:${cookingTask.object_id}`; | |||||
cookingTask.bundle_type = 'gitfast'; | |||||
} else { | |||||
// Log to the console + Sentry | |||||
console.error(`Unexpected cookingTask.object_type: ${cookingTask.object_type}`); | |||||
// Ignore it for now and hope a future version will fix it | |||||
continue; | |||||
} | |||||
delete cookingTask.object_type; | |||||
delete cookingTask.object_id; | |||||
} | |||||
anlambert: remaining debug log here | |||||
currentObjectIds.push(cookingTask.swhid); | |||||
tasks[cookingTask.swhid] = cookingTask; | |||||
let cookingUrl; | |||||
if (cookingTask.bundle_type === 'flat') { | |||||
cookingUrl = Urls.api_1_vault_cook_flat(cookingTask.swhid); | |||||
} else { | } else { | ||||
cookingUrl = Urls.api_1_vault_cook_revision_gitfast(cookingTask.object_id); | cookingUrl = Urls.api_1_vault_cook_gitfast(cookingTask.swhid); | ||||
} | } | ||||
if (cookingTask.status !== 'done' && cookingTask.status !== 'failed') { | if (cookingTask.status !== 'done' && cookingTask.status !== 'failed') { | ||||
cookingTaskRequests.push(fetch(cookingUrl)); | cookingTaskRequests.push(fetch(cookingUrl)); | ||||
} | } | ||||
} | } | ||||
$('.swh-vault-table tbody tr').each((i, row) => { | $('.swh-vault-table tbody tr').each((i, row) => { | ||||
const objectId = $(row).find('.vault-object-info').data('object-id'); | const swhid = $(row).find('.vault-object-info').data('swhid'); | ||||
if ($.inArray(objectId, currentObjectIds) === -1) { | if ($.inArray(swhid, currentObjectIds) === -1) { | ||||
$(row).remove(); | $(row).remove(); | ||||
} | } | ||||
}); | }); | ||||
try { | try { | ||||
const responses = await Promise.all(cookingTaskRequests); | const responses = await Promise.all(cookingTaskRequests); | ||||
handleFetchErrors(responses); | handleFetchErrors(responses); | ||||
const cookingTasks = await Promise.all(responses.map(r => r.json())); | const cookingTasks = await Promise.all(responses.map(r => r.json())); | ||||
const table = $('#vault-cooking-tasks tbody'); | const table = $('#vault-cooking-tasks tbody'); | ||||
for (let i = 0; i < cookingTasks.length; ++i) { | for (let i = 0; i < cookingTasks.length; ++i) { | ||||
const cookingTask = tasks[cookingTasks[i].obj_id]; | const cookingTask = tasks[cookingTasks[i].swhid]; | ||||
cookingTask.status = cookingTasks[i].status; | cookingTask.status = cookingTasks[i].status; | ||||
cookingTask.fetch_url = cookingTasks[i].fetch_url; | cookingTask.fetch_url = cookingTasks[i].fetch_url; | ||||
cookingTask.progress_message = cookingTasks[i].progress_message; | cookingTask.progress_message = cookingTasks[i].progress_message; | ||||
} | } | ||||
for (let i = 0; i < vaultCookingTasks.length; ++i) { | for (let i = 0; i < vaultCookingTasks.length; ++i) { | ||||
const cookingTask = vaultCookingTasks[i]; | const cookingTask = vaultCookingTasks[i]; | ||||
const rowTask = $(`#vault-task-${cookingTask.object_id}`); | const rowTask = $(`#vault-task-${CSS.escape(cookingTask.swhid)}`); | ||||
if (!rowTask.length) { | if (!rowTask.length) { | ||||
let browseUrl = cookingTask.browse_url; | let browseUrl = cookingTask.browse_url; | ||||
if (!browseUrl) { | if (!browseUrl) { | ||||
if (cookingTask.object_type === 'directory') { | browseUrl = Urls.browse_swhid(cookingTask.swhid); | ||||
browseUrl = Urls.browse_directory(cookingTask.object_id); | |||||
} else { | |||||
browseUrl = Urls.browse_revision(cookingTask.object_id); | |||||
} | |||||
} | } | ||||
const progressBar = $.parseHTML(progress)[0]; | const progressBar = $.parseHTML(progress)[0]; | ||||
const progressBarContent = $(progressBar).find('.progress-bar'); | const progressBarContent = $(progressBar).find('.progress-bar'); | ||||
updateProgressBar(progressBarContent, cookingTask); | updateProgressBar(progressBarContent, cookingTask); | ||||
table.prepend(vaultTableRowTemplate({ | table.prepend(vaultTableRowTemplate({ | ||||
browseUrl: browseUrl, | browseUrl: browseUrl, | ||||
cookingTask: cookingTask, | cookingTask: cookingTask, | ||||
Show All 24 Lines | |||||
} | } | ||||
export function removeCookingTaskInfo(tasksToRemove) { | export function removeCookingTaskInfo(tasksToRemove) { | ||||
let vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | let vaultCookingTasks = JSON.parse(localStorage.getItem('swh-vault-cooking-tasks')); | ||||
if (!vaultCookingTasks) { | if (!vaultCookingTasks) { | ||||
return; | return; | ||||
} | } | ||||
vaultCookingTasks = $.grep(vaultCookingTasks, task => { | vaultCookingTasks = $.grep(vaultCookingTasks, task => { | ||||
return $.inArray(task.object_id, tasksToRemove) === -1; | return $.inArray(task.swhid, tasksToRemove) === -1; | ||||
}); | }); | ||||
localStorage.setItem('swh-vault-cooking-tasks', JSON.stringify(vaultCookingTasks)); | localStorage.setItem('swh-vault-cooking-tasks', JSON.stringify(vaultCookingTasks)); | ||||
} | } | ||||
export function initUi() { | export function initUi() { | ||||
$('#vault-tasks-toggle-selection').change(event => { | $('#vault-tasks-toggle-selection').change(event => { | ||||
$('.vault-task-toggle-selection').prop('checked', event.currentTarget.checked); | $('.vault-task-toggle-selection').prop('checked', event.currentTarget.checked); | ||||
}); | }); | ||||
$('#vault-remove-tasks').click(() => { | $('#vault-remove-tasks').click(() => { | ||||
clearTimeout(checkVaultId); | clearTimeout(checkVaultId); | ||||
const tasksToRemove = []; | const tasksToRemove = []; | ||||
$('.swh-vault-table tbody tr').each((i, row) => { | $('.swh-vault-table tbody tr').each((i, row) => { | ||||
const taskSelected = $(row).find('.vault-task-toggle-selection').prop('checked'); | const taskSelected = $(row).find('.vault-task-toggle-selection').prop('checked'); | ||||
if (taskSelected) { | if (taskSelected) { | ||||
const objectId = $(row).find('.vault-object-info').data('object-id'); | const swhid = $(row).find('.vault-object-info').data('swhid'); | ||||
tasksToRemove.push(objectId); | tasksToRemove.push(swhid); | ||||
$(row).remove(); | $(row).remove(); | ||||
} | } | ||||
}); | }); | ||||
removeCookingTaskInfo(tasksToRemove); | removeCookingTaskInfo(tasksToRemove); | ||||
$('#vault-tasks-toggle-selection').prop('checked', false); | $('#vault-tasks-toggle-selection').prop('checked', false); | ||||
checkVaultId = setTimeout(checkVaultCookingTasks, pollingInterval); | checkVaultId = setTimeout(checkVaultCookingTasks, pollingInterval); | ||||
}); | }); | ||||
checkVaultCookingTasks(); | checkVaultCookingTasks(); | ||||
window.onfocus = () => { | window.onfocus = () => { | ||||
clearTimeout(checkVaultId); | clearTimeout(checkVaultId); | ||||
checkVaultCookingTasks(); | checkVaultCookingTasks(); | ||||
}; | }; | ||||
} | } |
remaining debug log here