Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metaprotocol labeling: mappings and protobufs #4987

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
413 changes: 413 additions & 0 deletions distributor-node/src/services/networking/query-node/schema.graphql

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions metadata-protobuf/proto/WorkingGroups.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,97 @@ message ModeratePost {
required string rationale = 2;
}

message CreateTag {
// Name of new Tag, e.g : roadmap
required string name = 1;

// Description of new Tag, e.g : Roadmap covering the Joystream ecosystem
required string description = 2;

// Type of new Tag : e.g : roadmap
required string type = 3;

// Visibility of new Tag : e.g : true
required bool visibility = 4;
}

message UpdateTag {
// ID of tag
required string tag_id = 1;

// name of tag
required string name = 2;
kdembler marked this conversation as resolved.
Show resolved Hide resolved

// Updated Description of Tag
optional string description = 3;

// Updated Type of Tag
optional string type = 4;

// Updated Visibility of Tag
optional bool visibility = 5;
}

message AssignTagToThread {
// ID of tag
required string tag_id = 1;

// ID of thread
required string thread_id = 2;
}

message UnassignTagFromThread {
// ID of tag
required string tag_id = 1;

// ID of thread
required string thread_id = 2;
}

message AssignTagToProposal {
// ID of tag
required string tag_id = 1;

// ID of proposal
required string proposal_id = 2;
}

message UnassignTagFromProposal {
// ID of tag
required string tag_id = 1;

// ID of proposal
required string proposal_id = 2;
}

message AllowTagToWorker {
// ID of tag
required string tag_id = 1;

// ID of worker
required uint64 worker_id = 2;
}

message DisallowTagToWorker {
// ID of tag
required string tag_id = 1;

// ID of worker
required uint64 worker_id = 2;
}
kdembler marked this conversation as resolved.
Show resolved Hide resolved

message RemarkMetadataAction {
oneof action {
ModeratePost moderate_post = 1;
VerifyValidator verify_validator = 2;
CreateTag create_tag = 3;
UpdateTag update_tag = 4;
AssignTagToThread assign_tag_to_thread = 5;
UnassignTagFromThread unassign_tag_from_thread = 6;
AssignTagToProposal assign_tag_to_proposal = 7;
UnassignTagFromProposal unassign_tag_from_proposal = 8;
AllowTagToWorker allow_tag_to_worker = 9;
DisallowTagToWorker disallow_tag_to_worker = 10;
}
}

