diff --git a/forum-network/src/classes/dao/forum.js b/forum-network/src/classes/dao/forum.js index cec6fdb..5d2a758 100644 --- a/forum-network/src/classes/dao/forum.js +++ b/forum-network/src/classes/dao/forum.js @@ -69,6 +69,34 @@ class Post extends Actor { } } +class Author { + constructor(forum, publicKey) { + this.forum = forum; + this.publicKey = publicKey; + this.name = publicKey; + } + + getReputation() { + const authorVertex = this.forum.graph.getVertex(this.publicKey); + if (!authorVertex) { + return 0; + } + const authorEdges = authorVertex.getEdges(EdgeTypes.AUTHOR, false); + const tokenValues = authorEdges.map(({ data: { tokenId } }) => this.forum.dao.reputation.valueOf(tokenId)); + console.log('getReputation', { authorEdges, tokenValues }); + return tokenValues.reduce((value, total) => total += value, 0); + } + + getLabel() { + return `${this.name} + + + +
reputation${displayNumber(this.getReputation())}
` + .replaceAll(/\n\s*/g, ''); + } +} + /** * Purpose: * - Forum: Maintain a directed, acyclic, graph of positively and negatively weighted citations. @@ -129,8 +157,9 @@ export class Forum extends ReputationHolder { this.graph.setVertexDisplayLabel(post.id, post.getLabel()); const addAuthorToGraph = (publicKey, weight, authorTokenId) => { + const author = new Author(this, publicKey); const authorVertex = this.graph.getVertex(publicKey) - ?? this.graph.addVertex(VertexTypes.AUTHOR, publicKey, { name: publicKey, publicKey }, publicKey); + ?? this.graph.addVertex(VertexTypes.AUTHOR, publicKey, author, author.getLabel()); const authorEdge = this.graph.addEdge( EdgeTypes.AUTHOR, postVertex, @@ -184,9 +213,10 @@ export class Forum extends ReputationHolder { // Transfer ownership of the minted tokens to the authors for (const authorEdge of postVertex.getEdges(EdgeTypes.AUTHOR, true)) { const authorVertex = authorEdge.to; - const { publicKey } = authorVertex.data; + const author = authorVertex.data; const { tokenId: authorTokenId } = authorEdge.data; - this.dao.reputation.transfer(this.id, publicKey, authorTokenId); + this.dao.reputation.transfer(this.id, author.publicKey, authorTokenId); + authorVertex.setDisplayLabel(author.getLabel()); } } diff --git a/forum-network/src/classes/reputation/reputation-token.js b/forum-network/src/classes/reputation/reputation-token.js index a48b00e..65847d6 100644 --- a/forum-network/src/classes/reputation/reputation-token.js +++ b/forum-network/src/classes/reputation/reputation-token.js @@ -79,7 +79,11 @@ export class ReputationTokenContract extends ERC721 { } valueOf(tokenId) { - return this.values.get(tokenId); + const value = this.values.get(tokenId); + if (value === undefined) { + throw new Error(`Token not found: ${tokenId}`); + } + return value; } availableValueOf(tokenId) { diff --git a/forum-network/src/classes/supporting/wdag.js b/forum-network/src/classes/supporting/wdag.js index 1b96124..ac35558 100644 --- a/forum-network/src/classes/supporting/wdag.js +++ b/forum-network/src/classes/supporting/wdag.js @@ -15,6 +15,10 @@ export class Vertex { (edge) => edge.type === type, ); } + + setDisplayLabel(label) { + this.graph.setVertexDisplayLabel(this.id, label); + } } export class Edge {