Skip to content

Commit d9e1cf2

Browse files
committed
Address review comments
Tool: gitpod/catfood.gitpod.cloud
1 parent 8b563ec commit d9e1cf2

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

components/server/src/orgs/organization-service.ts

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import { CreateUserParams, UserAuthentication } from "../user/user-authenticatio
3838
import isURL from "validator/lib/isURL";
3939
import { merge } from "ts-deepmerge";
4040
import { EntitlementService } from "../billing/entitlement-service";
41-
import { TrustedValue } from "@gitpod/gitpod-protocol/lib/util/scrubbing";
4241

4342
@injectable()
4443
export class OrganizationService {
@@ -608,6 +607,13 @@ export class OrganizationService {
608607
}
609608
}
610609

610+
const resolvedFeaturedMemberAvatarUrl = settings.onboardingSettings?.welcomeMessage?.featuredMemberId
611+
? await this.resolveMemberAvatarUrl(settings.onboardingSettings.welcomeMessage.featuredMemberId, orgId)
612+
: undefined;
613+
if (!resolvedFeaturedMemberAvatarUrl && settings.onboardingSettings?.welcomeMessage?.featuredMemberId) {
614+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "featuredMemberId not found");
615+
}
616+
611617
const mergeSettings = (
612618
currentSettings: OrganizationSettings,
613619
partialUpdate: Partial<OrganizationSettings>,
@@ -625,57 +631,52 @@ export class OrganizationService {
625631
settings.roleRestrictions = partialUpdate.roleRestrictions;
626632
}
627633

628-
if (
629-
settings.onboardingSettings?.welcomeMessage?.enabled &&
630-
(!settings.onboardingSettings?.welcomeMessage?.message ||
631-
settings.onboardingSettings?.welcomeMessage?.message.length === 0)
632-
) {
633-
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "welcomeMessage must not be empty when enabled");
634-
}
635-
636634
return settings;
637635
};
638636

639-
return this.toSettings(await this.teamDB.setOrgSettings(orgId, settings, mergeSettings));
637+
const dbSettings = await this.teamDB.setOrgSettings(orgId, settings, mergeSettings);
638+
if (settings.onboardingSettings?.welcomeMessage?.featuredMemberId) {
639+
if (resolvedFeaturedMemberAvatarUrl && dbSettings.onboardingSettings?.welcomeMessage) {
640+
dbSettings.onboardingSettings.welcomeMessage.featuredMemberResolvedAvatarUrl =
641+
resolvedFeaturedMemberAvatarUrl;
642+
}
643+
}
644+
645+
return this.toSettings(dbSettings);
640646
}
641647

648+
/**
649+
* In addition to the `getSettings` method, this method also resolves the avatar URL for the featured member in the welcome message.
650+
*/
642651
async getSettingsWithResolvedWelcomeMessage(userId: string, orgId: string): Promise<OrganizationSettings> {
643652
const settings = await this.getSettings(userId, orgId);
644-
return this.resolveWelcomeMessage(settings, orgId);
645-
}
646-
647-
async updateSettingsWithResolvedWelcomeMessage(
648-
userId: string,
649-
orgId: string,
650-
settings: Partial<OrganizationSettings>,
651-
): Promise<OrganizationSettings> {
652-
const newSettings = await this.updateSettings(userId, orgId, settings);
653-
return this.resolveWelcomeMessage(newSettings, orgId);
653+
if (settings.onboardingSettings?.welcomeMessage?.featuredMemberId) {
654+
const featuredMemberAvatarUrl = await this.resolveMemberAvatarUrl(
655+
settings.onboardingSettings.welcomeMessage.featuredMemberId,
656+
orgId,
657+
);
658+
if (featuredMemberAvatarUrl) {
659+
settings.onboardingSettings.welcomeMessage.featuredMemberResolvedAvatarUrl = featuredMemberAvatarUrl;
660+
}
661+
}
662+
return settings;
654663
}
655664

656665
/**
657-
* Resolves the welcome message by resolving the featured member avatar URL. This is not done in the `updateSettings` and `getSettings` methods
666+
* Resolves the avatar URL for a member of an organization.
667+
* This is not done in methods like `getSettings` directly
658668
* because we don't need to pay the extra lookup cost for the avatar URL for most requests.
659669
*/
660-
private async resolveWelcomeMessage(settings: OrganizationSettings, orgId: string): Promise<OrganizationSettings> {
661-
if (settings.onboardingSettings?.welcomeMessage) {
662-
const { welcomeMessage } = settings.onboardingSettings;
663-
if (welcomeMessage.featuredMemberId) {
664-
const membership = await this.teamDB.findTeamMembership(welcomeMessage.featuredMemberId, orgId);
665-
if (!membership) {
666-
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "featuredMemberId is invalid");
667-
}
668-
const user = await this.userDB.findUserById(membership.userId);
669-
if (!user) {
670-
throw new ApplicationError(
671-
ErrorCodes.NOT_FOUND,
672-
`user for featuredMemberId ${membership.userId} not found`,
673-
);
674-
}
675-
welcomeMessage.featuredMemberResolvedAvatarUrl = user.avatarUrl;
676-
}
670+
private async resolveMemberAvatarUrl(userId: string, orgId: string): Promise<string | undefined> {
671+
const membership = await this.teamDB.findTeamMembership(userId, orgId);
672+
if (!membership) {
673+
return undefined;
677674
}
678-
return settings;
675+
const user = await this.userDB.findUserById(membership.userId);
676+
if (!user) {
677+
return undefined;
678+
}
679+
return user.avatarUrl;
679680
}
680681

681682
private async toSettings(settings: OrganizationSettings = {}): Promise<OrganizationSettings> {

0 commit comments

Comments
 (0)