refactor: improve code quality (#8751)

* remove unnecessary if

`Array.prototype.some` already returns a boolean so an if to return
true or false is completely unnecessary in this case.

* perf: use count instead of find

When using `count` instead of `findOneBy`, the data is not
unnecessarily loaded.

* remove duplicate null check

The variable is checked for null in the lines above and the function
returns if so. Therefore, it can not be null at this point.

* simplify `getJsonSchema`

Because the assigned value is `null` and the used keys are only
shallow, use of `nestedProperty.set` seems inappropriate. Because the
value is not read, the initial for loop can be replaced by a `for..in`
loop.

Since all keys will be assigned `null`, the condition of the ternary
expression in the nested function will always be true. Therefore the
recursion case will never happen. With this the nested function can be
eliminated.

* remove duplicate condition

The code above already checks `dragging` and returns if it is truthy.
Checking it again later is therefore unnecessary.

To make this more obvious the `return` is removed in favour of using
an if...else construct.

* remove impossible "unknown" time

The `ago` variable will always be a number and all non-negative numbers
are already covered by other cases, the negative case is handled with
`future` so there is no case when `unkown` could be achieved.
This commit is contained in:
Johann150 2022-05-29 08:15:52 +02:00 committed by GitHub
parent f1d2398eac
commit 804fa33535
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 77 additions and 79 deletions

View File

@ -1111,7 +1111,6 @@ _sfx:
channel: "チャンネル通知" channel: "チャンネル通知"
_ago: _ago:
unknown: "謎"
future: "未来" future: "未来"
justNow: "たった今" justNow: "たった今"
secondsAgo: "{n}秒前" secondsAgo: "{n}秒前"

View File

@ -144,13 +144,7 @@ export const NoteRepository = db.getRepository(Note).extend({
return true; return true;
} else { } else {
// 指定されているかどうか // 指定されているかどうか
const specified = note.visibleUserIds.some((id: any) => meId === id); return note.visibleUserIds.some((id: any) => meId === id);
if (specified) {
return true;
} else {
return false;
}
} }
} }
@ -169,9 +163,12 @@ export const NoteRepository = db.getRepository(Note).extend({
} else { } else {
// フォロワーかどうか // フォロワーかどうか
const [following, user] = await Promise.all([ const [following, user] = await Promise.all([
Followings.findOneBy({ Followings.count({
where: {
followeeId: note.userId, followeeId: note.userId,
followerId: meId, followerId: meId,
},
take: 1,
}), }),
Users.findOneByOrFail({ id: meId }), Users.findOneByOrFail({ id: meId }),
]); ]);
@ -183,7 +180,7 @@ export const NoteRepository = db.getRepository(Note).extend({
in which case we can never know the following. Instead we have in which case we can never know the following. Instead we have
to assume that the users are following each other. to assume that the users are following each other.
*/ */
return following != null || (note.userHost != null && user.host != null); return following > 0 || (note.userHost != null && user.host != null);
} }
} }

View File

