Skip to content

Commit

Permalink
remove duplicate entries and cleanings
Browse files Browse the repository at this point in the history
  • Loading branch information
bourgeoa committed Feb 18, 2024
1 parent 8e703dc commit adfbed2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 140 deletions.
49 changes: 1 addition & 48 deletions src/chat/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
32 changes: 1 addition & 31 deletions src/chat/chatLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
22 changes: 11 additions & 11 deletions src/chat/dateFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
61 changes: 31 additions & 30 deletions src/chat/infinite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/chat/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
}
Expand Down
Loading

0 comments on commit adfbed2

Please sign in to comment.