Skip to content

Commit

Permalink
Merge pull request #221 from gnosis/fix-subgraph-delete-image
Browse files Browse the repository at this point in the history
Fix various subgraph bugs
  • Loading branch information
juliopavila authored Mar 6, 2023
2 parents c7d4a24 + d5b87af commit 400ffcc
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 22 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Any properties can be added to articles; this is just a representation of the pr

Lets any account create a new publication. On creation, the message sender gets all permissions. This can be edited via the `publication/permissions` action.

| Property | Type | Value |
| ----------- | :----------: | ---------------------------------------------------------------- |
| action\* | String | "publication/create" |
| title\* | String | Publication title |
| tags | String Array | Relevant publication tags |
| description | String | Publication description |
| image | String | IPFS hash (pointing to a image) or a BASE64 encoded image string |
| Property | Type | Value |
| ----------- | :----------: | ------------------------- |
| action\* | String | "publication/create" |
| title\* | String | Publication title |
| tags | String Array | Relevant publication tags |
| description | String | Publication description |
| image | String | IPFS hash for an image |

#### Update Publication

Expand All @@ -35,9 +35,9 @@ Lets an account update a publication's information. The present properties will
| action* | String | "publication/update" |
| id* | String | ID of publication to update (available from the subgraph or created manually using the `event.transaction.hash + "-" + event.logIndex` from the publication creation event) |
| title | String | Content title |
| tags | String Array | Relevant content tags |
| tags | String Array | Relevant content tags. Providing an empty string (`""`) will set tags to `[]`. |
| description | String | Content description |
| image | String | IPFS hash (pointing to a image) or a BASE64 encoded image string |
| image | String | IPFS hash for an image. Providing an empty string (`""`) will set image to `null`. |

#### Delete Publication

Expand Down Expand Up @@ -80,7 +80,7 @@ Lets an Account post a new article to a publication. The message sender needs `a
| authors | String Array | Author addresses or names |
| tags | String Array | Relevant content tags |
| description | String | Content description |
| image | String | IPFS hash (pointing to a image) or a BASE64 encoded image string |
| image | String | IPFS hash for an image |

#### Update Article

Expand All @@ -91,10 +91,10 @@ 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 |
| tags | String Array | Relevant content tags |
| 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 (pointing to a image) or a BASE64 encoded image string. Providing an empty string (`""`) will set image to `null`. |
| image | String | IPFS hash for an image string. Providing an empty string (`""`) will set image to `null`. |

#### Delete Article

Expand Down
10 changes: 6 additions & 4 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 All @@ -95,8 +96,9 @@ export function handleArticleAction(subAction: String, content: TypedMap<string,
}
hasChanges = true
}
const tags = jsonToArrayString(content.get("tags"))
if (tags != []) {
const tagsData = content.get("tags")
const tags = jsonToArrayString(tagsData)
if (tagsData != null) {
article.tags = tags
hasChanges = true
}
Expand Down
16 changes: 11 additions & 5 deletions packages/subgraph/src/publication.mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,19 @@ export function handlePublicationAction(subAction: String, content: TypedMap<str
publication.description = description
hasChanges = true
}
const image = jsonToString(content.get("image"))
if (image != "") {
publication.image = image
const imageData = content.get("image")
const image = jsonToString(imageData)
if (imageData != null) {
if (image == "") {
publication.image = null
} else {
publication.image = image
}
hasChanges = true
}
const tags = jsonToArrayString(content.get("tags"))
if (tags != []) {
const tagsData = content.get("tags")
const tags = jsonToArrayString(tagsData)
if (tagsData != null) {
publication.tags = tags
hasChanges = true
}
Expand Down
167 changes: 167 additions & 0 deletions packages/subgraph/tests/article.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,170 @@ test("An account can NOT update an article in a publication where the account ha

clearStore()
})

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

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

const articleTitle = "My First Blog Post"
const article = "QmbtLeBCvT1FW1Kr1JdFCPAgsVsgowg3zMJQS8eFrwPP2j"
const articleImage = "QmaAgvb4fHsP2HQmdiuWc6k5BBsmxzgdBibgQuGurhfL5m"
const articleContent = `{
"action": "article/create",
"publicationId": "${publicationId}",
"article": "${article}",
"title": "${articleTitle}",
"image": "${articleImage}"
}`

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

assert.fieldEquals(ARTICLE_ENTITY_TYPE, articleId, "image", articleImage)

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

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

// check that the article is updated
assert.fieldEquals(ARTICLE_ENTITY_TYPE, articleId, "image", "null")

clearStore()
})

test("An account can delete all tags 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",
"tags": ["tag1", "tag2"]
}`

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

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

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

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

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

