Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Add user group by activating membership for group admin #85

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions src/models/user-group.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { getPublicKeyFromPrivate } from 'blockstack/lib/keys';
import { getPublicKeyFromPrivate } from "blockstack/lib/keys";

import Model from '../model';
import GroupMembership from './group-membership';
import GroupInvitation from './group-invitation';
import SigningKey from './signing-key';
import Model from "../model";
import GroupMembership from "./group-membership";
import GroupInvitation from "./group-invitation";
import SigningKey from "./signing-key";
import {
userGroupKeys, addUserGroupKey, loadUserData, requireUserSession,
} from '../helpers';
import { Schema, Attrs } from '../types/index';
userGroupKeys,
addUserGroupKey,
loadUserData,
requireUserSession
} from "../helpers";
import { Schema, Attrs } from "../types/index";

interface Member {
username: string,
inviteId: string
username: string;
inviteId: string;
}

interface UserGroupAttrs extends Attrs {
name?: string | any,
gaiaConfig: Record<string, any> | any,
members: any[] | any,
name?: string | any;
gaiaConfig: Record<string, any> | any;
members: any[] | any;
}

const defaultMembers: Member[] = [];
Expand All @@ -29,18 +32,20 @@ export default class UserGroup extends Model {
name: String,
gaiaConfig: Object,
members: {
type: Array,
},
}
type: Array
}
};

static defaults = {
members: defaultMembers,
}
members: defaultMembers
};

static async find(id: string) {
const { userGroups, signingKeys } = GroupMembership.userGroupKeys();
if (!userGroups || !userGroups[id]) {
throw new Error(`UserGroup not found with id: '${id}'. Have you called \`GroupMembership.cacheKeys()\`?`);
throw new Error(
`UserGroup not found with id: '${id}'. Have you called \`GroupMembership.cacheKeys()\`?`
);
}
const signingKey = userGroups[id];
const privateKey = signingKeys[signingKey];
Expand All @@ -54,7 +59,6 @@ export default class UserGroup extends Model {
const signingKey = await SigningKey.create({ userGroupId: this._id });
this.attrs.signingKeyId = signingKey._id;
this.privateKey = signingKey.attrs.privateKey;
addUserGroupKey(this);
// await this.makeGaiaConfig();
const { username } = loadUserData();
const invitation = await this.makeGroupMembership(username);
Expand All @@ -70,16 +74,15 @@ export default class UserGroup extends Model {
}
});
if (existingInviteId) {
const invitation = await GroupInvitation.findById(
existingInviteId,
{ decrypt: false },
);
const invitation = await GroupInvitation.findById(existingInviteId, {
decrypt: false
});
return invitation as GroupInvitation;
}
const invitation = await GroupInvitation.makeInvitation(username, this);
this.attrs.members.push({
username,
inviteId: invitation._id,
inviteId: invitation._id
});
await this.save();
return invitation;
Expand All @@ -88,7 +91,7 @@ export default class UserGroup extends Model {
static myGroups() {
const { userGroups } = userGroupKeys();
const keys = Object.keys(userGroups);
return this.fetchList({ _id: keys.join(',') });
return this.fetchList({ _id: keys.join(",") });
}

publicKey() {
Expand Down Expand Up @@ -122,15 +125,15 @@ export default class UserGroup extends Model {
// return gaiaConfig;
// }

static modelName = () => 'UserGroup'
static modelName = () => "UserGroup";

getSigningKey() {
const { userGroups, signingKeys } = userGroupKeys();
const id = userGroups[this._id];
const privateKey = signingKeys[id];
return {
privateKey,
id,
id
};
}
}