From adfbed284ceffb2bba95ebb84e28aa6746f7de9d Mon Sep 17 00:00:00 2001 From: bourgeoa Date: Sun, 18 Feb 2024 18:36:14 +0100 Subject: [PATCH] remove duplicate entries and cleanings --- src/chat/bookmarks.js | 49 +---------------------------- src/chat/chatLogic.js | 32 +------------------ src/chat/dateFolder.js | 22 ++++++------- src/chat/infinite.js | 61 +++++++++++++++++++------------------ src/chat/keys.ts | 6 ++-- src/chat/message.js | 15 +++------ src/chat/messageTools.js | 12 ++++---- src/utils/keyHelpers/acl.ts | 2 +- 8 files changed, 59 insertions(+), 140 deletions(-) diff --git a/src/chat/bookmarks.js b/src/chat/bookmarks.js index 1ba1099a..74d166b1 100644 --- a/src/chat/bookmarks.js +++ b/src/chat/bookmarks.js @@ -24,53 +24,6 @@ const BOOKMARK_ICON = 'noun_45961.svg' const label = utils.label const dom = window.document || null -/** Create a resource if it really does not exist - * Be absolutely sure something does not exist before creating a new empty file - * as otherwise existing could be deleted. - * @param doc {NamedNode} - The resource - */ -function createIfNotExists (doc) { - return new Promise(function (resolve, reject) { - store.fetcher.load(doc).then( - response => { - debug.log('createIfNotExists doc exists, all good ' + doc) - // store.fetcher.webOperation('HEAD', doc.uri).then(response => { - resolve(response) - }, - err => { - if (err.response.status === 404) { - debug.log( - 'createIfNotExists doc does NOT exist, will create... ' + doc - ) - - store.fetcher - .webOperation('PUT', doc.uri, { - data: '', - contentType: 'text/turtle' - }) - .then( - response => { - // fetcher.requested[doc.uri] = 'done' // do not need to read ?? but no headers - delete store.fetcher.requested[doc.uri] // delete cached 404 error - debug.log('createIfNotExists doc created ok ' + doc) - resolve(response) - }, - err => { - debug.log('createIfNotExists doc FAILED: ' + doc + ': ' + err) - reject(err) - } - ) - } else { - debug.log( - 'createIfNotExists doc load error NOT 404: ' + doc + ': ' + err - ) - reject(err) - } - } - ) - }) -} - // @@@@ use the one in rdflib.js when it is avaiable and delete this function updatePromise (del, ins) { return new Promise(function (resolve, reject) { @@ -109,7 +62,7 @@ export async function findBookmarkDocument (userContext) { ) try { debug.log('Creating new bookmark file ' + newBookmarkFile) - await createIfNotExists(newBookmarkFile) + await store.fetcher.createIfNotExists(newBookmarkFile) } catch (e) { debug.warn("Can't make fresh bookmark file:" + e) return userContext diff --git a/src/chat/chatLogic.js b/src/chat/chatLogic.js index 2b16bf3b..cb5a28a9 100644 --- a/src/chat/chatLogic.js +++ b/src/chat/chatLogic.js @@ -101,6 +101,7 @@ export class ChatChannel { sts.push($rdf.st(thread, ns.sioc('has_member'), message, thread.doc())) } } + try { await store.updater.updateMany([], sts) } catch (err) { @@ -217,35 +218,4 @@ export function nick (person) { if (s) return '' + s.value return '' + utils.label(person) } - -export async function _createIfNotExists (doc, contentType = 'text/turtle', data = '') { - let response - try { - response = await store.fetcher.load(doc) - } catch (err) { - if (err.response.status === 404) { - debug.log( - 'createIfNotExists: doc does NOT exist, will create... ' + doc - ) - try { - response = await store.fetcher.webOperation('PUT', doc.uri, { - data, - contentType - }) - } catch (err) { - debug.log('createIfNotExists doc FAILED: ' + doc + ': ' + err) - throw err - } - delete store.fetcher.requested[doc.uri] // delete cached 404 error - // debug.log('createIfNotExists doc created ok ' + doc) - return response - } else { - debug.log( - 'createIfNotExists doc load error NOT 404: ' + doc + ': ' + err - ) - throw err - } - } - return response -} // ends diff --git a/src/chat/dateFolder.js b/src/chat/dateFolder.js index f538ec63..b4ffc864 100644 --- a/src/chat/dateFolder.js +++ b/src/chat/dateFolder.js @@ -47,7 +47,7 @@ export class DateFolder { // let date = new Date(str + 'Z') // GMT - but fails in FF - invalid format :-( const date = new Date(str) // not explicitly UTC but is assumed so in spec // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse - debug.log('Date for ' + doc + ':' + date.toISOString()) + // debug.log('Date for ' + doc + ':' + date.toISOString()) return date } @@ -80,7 +80,7 @@ export class DateFolder { siblings = siblings.filter(younger) const folder = lastOrFirst(siblings) if (folder) return folder - debug.log(' parent no suitable offspring ' + parent) + // debug.log(' parent no suitable offspring ' + parent) } catch (err) { if (err.response && err.response.status && err.response.status === 404) { debug.log('Error 404 for chat parent file ' + parent) @@ -91,20 +91,20 @@ export class DateFolder { } } if (level === 0) { - debug.log('loadPrevious: returning as level is zero') + // debug.log('loadPrevious: returning as level is zero') return null // 3:day, 2:month, 1: year 0: no } const uncle = await previousPeriod(parent, level - 1) if (!uncle) { - debug.log(' previousPeriod: nothing left before. ', parent) + // debug.log(' previousPeriod: nothing left before. ', parent) return null // reached first ever } await store.fetcher.load(uncle) const cousins = store.each(uncle, ns.ldp('contains')) const result = lastOrFirst(cousins) - debug.log(' previousPeriod: returning cousins at level ' + level, cousins) - debug.log(' previousPeriod: returning result at level ' + level, result) + // debug.log(' previousPeriod: returning cousins at level ' + level, cousins) + // debug.log(' previousPeriod: returning result at level ' + level, result) return result } // previousPeriod @@ -118,10 +118,10 @@ export class DateFolder { if (!await emptyLeaf(leafDocument)) { return nextDate } else { - debug.log(' loadPrevious: skipping empty ' + leafDocument) + // debug.log(' loadPrevious: skipping empty ' + leafDocument) date = nextDate folder = this.leafDocumentFromDate(date).dir() - debug.log(' loadPrevious: moved back to ' + folder) + // debug.log(' loadPrevious: moved back to ' + folder) } } else { return null // no more left @@ -142,7 +142,7 @@ export class DateFolder { if (!'0123456789'.includes(tail[0])) return false // not numeric return true } - debug.log(' parent ' + parent) + // debug.log(' parent ' + parent) delete folderFetcher.requested[parent.uri] // try { await folderFetcher.load(parent, { force: true }) // Force fetch as will have changed @@ -182,9 +182,9 @@ export class DateFolder { ]) sortMe.sort() if (backwards) sortMe.reverse() - debug.log( + /* debug.log( (backwards ? 'Latest' : 'Earliest') + ' leafObject is ' + sortMe[0][1] - ) + ) */ return sortMe[0][1] } // firstleafObject } // class diff --git a/src/chat/infinite.js b/src/chat/infinite.js index 53f32e1d..410ed1f0 100644 --- a/src/chat/infinite.js +++ b/src/chat/infinite.js @@ -113,8 +113,8 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options) displayed[ele.AJAR_subject.uri] = true } } - const messages = store.each(chatChannel, ns.wf('message'), null, messageTable.chatDocument) + const stored = {} for (const m of messages) { stored[m.uri] = true @@ -157,29 +157,33 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options) if (options.thread) { // only show things in thread if (store.holds(message, ns.sioc('has_reply'), options.thread)) { // root of thread - debug.log(' addMessage: displaying root of thread ' + thread) + // debug.log(' addMessage: displaying root of thread ' + thread) } else if (thread && thread.sameTerm(options.thread)) { - debug.log(' addMessage: Displaying body of thread ' + message.uri.slice(-10)) + // debug.log(' addMessage: Displaying body of thread ' + message.uri.slice(-10)) } else { - debug.log(' addMessage: Suppress non-thread message in thread table ' + message.uri.slice(-10)) + // debug.log(' addMessage: Suppress non-thread message in thread table ' + message.uri.slice(-10)) return // suppress message not in thread } } else { // Not threads if (thread) { - debug.log(' addMessage: Suppress thread message in non-thread table ' + message.uri.slice(-10)) + // debug.log(' addMessage: Suppress thread message in non-thread table ' + message.uri.slice(-10)) return // supress thread messages in body } else { - debug.log(' addMessage: Normal non-thread message in non-thread table ' + message.uri.slice(-10)) + // debug.log(' addMessage: Normal non-thread message in non-thread table ' + message.uri.slice(-10)) } } - - await insertMessageIntoTable(channelObject, - messageTable, - message, - messageTable.fresh, - options, - userContext - ) // fresh from elsewhere + if (!messageTable.fresh) { // if messageTable has been updated with insertMessageIntoTable() don't do it again + // debug.log('@@@ infinite insertMessageIntoTable ' + message) // alain + // debug.log('fresh ' + messageTable.fresh) + // debug.log(messageTable) + await insertMessageIntoTable(channelObject, + messageTable, + message, + messageTable.fresh, + options, + userContext + ) // fresh from elsewhere + } } /* Add a new messageTable at the top/bottom @@ -194,16 +198,16 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options) if (!liveMessageTable) await appendCurrentMessages() // If necessary skip to today and add that return true // done } - debug.log(' insertPreviousMessages: loadPrevious given date ' + date) + // debug.log(' insertPreviousMessages: loadPrevious given date ' + date) date = await dateFolder.loadPrevious(date, backwards) // backwards - debug.log(' insertPreviousMessages: loadPrevious returns date ' + date) + // debug.log(' insertPreviousMessages: loadPrevious returns date ' + date) - debug.log( + /* debug.log( `insertPreviousMessages: from ${ backwards ? 'backwards' : 'forwards' } loadPrevious: ${date}` - ) + ) */ if (!date && !backwards && !liveMessageTable) { await appendCurrentMessages() // If necessary skip to today and add that } @@ -248,15 +252,15 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options) ** @returns DOM element generates */ async function createMessageTable (date, live) { - debug.log(' createMessageTable for ' + date) + // debug.log(' createMessageTable for ' + date) const chatDocument = dateFolder.leafDocumentFromDate(date) try { - await store.fetcher.load(chatDocument) + await store.fetcher.createIfNotExists(chatDocument) } catch (err) { const messageTable = dom.createElement('table') const statusTR = messageTable.appendChild(dom.createElement('tr')) // ### find status in exception if (err.response && err.response.status && err.response.status === 404) { - debug.log('Error 404 for chat file ' + chatDocument) + // debug.log('Error 404 for chat file ' + chatDocument) return await renderMessageTable(date, live) // no message file is fine. will be created later // statusTR.appendChild(widgets.errorMessageBlock(dom, 'no message file', 'white')) } else { @@ -370,7 +374,7 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options) const userContext = { dom, statusArea, div: statusArea } // logged on state, pointers to user's stuff */ - debug.log('Options for called message Area', options) + // debug.log('Options for called message Area', options) const messageTable = dom.createElement('table') messageTable.style.width = '100%' // fill the pane div messageTable.extendBackwards = extendBackwards // Make function available to scroll stuff @@ -473,10 +477,7 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options) for (const st of sts) { await addMessage(st.object, messageTable) } - messageTable.fresh = true - - // loadMessageTable(messageTable, chatDocument) - messageTable.fresh = false + messageTable.fresh = true // message table updated with insertMessageIntoTable() return messageTable } // renderMessageTable @@ -576,7 +577,7 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options) } // debug.log(' chat/loadMoreWhereNeeded: Going now') const scrollBottom = div.scrollHeight - div.scrollTop - debug.log('infinite scroll: adding above: top ' + div.scrollTop) + // debug.log('infinite scroll: adding above: top ' + div.scrollTop) done = await earliest.messageTable.extendBackwards() if (freeze) { div.scrollTop = div.scrollHeight - scrollBottom @@ -592,10 +593,10 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options) latest.messageTable.extendForwards ) { const scrollTop = div.scrollTop - debug.log( + /* debug.log( 'infinite scroll: adding below: bottom: ' + (div.scrollHeight - div.scrollTop - div.clientHeight) - ) + ) */ done = await latest.messageTable.extendForwards() // then add more data on the bottom if (freeze) { div.scrollTop = scrollTop // while adding below keep same things in view @@ -689,7 +690,7 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options) const threadTime = store.any(threadRootMessage, ns.dct('created'), null, threadRootMessage.doc()) if (threadTime) { earliest.limit = new Date(threadTime.value) - debug.log(' inifinite: thread start at ' + earliest.limit) + // debug.log(' infinite: thread start at ' + earliest.limit) } } } diff --git a/src/chat/keys.ts b/src/chat/keys.ts index f2b9be37..18126e7f 100644 --- a/src/chat/keys.ts +++ b/src/chat/keys.ts @@ -68,7 +68,7 @@ export async function getPrivateKey (webId: NamedNode) { // delete invalid public key if (publicKey) { del = [$rdf.st(webId, ns.solid('publicKey'), $rdf.lit(publicKey), store.sym(publicKeyDoc))] - debug.log(del) + debug.log('delete invalid publicKey ' + del) } // update new valid key const newPublicKey = generatePublicKey(privateKey) @@ -89,10 +89,10 @@ const deleteKeyAcl = async (keyDoc: string) => { // delete READ only keyAclDoc. This is possible if the webId is an owner try { const response = await store.fetcher.webOperation('DELETE', keyAclDoc.value) // this may fail if webId is not an owner - debug.log('delete ' + keyAclDoc.value + ' ' + response.status) // should test 404 and 2xx + debug.log('delete keyAcl' + keyAclDoc.value + ' ' + response.status) // should test 404 and 2xx } catch (err) { if (err.response.status !== 404) { throw new Error(err) } - debug.log('delete ' + keyAclDoc.value + ' ' + err.response.status) // should test 404 and 2xx + debug.log('delete keyAcl' + keyAclDoc.value + ' ' + err.response.status) // should test 404 and 2xx } } } diff --git a/src/chat/message.js b/src/chat/message.js index 2de6020a..7fc8ffce 100644 --- a/src/chat/message.js +++ b/src/chat/message.js @@ -142,16 +142,10 @@ export async function renderMessageRow (channelObject, message, fresh, options, const creator = store.any(message, ns.foaf('maker')) const date = store.any(message, ns.dct('created')) const latestVersion = await mostRecentVersion(message) - debug.log('@@@@ alain mostRecentVersion') - debug.log(message) - debug.log(latestVersion) const latestVersionCreator = store.any(latestVersion, ns.foaf('maker')) // use latest content if same owner, else use original // this is may be too strict. Should we find latest valid version if any ? - debug.log('@@@@ alain creator') - debug.log(creator) - debug.log(latestVersionCreator) const msgId = creator.uri === latestVersionCreator?.uri ? latestVersion : message const content = store.any(msgId, ns.sioc('content')) @@ -175,11 +169,12 @@ export async function renderMessageRow (channelObject, message, fresh, options, if (straightReplies.length > 1) { debug.log('renderMessageRow: found normal replies: ', straightReplies) } - debug.log('@@@@ is thread') if (!thread) { // thread = store.any(message, ns.sioc('has_reply')) thread = store.any(null, ns.sioc('has_member'), message) } + // debug.log('@@@@ is thread' + thread) + // get signature const signature = store.any(msgId, $rdf.sym(`${SEC}proofValue`)) @@ -196,7 +191,7 @@ export async function renderMessageRow (channelObject, message, fresh, options, debug.warn(msgId.uri + ' is unsigned') // TODO replace with UI (colored message ?) } else { // signed message, get public key and check signature getPublicKey(creator).then(publicKey => { - debug.log(creator.uri + '\n' + msg.created + '\n' + msg.id + '\n' + publicKey) + // debug.log(creator.uri + '\n' + msg.created + '\n' + msg.id + '\n' + publicKey) if (!publicKey) { debug.warn('message is signed but ' + creator.uri + ' is missing publicKey') } @@ -324,13 +319,13 @@ export async function renderMessageRow (channelObject, message, fresh, options, toolsTD.appendChild(tools) }) if (thread && options.showThread) { - debug.log(' message has thread ' + thread) + // debug.log(' message has thread ' + thread) td3.appendChild(widgets.button( dom, icons.iconBase + 'noun_1180164.svg', // right arrow .. @@ think of stg better 'see thread', _e => { - debug.log('@@@@ Calling showThread thread ' + thread) + // debug.log('@@@@ Calling showThread thread ' + thread) options.showThread(thread, options) } )) diff --git a/src/chat/messageTools.js b/src/chat/messageTools.js index 2b278dc7..499e9cb0 100644 --- a/src/chat/messageTools.js +++ b/src/chat/messageTools.js @@ -75,9 +75,9 @@ export function emojiFromAction (action) { */ export async function sentimentStrip (target, doc) { // alain: seems not used const versions = await allVersions(target) - debug.log('sentimentStrip Versions for ' + target, versions) + // debug.log('sentimentStrip Versions for ' + target, versions) const actions = versions.map(version => store.each(null, ns.schema('target'), version, doc)).flat() - debug.log('sentimentStrip: Actions for ' + target, actions) + // debug.log('sentimentStrip: Actions for ' + target, actions) const strings = actions.map(action => emojiFromAction(action) || '') return dom.createTextNode(strings.join(' ')) } @@ -93,16 +93,16 @@ export async function sentimentStripLinked (target, doc) { strip.innerHTML = '' if (isDeleted(target)) return strip const versions = await allVersions(target) - debug.log('sentimentStripLinked: Versions for ' + target, versions) + // debug.log('sentimentStripLinked: Versions for ' + target, versions) const actions = versions.map(version => store.each(null, ns.schema('target'), version, doc)).flat() - debug.log('sentimentStripLinked: Actions for ' + target, actions) + // debug.log('sentimentStripLinked: Actions for ' + target, actions) if (actions.length === 0) return strip const sentiments = actions.map(a => [ store.any(a, ns.rdf('type'), null, doc), store.any(a, ns.sioc('content'), null, doc), store.any(a, ns.schema('agent'), null, doc) ]) - debug.log(' Actions sentiments ', sentiments) + // debug.log(' Actions sentiments ', sentiments) sentiments.sort() sentiments.forEach(ss => { const [theClass, content, agent] = ss @@ -116,7 +116,7 @@ export async function sentimentStripLinked (target, doc) { res.textContent = content || emojiMap[theClass] || '⬜️' strip.appendChild(res) }) - debug.log(' Actions strip ', strip) + // debug.log(' Actions strip ', strip) } refresh().then(debug.log('sentimentStripLinked: sentimentStripLinked async refreshed')) strip.refresh = refresh diff --git a/src/utils/keyHelpers/acl.ts b/src/utils/keyHelpers/acl.ts index 2b6309d7..f68b0e0c 100644 --- a/src/utils/keyHelpers/acl.ts +++ b/src/utils/keyHelpers/acl.ts @@ -13,7 +13,7 @@ export async function setAcl (keyDoc: string, aclBody: string) { await store.fetcher.load(keyDoc) // FIXME: check the Why value on this quad: - debug.log(store.statementsMatching(store.sym(keyDoc), store.sym('http://www.iana.org/assignments/link-relations/acl'))) + // debug.log(store.statementsMatching(store.sym(keyDoc), store.sym('http://www.iana.org/assignments/link-relations/acl'))) const keyAclDoc = store.any(store.sym(keyDoc), store.sym('http://www.iana.org/assignments/link-relations/acl')) if (!keyAclDoc) { throw new Error('Key ACL doc not found!')