clearStore()
})

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",
"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",
"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",
"id": "${articleId}",
"title": "New Title"
}`

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

// 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()
})
91 changes: 91 additions & 0 deletions packages/subgraph/tests/publication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,94 @@ test("The account that created the publication can add new, update and delete a

clearStore()
})

test("An account can delete the publication image of a publication where the account has `publication/update` permissions", () => {
const user = Address.fromString("0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7")
const publicationTitle = "My First Publication"
const publicationImage = "QmaAgvb4fHsP2HQmdiuWc6k5BBsmxzgdBibgQuGurhfL5m"
const publicationContent = `{
"action": "publication/create",
"title": "${publicationTitle}",
"image": "${publicationImage}"
}`

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

assert.fieldEquals(PUBLICATION_ENTITY_TYPE, publicationId, "image", publicationImage)

const newPublicationTitle = "My First Edited Publication"
const newPublicationDescription = "This is actually the first description for this publication."
const publicationUpdate = `{
"action": "publication/update",
"id": "${publicationId}",
"title": "${newPublicationTitle}",
"description": "${newPublicationDescription}",
"image": ""
}`

const newPublicationUpdatePostEvent = createNewPostEvent(user, publicationUpdate, PUBLICATION_TAG)
handleNewPost(newPublicationUpdatePostEvent)

assert.fieldEquals(PUBLICATION_ENTITY_TYPE, publicationId, "image", "null")

clearStore()
})

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

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

assert.fieldEquals(PUBLICATION_ENTITY_TYPE, publicationId, "tags", "[tag1, tag2]")

const publicationUpdate = `{
"action": "publication/update",
"id": "${publicationId}",
"title": "New Title",
"tags": ""
}`

const newPublicationUpdatePostEvent = createNewPostEvent(user, publicationUpdate, PUBLICATION_TAG)
handleNewPost(newPublicationUpdatePostEvent)

assert.fieldEquals(PUBLICATION_ENTITY_TYPE, publicationId, "tags", "[]")

clearStore()
})

test("An account can updating a publication with tags should not change the tags if no tags key is provided in the update object where the account has `publication/update` permissions", () => {
const user = Address.fromString("0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7")
const publicationContent = `{
"action": "publication/create",
"title": "My First Publication",
"tags": ["tag1", "tag2"]
}`

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

assert.fieldEquals(PUBLICATION_ENTITY_TYPE, publicationId, "tags", "[tag1, tag2]")

const publicationUpdate = `{
"action": "publication/update",
"id": "${publicationId}",
"title": "New Title"
}`

const newPublicationUpdatePostEvent = createNewPostEvent(user, publicationUpdate, PUBLICATION_TAG)
handleNewPost(newPublicationUpdatePostEvent)

assert.fieldEquals(PUBLICATION_ENTITY_TYPE, publicationId, "tags", "[tag1, tag2]")

clearStore()
})

1 comment on commit 400ffcc

@vercel
Copy link

@vercel vercel bot commented on 400ffcc Mar 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

tabula – ./

tabula-git-main-gnosis-guild.vercel.app
tabula-gnosis-guild.vercel.app
tabula-eight.vercel.app

Please sign in to comment.