From 2c36844d34c41266005dadc58b79e71f1d1a92f8 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 16 Jan 2022 10:45:48 +0900 Subject: refactor: more common name --- packages/backend/src/misc/gen-avatar.ts | 90 ----------------------- packages/backend/src/misc/gen-identicon.ts | 91 ++++++++++++++++++++++++ packages/backend/src/models/repositories/user.ts | 2 +- packages/backend/src/server/index.ts | 6 +- 4 files changed, 95 insertions(+), 94 deletions(-) delete mode 100644 packages/backend/src/misc/gen-avatar.ts create mode 100644 packages/backend/src/misc/gen-identicon.ts diff --git a/packages/backend/src/misc/gen-avatar.ts b/packages/backend/src/misc/gen-avatar.ts deleted file mode 100644 index 8838ec8d15..0000000000 --- a/packages/backend/src/misc/gen-avatar.ts +++ /dev/null @@ -1,90 +0,0 @@ -/** - * Random avatar generator - */ - -import * as p from 'pureimage'; -import * as gen from 'random-seed'; -import { WriteStream } from 'fs'; - -const size = 256; // px -const n = 5; // resolution -const margin = (size / n); -const colors = [ - '#e57373', - '#F06292', - '#BA68C8', - '#9575CD', - '#7986CB', - '#64B5F6', - '#4FC3F7', - '#4DD0E1', - '#4DB6AC', - '#81C784', - '#8BC34A', - '#AFB42B', - '#F57F17', - '#FF5722', - '#795548', - '#455A64', -]; -const bg = '#e9e9e9'; - -const actualSize = size - (margin * 2); -const cellSize = actualSize / n; -const sideN = Math.floor(n / 2); - -/** - * Generate buffer of random avatar by seed - */ -export function genAvatar(seed: string, stream: WriteStream): Promise { - const rand = gen.create(seed); - const canvas = p.make(size, size); - const ctx = canvas.getContext('2d'); - - ctx.fillStyle = bg; - ctx.beginPath(); - ctx.fillRect(0, 0, size, size); - - ctx.fillStyle = colors[rand(colors.length)]; - - // side bitmap (filled by false) - const side: boolean[][] = new Array(sideN); - for (let i = 0; i < side.length; i++) { - side[i] = new Array(n).fill(false); - } - - // 1*n (filled by false) - const center: boolean[] = new Array(n).fill(false); - - // eslint:disable-next-line:prefer-for-of - for (let x = 0; x < side.length; x++) { - for (let y = 0; y < side[x].length; y++) { - side[x][y] = rand(3) === 0; - } - } - - for (let i = 0; i < center.length; i++) { - center[i] = rand(3) === 0; - } - - // Draw - for (let x = 0; x < n; x++) { - for (let y = 0; y < n; y++) { - const isXCenter = x === ((n - 1) / 2); - if (isXCenter && !center[y]) continue; - - const isLeftSide = x < ((n - 1) / 2); - if (isLeftSide && !side[x][y]) continue; - - const isRightSide = x > ((n - 1) / 2); - if (isRightSide && !side[sideN - (x - sideN)][y]) continue; - - const actualX = margin + (cellSize * x); - const actualY = margin + (cellSize * y); - ctx.beginPath(); - ctx.fillRect(actualX, actualY, cellSize, cellSize); - } - } - - return p.encodePNGToStream(canvas, stream); -} diff --git a/packages/backend/src/misc/gen-identicon.ts b/packages/backend/src/misc/gen-identicon.ts new file mode 100644 index 0000000000..5cedd7afaf --- /dev/null +++ b/packages/backend/src/misc/gen-identicon.ts @@ -0,0 +1,91 @@ +/** + * Identicon generator + * https://en.wikipedia.org/wiki/Identicon + */ + +import * as p from 'pureimage'; +import * as gen from 'random-seed'; +import { WriteStream } from 'fs'; + +const size = 256; // px +const n = 5; // resolution +const margin = (size / n); +const colors = [ + '#e57373', + '#F06292', + '#BA68C8', + '#9575CD', + '#7986CB', + '#64B5F6', + '#4FC3F7', + '#4DD0E1', + '#4DB6AC', + '#81C784', + '#8BC34A', + '#AFB42B', + '#F57F17', + '#FF5722', + '#795548', + '#455A64', +]; +const bg = '#e9e9e9'; + +const actualSize = size - (margin * 2); +const cellSize = actualSize / n; +const sideN = Math.floor(n / 2); + +/** + * Generate buffer of an identicon by seed + */ +export function genIdenticon(seed: string, stream: WriteStream): Promise { + const rand = gen.create(seed); + const canvas = p.make(size, size); + const ctx = canvas.getContext('2d'); + + ctx.fillStyle = bg; + ctx.beginPath(); + ctx.fillRect(0, 0, size, size); + + ctx.fillStyle = colors[rand(colors.length)]; + + // side bitmap (filled by false) + const side: boolean[][] = new Array(sideN); + for (let i = 0; i < side.length; i++) { + side[i] = new Array(n).fill(false); + } + + // 1*n (filled by false) + const center: boolean[] = new Array(n).fill(false); + + // eslint:disable-next-line:prefer-for-of + for (let x = 0; x < side.length; x++) { + for (let y = 0; y < side[x].length; y++) { + side[x][y] = rand(3) === 0; + } + } + + for (let i = 0; i < center.length; i++) { + center[i] = rand(3) === 0; + } + + // Draw + for (let x = 0; x < n; x++) { + for (let y = 0; y < n; y++) { + const isXCenter = x === ((n - 1) / 2); + if (isXCenter && !center[y]) continue; + + const isLeftSide = x < ((n - 1) / 2); + if (isLeftSide && !side[x][y]) continue; + + const isRightSide = x > ((n - 1) / 2); + if (isRightSide && !side[sideN - (x - sideN)][y]) continue; + + const actualX = margin + (cellSize * x); + const actualY = margin + (cellSize * y); + ctx.beginPath(); + ctx.fillRect(actualX, actualY, cellSize, cellSize); + } + } + + return p.encodePNGToStream(canvas, stream); +} diff --git a/packages/backend/src/models/repositories/user.ts b/packages/backend/src/models/repositories/user.ts index 3dc7c67ec2..85141cdc41 100644 --- a/packages/backend/src/models/repositories/user.ts +++ b/packages/backend/src/models/repositories/user.ts @@ -159,7 +159,7 @@ export class UserRepository extends Repository { if (user.avatarUrl) { return user.avatarUrl; } else { - return `${config.url}/random-avatar/${user.id}`; + return `${config.url}/identicon/${user.id}`; } } diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index 85fe21accb..764306c7d8 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -23,7 +23,7 @@ import Logger from '@/services/logger'; import { envOption } from '../env'; import { UserProfiles, Users } from '@/models/index'; import { networkChart } from '@/services/chart/index'; -import { genAvatar } from '@/misc/gen-avatar'; +import { genIdenticon } from '@/misc/gen-identicon'; import { createTemp } from '@/misc/create-temp'; import { publishMainStream } from '@/services/stream'; import * as Acct from 'misskey-js/built/acct'; @@ -84,9 +84,9 @@ router.get('/avatar/@:acct', async ctx => { } }); -router.get('/random-avatar/:x', async ctx => { +router.get('/identicon/:x', async ctx => { const [temp] = await createTemp(); - await genAvatar(ctx.params.x, fs.createWriteStream(temp)); + await genIdenticon(ctx.params.x, fs.createWriteStream(temp)); ctx.set('Content-Type', 'image/png'); ctx.body = fs.createReadStream(temp); }); -- cgit v1.3.1-freya