Display author reputations

This commit is contained in:
Ladd Hoffman 2023-04-16 07:59:12 -05:00
parent 7ae5ff9b03
commit 77643acf6f
3 changed files with 42 additions and 4 deletions

View File

@ -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}
<table><tr>
<td>reputation</td>
<td>${displayNumber(this.getReputation())}</td>
</tr></table>`
.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());
}
}

View File

@ -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) {

View File

@ -15,6 +15,10 @@ export class Vertex {
(edge) => edge.type === type,
);
}
setDisplayLabel(label) {
this.graph.setVertexDisplayLabel(this.id, label);
}
}
export class Edge {