OrderedCollection use orderedItems attribute instead of items.

This commit is contained in:
Feufochmar 2020-04-05 17:03:14 +02:00
parent 6c1ac941eb
commit 778ee0b9be
2 changed files with 24 additions and 66 deletions

View File

@ -473,13 +473,27 @@ Object.assign(ASCollection.prototype,
// Array of properties that may need to be fetched
// This array should be overwritten when inheriting prototypes (by completing it)
_mayNeedFetch: ASObject.prototype._mayNeedFetch.concat([
'current', 'first', 'last', 'items',
'current', 'first', 'last', 'items'
]),
})
ASCollection.prototype.constructor = ASCollection
// ASOrderedCollection prototype
ASOrderedCollection.prototype = Object.create(ASCollection.prototype)
Object.assign(ASOrderedCollection.prototype,
{
// Attributes
orderedItems: undefined, // Shouldn't be needed, as it has the same meaning as 'items' of ASCollection, but present in implementations
// Array of properties that do not need to be fetched
// This array should be overwritten when inheriting prototypes (by completing it)
_alwaysAvailable: ASCollection.prototype._alwaysAvailable.concat([
]),
// Array of properties that may need to be fetched
// This array should be overwritten when inheriting prototypes (by completing it)
_mayNeedFetch: ASCollection.prototype._mayNeedFetch.concat([
'orderedItems',
]),
})
ASOrderedCollection.prototype.constructor = ASOrderedCollection
// ASCollectionPage prototype
@ -504,22 +518,25 @@ ASCollectionPage.prototype.constructor = ASCollectionPage
// ASOrderedCollectionPage prototype
// Specification says that it inherits from both ASCollectionPage and ASOrderedCollection,
// but ASOrderedCollection does not defines more fields than ASCollection,
// so we only inherit from ASCollectionPage, which already inherits from ASCollection
ASOrderedCollectionPage.prototype = Object.create(ASCollectionPage.prototype)
Object.assign(ASOrderedCollectionPage.prototype, ASOrderedCollection.prototype)
// Helping function for removing duplicates
unique = function(arr) {
return arr.filter(function (elem, idx) { return idx === arr.indexOf(elem) })
}
Object.assign(ASOrderedCollectionPage.prototype,
{
// Attributes
startIndex: undefined,
// Array of properties that do not need to be fetched
// This array should be overwritten when inheriting prototypes (by completing it)
_alwaysAvailable: ASCollectionPage.prototype._alwaysAvailable.concat([
_alwaysAvailable: unique(ASOrderedCollection.prototype._alwaysAvailable.concat(ASCollectionPage.prototype._alwaysAvailable.concat([
'startIndex',
]),
]))),
// Array of properties that may need to be fetched
// This array should be overwritten when inheriting prototypes (by completing it)
_mayNeedFetch: ASCollectionPage.prototype._mayNeedFetch.concat([
]),
_mayNeedFetch: unique(ASOrderedCollection.prototype._mayNeedFetch.concat(ASCollectionPage.prototype._mayNeedFetch.concat([
]))),
})
ASOrderedCollectionPage.prototype.constructor = ASOrderedCollectionPage

View File

@ -1,59 +0,0 @@
const {Activity} = require('./activity.js')
// A cache for activities, to be able to reference them by their id
const KnownActivities = {
// Cache
activities: {},
// Methods
get: function(id) {
return KnownActivities.activities[id]
},
set: function(id, obj) {
KnownActivities.activities[id] = obj
},
// Retrieve an object
// The callback takes 3 arguments: load_ok, retrieved_activity, failure_message
retrieve: function(id, token, callback) {
if (KnownActivities.get(id)) {
callback(true, KnownActivities.get(id), undefined)
} else {
// Use ActivityPub protocol to get the activity
// The id is the link to the activity on the server
const request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
const answer = JSON.parse(request.responseText)
if (answer) {
const activity = new Activity(answer)
activity.load(
token,
function(load_ok, failure_message) {
if (load_ok) {
KnownActivities.set(id, activity)
callback(true, activity, undefined)
} else {
callback(false, undefined, 'Unable to retrieve actors of activity.')
console.log(answer)
}
})
} else {
callback(false, undefined, 'Unable to retrieve activity.')
console.log(answer)
}
} else if (request.readyState == 4) {
callback(false, undefined, 'Error during retrieval of activity.')
}
}
request.open('GET', id, true)
if (token) {
request.setRequestHeader('Authorization', 'Bearer ' + token)
}
request.setRequestHeader('Content-Type', 'application/activity+json')
request.setRequestHeader('Accept', 'application/activity+json')
request.send()
}
}
}
// Exported structures
exports.KnownActivities = KnownActivities