Public sharing and invites
Public sharing
You can share CoValues publicly by setting the owner to a Group, and granting access to "everyone".
constconst group: Groupgroup =class GroupGroup.create();Group.create<Group>(this: CoValueClass<Group>, options?: { owner: Account; } | Account): Groupconst group: Groupgroup.Group.addMember(member: Everyone, role: "writer" | "reader" | "writeOnly"): void (+2 overloads)addMember("everyone", "writer");
You can also use makePublic(role) alias to grant access to everyone with a specific role (defaults to reader).
constconst group: Groupgroup =class GroupGroup.create();Group.create<Group>(this: CoValueClass<Group>, options?: { owner: Account; } | Account): Groupconst group: Groupgroup.Group.addMember(member: Everyone, role: "writer" | "reader" | "writeOnly"): void (+2 overloads)addMember("everyone", "writer");const group: Groupgroup.Group.makePublic(role?: "reader" | "writer"): GroupMake the group public, so that everyone can read it. Alias for `addMember("everyone", role)`.makePublic("writer"); // group.makePublic(); // Defaults to "reader" access
This is done in the chat example where anyone can join the chat, and send messages.
You can also add members by Account ID.
Invites
You can grant users access to a CoValue by sending them an invite link.
This is used in the todo example.
It generates a URL that looks like .../invite/[CoValue ID]/[inviteSecret]
In your app, you need to handle this route, and let the user accept the invitation, as done here.
You can accept an invitation programmatically by using the acceptInvite method on an account.
Pass the ID of the CoValue you're being invited to, the secret from the invite link, and the schema of the CoValue.
awaitconst account: Accountaccount.acceptInvite(Account.acceptInvite<CoMapSchema<{ name: z.z.ZodString; }>>(valueID: string, inviteSecret: InviteSecret, coValueClass: CoMapSchema<{ name: z.z.ZodString; }>): Promise<...>const organizationId: ""organizationId,const inviteSecret: "inviteSecret_z"inviteSecret,Organization );const Organization: CoMapSchema<{ name: z.z.ZodString; }>
Requesting Invites
To allow a non-group member to request an invitation to a group you can use the writeOnly role.
This means that users only have write access to a specific requests list (they can't read other requests).
However, Administrators can review and approve these requests.
Create the data models.
constJoinRequest =const JoinRequest: CoMapSchema<{ account: <Shape extends { profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>(shape?: Shape) => AccountSchema<Shape>; status: z.z.ZodLiteral<...>; }>import coco.map({map<{ account: <Shape extends { profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>(shape?: Shape) => AccountSchema<Shape>; status: z.z.ZodLiteral<...>; }>(shape: { account: <Shape extends { profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>(shape?: Shape) => AccountSchema<Shape>; status: z.z.ZodLiteral<...>; }): CoMapSchema<...> export mapaccount:account: <Shape extends { profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>(shape?: Shape) => AccountSchema<Shape>import coco.account,const account: <Shape extends { profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>(shape?: Shape) => AccountSchema<Shape> export accountstatus: z.z.ZodLiteral<"pending" | "approved" | "rejected">status:import zz.literal(["pending", "approved", "rejected"]), }); constliteral<["pending", "approved", "rejected"]>(value: ["pending", "approved", "rejected"], params?: string | z.z.core.$ZodLiteralParams): z.z.ZodLiteral<"pending" | "approved" | "rejected"> (+1 overload) export literalRequestsList =const RequestsList: CoListSchema<CoMapSchema<{ account: <Shape extends { profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>(shape?: Shape) => AccountSchema<Shape>; status: z.z.ZodLiteral<...>; }>>import coco.list(list<CoMapSchema<{ account: <Shape extends { profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>(shape?: Shape) => AccountSchema<Shape>; status: z.z.ZodLiteral<...>; }>>(element: CoMapSchema<...>): CoListSchema<...> export listJoinRequest);const JoinRequest: CoMapSchema<{ account: <Shape extends { profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>(shape?: Shape) => AccountSchema<Shape>; status: z.z.ZodLiteral<...>; }>
Set up the request system with appropriate access controls.
functioncreateRequestsToJoin() { constfunction createRequestsToJoin(): CoList<{ account: { profile: { name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap; root: { [x: string]: any; } & CoMap; } & { profile: Profile; } & Account; status: "pending" | ... 1 more ... | "rejected"; } & CoMap>const requestsGroup: GrouprequestsGroup =class GroupGroup.create();Group.create<Group>(this: CoValueClass<Group>, options?: { owner: Account; } | Account): Groupconst requestsGroup: GrouprequestsGroup.Group.addMember(member: Everyone, role: "writer" | "reader" | "writeOnly"): void (+2 overloads)addMember("everyone", "writeOnly"); returnRequestsList.const RequestsList: CoListSchema<CoMapSchema<{ account: AccountSchema<{ profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>; status: z.z.ZodLiteral<...>; }>>create([],create: (items: CoListInit<CoMapSchema<{ account: AccountSchema<{ profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>; status: z.z.ZodLiteral<...>; }>>, options?: { owner: Account | Group; } | Account | Group) => CoList<...>const requestsGroup: GrouprequestsGroup); } async functionsendJoinRequest(function sendJoinRequest(requestsList: co.loaded<typeof RequestsList>, account: Account): Promise<{ account: { profile: { name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap; root: { ...; } & CoMap; } & { ...; } & Account; status: "pending" | ... 1 more ... | "rejected"; } & CoMap>requestsList:requestsList: CoList<({ account: ({ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account) | null; status: "pending" | ... 1 more ... | "rejected"; } & CoMap) | null>import coco.loaded<typeoftype loaded<T extends CoValueClass | AnyCoSchema, R extends ResolveQuery<T> = true> = R extends boolean | undefined ? NonNullable<InstanceOfSchemaCoValuesNullable<T>> : [NonNullable<InstanceOfSchemaCoValuesNullable<T>>] extends [...] ? Exclude<...> extends CoValue ? R extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<... export loadedRequestsList>,const RequestsList: CoListSchema<CoMapSchema<{ account: AccountSchema<{ profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>; status: z.z.ZodLiteral<...>; }>>account:account: { profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & AccountAccount, ) { consttype Account = { profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & Accountrequest =const request: { account: { profile: { name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap; root: { [x: string]: any; } & CoMap; } & { profile: Profile; } & Account; status: "pending" | ... 1 more ... | "rejected"; } & CoMapJoinRequest.const JoinRequest: CoMapSchema<{ account: AccountSchema<{ profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>; status: z.z.ZodLiteral<...>; }>create( {create: (init: { account: { profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account; status: NonNullable<...>; }, options?: Account | ... 2 more ... | undefined) => { ...; } & CoMapaccount,account: { profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { profile: Profile | null; } & Accountstatus: NonNullable<"pending" | "approved" | "rejected">status: "pending", },requestsList.requestsList: CoList<({ account: ({ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account) | null; status: "pending" | ... 1 more ... | "rejected"; } & CoMap) | null>CoList<({ account: ({ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account) | null; status: "pending" | ... 1 more ... | "rejected"; } & CoMap) | null>._owner: Account | Group_owner // Inherit the access controls of the requestsList );requestsList.requestsList: CoList<({ account: ({ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account) | null; status: "pending" | ... 1 more ... | "rejected"; } & CoMap) | null>CoList<({ account: ({ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account) | null; status: "pending" | ... 1 more ... | "rejected"; } & CoMap) | null>.push(...items: (({ account: ({ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account) | null; status: "pending" | ... 1 more ... | "rejected"; } & CoMap) | null)[]): numberAppends new elements to the end of an array, and returns the new length of the array.push(request); returnconst request: { account: { profile: { name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap; root: { [x: string]: any; } & CoMap; } & { profile: Profile; } & Account; status: "pending" | ... 1 more ... | "rejected"; } & CoMaprequest; }const request: { account: { profile: { name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap; root: { [x: string]: any; } & CoMap; } & { profile: Profile; } & Account; status: "pending" | ... 1 more ... | "rejected"; } & CoMap
Using the write-only access users can submit requests that only administrators can review and approve.
async functionapproveJoinRequest(function approveJoinRequest(joinRequest: co.loaded<typeof JoinRequest, { account: true; }>, targetGroup: Group): Promise<boolean>joinRequest:joinRequest: { account: { profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account; } & { ...; } & CoMapimport coco.loaded<typeoftype loaded<T extends CoValueClass | AnyCoSchema, R extends ResolveQuery<T> = true> = R extends boolean | undefined ? NonNullable<InstanceOfSchemaCoValuesNullable<T>> : [NonNullable<InstanceOfSchemaCoValuesNullable<T>>] extends [...] ? Exclude<...> extends CoValue ? R extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends { ...; } ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<... export loadedJoinRequest, {const JoinRequest: CoMapSchema<{ account: AccountSchema<{ profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>; status: z.z.ZodLiteral<...>; }>account: trueaccount: true }>,targetGroup: GrouptargetGroup:class GroupGroup, ) { constaccount = awaitconst account: ({ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & Account) | ({ ...; } & ... 1 more ... & Account) | nullAccount.const Account: AccountSchema<{ profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>load(load: <ResolveQuery<AccountSchema<{ profile: AnyCoMapSchema<{ name: z.z.core.$ZodString<string>; inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>; inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>; }>; root: AnyCoMapSchema; }>>>(id: string, options?: { ...; } | undefined) => Promise<...>joinRequest.joinRequest: { account: { profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account; } & { ...; } & CoMapCoMap._refs: { account: Ref<NonNullable<{ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account>>; }If property `prop` is a `coField.ref(...)`, you can use `coMaps._refs.prop` to access the `Ref` instead of the potentially loaded/null value. This allows you to always get the ID or load the value manually._refs.account.account: Ref<NonNullable<{ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { profile: Profile | null; } & Account>>Ref<NonNullable<{ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { profile: Profile | null; } & Account>>.id: stringid); if (account) {const account: ({ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & Account) | ({ ...; } & ... 1 more ... & Account) | nulltargetGroup: GrouptargetGroup.Group.addMember(member: Account, role: AccountRole): void (+2 overloads)addMember(account, "reader");const account: ({ profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & Account) | ({ ...; } & ... 1 more ... & Account)joinRequest.joinRequest: { account: { profile: ({ name: string; inbox?: string | undefined; inboxInvite?: string | undefined; } & CoMap) | null; root: ({ [x: string]: any; } & CoMap) | null; } & { ...; } & Account; } & { ...; } & CoMapstatus: "pending" | "approved" | "rejected"status = "approved"; return true; } else { return false; } }