Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Add pretty profile pictures for domain, group, user
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 3, 2020
1 parent 68fb2da commit 27066a6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { compare, hash } from 'bcrypt';
import anonymize from 'ip-anonymize';
import { authenticator } from 'otplib';
import qrcode from 'qrcode';
import randomColor from 'randomcolor';
import { safeEmail } from '../../helpers/safe-email';
import { ApprovedSubnetsService } from '../approved-subnets/approved-subnets.service';
import { EmailService } from '../email/email.service';
Expand All @@ -24,11 +25,11 @@ import { PrismaService } from '../prisma/prisma.service';
import { PwnedService } from '../pwned/pwned.service';
import {
APPROVE_SUBNET_TOKEN,
EMAIL_MFA_TOKEN,
EMAIL_VERIFY_TOKEN,
LOGIN_ACCESS_TOKEN,
MULTI_FACTOR_TOKEN,
PASSWORD_RESET_TOKEN,
EMAIL_MFA_TOKEN,
LOGIN_ACCESS_TOKEN,
} from '../tokens/tokens.constants';
import { TokensService } from '../tokens/tokens.service';
import { RegisterDto } from './auth.dto';
Expand Down Expand Up @@ -155,6 +156,18 @@ export class AuthService {
data.password,
ignorePwnedPassword,
);
let initials = data.name.trim().substr(0, 2).toUpperCase();
if (data.name.includes(' '))
initials = data.name
.split(' ')
.map((i) => i.trim().substr(0, 1))
.join('')
.toUpperCase();
data.profilePictureUrl =
data.profilePictureUrl ??
`https://ui-avatars.com/api/?name=${initials}&background=${randomColor({
luminosity: 'light',
})}&color=000000`;

const user = await this.prisma.users.create({
data: {
Expand Down
22 changes: 22 additions & 0 deletions src/modules/domains/domains.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ export class DomainsService {
const fullUrl = new URL(data.domain);
data.domain = fullUrl.hostname;
const verificationCode = this.tokensService.generateUuid();
const currentProfilePicture = await this.prisma.groups.findOne({
where: { id: groupId },
select: { profilePictureUrl: true },
});
if (
currentProfilePicture.profilePictureUrl.startsWith(
'https://ui-avatars.com',
)
)
try {
const img = await got('https://logo.clearbit.com/${data.domain}', {
responseType: 'buffer',
});
if (img.body.byteLength > 1)
await this.prisma.groups.update({
where: { id: groupId },
data: {
profilePictureUrl: `https://logo.clearbit.com/${data.domain}`,
},
});
} catch (error) {}

return this.prisma.domains.create({
data: {
...data,
Expand Down
13 changes: 13 additions & 0 deletions src/modules/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@prisma/client';
import { Expose } from '../../modules/prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import randomColor from 'randomcolor';

@Injectable()
export class GroupsService {
Expand All @@ -18,6 +19,18 @@ export class GroupsService {
userId: number,
data: Omit<Omit<groupsCreateInput, 'group'>, 'user'>,
): Promise<groups> {
let initials = data.name.trim().substr(0, 2).toUpperCase();
if (data.name.includes(' '))
initials = data.name
.split(' ')
.map((i) => i.trim().substr(0, 1))
.join('')
.toUpperCase();
data.profilePictureUrl =
data.profilePictureUrl ??
`https://ui-avatars.com/api/?name=${initials}&background=${randomColor({
luminosity: 'light',
})}&color=000000`;
return this.prisma.groups.create({
data: {
...data,
Expand Down

0 comments on commit 27066a6

Please sign in to comment.