chore: Utils.qml content divided into two categories

Initial separation of backend-independ and stateless content
from backend-dependent method/properties which are a subject
of further refactors.

Closes: #16481
This commit is contained in:
Michał Cieślak 2024-10-08 12:38:39 +02:00 committed by Michał
parent a4646cd71c
commit 62cfed201f
1 changed files with 262 additions and 260 deletions

View File

@ -9,18 +9,9 @@ import StatusQ.Core.Theme 0.1
import StatusQ.Core.Utils 0.1 as StatusQUtils import StatusQ.Core.Utils 0.1 as StatusQUtils
QtObject { QtObject {
property var mainModuleInst: typeof mainModule !== "undefined" ? mainModule : null
property var sharedUrlsModuleInst: typeof sharedUrlsModule !== "undefined" ? sharedUrlsModule : null
property var globalUtilsInst: typeof globalUtils !== "undefined" ? globalUtils : null
property var communitiesModuleInst: typeof communitiesModule !== "undefined" ? communitiesModule : null
readonly property int maxImgSizeBytes: Constants.maxUploadFilesizeMB * 1048576 /* 1 MB in bytes */ readonly property int maxImgSizeBytes: Constants.maxUploadFilesizeMB * 1048576 /* 1 MB in bytes */
readonly property int communityIdLength: 68 readonly property int communityIdLength: 68
function restartApplication() {
globalUtilsInst.restartApplication()
}
function isDigit(value) { function isDigit(value) {
return /^\d$/.test(value); return /^\d$/.test(value);
} }
@ -33,22 +24,6 @@ QtObject {
return !!value && value.startsWith('0x') return !!value && value.startsWith('0x')
} }
function isChatKey(value) {
return (startsWith0x(value) && isHex(value) && value.length === 132) || globalUtilsInst.isCompressedPubKey(value)
}
function isCommunityPublicKey(value) {
return (startsWith0x(value) && isHex(value) && value.length === communityIdLength) || globalUtilsInst.isCompressedPubKey(value)
}
function isCompressedPubKey(pubKey) {
return globalUtilsInst.isCompressedPubKey(pubKey)
}
function isAlias(name) {
return globalUtilsInst.isAlias(name)
}
function getCommunityIdFromFullChatId(fullChatId) { function getCommunityIdFromFullChatId(fullChatId) {
return fullChatId.substr(0, communityIdLength) return fullChatId.substr(0, communityIdLength)
} }
@ -210,7 +185,6 @@ QtObject {
return strNumber.replace(/^(0*)([0-9\.]+)/, "$2") return strNumber.replace(/^(0*)([0-9\.]+)/, "$2")
} }
function setColorAlpha(color, alpha) { function setColorAlpha(color, alpha) {
return Qt.hsla(color.hslHue, color.hslSaturation, color.hslLightness, alpha) return Qt.hsla(color.hslHue, color.hslSaturation, color.hslLightness, alpha)
} }
@ -267,7 +241,6 @@ QtObject {
return Date.now().toString(36) + Math.random().toString(36).substr(2, 5) return Date.now().toString(36) + Math.random().toString(36).substr(2, 5)
} }
function validatePasswords(item, firstPasswordField, repeatPasswordField) { function validatePasswords(item, firstPasswordField, repeatPasswordField) {
switch (item) { switch (item) {
case "first": case "first":
@ -407,88 +380,6 @@ QtObject {
/* Validation section end */ /* Validation section end */
function getContactDetailsAsJson(publicKey, getVerificationRequest=true, getOnlineStatus=false, includeDetails=false) {
const defaultValue = {
defaultDisplayName: "",
optionalName: "",
icon: "",
isCurrentUser: "",
colorId: "",
colorHash: "",
displayName: "",
publicKey: publicKey,
name: "",
ensVerified: false,
alias: "",
lastUpdated: 0,
lastUpdatedLocally: 0,
localNickname: "",
thumbnailImage: "",
largeImage: "",
isContact: false,
isBlocked: false,
isContactRequestReceived: false,
isContactRequestSent: false,
isSyncing: false,
removed: false,
trustStatus: Constants.trustStatus.unknown,
contactRequestState: Constants.ContactRequestState.None,
verificationStatus: Constants.verificationStatus.unverified,
incomingVerificationStatus: Constants.verificationStatus.unverified,
socialLinks: [],
bio: "",
onlineStatus: Constants.onlineStatus.inactive
}
if (!mainModuleInst || !publicKey)
return defaultValue
const jsonObj = mainModuleInst.getContactDetailsAsJson(publicKey, getVerificationRequest, getOnlineStatus, includeDetails)
try {
return JSON.parse(jsonObj)
}
catch (e) {
// This log is available only in debug mode, if it's annoying we can remove it
console.warn("error parsing contact details for public key: ", publicKey, " error: ", e.message)
return defaultValue
}
}
function isEnsVerified(publicKey) {
if (publicKey === "" || !isChatKey(publicKey) )
return false
if (!mainModuleInst)
return false
return mainModuleInst.isEnsVerified(publicKey)
}
function getEmojiHashAsJson(publicKey) {
if (publicKey === "" || !isChatKey(publicKey)) {
return ""
}
let jsonObj = globalUtilsInst.getEmojiHashAsJson(publicKey)
return JSON.parse(jsonObj)
}
function getColorHashAsJson(publicKey, skipEnsVerification=false) {
if (publicKey === "" || !isChatKey(publicKey))
return
if (skipEnsVerification) // we know already the user is ENS verified -> no color ring
return
if (isEnsVerified(publicKey)) // ENS verified -> no color ring
return
let jsonObj = globalUtilsInst.getColorHashAsJson(publicKey)
return JSON.parse(jsonObj)
}
function colorIdForPubkey(publicKey) {
if (publicKey === "" || !isChatKey(publicKey)) {
return 0
}
return globalUtilsInst.getColorId(publicKey)
}
function colorForColorId(colorId) { function colorForColorId(colorId) {
if (colorId < 0 || colorId >= Theme.palette.userCustomizationColors.length) { if (colorId < 0 || colorId >= Theme.palette.userCustomizationColors.length) {
console.warn("Utils.colorForColorId : colorId is out of bounds") console.warn("Utils.colorForColorId : colorId is out of bounds")
@ -497,31 +388,6 @@ QtObject {
return Theme.palette.userCustomizationColors[colorId] return Theme.palette.userCustomizationColors[colorId]
} }
function colorForPubkey(publicKey) {
const pubKeyColorId = colorIdForPubkey(publicKey)
return colorForColorId(pubKeyColorId)
}
function getCommunityShareLink(communityId) {
if (communityId === "") {
return ""
}
return communitiesModuleInst.shareCommunityUrlWithData(communityId)
}
function getCommunityChannelShareLink(communityId, channelId) {
if (communityId === "" || channelId === "")
return ""
return communitiesModuleInst.shareCommunityChannelUrlWithData(communityId, channelId)
}
function getCommunityChannelShareLinkWithChatId(chatId) {
const communityId = getCommunityIdFromFullChatId(chatId)
const channelId = getChannelUuidFromFullChatId(chatId)
return getCommunityChannelShareLink(communityId, channelId)
}
function getChatKeyFromShareLink(link) { function getChatKeyFromShareLink(link) {
let index = link.lastIndexOf("/u/") let index = link.lastIndexOf("/u/")
if (index === -1) { if (index === -1) {
@ -530,46 +396,6 @@ QtObject {
return link.substring(index + 3) return link.substring(index + 3)
} }
function getCommunityIdFromShareLink(link) {
let index = link.lastIndexOf("/c/")
if (index === -1) {
return ""
}
const communityKey = link.substring(index + 3)
if (globalUtilsInst.isCompressedPubKey(communityKey)) {
// is zQ.., need to be converted to standard compression
return globalUtilsInst.changeCommunityKeyCompression(communityKey)
}
return communityKey
}
function getCommunityDataFromSharedLink(link) {
const index = link.lastIndexOf("/c/")
if (index === -1)
return null
const communityDataString = sharedUrlsModuleInst.parseCommunitySharedUrl(link)
try {
return JSON.parse(communityDataString)
} catch (e) {
console.warn("Error while parsing community data from url:", e.message)
return null
}
}
function changeCommunityKeyCompression(communityKey) {
return globalUtilsInst.changeCommunityKeyCompression(communityKey)
}
function getCompressedPk(publicKey) {
if (publicKey === "") {
return ""
}
if (!isChatKey(publicKey))
return publicKey
return globalUtilsInst.getCompressedPk(publicKey)
}
function getElidedPk(publicKey) { function getElidedPk(publicKey) {
if (publicKey === "") { if (publicKey === "") {
return "" return ""
@ -584,22 +410,10 @@ QtObject {
return StatusQUtils.Utils.elideText(publicKey, 16) return StatusQUtils.Utils.elideText(publicKey, 16)
} }
function getElidedCompressedPk(publicKey) {
if (publicKey === "") {
return ""
}
let compressedPk = getCompressedPk(publicKey)
return getElidedPk(compressedPk)
}
function elideIfTooLong(str, maxLength) { function elideIfTooLong(str, maxLength) {
return (str.length > maxLength) ? str.substr(0, maxLength-4) + '...' : str; return (str.length > maxLength) ? str.substr(0, maxLength-4) + '...' : str;
} }
function plainText(text) {
return globalUtilsInst.plainText(text)
}
function isInvalidPasswordMessage(msg) { function isInvalidPasswordMessage(msg) {
return ( return (
msg.includes("could not decrypt key with given password") || msg.includes("could not decrypt key with given password") ||
@ -690,34 +504,12 @@ QtObject {
return text return text
} }
function parseContactUrl(link) {
let index = link.lastIndexOf("/u/")
if (index === -1) {
index = link.lastIndexOf("/u#")
}
if (index === -1)
return null
const contactDataString = sharedUrlsModuleInst.parseContactSharedUrl(link)
try {
return JSON.parse(contactDataString)
} catch (e) {
return null
}
}
function dropCommunityLinkPrefix(text) { function dropCommunityLinkPrefix(text) {
if (text.startsWith(Constants.communityLinkPrefix)) if (text.startsWith(Constants.communityLinkPrefix))
text = text.slice(Constants.communityLinkPrefix.length) text = text.slice(Constants.communityLinkPrefix.length)
return text return text
} }
function downloadImageByUrl(url, path) {
globalUtilsInst.downloadImageByUrl(url, path)
}
function getHoveredColor(colorId) { function getHoveredColor(colorId) {
let isLightTheme = Theme.palette.name === Constants.lightThemeName let isLightTheme = Theme.palette.name === Constants.lightThemeName
switch(colorId.toString().toUpperCase()) { switch(colorId.toString().toUpperCase()) {
@ -752,7 +544,6 @@ QtObject {
} }
} }
function getIdForColor(color){ function getIdForColor(color){
let c = color.toString().toUpperCase() let c = color.toString().toUpperCase()
switch(c) { switch(c) {
@ -853,37 +644,6 @@ QtObject {
Theme.palette.warningColor1 Theme.palette.warningColor1
} }
function getKeypairLocation(keypair, fromAccountDetailsView) {
if (!keypair || keypair.pairType === Constants.keypair.type.watchOnly) {
return ""
}
let profileTitle = ""
if (keypair.pairType === Constants.keypair.type.profile) {
profileTitle = Utils.getElidedCompressedPk(keypair.pubKey) + Constants.settingsSection.dotSepString
}
if (keypair.migratedToKeycard) {
return profileTitle + qsTr("On Keycard")
}
if (keypair.operability === Constants.keypair.operability.fullyOperable ||
keypair.operability === Constants.keypair.operability.partiallyOperable) {
return profileTitle + qsTr("On device")
}
if (keypair.operability === Constants.keypair.operability.nonOperable) {
if (fromAccountDetailsView) {
return qsTr("Requires import")
} else if (keypair.syncedFrom === Constants.keypair.syncedFrom.backup) {
if (keypair.pairType === Constants.keypair.type.seedImport ||
keypair.pairType === Constants.keypair.type.privateKeyImport) {
return qsTr("Restored from backup. Import key pair to use derived accounts.")
}
}
return qsTr("Import key pair to use derived accounts")
}
return ""
}
function getActionNameForDisplayingAddressOnNetwork(networkShortName) { function getActionNameForDisplayingAddressOnNetwork(networkShortName) {
if (networkShortName === Constants.networkShortChainNames.arbitrum) { if (networkShortName === Constants.networkShortChainNames.arbitrum) {
return qsTr("View on Arbiscan") return qsTr("View on Arbiscan")
@ -946,26 +706,6 @@ QtObject {
return /(!|\@|#|\$|%|\^|&|\*|\(|\)|\+|\||-|=|\\|{|}|[|]|"|;|'|<|>|\?|,|\.|\/)/.test(c) return /(!|\@|#|\$|%|\^|&|\*|\(|\)|\+|\||-|=|\\|{|}|[|]|"|;|'|<|>|\?|,|\.|\/)/.test(c)
} }
function addTimestampToURL(url) {
return globalUtilsInst.addTimestampToURL(url)
}
// Returns true if the provided displayName occurs in community members
function isDisplayNameDupeOfCommunityMember(displayName) {
if (!communitiesModuleInst)
return false
if (displayName === "")
return false
const myDisplayName = Global.userProfile ? Global.userProfile.name : ""
if (displayName === myDisplayName)
return false
return communitiesModuleInst.isDisplayNameDupeOfCommunityMember(displayName)
}
function getUrlStatus(url) { function getUrlStatus(url) {
// TODO: Analyse and implement // TODO: Analyse and implement
// #15331 // #15331
@ -1016,4 +756,266 @@ QtObject {
} }
xhr.send(); xhr.send();
} }
// BACKEND DEPENDENT PART
//
// Methods and properties below are intended to be refactored in various
// ways to finally make that singleton fully stateless and backend-independent.
property var mainModuleInst: typeof mainModule !== "undefined" ? mainModule : null
property var sharedUrlsModuleInst: typeof sharedUrlsModule !== "undefined" ? sharedUrlsModule : null
property var globalUtilsInst: typeof globalUtils !== "undefined" ? globalUtils : null
property var communitiesModuleInst: typeof communitiesModule !== "undefined" ? communitiesModule : null
function restartApplication() {
globalUtilsInst.restartApplication()
}
function isChatKey(value) {
return (startsWith0x(value) && isHex(value) && value.length === 132) || globalUtilsInst.isCompressedPubKey(value)
}
function isCommunityPublicKey(value) {
return (startsWith0x(value) && isHex(value) && value.length === communityIdLength) || globalUtilsInst.isCompressedPubKey(value)
}
function isCompressedPubKey(pubKey) {
return globalUtilsInst.isCompressedPubKey(pubKey)
}
function isAlias(name) {
return globalUtilsInst.isAlias(name)
}
function getContactDetailsAsJson(publicKey, getVerificationRequest=true, getOnlineStatus=false, includeDetails=false) {
const defaultValue = {
defaultDisplayName: "",
optionalName: "",
icon: "",
isCurrentUser: "",
colorId: "",
colorHash: "",
displayName: "",
publicKey: publicKey,
name: "",
ensVerified: false,
alias: "",
lastUpdated: 0,
lastUpdatedLocally: 0,
localNickname: "",
thumbnailImage: "",
largeImage: "",
isContact: false,
isBlocked: false,
isContactRequestReceived: false,
isContactRequestSent: false,
isSyncing: false,
removed: false,
trustStatus: Constants.trustStatus.unknown,
contactRequestState: Constants.ContactRequestState.None,
verificationStatus: Constants.verificationStatus.unverified,
incomingVerificationStatus: Constants.verificationStatus.unverified,
socialLinks: [],
bio: "",
onlineStatus: Constants.onlineStatus.inactive
}
if (!mainModuleInst || !publicKey)
return defaultValue
const jsonObj = mainModuleInst.getContactDetailsAsJson(publicKey, getVerificationRequest, getOnlineStatus, includeDetails)
try {
return JSON.parse(jsonObj)
}
catch (e) {
// This log is available only in debug mode, if it's annoying we can remove it
console.warn("error parsing contact details for public key: ", publicKey, " error: ", e.message)
return defaultValue
}
}
function isEnsVerified(publicKey) {
if (publicKey === "" || !isChatKey(publicKey) )
return false
if (!mainModuleInst)
return false
return mainModuleInst.isEnsVerified(publicKey)
}
function getEmojiHashAsJson(publicKey) {
if (publicKey === "" || !isChatKey(publicKey)) {
return ""
}
let jsonObj = globalUtilsInst.getEmojiHashAsJson(publicKey)
return JSON.parse(jsonObj)
}
function getColorHashAsJson(publicKey, skipEnsVerification=false) {
if (publicKey === "" || !isChatKey(publicKey))
return
if (skipEnsVerification) // we know already the user is ENS verified -> no color ring
return
if (isEnsVerified(publicKey)) // ENS verified -> no color ring
return
let jsonObj = globalUtilsInst.getColorHashAsJson(publicKey)
return JSON.parse(jsonObj)
}
function colorIdForPubkey(publicKey) {
if (publicKey === "" || !isChatKey(publicKey)) {
return 0
}
return globalUtilsInst.getColorId(publicKey)
}
function colorForPubkey(publicKey) {
const pubKeyColorId = colorIdForPubkey(publicKey)
return colorForColorId(pubKeyColorId)
}
function getCommunityShareLink(communityId) {
if (communityId === "") {
return ""
}
return communitiesModuleInst.shareCommunityUrlWithData(communityId)
}
function getCommunityChannelShareLink(communityId, channelId) {
if (communityId === "" || channelId === "")
return ""
return communitiesModuleInst.shareCommunityChannelUrlWithData(communityId, channelId)
}
function getCommunityChannelShareLinkWithChatId(chatId) {
const communityId = getCommunityIdFromFullChatId(chatId)
const channelId = getChannelUuidFromFullChatId(chatId)
return getCommunityChannelShareLink(communityId, channelId)
}
function getCommunityIdFromShareLink(link) {
let index = link.lastIndexOf("/c/")
if (index === -1) {
return ""
}
const communityKey = link.substring(index + 3)
if (globalUtilsInst.isCompressedPubKey(communityKey)) {
// is zQ.., need to be converted to standard compression
return globalUtilsInst.changeCommunityKeyCompression(communityKey)
}
return communityKey
}
function getCommunityDataFromSharedLink(link) {
const index = link.lastIndexOf("/c/")
if (index === -1)
return null
const communityDataString = sharedUrlsModuleInst.parseCommunitySharedUrl(link)
try {
return JSON.parse(communityDataString)
} catch (e) {
console.warn("Error while parsing community data from url:", e.message)
return null
}
}
function changeCommunityKeyCompression(communityKey) {
return globalUtilsInst.changeCommunityKeyCompression(communityKey)
}
function getCompressedPk(publicKey) {
if (publicKey === "") {
return ""
}
if (!isChatKey(publicKey))
return publicKey
return globalUtilsInst.getCompressedPk(publicKey)
}
function getElidedCompressedPk(publicKey) {
if (publicKey === "") {
return ""
}
let compressedPk = getCompressedPk(publicKey)
return getElidedPk(compressedPk)
}
function plainText(text) {
return globalUtilsInst.plainText(text)
}
function parseContactUrl(link) {
let index = link.lastIndexOf("/u/")
if (index === -1) {
index = link.lastIndexOf("/u#")
}
if (index === -1)
return null
const contactDataString = sharedUrlsModuleInst.parseContactSharedUrl(link)
try {
return JSON.parse(contactDataString)
} catch (e) {
return null
}
}
function downloadImageByUrl(url, path) {
globalUtilsInst.downloadImageByUrl(url, path)
}
function getKeypairLocation(keypair, fromAccountDetailsView) {
if (!keypair || keypair.pairType === Constants.keypair.type.watchOnly) {
return ""
}
let profileTitle = ""
if (keypair.pairType === Constants.keypair.type.profile) {
profileTitle = Utils.getElidedCompressedPk(keypair.pubKey) + Constants.settingsSection.dotSepString
}
if (keypair.migratedToKeycard) {
return profileTitle + qsTr("On Keycard")
}
if (keypair.operability === Constants.keypair.operability.fullyOperable ||
keypair.operability === Constants.keypair.operability.partiallyOperable) {
return profileTitle + qsTr("On device")
}
if (keypair.operability === Constants.keypair.operability.nonOperable) {
if (fromAccountDetailsView) {
return qsTr("Requires import")
} else if (keypair.syncedFrom === Constants.keypair.syncedFrom.backup) {
if (keypair.pairType === Constants.keypair.type.seedImport ||
keypair.pairType === Constants.keypair.type.privateKeyImport) {
return qsTr("Restored from backup. Import key pair to use derived accounts.")
}
}
return qsTr("Import key pair to use derived accounts")
}
return ""
}
function addTimestampToURL(url) {
return globalUtilsInst.addTimestampToURL(url)
}
// Returns true if the provided displayName occurs in community members
function isDisplayNameDupeOfCommunityMember(displayName) {
if (!communitiesModuleInst)
return false
if (displayName === "")
return false
const myDisplayName = Global.userProfile ? Global.userProfile.name : ""
if (displayName === myDisplayName)
return false
return communitiesModuleInst.isDisplayNameDupeOfCommunityMember(displayName)
}
} }