Skip to content

Commit

Permalink
feat: ⚡ batch channel verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignazio Bovo committed Nov 24, 2023
1 parent 21fc5b0 commit 4fd1948
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
94 changes: 49 additions & 45 deletions src/server-extension/resolvers/ChannelsResolver/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata'
import { Args, Query, Mutation, Resolver, Info, Ctx, UseMiddleware } from 'type-graphql'
import { EntityManager, IsNull } from 'typeorm'
import { ArrayContains, EntityManager, IsNull } from 'typeorm'
import {
ChannelNftCollector,
ChannelNftCollectorsArgs,
Expand Down Expand Up @@ -394,9 +394,9 @@ export class ChannelsResolver {

@Mutation(() => VerifyChannelResult)
@UseMiddleware(OperatorOnly())
async verifyChannel(@Args() { channelId }: VerifyChannelArgs): Promise<VerifyChannelResult> {
async verifyChannel(@Args() { channelIds }: VerifyChannelArgs): Promise<VerifyChannelResult[]> {
const em = await this.em()
return await verifyChannelService(em, channelId)
return await verifyChannelService(em, channelIds)
}

@Mutation(() => ExcludeChannelResult)
Expand Down Expand Up @@ -468,51 +468,55 @@ export const excludeChannelService = async (
})
}

export const verifyChannelService = async (em: EntityManager, channelId: string) => {
export const verifyChannelService = async (em: EntityManager, channelIds: string[]) => {
return withHiddenEntities(em, async () => {
const channel = await em.findOne(Channel, {
where: { id: channelId },
})

if (!channel) {
throw new Error(`Channel by id ${channelId} not found!`)
}
// If channel already verified - return its data
if (channel.yppStatus.isTypeOf === 'YppVerified') {
const existingVerification = await em.getRepository(ChannelVerification).findOneOrFail({
where: { channelId: channel.id },
})
return {
id: existingVerification.id,
channelId: channel.id,
createdAt: existingVerification.timestamp,
}
}
// othewise create new verification
const newVerification = new ChannelVerification({
id: uniqueId(),
channelId: channel.id,
timestamp: new Date(),
const channels = await em.getRepository(Channel).find({
where: { id: ArrayContains(channelIds) },
})
channel.yppStatus = new YppVerified({ verification: newVerification.id })
await em.save([newVerification, channel])

// in case account exist deposit notification
const channelOwnerMemberId = channel.ownerMemberId
if (channelOwnerMemberId) {
const account = await em.findOne(Account, { where: { membershipId: channelOwnerMemberId } })
await addNotification(
em,
account,
new ChannelRecipient({ channel: channel.id }),
new ChannelVerified({})
)
}
const results = channels
.filter((channel) => channel)
.map(async (channel) => {
// If channel already verified - return its data
if (channel.yppStatus.isTypeOf === 'YppVerified') {
const existingVerification = await em.getRepository(ChannelVerification).findOneOrFail({
where: { channelId: channel.id },
})
return {
id: existingVerification.id,
channelId: channel.id,
createdAt: existingVerification.timestamp,
}
}
// othewise create new verification regardless whether the channel was previously verified
const newVerification = new ChannelVerification({
id: uniqueId(),
channelId: channel.id,
timestamp: new Date(),
})
channel.yppStatus = new YppVerified({ verification: newVerification.id })
await em.save([newVerification, channel])

// in case account exist deposit notification
const channelOwnerMemberId = channel.ownerMemberId
if (channelOwnerMemberId) {
const account = await em.findOne(Account, {
where: { membershipId: channelOwnerMemberId },
})
await addNotification(
em,
account,
new ChannelRecipient({ channel: channel.id }),
new ChannelVerified({})
)
}

return {
id: newVerification.id,
channelId: channel.id,
createdAt: newVerification.timestamp,
}
return {
id: newVerification.id,
channelId: channel.id,
createdAt: newVerification.timestamp,
}
})
return await Promise.all(results)
})
}
4 changes: 2 additions & 2 deletions src/server-extension/resolvers/ChannelsResolver/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ export class SuspendChannelArgs {

@ArgsType()
export class VerifyChannelArgs {
@Field(() => String, { nullable: false })
channelId!: string
@Field(() => [String], { nullable: false })
channelIds!: string[]
}

@ObjectType()
Expand Down

0 comments on commit 4fd1948

Please sign in to comment.