Skip to content

Commit

Permalink
feat(mappings): add handlers for avatar and handle
Browse files Browse the repository at this point in the history
  • Loading branch information
metmirr committed Jun 15, 2020
1 parent 91fe1dd commit a99a0da
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion joystream-query-node/mappings/members.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import * as assert from 'assert';
import { CheckedUserInfo } from '@joystream/types/lib/members';

import { Member } from '../generated/graphql-server/src/modules/member/member.model';
import { DB, SubstrateEvent } from '../generated/indexer';

export async function handleMemberRegistered(db: DB, event: SubstrateEvent) {
const { AccountId, MemberId } = event.event_params;

// Not safe type casting!
const userInfo = (event.extrinsic?.args[1].toJSON() as unknown) as CheckedUserInfo;

let member = new Member();
member.rootAccount = Buffer.from(AccountId);
member.registeredAtBlock = event.block_number.toString();
member.memberId = MemberId.toString();
member.rootAccount = Buffer.from(AccountId);
member.controllerAccount = Buffer.from(AccountId);
member.handle = userInfo.handle.toString();
member.avatarUri = userInfo.avatar_uri.toString();
member.about = userInfo.about.toString();

db.save<Member>(member);
}
Expand All @@ -22,3 +31,30 @@ export async function handleMemberUpdatedAboutText(db: DB, event: SubstrateEvent
member.updatedAt = new Date();
db.save<Member>(member);
}

export async function handleMemberUpdatedAvatar(db: DB, event: SubstrateEvent) {
const { MemberId } = event.event_params;
const member = await db.get(Member, { where: { memberId: MemberId.toString() } });
console.log('member', member);
assert(member);

// Not safe type casting!
const userInfo = (event.extrinsic?.args[1].toJSON() as unknown) as CheckedUserInfo;
member.avatarUri = userInfo.avatar_uri.toString();

db.save<Member>(member);
}

export async function handleMemberUpdatedHandle(db: DB, event: SubstrateEvent) {
const { MemberId } = event.event_params;
const member = await db.get(Member, { where: { memberId: MemberId.toString() } });

assert(member);

// Not safe type casting!
const userInfo = (event.extrinsic?.args[1].toJSON() as unknown) as CheckedUserInfo;
member.handle = userInfo.handle.toString();

db.save<Member>(member);
}

0 comments on commit a99a0da

Please sign in to comment.