Skip to content

Commit

Permalink
chore: auto-populate avatar field with getGravatar function
Browse files Browse the repository at this point in the history
  • Loading branch information
psanders committed Dec 22, 2023
1 parent a5a1bad commit 3b4162d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
26 changes: 26 additions & 0 deletions mods/apiserver/src/users/getGravatar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2023 by Fonoster Inc (https://fonoster.com)
* http://github.com/fonoster/goodtok
*
* This file is part of Goodtok
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import crypt from "crypto";

export function getGravatarURL(email: string, size = 200) {
email = email.trim().toLowerCase();
const hash = crypt.createHash("md5").update(email).digest("hex");
const url = `https://www.gravatar.com/avatar/${hash}?s=${size}`;
return url;
}
5 changes: 2 additions & 3 deletions mods/apiserver/src/users/updateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export async function updateUser(
ctx: Context,
request: UpdateUserRequest
): Promise<User> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password, ...rest } = request;
const { password } = request;

logger.verbose("updating user", { id: ctx.userId });

Expand All @@ -46,7 +45,7 @@ export async function updateUser(
},
data: {
...request,
password: password ? password : userFromDB.password,
password: password || userFromDB.password,
updatedAt: new Date()
}
});
Expand Down
2 changes: 2 additions & 0 deletions mods/apiserver/src/users/upsertDefaultUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
import { getLogger } from "@fonoster/logger";
import { prisma } from "../db";
import { getGravatarURL } from "./getGravatar";

const logger = getLogger({ service: "apiserver", filePath: __filename });

Expand Down Expand Up @@ -59,6 +60,7 @@ async function upsertDefaultUser(request: { email: string; password: string }) {
create: {
name: "Admin",
email,
avatar: getGravatarURL(email),
password,
createdAt: today,
updatedAt: today
Expand Down
8 changes: 6 additions & 2 deletions mods/apiserver/src/workspaces/addWorkspaceMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ import {
} from "./types";
import { Context } from "../context";
import { getLogger } from "@fonoster/logger";
import { WorkspaceMemberStatus as PrismaWorkspaceMemberStatus } from "@prisma/client";
import { WorkspaceMemberRole as PrismaWorkspaceMemberRole } from "@prisma/client";
import {
WorkspaceMemberStatus as PrismaWorkspaceMemberStatus,
WorkspaceMemberRole as PrismaWorkspaceMemberRole
} from "@prisma/client";
import { customAlphabet } from "nanoid";
import { sendInvite } from "../notifications/sendInvite";
import { getGravatarURL } from "../users/getGravatar";

const logger = getLogger({ service: "apiserver", filePath: __filename });

Expand Down Expand Up @@ -78,6 +81,7 @@ export async function addWorkspaceMember(
data: {
email,
name,
avatar: getGravatarURL(email),
password: oneTimePassword
}
});
Expand Down

0 comments on commit 3b4162d

Please sign in to comment.