Skip to content

Commit

Permalink
[subgraph] Fix article authors bug
Browse files Browse the repository at this point in the history
  • Loading branch information
asgeir-s committed Mar 6, 2023
1 parent 9103bcf commit d5b87af
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Lets an account update an article. The present properties will overwrite old pro
| id* | String | ID of article to update (available from the subgraph or created manually using the `event.transaction.hash + "-" + event.logIndex` from the article creation event) |
| article | String | IPFS hash (pointing to a Markdown document) or a markdown formatted string |
| title | String | Content title |
| authors | String Array | Author addresses or names |
| authors | String Array | Author addresses or names. Providing an empty string (`""`) will set authors to `[]`. |
| tags | String Array | Relevant content tags. Providing an empty string (`""`) will set tags to `[]`. |
| description | String | Content description. Providing an empty string (`""`) will set description to `null`. |
| image | String | IPFS hash for an image string. Providing an empty string (`""`) will set image to `null`. |
Expand Down
5 changes: 3 additions & 2 deletions packages/subgraph/src/article.mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ export function handleArticleAction(subAction: String, content: TypedMap<string,
article.title = title
hasChanges = true
}
const authors = jsonToArrayString(content.get("authors"))
if (authors != []) {
const authorsData = content.get("authors")
const authors = jsonToArrayString(authorsData)
if (authorsData != null) {
article.authors = authors
hasChanges = true
}
Expand Down
47 changes: 45 additions & 2 deletions packages/subgraph/tests/article.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ test("An account can delete all tags for an article in a publication where the a
clearStore()
})

test("An account can updating an article with tags should not change the tags if no tags key is provided in the update object where the account has `article/update` permissions", () => {
test("An account can updating an article with should not change keys that are not provided in the update object where the account has `article/update` permissions", () => {
const user = Address.fromString("0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7")
const publicationContent = `{
"action": "publication/create",
Expand All @@ -369,14 +369,16 @@ test("An account can updating an article with tags should not change the tags if
"publicationId": "${publicationId}",
"article": "QmbtLeBCvT1FW1Kr1JdFCPAgsVsgowg3zMJQS8eFrwPP2j",
"title": "My First Blog Post",
"tags": ["tag1", "tag2"]
"tags": ["tag1", "tag2"],
"authors": ["author1", "author2"]
}`

const newArticlePostEvent = createNewPostEvent(user, articleContent, PUBLICATION_TAG)
const articleId = getArticleId(newArticlePostEvent)
handleNewPost(newArticlePostEvent)

assert.fieldEquals(ARTICLE_ENTITY_TYPE, articleId, "tags", "[tag1, tag2]")
assert.fieldEquals(ARTICLE_ENTITY_TYPE, articleId, "authors", "[author1, author2]")

const updateArticleContent = `{
"action": "article/update",
Expand All @@ -389,6 +391,47 @@ test("An account can updating an article with tags should not change the tags if

// check that the article is updated
assert.fieldEquals(ARTICLE_ENTITY_TYPE, articleId, "tags", "[tag1, tag2]")
assert.fieldEquals(ARTICLE_ENTITY_TYPE, articleId, "authors", "[author1, author2]")

clearStore()
})

test("An account can delete all authors for an article in a publication where the account has `article/update` permissions", () => {
const user = Address.fromString("0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7")
const publicationContent = `{
"action": "publication/create",
"title": "My First Publication"
}`

const newPublicationPostEvent = createNewPostEvent(user, publicationContent, PUBLICATION_TAG)
const publicationId = getPublicationId(newPublicationPostEvent)
handleNewPost(newPublicationPostEvent)

const articleContent = `{
"action": "article/create",
"publicationId": "${publicationId}",
"article": "QmbtLeBCvT1FW1Kr1JdFCPAgsVsgowg3zMJQS8eFrwPP2j",
"title": "My First Blog Post",
"authors": ["author1", "author2"]
}`

const newArticlePostEvent = createNewPostEvent(user, articleContent, PUBLICATION_TAG)
const articleId = getArticleId(newArticlePostEvent)
handleNewPost(newArticlePostEvent)

assert.fieldEquals(ARTICLE_ENTITY_TYPE, articleId, "authors", "[author1, author2]")

const updateArticleContent = `{
"action": "article/update",
"id": "${articleId}",
"authors": ""
}`

const newUpdatePostEvent = createNewPostEvent(user, updateArticleContent, PUBLICATION_TAG)
handleNewPost(newUpdatePostEvent)

// check that the article is updated
assert.fieldEquals(ARTICLE_ENTITY_TYPE, articleId, "authors", "[]")

clearStore()
})

0 comments on commit d5b87af

Please sign in to comment.