Skip to content

Commit

Permalink
add github username support
Browse files Browse the repository at this point in the history
  • Loading branch information
CassOnMars committed Oct 25, 2024
1 parent b8a9d58 commit 74f7a56
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/protobufs/generated/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ export enum UserDataType {
LOCATION = 7,
/** TWITTER - Username of user on twitter */
TWITTER = 8,
/** GITHUB - Username of user on github */
GITHUB = 9,
}

export function userDataTypeFromJSON(object: any): UserDataType {
Expand Down Expand Up @@ -274,6 +276,9 @@ export function userDataTypeFromJSON(object: any): UserDataType {
case 8:
case "USER_DATA_TYPE_TWITTER":
return UserDataType.TWITTER;
case 9:
case "USER_DATA_TYPE_GITHUB":
return UserDataType.GITHUB;
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum UserDataType");
}
Expand All @@ -297,6 +302,8 @@ export function userDataTypeToJSON(object: UserDataType): string {
return "USER_DATA_TYPE_LOCATION";
case UserDataType.TWITTER:
return "USER_DATA_TYPE_TWITTER";
case UserDataType.GITHUB:
return "USER_DATA_TYPE_GITHUB";
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum UserDataType");
}
Expand Down
42 changes: 42 additions & 0 deletions packages/core/src/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const ALLOWED_CLOCK_SKEW_SECONDS = 10 * 60;
export const FNAME_REGEX = /^[a-z0-9][a-z0-9-]{0,15}$/;
export const HEX_REGEX = /^(0x)?[0-9A-Fa-f]+$/;
export const TWITTER_REGEX = /^[a-z0-9_]{0,15}$/;
export const GITHUB_REGEX = /^[a-z\d](?:[a-z\d]|-(?!-)){0,38}$/i;

export const USERNAME_MAX_LENGTH = 20;

Expand Down Expand Up @@ -1030,6 +1031,13 @@ export const validateUserDataAddBody = (body: protobufs.UserDataBody): HubResult
}
break;
}
case protobufs.UserDataType.GITHUB: {
const validatedGithubUsername = validateGithubUsername(value);
if (validatedGithubUsername.isErr()) {
return err(validatedGithubUsername.error);
}
break;
}
default:
return err(new HubError("bad_request.validation_failure", "invalid user data type"));
}
Expand Down Expand Up @@ -1150,3 +1158,37 @@ export const validateTwitterUsername = <T extends string | Uint8Array>(username?

return ok(username);
};

export const validateGithubUsername = <T extends string | Uint8Array>(username?: T | null): HubResult<T> => {
if (username === undefined || username === null || username === "") {
return err(new HubError("bad_request.validation_failure", "username is missing"));
}

let githubUsername;
if (username instanceof Uint8Array) {
const fromBytes = bytesToUtf8String(username);
if (fromBytes.isErr()) {
return err(fromBytes.error);
}
githubUsername = fromBytes.value;
} else {
githubUsername = username;
}

if (githubUsername === undefined || githubUsername === null || githubUsername === "") {
return err(new HubError("bad_request.validation_failure", "username is missing"));
}

if (githubUsername.length > 38) {
return err(new HubError("bad_request.validation_failure", `username "${githubUsername}" > 38 characters`));
}

const hasValidChars = GITHUB_REGEX.test(githubUsername);
if (hasValidChars === false) {
return err(
new HubError("bad_request.validation_failure", `username "${githubUsername}" doesn't match ${GITHUB_REGEX}`),
);
}

return ok(username);
};
7 changes: 7 additions & 0 deletions packages/hub-nodejs/src/generated/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ export enum UserDataType {
LOCATION = 7,
/** TWITTER - Username of user on twitter */
TWITTER = 8,
/** GITHUB - Username of user on github */
GITHUB = 9,
}

export function userDataTypeFromJSON(object: any): UserDataType {
Expand Down Expand Up @@ -274,6 +276,9 @@ export function userDataTypeFromJSON(object: any): UserDataType {
case 8:
case "USER_DATA_TYPE_TWITTER":
return UserDataType.TWITTER;
case 9:
case "USER_DATA_TYPE_GITHUB":
return UserDataType.GITHUB;
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum UserDataType");
}
Expand All @@ -297,6 +302,8 @@ export function userDataTypeToJSON(object: UserDataType): string {
return "USER_DATA_TYPE_LOCATION";
case UserDataType.TWITTER:
return "USER_DATA_TYPE_TWITTER";
case UserDataType.GITHUB:
return "USER_DATA_TYPE_GITHUB";
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum UserDataType");
}
Expand Down
7 changes: 7 additions & 0 deletions packages/hub-web/src/generated/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ export enum UserDataType {
LOCATION = 7,
/** TWITTER - Username of user on twitter */
TWITTER = 8,
/** GITHUB - Username of user on github */
GITHUB = 9,
}

export function userDataTypeFromJSON(object: any): UserDataType {
Expand Down Expand Up @@ -274,6 +276,9 @@ export function userDataTypeFromJSON(object: any): UserDataType {
case 8:
case "USER_DATA_TYPE_TWITTER":
return UserDataType.TWITTER;
case 9:
case "USER_DATA_TYPE_GITHUB":
return UserDataType.GITHUB;
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum UserDataType");
}
Expand All @@ -297,6 +302,8 @@ export function userDataTypeToJSON(object: UserDataType): string {
return "USER_DATA_TYPE_LOCATION";
case UserDataType.TWITTER:
return "USER_DATA_TYPE_TWITTER";
case UserDataType.GITHUB:
return "USER_DATA_TYPE_GITHUB";
default:
throw new tsProtoGlobalThis.Error("Unrecognized enum value " + object + " for enum UserDataType");
}
Expand Down
1 change: 1 addition & 0 deletions protobufs/schemas/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ enum UserDataType {
USER_DATA_TYPE_USERNAME = 6; // Preferred Name for the user
USER_DATA_TYPE_LOCATION = 7; // Current location for the user
USER_DATA_TYPE_TWITTER = 8; // Username of user on twitter
USER_DATA_TYPE_GITHUB = 9; // Username of user on github
}

message Embed {
Expand Down

0 comments on commit 74f7a56

Please sign in to comment.