Manage conflicts

This commit is contained in:
Nicola Benaglia 2025-05-24 14:53:46 +02:00
parent a778aa7a0f
commit 506d65dc08
2 changed files with 114 additions and 7114 deletions

View File

@ -38,7 +38,11 @@ import {
getPublicKey,
transferAsset,
} from '../background/background.ts';
import { getNameInfo, uint8ArrayToObject } from '../encryption/encryption.ts';
import {
getAllUserNames,
getNameInfo,
uint8ArrayToObject,
} from '../encryption/encryption.ts';
import { showSaveFilePicker } from '../hooks/useQortalMessageListener.tsx';
import { getPublishesFromAdminsAdminSpace } from '../components/Chat/AdminSpaceInner.tsx';
import { extractComponents } from '../components/Chat/MessageDisplay.tsx';
@ -1315,7 +1319,7 @@ export const publishQDNResource = async (
if (appFee && appFee > 0 && appFeeRecipient) {
hasAppFee = true;
}
const registeredName = await getNameInfo();
const registeredName = data?.name || (await getNameInfo());
const name = registeredName;
if (!name) {
throw new Error(
@ -1331,7 +1335,7 @@ export const publishQDNResource = async (
const title = data.title;
const description = data.description;
const category = data.category;
const file = data?.file || data?.blob;
const tags = data?.tags || [];
const result = {};
@ -1346,9 +1350,7 @@ export const publishQDNResource = async (
if (data.identifier == null) {
identifier = 'default';
}
if (data?.file || data?.blob) {
data64 = await fileToBase64(data?.file || data?.blob);
}
if (
data.encrypt &&
(!data.publicKeys ||
@ -1367,6 +1369,9 @@ export const publishQDNResource = async (
const parsedData = resKeyPair;
const privateKey = parsedData.privateKey;
const userPublicKey = parsedData.publicKey;
if (data?.file || data?.blob) {
data64 = await fileToBase64(data?.file || data?.blob);
}
const encryptDataResponse = encryptDataGroup({
data64,
publicKeys: data.publicKeys,
@ -1410,6 +1415,7 @@ export const publishQDNResource = async (
}),
text2: `service: ${service}`,
text3: `identifier: ${identifier || null}`,
text4: `name: ${registeredName}`,
fee: fee.fee,
...handleDynamicValues,
},
@ -1420,11 +1426,10 @@ export const publishQDNResource = async (
try {
const resPublish = await publishData({
registeredName: encodeURIComponent(name),
file: data64,
data: data64 ? data64 : file,
service: service,
identifier: encodeURIComponent(identifier),
uploadType: 'file',
isBase64: true,
uploadType: data64 ? 'base64' : 'file',
filename: filename,
title,
description,
@ -1558,6 +1563,7 @@ export const publishMultipleQDNResources = async (
const fee = await getFee('ARBITRARY');
const registeredName = await getNameInfo();
const name = registeredName;
if (!name) {
@ -1568,6 +1574,14 @@ export const publishMultipleQDNResources = async (
);
}
const userNames = await getAllUserNames();
data.resources?.forEach((item) => {
if (item?.name && !userNames?.includes(item.name))
throw new Error(
`The name ${item.name}, does not belong to the publisher.`
);
});
const appFee = data?.appFee ? +data.appFee : undefined;
const appFeeRecipient = data?.appFeeRecipient;
let hasAppFee = false;
@ -1638,7 +1652,7 @@ export const publishMultipleQDNResources = async (
<div class="resource-detail"><span>Service:</span> ${
resource.service
}</div>
<div class="resource-detail"><span>Name:</span> ${name}</div>
<div class="resource-detail"><span>Name:</span> ${resource?.name || name}</div>
<div class="resource-detail"><span>Identifier:</span> ${
resource.identifier
}</div>
@ -1695,6 +1709,7 @@ export const publishMultipleQDNResources = async (
reason: errorMsg,
identifier: resource.identifier,
service: resource.service,
name: resource?.name || name,
});
continue;
}
@ -1709,12 +1724,13 @@ export const publishMultipleQDNResources = async (
reason: errorMsg,
identifier: resource.identifier,
service: resource.service,
name: resource?.name || name,
});
continue;
}
const service = resource.service;
let identifier = resource.identifier;
let data64 = resource?.data64 || resource?.base64;
let rawData = resource?.data64 || resource?.base64;
const filename = resource.filename;
const title = resource.title;
const description = resource.description;
@ -1741,26 +1757,31 @@ export const publishMultipleQDNResources = async (
reason: errorMsg,
identifier: resource.identifier,
service: resource.service,
name: resource?.name || name,
});
continue;
}
if (resource.file) {
data64 = await fileToBase64(resource.file);
rawData = resource.file;
}
if (resourceEncrypt) {
try {
if (resource?.file) {
rawData = await fileToBase64(resource.file);
}
const resKeyPair = await getKeyPair();
const parsedData = resKeyPair;
const privateKey = parsedData.privateKey;
const userPublicKey = parsedData.publicKey;
const encryptDataResponse = encryptDataGroup({
data64,
data64: rawData,
publicKeys: data.publicKeys,
privateKey,
userPublicKey,
});
if (encryptDataResponse) {
data64 = encryptDataResponse;
rawData = encryptDataResponse;
}
} catch (error) {
const errorMsg =
@ -1772,32 +1793,37 @@ export const publishMultipleQDNResources = async (
reason: errorMsg,
identifier: resource.identifier,
service: resource.service,
name: resource?.name || name,
});
continue;
}
}
try {
const dataType =
resource?.base64 || resource?.data64 || resourceEncrypt
? 'base64'
: 'file';
console.log('dataType', dataType);
await retryTransaction(
publishData,
[
{
registeredName: encodeURIComponent(name),
file: data64,
service: service,
identifier: encodeURIComponent(identifier),
uploadType: 'file',
isBase64: true,
filename: filename,
title,
description,
apiVersion: 2,
category,
data: rawData,
description,
filename: filename,
identifier: encodeURIComponent(identifier),
registeredName: encodeURIComponent(resource?.name || name),
service: service,
tag1,
tag2,
tag3,
tag4,
tag5,
apiVersion: 2,
title,
uploadType: dataType,
withFee: true,
},
],
@ -1818,6 +1844,7 @@ export const publishMultipleQDNResources = async (
reason: errorMsg,
identifier: resource.identifier,
service: resource.service,
name: resource?.name || name,
});
}
} catch (error) {
@ -1829,6 +1856,7 @@ export const publishMultipleQDNResources = async (
}),
identifier: resource.identifier,
service: resource.service,
name: resource?.name || name,
});
}
}
@ -2324,6 +2352,44 @@ export const joinGroup = async (data, isFromExtension) => {
export const saveFile = async (data, sender, isFromExtension, snackMethods) => {
try {
if (!data?.filename) throw new Error('Missing filename');
if (data?.location) {
const requiredFieldsLocation = ['service', 'name'];
const missingFieldsLocation: string[] = [];
requiredFieldsLocation.forEach((field) => {
if (!data?.location[field]) {
missingFieldsLocation.push(field);
}
});
if (missingFieldsLocation.length > 0) {
const missingFieldsString = missingFieldsLocation.join(', ');
const errorMsg = `Missing fields: ${missingFieldsString}`;
throw new Error(errorMsg);
}
const resPermission = await getUserPermission(
{
text1: 'Would you like to download:',
highlightedText: `${data?.filename}`,
},
isFromExtension
);
const { accepted } = resPermission;
if (!accepted) throw new Error('User declined to save file');
const a = document.createElement('a');
let locationUrl = `/arbitrary/${data.location.service}/${data.location.name}`;
if (data.location.identifier) {
locationUrl = locationUrl + `/${data.location.identifier}`;
}
const endpoint = await createEndpoint(
locationUrl + `?attachment=true&attachmentFilename=${data?.filename}`
);
a.href = endpoint;
a.download = data.filename;
document.body.appendChild(a);
a.click();
a.remove();
return true;
}
const requiredFields = ['filename', 'blob'];
const missingFields: string[] = [];
requiredFields.forEach((field) => {
@ -2341,6 +2407,8 @@ export const saveFile = async (data, sender, isFromExtension, snackMethods) => {
}
const filename = data.filename;
const blob = data.blob;
const mimeType = blob.type || data.mimeType;
const resPermission = await getUserPermission(
{
text1: i18n.t('question:download_file', {
@ -2351,6 +2419,17 @@ export const saveFile = async (data, sender, isFromExtension, snackMethods) => {
isFromExtension
);
const { accepted } = resPermission;
if (!accepted) throw new Error('User declined to save file'); // TODO translate
showSaveFilePicker(
{
filename,
mimeType,
blob,
},
snackMethods
);
return true;
if (accepted) {
const mimeType = blob.type || data.mimeType;
@ -5396,11 +5475,10 @@ export const updateNameRequest = async (data, isFromExtension) => {
const fee = await getFee('UPDATE_NAME');
const resPermission = await getUserPermission(
{
text1: i18n.t('question:permission.register_name', {
postProcess: 'capitalizeFirstChar',
}),
highlightedText: data.newName,
text2: data?.description,
text1: `Do you give this application permission to update this name?`, // TODO translate
text2: `previous name: ${oldName}`,
text3: `new name: ${newName}`,
text4: data?.description,
fee: fee.fee,
},
isFromExtension
@ -6012,7 +6090,7 @@ export const createGroupRequest = async (data, isFromExtension) => {
];
const missingFields: string[] = [];
requiredFields.forEach((field) => {
if (data[field] !== undefined && data[field] !== null) {
if (data[field] === undefined || data[field] === null) {
missingFields.push(field);
}
});
@ -6076,7 +6154,7 @@ export const updateGroupRequest = async (data, isFromExtension) => {
];
const missingFields: string[] = [];
requiredFields.forEach((field) => {
if (data[field] !== undefined && data[field] !== null) {
if (data[field] === undefined || data[field] === null) {
missingFields.push(field);
}
});
@ -6250,7 +6328,7 @@ export const sellNameRequest = async (data, isFromExtension) => {
const requiredFields = ['salePrice', 'nameForSale'];
const missingFields: string[] = [];
requiredFields.forEach((field) => {
if (data[field] !== undefined && data[field] !== null) {
if (data[field] === undefined || data[field] === null) {
missingFields.push(field);
}
});
@ -6320,7 +6398,7 @@ export const cancelSellNameRequest = async (data, isFromExtension) => {
const requiredFields = ['nameForSale'];
const missingFields: string[] = [];
requiredFields.forEach((field) => {
if (data[field] !== undefined && data[field] !== null) {
if (data[field] === undefined || data[field] === null) {
missingFields.push(field);
}
});
@ -6377,7 +6455,7 @@ export const buyNameRequest = async (data, isFromExtension) => {
const requiredFields = ['nameForSale'];
const missingFields: string[] = [];
requiredFields.forEach((field) => {
if (data[field] !== undefined && data[field] !== null) {
if (data[field] === undefined || data[field] === null) {
missingFields.push(field);
}
});
@ -6908,12 +6986,11 @@ export const multiPaymentWithPrivateData = async (data, isFromExtension) => {
[
{
registeredName: encodeURIComponent(name),
file: encryptDataResponse,
data: encryptDataResponse,
service: transaction.service,
identifier: encodeURIComponent(transaction.identifier),
uploadType: 'file',
uploadType: 'base64',
description: transaction?.description,
isBase64: true,
apiVersion: 2,
withFee: true,
},

File diff suppressed because it is too large Load Diff