Expand Down
2 changes: 1 addition & 1 deletion query-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- Refactor of mappings for more better handling of error cases. [#4856](https://github.com/Joystream/joystream/pull/4856)
- Bug fix [#4855](https://github.com/Joystream/joystream/issues/4855)
- Add support for UpdateGlobalNftLimit proposal.
- Add support for UpdateGlobalNftLimit proposal.

### 1.6.0

Expand Down
6 changes: 6 additions & 0 deletions query-node/mappings/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,10 @@ export enum MetaprotocolTxError {

// Channel errors
InvalidChannelRewardAccount = 'InvalidChannelRewardAccount',

// Tag errors
TagAlreadyExists = 'TagAlreadyExists',
TagIsNotLead = 'TagIsNotLead',
TagNotFound = 'TagNotFound',
TagPermNotAllowed = 'TagPermNotAllowed',
}
270 changes: 270 additions & 0 deletions query-node/mappings/src/label/tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
import { DatabaseManager, SubstrateEvent } from '@joystream/hydra-common'
import {
ICreateTag,
IUpdateTag,
IAssignTagToThread,
IAssignTagToProposal,
IUnassignTagFromThread,
IUnassignTagFromProposal,
IAllowTagToWorker,
IDisallowTagToWorker,
} from '@joystream/metadata-protobuf'
import { DecodedMetadataObject } from '@joystream/metadata-protobuf/types'
import {
WorkingGroup,
Tag,
TagToWorker,
TagToThread,
TagToProposal,
ForumThread,
Proposal,
} from 'query-node/dist/model'
import { MetaprotocolTxError, getOneBy, getById, logger, getOneByOrFail, getByIdOrFail } from 'src/common'

export async function processCreateTag(
store: DatabaseManager,
event: SubstrateEvent,
metadata: DecodedMetadataObject<ICreateTag>,
workgroup: WorkingGroup,
isLead: boolean,
workerId: number

Check warning on line 30 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / Ubuntu Checks (18.x)

'workerId' is defined but never used

Check warning on line 30 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / MacOS Checks (18.x)

'workerId' is defined but never used
kdembler marked this conversation as resolved.
Show resolved Hide resolved
): Promise<any> {
const { name, description, type, visibility } = metadata

if (isLead === false) {
return MetaprotocolTxError.TagPermNotAllowed
}

const tagId = `${event.blockNumber}-${event.indexInBlock}`

Check warning on line 38 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / Ubuntu Checks (18.x)

'tagId' is assigned a value but never used

Check warning on line 38 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / MacOS Checks (18.x)

'tagId' is assigned a value but never used

const isTagExists = await getOneBy(store, Tag, { name: name })

if (isTagExists) {
return MetaprotocolTxError.TagAlreadyExists
}
kdembler marked this conversation as resolved.
Show resolved Hide resolved

const newTag = new Tag({
name: name,
description: description,
type: type,
visibility: visibility,
})

await store.save<Tag>(newTag)
logger.info('Tag has been created', { name })

return newTag
}

export async function processUpdateTag(
store: DatabaseManager,
event: SubstrateEvent,
metadata: DecodedMetadataObject<IUpdateTag>,
workingGroup: WorkingGroup,
isLead: boolean,
workerId: number

Check warning on line 65 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / Ubuntu Checks (18.x)

'workerId' is defined but never used

Check warning on line 65 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / MacOS Checks (18.x)

'workerId' is defined but never used
): Promise<any> {
const { tagId, name, description, type, visibility } = metadata

if (isLead === false) {
return MetaprotocolTxError.TagPermNotAllowed
}

const tag: Tag | undefined = await getById(store, Tag, tagId)

if (!tag) {
return MetaprotocolTxError.TagNotFound
}

if (name) {
tag.name = name
}

if (description) {
tag.description = description
}

if (type) {
tag.type = type
}

if (visibility) {
tag.visibility = visibility
}

await store.save<Tag>(tag)

logger.info('Tag has been created', { name })
kdembler marked this conversation as resolved.
Show resolved Hide resolved
return tag
}

export async function processAssignTagToThread(
store: DatabaseManager,
event: SubstrateEvent,
metadata: DecodedMetadataObject<IAssignTagToThread>,
workingGroup: WorkingGroup,
isLead: boolean,
workerId: number
): Promise<any> {
const { tagId, threadId } = metadata

if (!isLead) {
await getOneByOrFail(store, TagToWorker, { tagId: tagId, workerId: workerId.toString() })
kdembler marked this conversation as resolved.
Show resolved Hide resolved
}

await getByIdOrFail(store, ForumThread, threadId)
const tagToThread: TagToThread | undefined = await getOneBy(store, TagToThread, { tagId: tagId, threadId: threadId })

if (!tagToThread) {
const newTagToThread = new TagToThread({
tagId: tagId,
threadId: threadId,
})
await store.save<TagToThread>(newTagToThread)
}

logger.info('TagToThread has been assigned', { tagId, threadId })
return tagToThread
}

export async function processAssignTagToProposal(
store: DatabaseManager,
event: SubstrateEvent,
metadata: DecodedMetadataObject<IAssignTagToProposal>,
workingGroup: WorkingGroup,
isLead: boolean,
workerId: number
): Promise<any> {
const { tagId, proposalId } = metadata

if (!isLead) {
await getOneByOrFail(store, TagToWorker, { tagId: tagId, workerId: workerId.toString() })
}

await getByIdOrFail(store, Proposal, proposalId)
const tagToProposal: TagToProposal | undefined = await getOneBy(store, TagToProposal, {
tagId: tagId,
proposalId: proposalId,
})

if (!tagToProposal) {
const newTagToProposal = new TagToProposal({
tagId: tagId,
proposalId: proposalId,
})
await store.save<TagToProposal>(newTagToProposal)
}

logger.info('TagToProposal has been assigned', { tagId, proposalId })
return tagToProposal
}

export async function processUnassignTagFromThread(
store: DatabaseManager,
event: SubstrateEvent,
metadata: DecodedMetadataObject<IUnassignTagFromThread>,
workingGroup: WorkingGroup,
isLead: boolean,
workerId: number
): Promise<any> {
const { tagId, threadId } = metadata

if (!isLead) {
await getOneByOrFail(store, TagToWorker, { tagId: tagId, workerId: workerId.toString() })
}

await getByIdOrFail(store, ForumThread, threadId)
const tagToThread: TagToThread | undefined = await getOneBy(store, TagToThread, { tagId: tagId, threadId: threadId })

if (tagToThread) {
await store.remove<TagToThread>(tagToThread)
}

logger.info('TagToThread has been unassigned', { tagId, threadId })
return tagToThread
}

export async function processUnassignTagFromProposal(
store: DatabaseManager,
event: SubstrateEvent,
metadata: DecodedMetadataObject<IUnassignTagFromProposal>,
workingGroup: WorkingGroup,
isLead: boolean,
workerId: number
): Promise<any> {
const { tagId, proposalId } = metadata

if (!isLead) {
await getOneByOrFail(store, TagToWorker, { tagId: tagId, workerId: workerId.toString() })
}

await getByIdOrFail(store, Proposal, proposalId)

const tagToProposal: TagToProposal | undefined = await getOneBy(store, TagToProposal, {
tagId: tagId,
proposalId: proposalId,
})

if (tagToProposal) {
await store.remove<TagToProposal>(tagToProposal)
}

logger.info('TagToProposal has been unassigned', { tagId, proposalId })
return tagToProposal
}

export async function processAllowTagToWorker(
store: DatabaseManager,
event: SubstrateEvent,
metadata: DecodedMetadataObject<IAllowTagToWorker>,
workingGroup: WorkingGroup,
isLead: boolean,
workerId: number

Check warning on line 222 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / Ubuntu Checks (18.x)

'workerId' is defined but never used

Check warning on line 222 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / MacOS Checks (18.x)

'workerId' is defined but never used
): Promise<any> {
const { tagId, workerId: assigneeId } = metadata

if (!isLead) {
return MetaprotocolTxError.TagPermNotAllowed
}

const tagToWorker: TagToWorker | undefined = await getOneBy(store, TagToWorker, {
tagId: tagId,
workerId: assigneeId,
})

if (!tagToWorker) {
const newTagToWorker = new TagToWorker({
tagId: tagId,
workerId: assigneeId,
})
await store.save<TagToWorker>(newTagToWorker)
}

logger.info('TagToWorker has been allowed', { tagId, assigneeId })
}

export async function processDisallowTagToWorker(
store: DatabaseManager,
event: SubstrateEvent,
metadata: DecodedMetadataObject<IDisallowTagToWorker>,
workingGroup: WorkingGroup,
isLead: boolean,
workerId: number

Check warning on line 252 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / Ubuntu Checks (18.x)

'workerId' is defined but never used

Check warning on line 252 in query-node/mappings/src/label/tag.ts

View workflow job for this annotation

GitHub Actions / MacOS Checks (18.x)

'workerId' is defined but never used
): Promise<any> {
const { tagId, workerId: assigneeId } = metadata

if (!isLead) {
return MetaprotocolTxError.TagPermNotAllowed
}

const tagToWorker: TagToWorker | undefined = await getOneBy(store, TagToWorker, {
tagId: tagId,
workerId: assigneeId,
})

if (tagToWorker) {
await store.remove<TagToWorker>(tagToWorker)
}

logger.info('TagToWorker has been disallowed', { tagId, assigneeId })
}
Loading
Loading