feat(server): improve team invite (#9092)

This commit is contained in:
DarkSky 2024-12-11 18:00:49 +08:00 committed by GitHub
parent 671c41cb1a
commit 9b0f1bb293
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 146 additions and 46 deletions

View File

@ -1,3 +1,5 @@
import { pick } from 'lodash-es';
import { PrismaTransaction } from '../../fundamentals';
import { formatDate, formatSize, Quota, QuotaSchema } from './types';
@ -5,7 +7,7 @@ const QuotaCache = new Map<number, QuotaConfig>();
export class QuotaConfig {
readonly config: Quota;
readonly override?: Quota['configs'];
readonly override?: Partial<Quota['configs']>;
static async get(tx: PrismaTransaction, featureId: number) {
const cachedQuota = QuotaCache.get(featureId);
@ -49,7 +51,10 @@ export class QuotaConfig {
configs: Object.assign({}, config.data.configs, override),
});
if (overrideConfig.success) {
this.override = overrideConfig.data.configs;
this.override = pick(
overrideConfig.data.configs,
Object.keys(override)
);
} else {
throw new Error(
`Invalid quota override config: ${override.error.message}, ${JSON.stringify(

View File

@ -280,7 +280,7 @@ export class QuotaService {
.findFirst({
where: {
workspaceId,
feature: { feature: type, type: FeatureKind.Feature },
feature: { feature: type, type: FeatureKind.Quota },
activated: true,
},
select: { configs: true },

View File

@ -191,7 +191,7 @@ export class QuotaManagementService {
FeatureType.UnlimitedWorkspace
);
const quota = {
const quota: QuotaBusinessType = {
name,
blobLimit,
businessBlobLimit,

View File

@ -16,12 +16,14 @@ import {
NotInSpace,
RequestMutex,
TooManyRequest,
URLHelper,
} from '../../../fundamentals';
import { CurrentUser } from '../../auth';
import { Permission, PermissionService } from '../../permission';
import { QuotaManagementService } from '../../quota';
import { UserService } from '../../user';
import {
InviteLink,
InviteResult,
WorkspaceInviteLinkExpireTime,
WorkspaceType,
@ -41,6 +43,7 @@ export class TeamWorkspaceResolver {
private readonly cache: Cache,
private readonly event: EventEmitter,
private readonly mailer: MailService,
private readonly url: URLHelper,
private readonly prisma: PrismaClient,
private readonly permissions: PermissionService,
private readonly users: UserService,
@ -71,6 +74,10 @@ export class TeamWorkspaceResolver {
Permission.Admin
);
if (emails.length > 512) {
return new TooManyRequest();
}
// lock to prevent concurrent invite
const lockFlag = `invite:${workspaceId}`;
await using lock = await this.mutex.lock(lockFlag);
@ -150,8 +157,27 @@ export class TeamWorkspaceResolver {
return results;
}
@ResolveField(() => InviteLink, {
description: 'invite link for workspace',
nullable: true,
})
async inviteLink(@Parent() workspace: WorkspaceType) {
const cacheId = `workspace:inviteLink:${workspace.id}`;
const id = await this.cache.get<{ inviteId: string }>(cacheId);
if (id) {
const expireTime = await this.cache.ttl(cacheId);
if (Number.isSafeInteger(expireTime)) {
return {
link: this.url.link(`/invite/${id.inviteId}`),
expireTime: new Date(Date.now() + expireTime),
};
}
}
return null;
}
@Mutation(() => String)
async inviteLink(
async createInviteLink(
@CurrentUser() user: CurrentUser,
@Args('workspaceId') workspaceId: string,
@Args('expireTime', { type: () => WorkspaceInviteLinkExpireTime })
@ -171,7 +197,11 @@ export class TeamWorkspaceResolver {
const inviteId = nanoid();
const cacheInviteId = `workspace:inviteLinkId:${inviteId}`;
await this.cache.set(cacheWorkspaceId, { inviteId }, { ttl: expireTime });
await this.cache.set(cacheInviteId, { workspaceId }, { ttl: expireTime });
await this.cache.set(
cacheInviteId,
{ workspaceId, inviteeUserId: user.id },
{ ttl: expireTime }
);
return inviteId;
}

View File

@ -485,12 +485,15 @@ export class WorkspaceResolver {
})
async getInviteInfo(@Args('inviteId') inviteId: string) {
let workspaceId = null;
let invitee = null;
// invite link
const invite = await this.cache.get<{ workspaceId: string }>(
`workspace:inviteLinkId:${inviteId}`
);
const invite = await this.cache.get<{
workspaceId: string;
inviteeUserId: string;
}>(`workspace:inviteLinkId:${inviteId}`);
if (typeof invite?.workspaceId === 'string') {
workspaceId = invite.workspaceId;
invitee = { user: await this.users.findUserById(invite.inviteeUserId) };
}
if (!workspaceId) {
workspaceId = await this.prisma.workspaceUserPermission
@ -508,10 +511,13 @@ export class WorkspaceResolver {
const workspaceContent = await this.doc.getWorkspaceContent(workspaceId);
const owner = await this.permissions.getWorkspaceOwner(workspaceId);
const invitee = await this.permissions.getWorkspaceInvitation(
inviteId,
workspaceId
);
if (!invitee) {
invitee = await this.permissions.getWorkspaceInvitation(
inviteId,
workspaceId
);
}
let avatar = '';
if (workspaceContent?.avatarKey) {

View File

@ -115,6 +115,15 @@ export class UpdateWorkspaceInput extends PickType(
id!: string;
}
@ObjectType()
export class InviteLink {
@Field(() => String, { description: 'Invite link' })
link!: string;
@Field(() => Date, { description: 'Invite link expire time' })
expireTime!: Date;
}
@ObjectType()
export class InviteResult {
@Field(() => String)

View File

@ -361,6 +361,14 @@ type InvitationWorkspaceType {
name: String!
}
type InviteLink {
"""Invite link expire time"""
expireTime: DateTime!
"""Invite link"""
link: String!
}
type InviteResult {
email: String!
@ -496,6 +504,7 @@ type Mutation {
"""Create a stripe customer portal to manage payment methods"""
createCustomerPortal: String!
createInviteLink(expireTime: WorkspaceInviteLinkExpireTime!, workspaceId: String!): String!
"""Create a new user"""
createUser(input: CreateUserInput!): UserType!
@ -514,7 +523,6 @@ type Mutation {
grantMember(permission: Permission!, userId: String!, workspaceId: String!): String!
invite(email: String!, permission: Permission!, sendInviteMail: Boolean, workspaceId: String!): String!
inviteBatch(emails: [String!]!, sendInviteMail: Boolean, workspaceId: String!): [InviteResult!]!
inviteLink(expireTime: WorkspaceInviteLinkExpireTime!, workspaceId: String!): String!
leaveWorkspace(sendLeaveMail: Boolean, workspaceId: String!, workspaceName: String!): Boolean!
publishPage(mode: PublicPageMode = Page, pageId: String!, workspaceId: String!): WorkspacePage!
recoverDoc(guid: String!, timestamp: DateTime!, workspaceId: String!): DateTime!
@ -996,6 +1004,9 @@ type WorkspaceType {
"""is current workspace initialized"""
initialized: Boolean!
"""invite link for workspace"""
inviteLink: InviteLink
"""Get user invoice count"""
invoiceCount: Int!
invoices(skip: Int, take: Int = 8): [InvoiceType!]!

View File

@ -17,6 +17,7 @@ import {
acceptInviteById,
createTestingApp,
createWorkspace,
getInviteInfo,
grantMember,
inviteLink,
inviteUser,
@ -95,11 +96,14 @@ const init = async (app: INestApplication, memberLimit = 10) => {
const createInviteLink = async () => {
const inviteId = await inviteLink(app, owner.token.token, ws.id, 'OneDay');
return async (email: string): Promise<UserAuthedType> => {
const member = await signUp(app, email.split('@')[0], email, '123456');
await acceptInviteById(app, ws.id, inviteId, false, member.token.token);
return member;
};
return [
inviteId,
async (email: string): Promise<UserAuthedType> => {
const member = await signUp(app, email.split('@')[0], email, '123456');
await acceptInviteById(app, ws.id, inviteId, false, member.token.token);
return member;
},
] as const;
};
const admin = await invite('admin@affine.pro', 'Admin');
@ -237,8 +241,15 @@ test('should be able to leave workspace', async t => {
test('should be able to invite by link', async t => {
const { app, permissions, quotaManager } = t.context;
const { createInviteLink, ws } = await init(app, 4);
const invite = await createInviteLink();
const { createInviteLink, owner, ws } = await init(app, 4);
const [inviteId, invite] = await createInviteLink();
{
// check invite link
const info = await getInviteInfo(app, owner.token.token, inviteId);
t.is(info.workspace.id, ws.id, 'should be able to get invite info');
}
{
// invite link
const members: UserAuthedType[] = [];

View File

@ -78,7 +78,7 @@ export async function inviteLink(
.send({
query: `
mutation {
inviteLink(workspaceId: "${workspaceId}", expireTime: ${expireTime})
createInviteLink(workspaceId: "${workspaceId}", expireTime: ${expireTime})
}
`,
})
@ -86,7 +86,7 @@ export async function inviteLink(
if (res.body.errors) {
throw new Error(res.body.errors[0].message);
}
return res.body.data.inviteLink;
return res.body.data.createInviteLink;
}
export async function acceptInviteById(
@ -187,5 +187,10 @@ export async function getInviteInfo(
`,
})
.expect(200);
if (res.body.errors) {
throw new Error(res.body.errors[0].message, {
cause: res.body.errors[0].cause,
});
}
return res.body.data.getInviteInfo;
}

View File

@ -2,11 +2,11 @@ import type { WorkspaceServerService } from '@affine/core/modules/cloud';
import {
acceptInviteByInviteIdMutation,
approveWorkspaceTeamMemberMutation,
createInviteLinkMutation,
getWorkspaceInfoQuery,
grantWorkspaceTeamMemberMutation,
inviteByEmailMutation,
inviteByEmailsMutation,
inviteLinkMutation,
leaveWorkspaceMutation,
type Permission,
revokeInviteLinkMutation,
@ -83,13 +83,13 @@ export class WorkspacePermissionStore extends Store {
throw new Error('No Server');
}
const inviteLink = await this.workspaceServerService.server.gql({
query: inviteLinkMutation,
query: createInviteLinkMutation,
variables: {
workspaceId,
expireTime,
},
});
return inviteLink.inviteLink;
return inviteLink.createInviteLink;
}
async revokeInviteLink(workspaceId: string, signal?: AbortSignal) {

View File

@ -1249,6 +1249,10 @@ query getWorkspaceConfig($id: String!) {
workspace(id: $id) {
enableAi
enableUrlPreview
inviteLink {
link
expireTime
}
}
}`,
};
@ -1431,14 +1435,14 @@ mutation inviteBatch($workspaceId: String!, $emails: [String!]!, $sendInviteMail
}`,
};
export const inviteLinkMutation = {
id: 'inviteLinkMutation' as const,
operationName: 'inviteLink',
definitionName: 'inviteLink',
export const createInviteLinkMutation = {
id: 'createInviteLinkMutation' as const,
operationName: 'createInviteLink',
definitionName: 'createInviteLink',
containsFile: false,
query: `
mutation inviteLink($workspaceId: String!, $expireTime: WorkspaceInviteLinkExpireTime!) {
inviteLink(workspaceId: $workspaceId, expireTime: $expireTime)
mutation createInviteLink($workspaceId: String!, $expireTime: WorkspaceInviteLinkExpireTime!) {
createInviteLink(workspaceId: $workspaceId, expireTime: $expireTime)
}`,
};

View File

@ -2,5 +2,9 @@ query getWorkspaceConfig($id: String!) {
workspace(id: $id) {
enableAi
enableUrlPreview
inviteLink {
link
expireTime
}
}
}

View File

@ -1,6 +1,6 @@
mutation inviteLink(
mutation createInviteLink(
$workspaceId: String!
$expireTime: WorkspaceInviteLinkExpireTime!
) {
inviteLink(workspaceId: $workspaceId, expireTime: $expireTime)
createInviteLink(workspaceId: $workspaceId, expireTime: $expireTime)
}

View File

@ -438,6 +438,14 @@ export interface InvitationWorkspaceType {
name: Scalars['String']['output'];
}
export interface InviteLink {
__typename?: 'InviteLink';
/** Invite link expire time */
expireTime: Scalars['DateTime']['output'];
/** Invite link */
link: Scalars['String']['output'];
}
export interface InviteResult {
__typename?: 'InviteResult';
email: Scalars['String']['output'];
@ -559,6 +567,7 @@ export interface Mutation {
createCopilotSession: Scalars['String']['output'];
/** Create a stripe customer portal to manage payment methods */
createCustomerPortal: Scalars['String']['output'];
createInviteLink: Scalars['String']['output'];
/** Create a new user */
createUser: UserType;
/** Create a new workspace */
@ -573,7 +582,6 @@ export interface Mutation {
grantMember: Scalars['String']['output'];
invite: Scalars['String']['output'];
inviteBatch: Array<InviteResult>;
inviteLink: Scalars['String']['output'];
leaveWorkspace: Scalars['Boolean']['output'];
publishPage: WorkspacePage;
recoverDoc: Scalars['DateTime']['output'];
@ -673,6 +681,11 @@ export interface MutationCreateCopilotSessionArgs {
options: CreateChatSessionInput;
}
export interface MutationCreateInviteLinkArgs {
expireTime: WorkspaceInviteLinkExpireTime;
workspaceId: Scalars['String']['input'];
}
export interface MutationCreateUserArgs {
input: CreateUserInput;
}
@ -719,11 +732,6 @@ export interface MutationInviteBatchArgs {
workspaceId: Scalars['String']['input'];
}
export interface MutationInviteLinkArgs {
expireTime: WorkspaceInviteLinkExpireTime;
workspaceId: Scalars['String']['input'];
}
export interface MutationLeaveWorkspaceArgs {
sendLeaveMail?: InputMaybe<Scalars['Boolean']['input']>;
workspaceId: Scalars['String']['input'];
@ -1342,6 +1350,8 @@ export interface WorkspaceType {
id: Scalars['ID']['output'];
/** is current workspace initialized */
initialized: Scalars['Boolean']['output'];
/** invite link for workspace */
inviteLink: Maybe<InviteLink>;
/** Get user invoice count */
invoiceCount: Scalars['Int']['output'];
invoices: Array<InvoiceType>;
@ -2526,6 +2536,11 @@ export type GetWorkspaceConfigQuery = {
__typename?: 'WorkspaceType';
enableAi: boolean;
enableUrlPreview: boolean;
inviteLink: {
__typename?: 'InviteLink';
link: string;
expireTime: string;
} | null;
};
};
@ -2670,14 +2685,14 @@ export type InviteBatchMutation = {
}>;
};
export type InviteLinkMutationVariables = Exact<{
export type CreateInviteLinkMutationVariables = Exact<{
workspaceId: Scalars['String']['input'];
expireTime: WorkspaceInviteLinkExpireTime;
}>;
export type InviteLinkMutation = {
export type CreateInviteLinkMutation = {
__typename?: 'Mutation';
inviteLink: string;
createInviteLink: string;
};
export type RevokeInviteLinkMutationVariables = Exact<{
@ -3228,9 +3243,9 @@ export type Mutations =
response: InviteBatchMutation;
}
| {
name: 'inviteLinkMutation';
variables: InviteLinkMutationVariables;
response: InviteLinkMutation;
name: 'createInviteLinkMutation';
variables: CreateInviteLinkMutationVariables;
response: CreateInviteLinkMutation;
}
| {
name: 'revokeInviteLinkMutation';