refactor: resolve #7139

This commit is contained in:
syuilo 2021-02-13 15:33:38 +09:00
parent ebadd7fd3f
commit 91172654e4
76 changed files with 107 additions and 221 deletions

View file

@ -1,7 +1,6 @@
import isNativeToken from './common/is-native-token';
import { User } from '../../models/entities/user';
import { Users, AccessTokens, Apps } from '../../models';
import { ensure } from '../../prelude/ensure';
import { AccessToken } from '../../models/entities/access-token';
export default async (token: string): Promise<[User | null | undefined, AccessToken | null | undefined]> => {
@ -43,7 +42,7 @@ export default async (token: string): Promise<[User | null | undefined, AccessTo
if (accessToken.appId) {
const app = await Apps
.findOne(accessToken.appId).then(ensure);
.findOneOrFail(accessToken.appId);
return [user, {
id: accessToken.id,

View file

@ -3,7 +3,6 @@ import { Note } from '../../../models/entities/note';
import { User } from '../../../models/entities/user';
import { Notes, UserProfiles, NoteReactions } from '../../../models';
import { generateMutedUserQuery } from './generate-muted-user-query';
import { ensure } from '../../../prelude/ensure';
// TODO: リアクション、Renote、返信などをしたートは除外する
@ -11,7 +10,7 @@ export async function injectFeatured(timeline: Note[], user?: User | null) {
if (timeline.length < 5) return;
if (user) {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
if (!profile.injectFeaturedNote) return;
}

View file

@ -2,7 +2,6 @@ import rndstr from 'rndstr';
import { Note } from '../../../models/entities/note';
import { User } from '../../../models/entities/user';
import { PromoReads, PromoNotes, Notes, Users } from '../../../models';
import { ensure } from '../../../prelude/ensure';
export async function injectPromo(timeline: Note[], user?: User | null) {
if (timeline.length < 5) return;
@ -23,10 +22,10 @@ export async function injectPromo(timeline: Note[], user?: User | null) {
// Pick random promo
const promo = promos[Math.floor(Math.random() * promos.length)];
const note = await Notes.findOne(promo.noteId).then(ensure);
const note = await Notes.findOneOrFail(promo.noteId);
// Join
note.user = await Users.findOne(note.userId).then(ensure);
note.user = await Users.findOneOrFail(note.userId);
(note as any)._prId_ = rndstr('a-z0-9', 8);

View file

@ -2,7 +2,6 @@ import $ from 'cafy';
import define from '../../../define';
import deleteFollowing from '../../../../../services/following/delete';
import { Followings, Users } from '../../../../../models';
import { ensure } from '../../../../../prelude/ensure';
export const meta = {
tags: ['admin'],
@ -23,8 +22,8 @@ export default define(meta, async (ps, me) => {
});
const pairs = await Promise.all(followings.map(f => Promise.all([
Users.findOne(f.followerId).then(ensure),
Users.findOne(f.followeeId).then(ensure)
Users.findOneOrFail(f.followerId),
Users.findOneOrFail(f.followeeId)
])));
for (const pair of pairs) {

View file

@ -4,7 +4,6 @@ import define from '../../define';
import { ApiError } from '../../error';
import { AuthSessions, AccessTokens, Apps } from '../../../../models';
import { genId } from '../../../../misc/gen-id';
import { ensure } from '../../../../prelude/ensure';
import { secureRndstr } from '../../../../misc/secure-rndstr';
export const meta = {
@ -49,7 +48,7 @@ export default define(meta, async (ps, user) => {
if (exist == null) {
// Lookup app
const app = await Apps.findOne(session.appId).then(ensure);
const app = await Apps.findOneOrFail(session.appId);
// Generate Hash
const sha256 = crypto.createHash('sha256');

View file

@ -2,7 +2,6 @@ import $ from 'cafy';
import define from '../../../define';
import { ApiError } from '../../../error';
import { Apps, AuthSessions, AccessTokens, Users } from '../../../../../models';
import { ensure } from '../../../../../prelude/ensure';
export const meta = {
tags: ['auth'],
@ -92,10 +91,10 @@ export default define(meta, async (ps) => {
}
// Lookup access token
const accessToken = await AccessTokens.findOne({
const accessToken = await AccessTokens.findOneOrFail({
appId: app.id,
userId: session.userId
}).then(ensure);
});
// Delete session
AuthSessions.delete(session.id);

View file

@ -1,6 +1,5 @@
import define from '../define';
import { RegistryItems, UserProfiles, Users } from '../../../models';
import { ensure } from '../../../prelude/ensure';
import { genId } from '../../../misc/gen-id';
export const meta = {
@ -25,7 +24,7 @@ export default define(meta, async (ps, user, token) => {
const isSecure = token == null;
// TODO: そのうち消す
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
for (const [k, v] of Object.entries(profile.clientData)) {
await RegistryItems.insert({
id: genId(),

View file

@ -2,7 +2,6 @@ import $ from 'cafy';
import * as speakeasy from 'speakeasy';
import define from '../../../define';
import { UserProfiles } from '../../../../../models';
import { ensure } from '../../../../../prelude/ensure';
export const meta = {
requireCredential: true as const,
@ -19,7 +18,7 @@ export const meta = {
export default define(meta, async (ps, user) => {
const token = ps.token.replace(/\s/g, '');
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
if (profile.twoFactorTempSecret == null) {
throw new Error('二段階認証の設定が開始されていません');

View file

@ -9,7 +9,6 @@ import {
AttestationChallenges,
Users
} from '../../../../../models';
import { ensure } from '../../../../../prelude/ensure';
import config from '../../../../../config';
import { procedures, hash } from '../../../2fa';
import { publishMainStream } from '../../../../../services/stream';
@ -43,7 +42,7 @@ export const meta = {
const rpIdHashReal = hash(Buffer.from(config.hostname, 'utf-8'));
export default define(meta, async (ps, user) => {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);

View file

@ -2,7 +2,6 @@ import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import define from '../../../define';
import { UserProfiles, AttestationChallenges } from '../../../../../models';
import { ensure } from '../../../../../prelude/ensure';
import { promisify } from 'util';
import * as crypto from 'crypto';
import { genId } from '../../../../../misc/gen-id';
@ -23,7 +22,7 @@ export const meta = {
};
export default define(meta, async (ps, user) => {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);

View file

@ -5,7 +5,6 @@ import * as QRCode from 'qrcode';
import config from '../../../../../config';
import define from '../../../define';
import { UserProfiles } from '../../../../../models';
import { ensure } from '../../../../../prelude/ensure';
export const meta = {
requireCredential: true as const,
@ -20,7 +19,7 @@ export const meta = {
};
export default define(meta, async (ps, user) => {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);

View file

@ -2,7 +2,6 @@ import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import define from '../../../define';
import { UserProfiles, UserSecurityKeys, Users } from '../../../../../models';
import { ensure } from '../../../../../prelude/ensure';
import { publishMainStream } from '../../../../../services/stream';
export const meta = {
@ -21,7 +20,7 @@ export const meta = {
};
export default define(meta, async (ps, user) => {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);

View file

@ -2,7 +2,6 @@ import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import define from '../../../define';
import { UserProfiles } from '../../../../../models';
import { ensure } from '../../../../../prelude/ensure';
export const meta = {
requireCredential: true as const,
@ -17,7 +16,7 @@ export const meta = {
};
export default define(meta, async (ps, user) => {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);

View file

@ -2,7 +2,6 @@ import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import define from '../../define';
import { UserProfiles } from '../../../../models';
import { ensure } from '../../../../prelude/ensure';
export const meta = {
requireCredential: true as const,
@ -21,7 +20,7 @@ export const meta = {
};
export default define(meta, async (ps, user) => {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(ps.currentPassword, profile.password!);

View file

@ -2,7 +2,6 @@ import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import define from '../../define';
import { Users, UserProfiles } from '../../../../models';
import { ensure } from '../../../../prelude/ensure';
import { doPostSuspend } from '../../../../services/suspend-user';
export const meta = {
@ -18,7 +17,7 @@ export const meta = {
};
export default define(meta, async (ps, user) => {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);

View file

@ -4,7 +4,6 @@ import { publishMainStream } from '../../../../services/stream';
import generateUserToken from '../../common/generate-native-user-token';
import define from '../../define';
import { Users, UserProfiles } from '../../../../models';
import { ensure } from '../../../../prelude/ensure';
export const meta = {
requireCredential: true as const,
@ -19,7 +18,7 @@ export const meta = {
};
export default define(meta, async (ps, user) => {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);

View file

@ -6,7 +6,6 @@ import config from '../../../../config';
import * as ms from 'ms';
import * as bcrypt from 'bcryptjs';
import { Users, UserProfiles } from '../../../../models';
import { ensure } from '../../../../prelude/ensure';
import { sendEmail } from '../../../../services/send-email';
import { ApiError } from '../../error';
@ -40,7 +39,7 @@ export const meta = {
};
export default define(meta, async (ps, user) => {
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(ps.password, profile.password!);

View file

@ -13,7 +13,6 @@ import { ApiError } from '../../error';
import { Users, DriveFiles, UserProfiles, Pages } from '../../../../models';
import { User } from '../../../../models/entities/user';
import { UserProfile } from '../../../../models/entities/user-profile';
import { ensure } from '../../../../prelude/ensure';
import { notificationTypes } from '../../../../types';
import { normalizeForSearch } from '../../../../misc/normalize-for-search';
@ -206,7 +205,7 @@ export default define(meta, async (ps, user, token) => {
const updates = {} as Partial<User>;
const profileUpdates = {} as Partial<UserProfile>;
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
if (ps.name !== undefined) updates.name = ps.name;
if (ps.description !== undefined) profileUpdates.description = ps.description;

View file

@ -6,7 +6,6 @@ import * as ms from 'ms';
import { getNote } from '../../common/getters';
import { ApiError } from '../../error';
import { Users } from '../../../../models';
import { ensure } from '../../../../prelude/ensure';
export const meta = {
desc: {
@ -62,5 +61,5 @@ export default define(meta, async (ps, user) => {
}
// この操作を行うのが投稿者とは限らない(例えばモデレーター)ため
await deleteNote(await Users.findOne(note.userId).then(ensure), note);
await deleteNote(await Users.findOneOrFail(note.userId), note);
});

View file

@ -13,7 +13,6 @@ import { PollVotes, NoteWatchings, Users, Polls } from '../../../../../models';
import { Not } from 'typeorm';
import { IRemoteUser } from '../../../../../models/entities/user';
import { genId } from '../../../../../misc/gen-id';
import { ensure } from '../../../../../prelude/ensure';
export const meta = {
desc: {
@ -87,7 +86,7 @@ export default define(meta, async (ps, user) => {
throw new ApiError(meta.errors.noPoll);
}
const poll = await Polls.findOne({ noteId: note.id }).then(ensure);
const poll = await Polls.findOneOrFail({ noteId: note.id });
if (poll.expiresAt && poll.expiresAt < createdAt) {
throw new ApiError(meta.errors.alreadyExpired);
@ -153,7 +152,7 @@ export default define(meta, async (ps, user) => {
// リモート投票の場合リプライ送信
if (note.userHost != null) {
const pollOwner = await Users.findOne(note.userId).then(ensure) as IRemoteUser;
const pollOwner = await Users.findOneOrFail(note.userId) as IRemoteUser;
deliver(user, renderActivity(await renderVote(user, vote, note, poll, pollOwner)), pollOwner.inbox);
}

View file

@ -3,7 +3,6 @@ import define from '../../define';
import { ApiError } from '../../error';
import { Users, UserProfiles } from '../../../../models';
import { ID } from '../../../../misc/cafy-id';
import { ensure } from '../../../../prelude/ensure';
import { toPunyNullable } from '../../../../misc/convert-host';
export const meta = {
@ -51,7 +50,7 @@ export default define(meta, async (ps, me) => {
throw new ApiError(meta.errors.noSuchUser);
}
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
if (profile.room.furnitures == null) {
await UserProfiles.update(user.id, {

View file

@ -6,7 +6,6 @@ import config from '../../../config';
import { Users, Signins, UserProfiles, UserSecurityKeys, AttestationChallenges } from '../../../models';
import { ILocalUser } from '../../../models/entities/user';
import { genId } from '../../../misc/gen-id';
import { ensure } from '../../../prelude/ensure';
import { verifyLogin, hash } from '../2fa';
import { randomBytes } from 'crypto';
@ -47,7 +46,7 @@ export default async (ctx: Koa.Context) => {
return;
}
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
// Compare password
const same = await bcrypt.compare(password, profile.password!);

View file

@ -10,7 +10,6 @@ import signin from '../common/signin';
import { fetchMeta } from '../../../misc/fetch-meta';
import { Users, UserProfiles } from '../../../models';
import { ILocalUser } from '../../../models/entities/user';
import { ensure } from '../../../prelude/ensure';
function getUserToken(ctx: Koa.Context) {
return ((ctx.headers['cookie'] || '').match(/igi=(\w+)/) || [null, null])[1];
@ -41,12 +40,12 @@ router.get('/disconnect/discord', async ctx => {
return;
}
const user = await Users.findOne({
const user = await Users.findOneOrFail({
host: null,
token: userToken
}).then(ensure);
});
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
delete profile.integrations.discord;
@ -253,12 +252,12 @@ router.get('/dc/cb', async ctx => {
return;
}
const user = await Users.findOne({
const user = await Users.findOneOrFail({
host: null,
token: userToken
}).then(ensure);
});
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
await UserProfiles.update(user.id, {
integrations: {

View file

@ -10,7 +10,6 @@ import signin from '../common/signin';
import { fetchMeta } from '../../../misc/fetch-meta';
import { Users, UserProfiles } from '../../../models';
import { ILocalUser } from '../../../models/entities/user';
import { ensure } from '../../../prelude/ensure';
function getUserToken(ctx: Koa.Context) {
return ((ctx.headers['cookie'] || '').match(/igi=(\w+)/) || [null, null])[1];
@ -41,12 +40,12 @@ router.get('/disconnect/github', async ctx => {
return;
}
const user = await Users.findOne({
const user = await Users.findOneOrFail({
host: null,
token: userToken
}).then(ensure);
});
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
delete profile.integrations.github;
@ -227,12 +226,12 @@ router.get('/gh/cb', async ctx => {
return;
}
const user = await Users.findOne({
const user = await Users.findOneOrFail({
host: null,
token: userToken
}).then(ensure);
});
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
await UserProfiles.update(user.id, {
integrations: {

View file

@ -9,7 +9,6 @@ import signin from '../common/signin';
import { fetchMeta } from '../../../misc/fetch-meta';
import { Users, UserProfiles } from '../../../models';
import { ILocalUser } from '../../../models/entities/user';
import { ensure } from '../../../prelude/ensure';
function getUserToken(ctx: Koa.Context) {
return ((ctx.headers['cookie'] || '').match(/igi=(\w+)/) || [null, null])[1];
@ -40,12 +39,12 @@ router.get('/disconnect/twitter', async ctx => {
return;
}
const user = await Users.findOne({
const user = await Users.findOneOrFail({
host: null,
token: userToken
}).then(ensure);
});
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
delete profile.integrations.twitter;
@ -163,12 +162,12 @@ router.get('/tw/cb', async ctx => {
const result = await twAuth!.done(JSON.parse(twCtx), verifier);
const user = await Users.findOne({
const user = await Users.findOneOrFail({
host: null,
token: userToken
}).then(ensure);
});
const profile = await UserProfiles.findOne(user.id).then(ensure);
const profile = await UserProfiles.findOneOrFail(user.id);
await UserProfiles.update(user.id, {
integrations: {