summaryrefslogtreecommitdiff
path: root/packages/backend/src
diff options
context:
space:
mode:
authorJohann150 <johann.galle@protonmail.com>2022-05-29 08:15:52 +0200
committerGitHub <noreply@github.com>2022-05-29 15:15:52 +0900
commit804fa33535baa9e5cdf49070a50a555cf2c3b1ea (patch)
tree61b2a5c705af743c8dff1cf77764e0bcb76f1820 /packages/backend/src
parentfix(client): Vite related boot mechanism revision (#8753) (diff)
downloadmisskey-804fa33535baa9e5cdf49070a50a555cf2c3b1ea.tar.gz
misskey-804fa33535baa9e5cdf49070a50a555cf2c3b1ea.tar.bz2
misskey-804fa33535baa9e5cdf49070a50a555cf2c3b1ea.zip
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.
Diffstat (limited to 'packages/backend/src')
-rw-r--r--packages/backend/src/models/repositories/note.ts19
-rw-r--r--packages/backend/src/models/repositories/user.ts91
-rw-r--r--packages/backend/src/remote/activitypub/renderer/index.ts2
-rw-r--r--packages/backend/src/services/chart/core.ts27
4 files changed, 70 insertions, 69 deletions
diff --git a/packages/backend/src/models/repositories/note.ts b/packages/backend/src/models/repositories/note.ts
index 638d78f626..c0abbb4f93 100644
--- a/packages/backend/src/models/repositories/note.ts
+++ b/packages/backend/src/models/repositories/note.ts
@@ -144,13 +144,7 @@ export const NoteRepository = db.getRepository(Note).extend({
return true;
} else {
// 指定されているかどうか
- const specified = note.visibleUserIds.some((id: any) => meId === id);
-
- if (specified) {
- return true;
- } else {
- return false;
- }
+ return note.visibleUserIds.some((id: any) => meId === id);
}
}
@@ -169,9 +163,12 @@ export const NoteRepository = db.getRepository(Note).extend({
} else {
// フォロワーかどうか
const [following, user] = await Promise.all([
- Followings.findOneBy({
- followeeId: note.userId,
- followerId: meId,
+ Followings.count({
+ where: {
+ followeeId: note.userId,
+ followerId: meId,
+ },
+ take: 1,
}),
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
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);
}
}
diff --git a/packages/backend/src/models/repositories/user.ts b/packages/backend/src/models/repositories/user.ts
index 541fbaf003..8a4e48efdd 100644
--- a/packages/backend/src/models/repositories/user.ts
+++ b/packages/backend/src/models/repositories/user.ts
@@ -61,47 +61,58 @@ export const UserRepository = db.getRepository(User).extend({
//#endregion
async getRelation(me: User['id'], target: User['id']) {
- const [following1, following2, followReq1, followReq2, toBlocking, fromBlocked, mute] = await Promise.all([
- Followings.findOneBy({
- followerId: me,
- followeeId: target,
- }),
- Followings.findOneBy({
- followerId: target,
- followeeId: me,
- }),
- FollowRequests.findOneBy({
- followerId: me,
- followeeId: target,
- }),
- FollowRequests.findOneBy({
- followerId: target,
- followeeId: me,
- }),
- Blockings.findOneBy({
- blockerId: me,
- blockeeId: target,
- }),
- Blockings.findOneBy({
- blockerId: target,
- blockeeId: me,
- }),
- Mutings.findOneBy({
- muterId: me,
- muteeId: target,
- }),
- ]);
-
- return {
+ return awaitAll({
id: target,
- isFollowing: following1 != null,
- hasPendingFollowRequestFromYou: followReq1 != null,
- hasPendingFollowRequestToYou: followReq2 != null,
- isFollowed: following2 != null,
- isBlocking: toBlocking != null,
- isBlocked: fromBlocked != null,
- isMuted: mute != null,
- };
+ isFollowing: Followings.count({
+ where: {
+ followerId: me,
+ followeeId: target,
+ },
+ take: 1,
+ }).then(n => n > 0),
+ isFollowed: Followings.count({
+ where: {
+ followerId: target,
+ followeeId: me,
+ },
+ take: 1,
+ }).then(n => n > 0),
+ hasPendingFollowRequestFromYou: FollowRequests.count({
+ where: {
+ followerId: me,
+ followeeId: target,
+ },
+ take: 1,
+ }).then(n => n > 0),
+ hasPendingFollowRequestToYou: FollowRequests.count({
+ where: {
+ followerId: target,
+ followeeId: me,
+ },
+ take: 1,
+ }).then(n => n > 0),
+ isBlocking: Blockings.count({
+ where: {
+ blockerId: me,
+ blockeeId: target,
+ },
+ take: 1,
+ }).then(n => n > 0),
+ isBlocked: Blockings.count({
+ where: {
+ blockerId: target,
+ blockeeId: me,
+ },
+ take: 1,
+ }).then(n => n > 0),
+ isMuted: Mutings.count({
+ where: {
+ muterId: me,
+ muteeId: target,
+ },
+ take: 1,
+ }).then(n => n > 0),
+ });
},
async getHasUnreadMessagingMessage(userId: User['id']): Promise<boolean> {
diff --git a/packages/backend/src/remote/activitypub/renderer/index.ts b/packages/backend/src/remote/activitypub/renderer/index.ts
index 5f69332266..f100b77ce5 100644
--- a/packages/backend/src/remote/activitypub/renderer/index.ts
+++ b/packages/backend/src/remote/activitypub/renderer/index.ts
@@ -8,7 +8,7 @@ import { User } from '@/models/entities/user.js';
export const renderActivity = (x: any): IActivity | 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()}`;
}
diff --git a/packages/backend/src/services/chart/core.ts b/packages/backend/src/services/chart/core.ts
index cf69e2194d..2960bac8f7 100644
--- a/packages/backend/src/services/chart/core.ts
+++ b/packages/backend/src/services/chart/core.ts
@@ -91,27 +91,20 @@ type ToJsonSchema<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);
- }
+ const jsonSchema = {
+ type: 'object',
+ properties: {} as Record<string, unknown>,
+ required: [],
+ };
- function f(obj: Record<string, null | Record<string, unknown>>) {
- const jsonSchema = {
- type: 'object',
- properties: {} as Record<string, unknown>,
- required: [],
+ for (const k in schema) {
+ jsonSchema.properties[k] = {
+ type: 'array',
+ items: { type: 'number' },
};
- for (const [k, v] of Object.entries(obj)) {
- jsonSchema.properties[k] = v === null ? {
- type: 'array',
- 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>>>;
}
/**