Use activity-vocabulary in timeline, activity and objects wrappers.
This commit is contained in:
parent
778ee0b9be
commit
0cb6a8555a
22
render.js
22
render.js
|
@ -1,8 +1,7 @@
|
|||
const {Actor} = require('./src/actor.js')
|
||||
const {Timeline} = require('./src/timeline.js')
|
||||
const {Timeline, KnownActivities} = require('./src/timeline.js')
|
||||
const {Message} = require('./src/message.js')
|
||||
const {ConnectedUser} = require('./src/connected-user.js')
|
||||
const {KnownActivities} = require('./src/known-activities.js')
|
||||
|
||||
// For access of elements
|
||||
const Elem = function(id) {
|
||||
|
@ -67,10 +66,7 @@ const Render = {
|
|||
+ '</a></p>'
|
||||
} else {
|
||||
display = display + '<img src="' + Icons.fallback['user'] + '" width="32" height="32" /> '
|
||||
+ '<p style="display:inline-block;">'
|
||||
+ '<a href="' + actor.data.id + '">'
|
||||
+ 'Other actor'
|
||||
+ '</a></p>'
|
||||
+ '<p style="display:inline-block;">Unknown actor</p>'
|
||||
}
|
||||
display = display + '</section>'
|
||||
return display
|
||||
|
@ -218,7 +214,7 @@ const UI = {
|
|||
function(element) {
|
||||
return '<li class="actor-display">' + Render.audienceActor(element) + '</li>'
|
||||
}).join('')
|
||||
Elem('activity-code-source').innerText = JSON.stringify(activity.raw, null, 1)
|
||||
Elem('activity-code-source').innerText = JSON.stringify(activity.data._raw, null, 1)
|
||||
// Object of activity
|
||||
if (activity.object) {
|
||||
Elem('activity-object').style.display = 'block'
|
||||
|
@ -236,7 +232,7 @@ const UI = {
|
|||
function(element) {
|
||||
return '<li class="actor-display">' + Render.audienceActor(element) + '</li>'
|
||||
}).join('')
|
||||
Elem('activity-object-code-source').innerText = JSON.stringify(activity.object.raw, null, 1)
|
||||
Elem('activity-object-code-source').innerText = JSON.stringify(activity.object.data._raw, null, 1)
|
||||
Elem('activity-object-name').innerText = activity.object.name
|
||||
Elem('activity-object-summary').innerHTML = activity.object.summary
|
||||
Elem('activity-object-content').innerHTML = activity.object.content
|
||||
|
@ -487,6 +483,14 @@ const UI = {
|
|||
},
|
||||
// Show contents of activities
|
||||
showActivity: function(activityId) {
|
||||
UI.showPage('show-activity', KnownActivities.get(activityId))
|
||||
const act = KnownActivities.get(activityId)
|
||||
act.loadAll(function (load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
UI.showPage('show-activity', act)
|
||||
}
|
||||
if (failure_message) {
|
||||
UI.displayError(failure_message)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,155 +1,114 @@
|
|||
const {Actor} = require('./actor.js')
|
||||
const {KnownActors} = require('./known-actors.js')
|
||||
|
||||
// ASObject class: represent of Object of the ActivityStream spec
|
||||
const ASObject = function(raw_object) {
|
||||
this.raw = raw_object
|
||||
this.id = raw_object.id
|
||||
this.type = raw_object.type
|
||||
this.published = raw_object.published ? new Date(raw_object.published) : undefined
|
||||
this.name = raw_object.name ? raw_object.name : ''
|
||||
this.summary = raw_object.summary ? raw_object.summary : ''
|
||||
this.content = raw_object.content ? raw_object.content : ''
|
||||
// actor, to, cc are filled in loadActors
|
||||
// ActivityObject class: wrapper around an Object of the ActivityStream spec
|
||||
const ActivityObject = function(as_object, token) {
|
||||
this.data = as_object
|
||||
this.token = token
|
||||
// Those should be available
|
||||
this.id = this.data.id
|
||||
this.type = this.data.type
|
||||
this.published = this.data.published ? new Date(this.data.published) : undefined
|
||||
this.name = this.data.name ? this.data.name : ''
|
||||
this.summary = this.data.summary ? this.data.summary : ''
|
||||
this.content = this.data.content ? this.data.content : ''
|
||||
// Other things filled later in loadAll
|
||||
this.actor = new Actor()
|
||||
this.to = []
|
||||
this.cc = []
|
||||
//
|
||||
this.attachments = (raw_object.attachment && Array.isArray(raw_object.attachment)) ? raw_object.attachment : []
|
||||
}
|
||||
// Fetch function
|
||||
// Callback takes 3 parameters: load_ok, fetched_object, failure_message
|
||||
ASObject.fetch = function(url, token, callback) {
|
||||
if (typeof url === 'object') {
|
||||
// No need to fetch: already an object
|
||||
const obj = new ASObject(url)
|
||||
obj.load(token, function(load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
callback(true, obj, undefined)
|
||||
} else {
|
||||
callback(false, undefined, failure_message)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// Use ActivityPub protocol to get the object
|
||||
// The id is the link to the object 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 obj = new ASObject(answer)
|
||||
obj.load(token, function(load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
callback(true, obj, undefined)
|
||||
} else {
|
||||
callback(false, undefined, 'Unable to retrieve actors of objects.')
|
||||
console.log(answer)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
callback(false, undefined, 'Unable to retrieve object.')
|
||||
console.log(answer)
|
||||
}
|
||||
} else if (request.readyState == 4) {
|
||||
callback(false, undefined, 'Error during retrieval of object.')
|
||||
}
|
||||
}
|
||||
request.open('GET', url, true)
|
||||
if (token) {
|
||||
request.setRequestHeader('Authorization', 'Bearer ' + token)
|
||||
}
|
||||
request.setRequestHeader('Content-Type', 'application/activity+json')
|
||||
request.setRequestHeader('Accept', 'application/activity+json')
|
||||
request.send()
|
||||
}
|
||||
this.loaded = false
|
||||
}
|
||||
|
||||
ASObject.prototype = {
|
||||
// Load the actors present in the actor, to and cc fields
|
||||
loadActors: function(token, callback) {
|
||||
// Load the author
|
||||
// Try: actor, then attributedTo
|
||||
var profile = this.raw.actor
|
||||
if (!profile) {
|
||||
profile = this.raw.attributedTo
|
||||
ActivityObject.prototype = {
|
||||
loadAll: function (callback) {
|
||||
// Do not load if already loaded
|
||||
if (this.loaded) {
|
||||
callback(true, undefined)
|
||||
return
|
||||
}
|
||||
KnownActors.retrieve(
|
||||
profile,
|
||||
token,
|
||||
function(load_ok, actor, failure_message) {
|
||||
this.loaded = true
|
||||
// Fetch attributes
|
||||
this.data.fetchAttributeList(
|
||||
['attributedTo', 'attachment'],
|
||||
this.token,
|
||||
function (load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
this.actor = actor
|
||||
}
|
||||
// Load the actors in the "to" array, skip to "cc" if there is no "to", and stop is there is no "cc" either
|
||||
if (this.raw.to && Array.isArray(this.raw.to)) {
|
||||
this.loadToActors(this.raw.to.values(), token, callback)
|
||||
} else if (this.raw.to && typeof this.raw.to === 'string') {
|
||||
// Only one element in the array
|
||||
this.loadToActors([this.raw.to].values(), token, callback)
|
||||
} else if (this.raw.cc && Array.isArray(this.raw.cc)) {
|
||||
this.loadCcActors(this.raw.cc.values(), token, callback)
|
||||
} else if (this.raw.cc && typeof this.raw.cc === 'string') {
|
||||
// Only one element in the array
|
||||
this.loadToActors([this.raw.cc].values(), token, callback)
|
||||
} else {
|
||||
// Actor
|
||||
this.actor.loadFromASActor(this.data.attributedTo, function (ok, error) {
|
||||
if (ok) {
|
||||
// Store actors in KnownActors
|
||||
KnownActors.set(this.actor.data.id, this.actor)
|
||||
} else {
|
||||
console.log(error)
|
||||
}
|
||||
}.bind(this))
|
||||
// attachment
|
||||
this.attachments = (this.data.attachment && Array.isArray(this.data.attachment)) ? this.data.attachment : []
|
||||
// Audience
|
||||
// to
|
||||
this.loadAudience(this.data.to, this.to, function (load_ok, failure_message) {
|
||||
if (!load_ok) {
|
||||
console.log(failure_message)
|
||||
}
|
||||
})
|
||||
// cc
|
||||
this.loadAudience(this.data.cc, this.cc, function (load_ok, failure_message) {
|
||||
if (!load_ok) {
|
||||
console.log(failure_message)
|
||||
}
|
||||
})
|
||||
// OK
|
||||
callback(true, undefined)
|
||||
} else {
|
||||
callback(false, failure_message)
|
||||
}
|
||||
}.bind(this))
|
||||
},
|
||||
// Load the "To" array
|
||||
loadToActors: function(iter, token, callback) {
|
||||
const next = iter.next()
|
||||
if (next.done) {
|
||||
// Finished: load the "cc" array (if possible)
|
||||
if (this.raw.cc && Array.isArray(this.raw.cc)) {
|
||||
this.loadCcActors(this.raw.cc.values(), token, callback)
|
||||
} else if (this.raw.cc && typeof this.raw.cc === 'string') {
|
||||
// Only one element in the array
|
||||
this.loadToActors([this.raw.cc].values(), token, callback)
|
||||
} else {
|
||||
callback(true, undefined)
|
||||
}
|
||||
// Load an audience array
|
||||
loadAudience: function (from, to, callback) {
|
||||
if (from && Array.isArray(from)) {
|
||||
this.loadAudienceIter(from.values(), to, callback, '')
|
||||
} else if (from && typeof from === 'string') {
|
||||
// Only one element in array
|
||||
this.loadAudienceIter([from].values(), to, callback, '')
|
||||
} else {
|
||||
const profile = next.value
|
||||
KnownActors.retrieve(
|
||||
profile,
|
||||
token,
|
||||
function(load_ok, actor, failure_message) {
|
||||
if (load_ok) {
|
||||
this.to.push(actor)
|
||||
} else {
|
||||
// Collection ?
|
||||
}
|
||||
this.loadToActors(iter, token, callback)
|
||||
}.bind(this))
|
||||
}
|
||||
},
|
||||
// Load the "Cc" array
|
||||
loadCcActors: function(iter, token, callback) {
|
||||
const next = iter.next()
|
||||
if (next.done) {
|
||||
callback(true, undefined)
|
||||
} else {
|
||||
const profile = next.value
|
||||
KnownActors.retrieve(
|
||||
profile,
|
||||
token,
|
||||
function(load_ok, actor, failure_message) {
|
||||
if (load_ok) {
|
||||
this.cc.push(actor)
|
||||
} else {
|
||||
// Collection ?
|
||||
}
|
||||
this.loadCcActors(iter, token, callback)
|
||||
}.bind(this))
|
||||
}
|
||||
},
|
||||
// Load everything
|
||||
load: function(token, callback) {
|
||||
this.loadActors(token, callback)
|
||||
loadAudienceIter: function (iter, to, callback, error_msg) {
|
||||
const next = iter.next()
|
||||
if (next.done) {
|
||||
callback(true, error_msg)
|
||||
} else {
|
||||
const act = next.value
|
||||
var err = error_msg
|
||||
if (typeof act === 'string') {
|
||||
// string => id of actor
|
||||
KnownActors.retrieve(act, undefined, function (load_ok, actor, failure_message) {
|
||||
if (load_ok) {
|
||||
to.push(actor)
|
||||
} else {
|
||||
err = err + failure_message + '<br/>'
|
||||
}
|
||||
})
|
||||
} else if (typeof act === 'object') {
|
||||
// Actor is present as object
|
||||
const actor = new Actor()
|
||||
actor.loadFromASActor(this.data.actor, function (load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
// Store actors in KnownActors
|
||||
KnownActors.set(this.actor.data.id, this.actor)
|
||||
to.push(actor)
|
||||
} else {
|
||||
err = err + failure_message + '<br/>'
|
||||
}
|
||||
}.bind(this))
|
||||
}
|
||||
this.loadAudienceIter(iter, to, callback, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Exported structures
|
||||
exports.ASObject = ASObject
|
||||
exports.ActivityObject = ActivityObject
|
||||
|
|
205
src/activity.js
205
src/activity.js
|
@ -1,117 +1,130 @@
|
|||
const {Actor} = require('./actor.js')
|
||||
const {KnownActors} = require('./known-actors.js')
|
||||
const {ASObject} = require('./activity-object.js')
|
||||
const {ActivityObject} = require('./activity-object.js')
|
||||
|
||||
// Activity class
|
||||
const Activity = function(raw_activity) {
|
||||
this.raw = raw_activity
|
||||
this.id = raw_activity.id
|
||||
this.type = raw_activity.type
|
||||
this.published = raw_activity.published ? new Date(raw_activity.published) : undefined
|
||||
// filled in loadObject
|
||||
this.object = undefined
|
||||
// actor, to, cc are filled in loadActors
|
||||
const Activity = function (as_activity, token) {
|
||||
this.data = as_activity
|
||||
this.token = token
|
||||
// Those should be available
|
||||
this.id = this.data.id
|
||||
this.type = this.data.type
|
||||
this.published = this.data.published ? new Date(this.data.published) : undefined
|
||||
// Actor
|
||||
this.actor = new Actor()
|
||||
this.object = undefined
|
||||
// Filled later, when displaying details with loadAll
|
||||
this.to = []
|
||||
this.cc = []
|
||||
//
|
||||
this.loaded = false
|
||||
}
|
||||
Activity.prototype = {
|
||||
// Load the actors present in the actor, to and cc fields
|
||||
loadActors: function(token, callback) {
|
||||
// Load the actor
|
||||
const profile = this.raw.actor
|
||||
KnownActors.retrieve(
|
||||
profile,
|
||||
token,
|
||||
function(load_ok, actor, failure_message) {
|
||||
// Load needed attributes
|
||||
loadNeeded: function (callback) {
|
||||
// Load the needed properties for limited display
|
||||
// actor, object
|
||||
this.data.fetchAttributeList(
|
||||
['actor', 'object'],
|
||||
this.token,
|
||||
function (load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
this.actor = actor
|
||||
}
|
||||
// Load the actors in the "to" array, skip to "cc" if there is no "to", and stop is there is no "cc" either
|
||||
if (this.raw.to && Array.isArray(this.raw.to)) {
|
||||
this.loadToActors(this.raw.to.values(), token, callback)
|
||||
} else if (this.raw.to && typeof this.raw.to === 'string') {
|
||||
// Only one element in the array
|
||||
this.loadToActors([this.raw.to].values(), token, callback)
|
||||
} else if (this.raw.cc && Array.isArray(this.raw.cc)) {
|
||||
this.loadCcActors(this.raw.cc.values(), token, callback)
|
||||
} else if (this.raw.cc && typeof this.raw.cc === 'string') {
|
||||
// Only one element in the array
|
||||
this.loadToActors([this.raw.cc].values(), token, callback)
|
||||
// Object
|
||||
if (this.data.object) {
|
||||
this.object = new ActivityObject(this.data.object, this.token)
|
||||
} else {
|
||||
this.object = undefined
|
||||
}
|
||||
// Actor
|
||||
this.actor.loadFromASActor(this.data.actor, function (ok, error) {
|
||||
if (ok) {
|
||||
// Store actors in KnownActors
|
||||
KnownActors.set(this.actor.data.id, this.actor)
|
||||
}
|
||||
callback(ok, error)
|
||||
}.bind(this))
|
||||
} else {
|
||||
callback(true, undefined)
|
||||
console.log(failure_message)
|
||||
callback(false, failure_message)
|
||||
}
|
||||
}.bind(this))
|
||||
},
|
||||
// Load the "To" array
|
||||
loadToActors: function(iter, token, callback) {
|
||||
const next = iter.next()
|
||||
if (next.done) {
|
||||
// Finished: load the "cc" array (if possible)
|
||||
if (this.raw.cc && Array.isArray(this.raw.cc)) {
|
||||
this.loadCcActors(this.raw.cc.values(), token, callback)
|
||||
} else if (this.raw.cc && typeof this.raw.cc === 'string') {
|
||||
// Only one element in the array
|
||||
this.loadToActors([this.raw.cc].values(), token, callback)
|
||||
} else {
|
||||
callback(true, undefined)
|
||||
}
|
||||
} else {
|
||||
const profile = next.value
|
||||
KnownActors.retrieve(
|
||||
profile,
|
||||
token,
|
||||
function(load_ok, actor, failure_message) {
|
||||
if (load_ok) {
|
||||
this.to.push(actor)
|
||||
} else {
|
||||
// Collection ?
|
||||
}
|
||||
this.loadToActors(iter, token, callback)
|
||||
}.bind(this))
|
||||
}
|
||||
},
|
||||
// Load the "Cc" array
|
||||
loadCcActors: function(iter, token, callback) {
|
||||
const next = iter.next()
|
||||
if (next.done) {
|
||||
// Load for full display
|
||||
loadAll: function(callback) {
|
||||
// Do not load if already loaded
|
||||
if (this.loaded) {
|
||||
callback(true, undefined)
|
||||
} else {
|
||||
const profile = next.value
|
||||
KnownActors.retrieve(
|
||||
profile,
|
||||
token,
|
||||
function(load_ok, actor, failure_message) {
|
||||
if (load_ok) {
|
||||
this.cc.push(actor)
|
||||
} else {
|
||||
// Collection ?
|
||||
}
|
||||
this.loadCcActors(iter, token, callback)
|
||||
}.bind(this))
|
||||
return
|
||||
}
|
||||
},
|
||||
// Load the object
|
||||
loadObject: function(token, callback) {
|
||||
// Fetch the object if there is one
|
||||
if (this.raw.object) {
|
||||
ASObject.fetch(this.raw.object, token, function(load_ok, activity_object, failure_message) {
|
||||
if (load_ok) {
|
||||
this.object = activity_object
|
||||
}
|
||||
callback(load_ok, failure_message)
|
||||
}.bind(this))
|
||||
}
|
||||
},
|
||||
// Load everything
|
||||
load: function(token, callback) {
|
||||
this.loadActors(token, function(load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
this.loadObject(token, callback)
|
||||
} else {
|
||||
callback(false, failure_message)
|
||||
this.loaded = true
|
||||
//
|
||||
var error_msg = ''
|
||||
// to
|
||||
this.loadAudience(this.data.to, this.to, function (load_ok, failure_message) {
|
||||
if (!load_ok) {
|
||||
error_msg = error_msg + failure_message + '<br/>'
|
||||
}
|
||||
// cc
|
||||
this.loadAudience(this.data.cc, this.cc, function (load_ok, failure_message) {
|
||||
if (!load_ok) {
|
||||
error_msg = error_msg + failure_message + '<br/>'
|
||||
}
|
||||
// object
|
||||
if (this.object) {
|
||||
this.object.loadAll(function (load_ok, failure_message) {
|
||||
if (!load_ok) {
|
||||
error_msg = error_msg + failure_message + '<br/>'
|
||||
}
|
||||
callback(true, error_msg === '' ? undefined : error_msg)
|
||||
}.bind(this))
|
||||
} else {
|
||||
callback(true, error_msg === '' ? undefined : error_msg)
|
||||
}
|
||||
}.bind(this))
|
||||
}.bind(this))
|
||||
},
|
||||
// Load an audience array
|
||||
loadAudience: function (from, to, callback) {
|
||||
if (from && Array.isArray(from)) {
|
||||
this.loadAudienceIter(from.values(), to, callback, '')
|
||||
} else if (from && typeof from === 'string') {
|
||||
// Only one element in array
|
||||
this.loadAudienceIter([from].values(), to, callback, '')
|
||||
} else {
|
||||
callback(true, undefined)
|
||||
}
|
||||
},
|
||||
loadAudienceIter: function (iter, to, callback, error_msg) {
|
||||
const next = iter.next()
|
||||
if (next.done) {
|
||||
callback(true, error_msg)
|
||||
} else {
|
||||
const act = next.value
|
||||
var err = error_msg
|
||||
if (typeof act === 'string') {
|
||||
// string => id of actor
|
||||
KnownActors.retrieve(act, undefined, function (load_ok, actor, failure_message) {
|
||||
if (load_ok) {
|
||||
to.push(actor)
|
||||
} else {
|
||||
err = err + failure_message + '<br/>'
|
||||
}
|
||||
})
|
||||
} else if (typeof act === 'object') {
|
||||
// Actor is present as object
|
||||
const actor = new Actor()
|
||||
actor.loadFromASActor(this.data.actor, function (load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
// Store actors in KnownActors
|
||||
KnownActors.set(this.actor.data.id, this.actor)
|
||||
to.push(actor)
|
||||
} else {
|
||||
err = err + failure_message + '<br/>'
|
||||
}
|
||||
}.bind(this))
|
||||
}
|
||||
this.loadAudienceIter(iter, to, callback, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
43
src/actor.js
43
src/actor.js
|
@ -84,30 +84,35 @@ Actor.prototype = {
|
|||
// Fetch the activity actor
|
||||
Fetcher.get(profile_url, undefined, function(load_ok, fetched_actor, failure_message) {
|
||||
if (load_ok) {
|
||||
this.data = fetched_actor
|
||||
// Fetch a few properties
|
||||
this.data.fetchAttributeList(
|
||||
['preferredUsername', 'name', 'summary', 'icon', 'endpoints'],
|
||||
undefined,
|
||||
function (ok, error) {
|
||||
if (ok) {
|
||||
if (!this.name) {
|
||||
this.name = this.data.preferredUsername
|
||||
}
|
||||
if (!this.server) {
|
||||
this.server = profile_url.replace('http://', '').replace('https://', '').split(/[/?#]/)[0]
|
||||
}
|
||||
this.valid = true
|
||||
callback(true, undefined)
|
||||
} else {
|
||||
callback(false, error)
|
||||
}
|
||||
}.bind(this))
|
||||
this.loadFromASActor(fetched_actor, callback)
|
||||
} else {
|
||||
callback(false, failure_message)
|
||||
}
|
||||
}.bind(this))
|
||||
},
|
||||
// Load from an ASActor
|
||||
loadFromASActor: function(as_actor, callback) {
|
||||
// Store the actor
|
||||
this.data = as_actor
|
||||
// Fetch a few properties
|
||||
this.data.fetchAttributeList(
|
||||
['preferredUsername', 'name', 'summary', 'icon', 'endpoints'],
|
||||
undefined,
|
||||
function (ok, error) {
|
||||
if (ok) {
|
||||
if (!this.name) {
|
||||
this.name = this.data.preferredUsername
|
||||
}
|
||||
if (!this.server) {
|
||||
this.server = this.data.id.replace('http://', '').replace('https://', '').split(/[/?#]/)[0]
|
||||
}
|
||||
this.valid = true
|
||||
callback(true, undefined)
|
||||
} else {
|
||||
callback(false, error)
|
||||
}
|
||||
}.bind(this))
|
||||
},
|
||||
// Fill the values of actor from fixed data
|
||||
// Usefull for displaying non-actors appearing in audience fields
|
||||
fromDummyData: function(name, server, display_name, url) {
|
||||
|
|
|
@ -11,7 +11,7 @@ const KnownActors = {
|
|||
set: function(profile, actor) {
|
||||
KnownActors.actors[profile] = actor
|
||||
// Make a false actor for followers
|
||||
if (actor.data.followers) {
|
||||
if (actor && actor.data && actor.data.followers) {
|
||||
const followers = new Actor()
|
||||
followers.fromDummyData(actor.address(), 'followers', 'Followers of ' + actor.displayName(), actor.data.followers)
|
||||
KnownActors.actors[actor.data.followers] = followers;
|
||||
|
|
139
src/timeline.js
139
src/timeline.js
|
@ -1,8 +1,21 @@
|
|||
const {Fetcher} = require('./activity-vocabulary.js')
|
||||
const {Activity} = require('./activity.js')
|
||||
const {KnownActivities} = require('./known-activities.js')
|
||||
|
||||
// Already fetched activities, as a map, in order to be able to fetch them by their id
|
||||
const KnownActivities = {
|
||||
// Cache
|
||||
activities: {},
|
||||
// Methods
|
||||
get: function(id) {
|
||||
return KnownActivities.activities[id]
|
||||
},
|
||||
set: function(id, obj) {
|
||||
KnownActivities.activities[id] = obj
|
||||
}
|
||||
}
|
||||
|
||||
// Timeline class
|
||||
// Represent a collection of activities
|
||||
// Represent a paginated collection of activities
|
||||
var Timeline = function() {}
|
||||
Timeline.prototype = {
|
||||
// attributes
|
||||
|
@ -14,84 +27,80 @@ Timeline.prototype = {
|
|||
next: undefined,
|
||||
// Token used when loading -- for loading prev/next within the same context
|
||||
token: undefined,
|
||||
//
|
||||
// Load a timeline from a url
|
||||
load: function(url, token, callback) {
|
||||
this.token = token
|
||||
const request = new XMLHttpRequest()
|
||||
request.onreadystatechange = function() {
|
||||
if (request.readyState == 4 && request.status == 200) {
|
||||
var answer = JSON.parse(request.responseText)
|
||||
if (answer.type === 'OrderedCollection' && answer.first && typeof answer.first === 'string') {
|
||||
// First is a link => load again with the first
|
||||
this.load(answer.first, token, callback)
|
||||
} else if (answer.type === 'OrderedCollection' && answer.first && answer.first.type === 'OrderedCollectionPage') {
|
||||
this.activities = []
|
||||
this.prev = answer.first.prev
|
||||
this.next = answer.first.next
|
||||
this.parseActivities(answer.first.orderedItems, token, callback)
|
||||
} else if (answer.type === 'OrderedCollection' && answer.orderedItems) {
|
||||
// A timeline must be reloaded each time
|
||||
Fetcher.refresh(url, token, function(load_ok, fetched_timeline, failure_message) {
|
||||
if (load_ok) {
|
||||
if (fetched_timeline.type === 'OrderedCollection' && fetched_timeline.first) {
|
||||
// Collection is paginated
|
||||
// Fetch the attribute
|
||||
fetched_timeline.fetchAttribute('first', token, function (ok, error) {
|
||||
if (load_ok) {
|
||||
this.activities = []
|
||||
this.prev = fetched_timeline.prev
|
||||
this.next = fetched_timeline.next
|
||||
this.parsePage(fetched_timeline.first, token, callback)
|
||||
} else {
|
||||
callback(false, error)
|
||||
}
|
||||
}.bind(this))
|
||||
} else if (fetched_timeline.type === 'OrderedCollection' && fetched_timeline.orderedItems) {
|
||||
// Collection is not paginated
|
||||
this.activities = []
|
||||
this.prev = answer.prev
|
||||
this.next = answer.next
|
||||
this.parseActivities(answer.orderedItems, token, callback)
|
||||
} else if (answer.type === 'OrderedCollectionPage') {
|
||||
this.prev = fetched_timeline.prev
|
||||
this.next = fetched_timeline.next
|
||||
this.parsePage(fetched_timeline, token, callback)
|
||||
} else if (fetched_timeline.type === 'OrderedCollectionPage') {
|
||||
this.activities = []
|
||||
this.prev = answer.prev
|
||||
this.next = answer.next
|
||||
this.parseActivities(answer.orderedItems, token, callback)
|
||||
this.prev = fetched_timeline.prev
|
||||
this.next = fetched_timeline.next
|
||||
this.parsePage(fetched_timeline, token, callback)
|
||||
} else {
|
||||
callback(false, 'Unexpected answer from server when fetching activity collection.')
|
||||
console.log(answer)
|
||||
}
|
||||
} else if (request.readyState == 4) {
|
||||
callback(false, 'Server error (' + request.status + ') when fetching activity collection.')
|
||||
} else {
|
||||
// Propagate error
|
||||
callback(false, failure_message)
|
||||
}
|
||||
}.bind(this)
|
||||
request.open('GET', url, true)
|
||||
if (token) {
|
||||
request.setRequestHeader('Authorization', 'Bearer ' + token)
|
||||
}
|
||||
request.setRequestHeader('Content-Type', 'application/activity+json')
|
||||
request.setRequestHeader('Accept', 'application/activity+json')
|
||||
request.send()
|
||||
}.bind(this))
|
||||
},
|
||||
parseActivities: function(raw_activities, token, callback) {
|
||||
// raw_activities must be an array
|
||||
if (!Array.isArray(raw_activities)) {
|
||||
console.log(raw_activities)
|
||||
callback(false, 'Unexpected format for activity collection.')
|
||||
return
|
||||
}
|
||||
// Get the next activity
|
||||
const raw_act = raw_activities.shift()
|
||||
if (raw_act && typeof raw_act === 'string') {
|
||||
// link, and not the object itself => fetch the activity
|
||||
KnownActivities.retrieve(raw_act, token, function(load_ok, activity, failure_message) {
|
||||
if (load_ok) {
|
||||
// Push to the list of activities
|
||||
this.activities.push(activity)
|
||||
}
|
||||
this.parseActivities(raw_activities, token, callback)
|
||||
}.bind(this))
|
||||
} else if (raw_act) {
|
||||
const act = new Activity(raw_act)
|
||||
act.load(
|
||||
token,
|
||||
function(load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
// Push to the list of activities
|
||||
this.activities.push(act)
|
||||
// Add the activity to the known activities
|
||||
KnownActivities.set(act.id, act)
|
||||
}
|
||||
this.parseActivities(raw_activities, token, callback)
|
||||
}.bind(this))
|
||||
parsePage: function (collectionPage, token, callback) {
|
||||
// Fetch attributes of collectionPage
|
||||
collectionPage.fetchAttribute('orderedItems', token, function (load_ok, failure_message) {
|
||||
if (load_ok) {
|
||||
// Elements of the page have been fetched
|
||||
// For each, convert them to Activity and put them in this.activities
|
||||
this.addActivity(collectionPage.orderedItems.values(), token, callback, '')
|
||||
} else {
|
||||
callback(false, failure_message)
|
||||
}
|
||||
}.bind(this))
|
||||
},
|
||||
addActivity: function (iter, token, callback, error_msg) {
|
||||
const next = iter.next()
|
||||
if (next.done) {
|
||||
callback(true, error_msg === '' ? undefined : error_msg)
|
||||
} else {
|
||||
callback(true, undefined)
|
||||
var err = error_msg
|
||||
const act = new Activity(next.value, token)
|
||||
act.loadNeeded(function (load_ok, failure_message) {
|
||||
if (!load_ok) {
|
||||
err = err + failure_message + '<br/>'
|
||||
}
|
||||
// Whether it's ok or not, push
|
||||
this.activities.push(act)
|
||||
// Store in known activities
|
||||
KnownActivities.set(act.id, act)
|
||||
// next
|
||||
this.addActivity(iter, token, callback, err)
|
||||
}.bind(this))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Exported structures
|
||||
exports.Timeline = Timeline
|
||||
exports.KnownActivities = KnownActivities
|
||||
|
|
Loading…
Reference in New Issue