@ -61,47 +61,58 @@ export const UserRepository = db.getRepository(User).extend({
//#endregion //#endregion
async getRelation(me: User['id'], target: User['id']) { async getRelation(me: User['id'], target: User['id']) {
const [following1, following2, followReq1, followReq2, toBlocking, fromBlocked, mute] = await Promise.all([ return awaitAll({
Followings.findOneBy({ id: target,
isFollowing: Followings.count({
where: {
followerId: me, followerId: me,
followeeId: target, followeeId: target,
}), },
Followings.findOneBy({ take: 1,
}).then(n => n > 0),
isFollowed: Followings.count({
where: {
followerId: target, followerId: target,
followeeId: me, followeeId: me,
}), },
FollowRequests.findOneBy({ take: 1,
}).then(n => n > 0),
hasPendingFollowRequestFromYou: FollowRequests.count({
where: {
followerId: me, followerId: me,
followeeId: target, followeeId: target,
}), },
FollowRequests.findOneBy({ take: 1,
}).then(n => n > 0),
hasPendingFollowRequestToYou: FollowRequests.count({
where: {
followerId: target, followerId: target,
followeeId: me, followeeId: me,
}), },
Blockings.findOneBy({ take: 1,
}).then(n => n > 0),
isBlocking: Blockings.count({
where: {
blockerId: me, blockerId: me,
blockeeId: target, blockeeId: target,
}), },
Blockings.findOneBy({ take: 1,
}).then(n => n > 0),
isBlocked: Blockings.count({
where: {
blockerId: target, blockerId: target,
blockeeId: me, blockeeId: me,
}), },
Mutings.findOneBy({ take: 1,
}).then(n => n > 0),
isMuted: Mutings.count({
where: {
muterId: me, muterId: me,
muteeId: target, muteeId: target,
}), },
]); take: 1,
}).then(n => n > 0),
return { });
id: target,
isFollowing: following1 != null,
hasPendingFollowRequestFromYou: followReq1 != null,
hasPendingFollowRequestToYou: followReq2 != null,
isFollowed: following2 != null,
isBlocking: toBlocking != null,
isBlocked: fromBlocked != null,
isMuted: mute != null,
};
}, },
async getHasUnreadMessagingMessage(userId: User['id']): Promise<boolean> { async getHasUnreadMessagingMessage(userId: User['id']): Promise<boolean> {

View File

@ -8,7 +8,7 @@ import { User } from '@/models/entities/user.js';
export const renderActivity = (x: any): IActivity | null => { export const renderActivity = (x: any): IActivity | null => {
if (x == null) return null; if (x == null) return null;
if (x !== null && typeof x === 'object' && x.id == null) { if (typeof x === 'object' && x.id == null) {
x.id = `${config.url}/${uuid()}`; x.id = `${config.url}/${uuid()}`;
} }

View File

@ -91,27 +91,20 @@ type ToJsonSchema<S> = {
}; };
export function getJsonSchema<S extends Schema>(schema: S): ToJsonSchema<Unflatten<ChartResult<S>>> { export function getJsonSchema<S extends Schema>(schema: S): ToJsonSchema<Unflatten<ChartResult<S>>> {
const object = {};
for (const [k, v] of Object.entries(schema)) {
nestedProperty.set(object, k, null);
}
function f(obj: Record<string, null | Record<string, unknown>>) {
const jsonSchema = { const jsonSchema = {
type: 'object', type: 'object',
properties: {} as Record<string, unknown>, properties: {} as Record<string, unknown>,
required: [], required: [],
}; };
for (const [k, v] of Object.entries(obj)) {
jsonSchema.properties[k] = v === null ? { for (const k in schema) {
jsonSchema.properties[k] = {
type: 'array', type: 'array',
items: { type: 'number' }, items: { type: 'number' },
} : f(v as Record<string, null | Record<string, unknown>>); };
}
return jsonSchema;
} }
return f(object) as ToJsonSchema<Unflatten<ChartResult<S>>>; return jsonSchema as ToJsonSchema<Unflatten<ChartResult<S>>>;
} }
/** /**

View File

@ -32,8 +32,7 @@ const relative = $computed(() => {
ago >= 60 ? i18n.t('_ago.minutesAgo', { n: (~~(ago / 60)).toString() }) : ago >= 60 ? i18n.t('_ago.minutesAgo', { n: (~~(ago / 60)).toString() }) :
ago >= 10 ? i18n.t('_ago.secondsAgo', { n: (~~(ago % 60)).toString() }) : ago >= 10 ? i18n.t('_ago.secondsAgo', { n: (~~(ago % 60)).toString() }) :
ago >= -1 ? i18n.ts._ago.justNow : ago >= -1 ? i18n.ts._ago.justNow :
ago < -1 ? i18n.ts._ago.future : i18n.ts._ago.future);
i18n.ts._ago.unknown);
}); });
function tick() { function tick() {

View File

@ -213,14 +213,13 @@ function onDragover(ev) {
if (dragging) { if (dragging) {
// //
ev.dataTransfer.dropEffect = 'none'; ev.dataTransfer.dropEffect = 'none';
return; } else {
}
const isDeckColumn = ev.dataTransfer.types[0] === _DATA_TRANSFER_DECK_COLUMN_; const isDeckColumn = ev.dataTransfer.types[0] === _DATA_TRANSFER_DECK_COLUMN_;
ev.dataTransfer.dropEffect = isDeckColumn ? 'move' : 'none'; ev.dataTransfer.dropEffect = isDeckColumn ? 'move' : 'none';
if (!dragging && isDeckColumn) draghover = true; if (isDeckColumn) draghover = true;
}
} }
function onDragleave() { function onDragleave() {