From 6d33b366f8f18b0724c4a3d8cbfa6a7a4112b805 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 17 Apr 2022 21:18:18 +0900 Subject: [PATCH] fix ogp rendering and refactor --- packages/backend/src/models/entities/user.ts | 4 +-- .../backend/src/models/repositories/user.ts | 28 +++++++++++++------ .../backend/src/server/api/common/signin.ts | 2 +- packages/backend/src/server/index.ts | 20 ++++++------- packages/backend/src/server/web/feed.ts | 6 ++-- packages/backend/src/server/web/index.ts | 19 ++++++++----- .../backend/src/server/web/views/clip.pug | 2 +- .../backend/src/server/web/views/note.pug | 2 +- .../backend/src/server/web/views/page.pug | 2 +- .../backend/src/server/web/views/user.pug | 3 +- 10 files changed, 51 insertions(+), 37 deletions(-) diff --git a/packages/backend/src/models/entities/user.ts b/packages/backend/src/models/entities/user.ts index c76824c977..29d9a0c2ca 100644 --- a/packages/backend/src/models/entities/user.ts +++ b/packages/backend/src/models/entities/user.ts @@ -1,6 +1,6 @@ import { Entity, Column, Index, OneToOne, JoinColumn, PrimaryColumn } from 'typeorm'; -import { DriveFile } from './drive-file.js'; import { id } from '../id.js'; +import { DriveFile } from './drive-file.js'; @Entity() @Index(['usernameLower', 'host'], { unique: true }) @@ -207,7 +207,7 @@ export class User { @Column('boolean', { default: false, - comment: 'Whether to show users replying to other users in the timeline' + comment: 'Whether to show users replying to other users in the timeline', }) public showTimelineReplies: boolean; diff --git a/packages/backend/src/models/repositories/user.ts b/packages/backend/src/models/repositories/user.ts index 2f4b7d6787..541fbaf003 100644 --- a/packages/backend/src/models/repositories/user.ts +++ b/packages/backend/src/models/repositories/user.ts @@ -1,7 +1,6 @@ import { EntityRepository, Repository, In, Not } from 'typeorm'; import Ajv from 'ajv'; import { User, ILocalUser, IRemoteUser } from '@/models/entities/user.js'; -import { Notes, NoteUnreads, FollowRequests, Notifications, MessagingMessages, UserNotePinings, Followings, Blockings, Mutings, UserProfiles, UserSecurityKeys, UserGroupJoinings, Pages, Announcements, AnnouncementReads, Antennas, AntennaNotes, ChannelFollowings, Instances, DriveFiles } from '../index.js'; import config from '@/config/index.js'; import { Packed } from '@/misc/schema.js'; import { awaitAll, Promiseable } from '@/prelude/await-all.js'; @@ -9,8 +8,9 @@ import { populateEmojis } from '@/misc/populate-emojis.js'; import { getAntennas } from '@/misc/antenna-cache.js'; import { USER_ACTIVE_THRESHOLD, USER_ONLINE_THRESHOLD } from '@/const.js'; import { Cache } from '@/misc/cache.js'; -import { Instance } from '../entities/instance.js'; import { db } from '@/db/postgre.js'; +import { Instance } from '../entities/instance.js'; +import { Notes, NoteUnreads, FollowRequests, Notifications, MessagingMessages, UserNotePinings, Followings, Blockings, Mutings, UserProfiles, UserSecurityKeys, UserGroupJoinings, Pages, Announcements, AnnouncementReads, Antennas, AntennaNotes, ChannelFollowings, Instances, DriveFiles } from '../index.js'; const userInstanceCache = new Cache(1000 * 60 * 60 * 3); @@ -112,7 +112,7 @@ export const UserRepository = db.getRepository(User).extend({ const joinings = await UserGroupJoinings.findBy({ userId: userId }); const groupQs = Promise.all(joinings.map(j => MessagingMessages.createQueryBuilder('message') - .where(`message.groupId = :groupId`, { groupId: j.userGroupId }) + .where('message.groupId = :groupId', { groupId: j.userGroupId }) .andWhere('message.userId != :userId', { userId: userId }) .andWhere('NOT (:userId = ANY(message.reads))', { userId: userId }) .andWhere('message.createdAt > :joinedAt', { joinedAt: j.createdAt }) // 自分が加入する前の会話については、未読扱いしない @@ -204,8 +204,18 @@ export const UserRepository = db.getRepository(User).extend({ ); }, - getAvatarUrl(user: User): string { - // TODO: avatarIdがあるがavatarがない(JOINされてない)場合のハンドリング + async getAvatarUrl(user: User): Promise { + if (user.avatar) { + return DriveFiles.getPublicUrl(user.avatar, true) || this.getIdenticonUrl(user.id); + } else if (user.avatarId) { + const avatar = await DriveFiles.findOneByOrFail({ id: user.avatarId }); + return DriveFiles.getPublicUrl(avatar, true) || this.getIdenticonUrl(user.id); + } else { + return this.getIdenticonUrl(user.id); + } + }, + + getAvatarUrlSync(user: User): string { if (user.avatar) { return DriveFiles.getPublicUrl(user.avatar, true) || this.getIdenticonUrl(user.id); } else { @@ -223,7 +233,7 @@ export const UserRepository = db.getRepository(User).extend({ options?: { detail?: D, includeSecrets?: boolean, - } + }, ): Promise> { const opts = Object.assign({ detail: false, @@ -274,7 +284,7 @@ export const UserRepository = db.getRepository(User).extend({ name: user.name, username: user.username, host: user.host, - avatarUrl: this.getAvatarUrl(user), + avatarUrl: this.getAvatarUrlSync(user), avatarBlurhash: user.avatar?.blurhash || null, avatarColor: null, // 後方互換性のため isAdmin: user.isAdmin || falsy, @@ -283,7 +293,7 @@ export const UserRepository = db.getRepository(User).extend({ isCat: user.isCat || falsy, instance: user.host ? userInstanceCache.fetch(user.host, () => Instances.findOneBy({ host: user.host! }), - v => v != null + v => v != null, ).then(instance => instance ? { name: instance.name, softwareName: instance.softwareName, @@ -403,7 +413,7 @@ export const UserRepository = db.getRepository(User).extend({ options?: { detail?: D, includeSecrets?: boolean, - } + }, ): Promise[]> { return Promise.all(users.map(u => this.pack(u, me, options))); }, diff --git a/packages/backend/src/server/api/common/signin.ts b/packages/backend/src/server/api/common/signin.ts index f1dccee2ce..038fd8d96e 100644 --- a/packages/backend/src/server/api/common/signin.ts +++ b/packages/backend/src/server/api/common/signin.ts @@ -9,7 +9,7 @@ import { publishMainStream } from '@/services/stream.js'; export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) { if (redirect) { //#region Cookie - ctx.cookies.set('igi', user.token, { + ctx.cookies.set('igi', user.token!, { path: '/', // SEE: https://github.com/koajs/koa/issues/974 // When using a SSL proxy it should be configured to add the "X-Forwarded-Proto: https" header diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index a68cebfeb2..b50e38a63b 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -10,23 +10,23 @@ import mount from 'koa-mount'; import koaLogger from 'koa-logger'; import * as slow from 'koa-slow'; -import activityPub from './activitypub.js'; -import nodeinfo from './nodeinfo.js'; -import wellKnown from './well-known.js'; +import { IsNull } from 'typeorm'; import config from '@/config/index.js'; -import apiServer from './api/index.js'; -import fileServer from './file/index.js'; -import proxyServer from './proxy/index.js'; -import webServer from './web/index.js'; import Logger from '@/services/logger.js'; -import { envOption } from '../env.js'; import { UserProfiles, Users } from '@/models/index.js'; import { genIdenticon } from '@/misc/gen-identicon.js'; import { createTemp } from '@/misc/create-temp.js'; import { publishMainStream } from '@/services/stream.js'; import * as Acct from '@/misc/acct.js'; +import { envOption } from '../env.js'; +import activityPub from './activitypub.js'; +import nodeinfo from './nodeinfo.js'; +import wellKnown from './well-known.js'; +import apiServer from './api/index.js'; +import fileServer from './file/index.js'; +import proxyServer from './proxy/index.js'; +import webServer from './web/index.js'; import { initializeStreamingServer } from './api/streaming.js'; -import { IsNull } from 'typeorm'; export const serverLogger = new Logger('server', 'gray', false); @@ -81,7 +81,7 @@ router.get('/avatar/@:acct', async ctx => { }); if (user) { - ctx.redirect(Users.getAvatarUrl(user)); + ctx.redirect(Users.getAvatarUrlSync(user)); } else { ctx.redirect('/static-assets/user-unknown.png'); } diff --git a/packages/backend/src/server/web/feed.ts b/packages/backend/src/server/web/feed.ts index eba8dc58d4..4abe2885cf 100644 --- a/packages/backend/src/server/web/feed.ts +++ b/packages/backend/src/server/web/feed.ts @@ -1,8 +1,8 @@ import { Feed } from 'feed'; +import { In, IsNull } from 'typeorm'; import config from '@/config/index.js'; import { User } from '@/models/entities/user.js'; -import { Notes, DriveFiles, UserProfiles } from '@/models/index.js'; -import { In, IsNull } from 'typeorm'; +import { Notes, DriveFiles, UserProfiles, Users } from '@/models/index.js'; export default async function(user: User) { const author = { @@ -29,7 +29,7 @@ export default async function(user: User) { generator: 'Misskey', description: `${user.notesCount} Notes, ${profile.ffVisibility === 'public' ? user.followingCount : '?'} Following, ${profile.ffVisibility === 'public' ? user.followersCount : '?'} Followers${profile.description ? ` · ${profile.description}` : ''}`, link: author.link, - image: user.avatarUrl ? user.avatarUrl : undefined, + image: await Users.getAvatarUrl(user), feedLinks: { json: `${author.link}.json`, atom: `${author.link}.atom`, diff --git a/packages/backend/src/server/web/index.ts b/packages/backend/src/server/web/index.ts index 48bf6f7338..34d56cfd0c 100644 --- a/packages/backend/src/server/web/index.ts +++ b/packages/backend/src/server/web/index.ts @@ -11,20 +11,20 @@ import send from 'koa-send'; import favicon from 'koa-favicon'; import views from 'koa-views'; import { createBullBoard } from '@bull-board/api'; -import { BullAdapter } from '@bull-board/api/bullAdapter.js'; +import { BullAdapter } from '@bull-board/api/bullAdapter.js'; import { KoaAdapter } from '@bull-board/koa'; -import packFeed from './feed.js'; +import { IsNull } from 'typeorm'; import { fetchMeta } from '@/misc/fetch-meta.js'; -import { genOpenapiSpec } from '../api/openapi/gen-spec.js'; import config from '@/config/index.js'; import { Users, Notes, UserProfiles, Pages, Channels, Clips, GalleryPosts } from '@/models/index.js'; import * as Acct from '@/misc/acct.js'; import { getNoteSummary } from '@/misc/get-note-summary.js'; +import { queues } from '@/queue/queues.js'; +import { genOpenapiSpec } from '../api/openapi/gen-spec.js'; import { urlPreviewHandler } from './url-preview.js'; import { manifestHandler } from './manifest.js'; -import { queues } from '@/queue/queues.js'; -import { IsNull } from 'typeorm'; +import packFeed from './feed.js'; const _filename = fileURLToPath(import.meta.url); const _dirname = dirname(_filename); @@ -127,7 +127,7 @@ router.get('/twemoji/(.*)', async ctx => { return; } - ctx.set('Content-Security-Policy', `default-src 'none'; style-src 'unsafe-inline'`); + ctx.set('Content-Security-Policy', 'default-src \'none\'; style-src \'unsafe-inline\''); await send(ctx as any, path, { root: `${_dirname}/../../../node_modules/@discordapp/twemoji/dist/svg/`, @@ -235,6 +235,7 @@ router.get(['/@:user', '/@:user/:sub'], async (ctx, next) => { await ctx.render('user', { user, profile, me, + avatarUrl: await Users.getAvatarUrl(user), sub: ctx.params.sub, instanceName: meta.name || 'Misskey', icon: meta.iconUrl, @@ -274,6 +275,7 @@ router.get('/notes/:note', async (ctx, next) => { await ctx.render('note', { note: _note, profile, + avatarUrl: await Users.getAvatarUrl(await Users.findOneByOrFail({ id: note.userId })), // TODO: Let locale changeable by instance setting summary: getNoteSummary(_note), instanceName: meta.name || 'Misskey', @@ -315,6 +317,7 @@ router.get('/@:user/pages/:page', async (ctx, next) => { await ctx.render('page', { page: _page, profile, + avatarUrl: await Users.getAvatarUrl(await Users.findOneByOrFail({ id: page.userId })), instanceName: meta.name || 'Misskey', icon: meta.iconUrl, themeColor: meta.themeColor, @@ -346,6 +349,7 @@ router.get('/clips/:clip', async (ctx, next) => { await ctx.render('clip', { clip: _clip, profile, + avatarUrl: await Users.getAvatarUrl(await Users.findOneByOrFail({ id: clip.userId })), instanceName: meta.name || 'Misskey', icon: meta.iconUrl, themeColor: meta.themeColor, @@ -370,6 +374,7 @@ router.get('/gallery/:post', async (ctx, next) => { await ctx.render('gallery-post', { post: _post, profile, + avatarUrl: await Users.getAvatarUrl(await Users.findOneByOrFail({ id: post.userId })), instanceName: meta.name || 'Misskey', icon: meta.iconUrl, themeColor: meta.themeColor, @@ -434,7 +439,7 @@ router.get('/cli', async ctx => { }); }); -const override = (source: string, target: string, depth: number = 0) => +const override = (source: string, target: string, depth = 0) => [, ...target.split('/').filter(x => x), ...source.split('/').filter(x => x).splice(depth)].join('/'); router.get('/flush', async ctx => { diff --git a/packages/backend/src/server/web/views/clip.pug b/packages/backend/src/server/web/views/clip.pug index 7a84d50f6c..4c692bf59b 100644 --- a/packages/backend/src/server/web/views/clip.pug +++ b/packages/backend/src/server/web/views/clip.pug @@ -16,7 +16,7 @@ block og meta(property='og:title' content= title) meta(property='og:description' content= clip.description) meta(property='og:url' content= url) - meta(property='og:image' content= user.avatarUrl) + meta(property='og:image' content= avatarUrl) block meta if profile.noCrawle diff --git a/packages/backend/src/server/web/views/note.pug b/packages/backend/src/server/web/views/note.pug index 34b03f9833..65696ea138 100644 --- a/packages/backend/src/server/web/views/note.pug +++ b/packages/backend/src/server/web/views/note.pug @@ -17,7 +17,7 @@ block og meta(property='og:title' content= title) meta(property='og:description' content= summary) meta(property='og:url' content= url) - meta(property='og:image' content= user.avatarUrl) + meta(property='og:image' content= avatarUrl) block meta if user.host || isRenote || profile.noCrawle diff --git a/packages/backend/src/server/web/views/page.pug b/packages/backend/src/server/web/views/page.pug index b6c9548025..4219e76a52 100644 --- a/packages/backend/src/server/web/views/page.pug +++ b/packages/backend/src/server/web/views/page.pug @@ -16,7 +16,7 @@ block og meta(property='og:title' content= title) meta(property='og:description' content= page.summary) meta(property='og:url' content= url) - meta(property='og:image' content= page.eyeCatchingImage ? page.eyeCatchingImage.thumbnailUrl : user.avatarUrl) + meta(property='og:image' content= page.eyeCatchingImage ? page.eyeCatchingImage.thumbnailUrl : avatarUrl) block meta if profile.noCrawle diff --git a/packages/backend/src/server/web/views/user.pug b/packages/backend/src/server/web/views/user.pug index 2adec0f889..119993fdb5 100644 --- a/packages/backend/src/server/web/views/user.pug +++ b/packages/backend/src/server/web/views/user.pug @@ -3,7 +3,6 @@ extends ./base block vars - const title = user.name ? `${user.name} (@${user.username})` : `@${user.username}`; - const url = `${config.url}/@${(user.host ? `${user.username}@${user.host}` : user.username)}`; - - const img = user.avatarUrl || null; block title = `${title} | ${instanceName}` @@ -16,7 +15,7 @@ block og meta(property='og:title' content= title) meta(property='og:description' content= profile.description) meta(property='og:url' content= url) - meta(property='og:image' content= img) + meta(property='og:image' content= avatarUrl) block meta if user.host || profile.noCrawle