Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[server, dashboard] Refactor User.getPrimaryEmail to return "string | undefined" instead of throwing an error #9446

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion components/dashboard/src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export default function Menu() {
<ContextMenu
menuEntries={[
{
title: (user && User.getPrimaryEmail(user)) || "",
title: (user && (User.getPrimaryEmail(user) || user?.name)) || "User",
customFontStyle: "text-gray-400",
separator: true,
},
Expand Down
11 changes: 9 additions & 2 deletions components/dashboard/src/admin/UserDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function UserDetail(p: { user: User }) {
}, [p.user]);

const email = User.getPrimaryEmail(p.user);
const emailDomain = email.split("@")[email.split("@").length - 1];
const emailDomain = email ? email.split("@")[email.split("@").length - 1] : undefined;
Copy link
Member

@easyCZ easyCZ Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Nothing to fix here, just a comment. This is in general quite fishy, but oh well. In practice, it's extremely hard to parse email addresses well as the spec is so very broad, the best we can often do is check the email is valid by sending an email.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely agree in theory. Currently the only assumptions we make is that a) it has a '@' and b) non-empty domain part.
I know that both are not guaranteed by the spec, but held in practice well enough.
And then there is the other point that we should not be doing this randomly in the frontend, but should try to keep in a central place (or library).... 👍


const updateUser: UpdateUserFunction = async (fun) => {
setActivity(true);
Expand All @@ -62,6 +62,11 @@ export default function UserDetail(p: { user: User }) {
};

const addStudentDomain = async () => {
if (!emailDomain) {
console.log("cannot add student's email domain because there is none!");
return;
}

await updateUser(async (u) => {
await getGitpodService().server.adminAddStudentEmailDomain(u.id, emailDomain);
await getGitpodService()
Expand Down Expand Up @@ -221,7 +226,9 @@ export default function UserDetail(p: { user: User }) {
<Property
name="Student"
actions={
!isStudent && !["gmail.com", "yahoo.com", "hotmail.com"].includes(emailDomain)
!isStudent &&
emailDomain &&
!["gmail.com", "yahoo.com", "hotmail.com"].includes(emailDomain)
? [
{
label: `Make '${emailDomain}' a student domain`,
Expand Down
8 changes: 1 addition & 7 deletions components/dashboard/src/admin/UserSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { AdminGetListResult, User } from "@gitpod/gitpod-protocol";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import moment from "moment";
import { useEffect, useState } from "react";
import { useLocation } from "react-router";
Expand Down Expand Up @@ -113,12 +112,7 @@ function UserEntry(p: { user: User }) {
if (!p) {
return <></>;
}
let email = "---";
try {
email = User.getPrimaryEmail(p.user);
} catch (e) {
log.error(e);
}
const email = User.getPrimaryEmail(p.user) || "---";
return (
<Link key={p.user.id} to={"/admin/users/" + p.user.id} data-analytics='{"button_type":"sidebar_menu"}'>
<div className="rounded-xl whitespace-nowrap flex space-x-2 py-6 px-6 w-full justify-between hover:bg-gray-100 dark:hover:bg-gray-800 focus:bg-gitpod-kumquat-light group">
Expand Down
4 changes: 2 additions & 2 deletions components/dashboard/src/settings/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function Account() {
const [modal, setModal] = useState(false);
const [typedEmail, setTypedEmail] = useState("");

const primaryEmail = User.getPrimaryEmail(user!);
const primaryEmail = User.getPrimaryEmail(user!) || "---";

const deleteAccount = async () => {
await getGitpodService().server.deleteAccount();
Expand Down Expand Up @@ -75,7 +75,7 @@ export default function Account() {
</div>
<div className="mt-4">
<h4>Email</h4>
<input type="text" disabled={true} value={User.getPrimaryEmail(user!)} />
<input type="text" disabled={true} value={primaryEmail} />
</div>
</div>
<div className="lg:pl-14">
Expand Down
12 changes: 9 additions & 3 deletions components/gitpod-protocol/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,19 @@ export namespace User {
});
return res;
}
export function getPrimaryEmail(user: User): string {

/**
* Tries to return the primaryEmail of the first identity this user signed up with.
* @param user
* @returns A primaryEmail, or undefined if there is none.
*/
export function getPrimaryEmail(user: User): string | undefined {
const identities = user.identities.filter((i) => !!i.primaryEmail);
if (identities.length <= 0) {
throw new Error(`No identity with primary email for user: ${user.id}!`);
return undefined;
}

return identities[0].primaryEmail!;
return identities[0].primaryEmail || undefined;
}
export function getName(user: User): string | undefined {
const name = user.fullName || user.name;
Expand Down
3 changes: 3 additions & 0 deletions components/server/ee/src/workspace/gitpod-server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,9 @@ export class GitpodServerEEImpl extends GitpodServerImpl {

try {
const email = User.getPrimaryEmail(user);
if (!email) {
throw new Error("No identity with primary email for user");
}

return new Promise((resolve, reject) => {
this.chargebeeProvider.hosted_page
Expand Down
2 changes: 1 addition & 1 deletion components/server/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function fullIdentify(user: User, request: Request, analytics: IAnalyticsWriter)
},
traits: {
...resolveIdentities(user),
email: User.getPrimaryEmail(user),
email: User.getPrimaryEmail(user) || "",
full_name: user.fullName,
created_at: user.creationDate,
unsubscribed_onboarding: user.additionalData?.emailNotificationSettings?.allowsOnboardingMail === false,
Expand Down