Add a dummy actor for 'public' audience.

This commit is contained in:
Feufochmar 2020-03-26 15:14:15 +01:00
parent d660bf8461
commit f1f60b4741
3 changed files with 49 additions and 56 deletions

View File

@ -2,12 +2,11 @@ const {Actor} = require('./actor.js')
const {KnownActors} = require('./known-actors.js')
// Activity class
var Activity = function(raw_activity) {
const Activity = function(raw_activity) {
this.raw = raw_activity
this.type = raw_activity.type
this.published = raw_activity.published ? new Date(raw_activity.published) : undefined
this.object = raw_activity.object
this.public_visibility = 'non'
// actor, to, cc are filled in loadActors
this.actor = undefined
this.to = []
@ -17,7 +16,7 @@ Activity.prototype = {
// Load the actors present in the actor, to and cc fields
loadActors: function(callback) {
// Load the actor
var profile = this.raw.actor
const profile = this.raw.actor
KnownActors.retrieve(
profile,
function(load_ok, failure_message) {
@ -36,7 +35,7 @@ Activity.prototype = {
},
// Load the "To" array
loadToActors: function(iter, callback) {
var next = iter.next()
const next = iter.next()
if (next.done) {
// Finished: load the "cc" array (if possible)
if (this.raw.cc) {
@ -45,46 +44,36 @@ Activity.prototype = {
callback(true, 'ok')
}
} else {
var profile = next.value
if (profile === 'https://www.w3.org/ns/activitystreams#Public') {
this.public_visibility = 'to'
this.loadToActors(iter, callback)
} else {
KnownActors.retrieve(
profile,
function(load_ok, failure_message) {
if (load_ok) {
this.to.push(KnownActors.get(profile))
} else {
// Collection ?
}
this.loadToActors(iter, callback)
}.bind(this))
}
const profile = next.value
KnownActors.retrieve(
profile,
function(load_ok, failure_message) {
if (load_ok) {
this.to.push(KnownActors.get(profile))
} else {
// Collection ?
}
this.loadToActors(iter, callback)
}.bind(this))
}
},
// Load the "Cc" array
loadCcActors: function(iter, callback) {
var next = iter.next()
const next = iter.next()
if (next.done) {
callback(true, 'ok')
} else {
var profile = next.value
if (profile === 'https://www.w3.org/ns/activitystreams#Public') {
this.public_visibility = 'cc'
this.loadCcActors(iter, callback)
} else {
KnownActors.retrieve(
profile,
function(load_ok, failure_message) {
if (load_ok) {
this.cc.push(KnownActors.get(profile))
} else {
// Collection ?
}
this.loadCcActors(iter, callback)
}.bind(this))
}
const profile = next.value
KnownActors.retrieve(
profile,
function(load_ok, failure_message) {
if (load_ok) {
this.cc.push(KnownActors.get(profile))
} else {
// Collection ?
}
this.loadCcActors(iter, callback)
}.bind(this))
}
}
}

View File

@ -99,10 +99,10 @@ Actor.prototype = {
loadFromProfileUrl: function(profile_url, callback) {
this.urls.profile = profile_url
// Use ActivityPub protocol to get the user infos
var request = new XMLHttpRequest()
const request = new XMLHttpRequest()
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
var answer = JSON.parse(request.responseText)
const answer = JSON.parse(request.responseText)
if (answer && this.isExpectedActorType(answer.type)) {
this.raw = answer
// name and server are previously filled if called from loadFromNameAndServer
@ -143,6 +143,15 @@ Actor.prototype = {
request.setRequestHeader('Content-Type', 'application/activity+json')
request.setRequestHeader('Accept', 'application/activity+json')
request.send()
},
// Fill the values of actor from fixed data
// Usefull for displaying non-actors appearing in audience fields
fromDummyData: function(name, server, display_name, url) {
this.valid = true
this.name = name
this.server = server
this.info.display_name = display_name
this.urls.profile = url
}
}

View File

@ -1,30 +1,20 @@
const {Actor} = require('./actor.js')
// A cache for actors, to avoid loading same actors several times when loading timelines
var KnownActors = {
// Cache of real actors
const KnownActors = {
// Cache of actors
actors: {},
// Cache of followers' collection of actors, as they can be recipients of messages
followers: {},
// Methods
get: function(profile) {
var act = KnownActors.actors[profile]
if (!act) {
act = KnownActors.followers[profile]
}
return act
return KnownActors.actors[profile]
},
set: function(profile, actor) {
KnownActors.actors[profile] = actor
// Make a false actor for followers
if (actor.urls.followers) {
var followers = new Actor()
followers.valid = true
followers.name = 'followers:' + actor.name
followers.server = actor.server
followers.info.display_name = "Followers of " + actor.displayName()
followers.urls.profile = actor.urls.followers
KnownActors.followers[actor.urls.followers] = followers;
const followers = new Actor()
followers.fromDummyData(actor.address(), 'followers', 'Followers of ' + actor.displayName(), actor.urls.followers)
KnownActors.actors[actor.urls.followers] = followers;
}
},
// Retrieve an actor
@ -32,7 +22,7 @@ var KnownActors = {
if (KnownActors.get(profile)) {
callback(true, 'ok')
} else {
var actor = new Actor()
const actor = new Actor()
actor.loadFromProfileUrl(
profile,
function(load_ok, failure_message) {
@ -43,5 +33,10 @@ var KnownActors = {
}
}
// A representation of "everyone" in Actor class
const publicActor = new Actor()
publicActor.fromDummyData('', 'public', 'Everyone', 'https://www.w3.org/ns/activitystreams#Public')
KnownActors.set(publicActor.urls.profile, publicActor)
// Exported structures
exports.KnownActors = KnownActors