Skip to content

Commit

Permalink
Merge branch 'io' into merge-upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
riku6460 committed Oct 10, 2023
2 parents 43e7f48 + 2119356 commit 2f437e3
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 24 deletions.
2 changes: 2 additions & 0 deletions packages/backend/src/core/PollService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export class PollService {
const note = await this.notesRepository.findOneBy({ id: noteId });
if (note == null) throw new Error('note not found');

if (note.localOnly) return;

const user = await this.usersRepository.findOneBy({ id: note.userId });
if (user == null) throw new Error('note not found');

Expand Down
13 changes: 12 additions & 1 deletion packages/backend/src/core/entities/NoteEntityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { MiNoteReaction } from '@/models/NoteReaction.js';
import type { UsersRepository, NotesRepository, FollowingsRepository, PollsRepository, PollVotesRepository, NoteReactionsRepository, ChannelsRepository } from '@/models/_.js';
import { bindThis } from '@/decorators.js';
import { isNotNull } from '@/misc/is-not-null.js';
import { DebounceLoader } from '@/misc/loader.js';
import type { OnModuleInit } from '@nestjs/common';
import type { CustomEmojiService } from '../CustomEmojiService.js';
import type { ReactionService } from '../ReactionService.js';
Expand All @@ -29,6 +30,7 @@ export class NoteEntityService implements OnModuleInit {
private driveFileEntityService: DriveFileEntityService;
private customEmojiService: CustomEmojiService;
private reactionService: ReactionService;
private noteLoader = new DebounceLoader(this.findNoteOrFail);

constructor(
private moduleRef: ModuleRef,
Expand Down Expand Up @@ -289,7 +291,7 @@ export class NoteEntityService implements OnModuleInit {
}, options);

const meId = me ? me.id : null;
const note = typeof src === 'object' ? src : await this.notesRepository.findOneOrFail({ where: { id: src }, relations: ['user'] });
const note = typeof src === 'object' ? src : await this.noteLoader.load(src);
const host = note.userHost;

let text = note.text;
Expand All @@ -307,6 +309,7 @@ export class NoteEntityService implements OnModuleInit {
const reactionEmojiNames = Object.keys(note.reactions)
.filter(x => x.startsWith(':') && x.includes('@') && !x.includes('@.')) // リモートカスタム絵文字のみ
.map(x => this.reactionService.decodeReaction(x).reaction.replaceAll(':', ''));
await this.customEmojiService.prefetchEmojis(this.aggregateNoteEmojis([note]));
const packedFiles = options?._hint_?.packedFiles;

const packed: Packed<'Note'> = await awaitAll({
Expand Down Expand Up @@ -472,4 +475,12 @@ export class NoteEntityService implements OnModuleInit {

return await query.getCount();
}

@bindThis
private async findNoteOrFail(id: string): Promise<MiNote> {
return await this.notesRepository.findOneOrFail({
where: { id },
relations: ["user"],
});
}
}
5 changes: 4 additions & 1 deletion packages/backend/src/misc/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ export class RedisSingleCache<T> {

const cached = await this.redisClient.get(`singlecache:${this.name}`);
if (cached == null) return undefined;
return this.fromRedisConverter(cached);
const parsed = this.fromRedisConverter(cached);
if (parsed == null) return undefined;
this.memoryCache.set(parsed);
return parsed;
}

@bindThis
Expand Down
49 changes: 49 additions & 0 deletions packages/backend/src/misc/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export type FetchFunction<K, V> = (key: K) => Promise<V>;
type ResolveReject<V> = Parameters<ConstructorParameters<typeof Promise<V>>[0]>;
type ResolverPair<V> = {
resolve: ResolveReject<V>[0];
reject: ResolveReject<V>[1];
};
export class DebounceLoader<K, V> {
private resolverMap = new Map<K, ResolverPair<V>>();
private promiseMap = new Map<K, Promise<V>>();
private resolvedPromise = Promise.resolve();
constructor(private loadFn: FetchFunction<K, V>) {}

public load(key: K): Promise<V> {
const promise = this.promiseMap.get(key);
if (typeof promise !== 'undefined') {
return promise;
}

const isFirst = this.promiseMap.size === 0;
const newPromise = new Promise<V>((resolve, reject) => {
this.resolverMap.set(key, { resolve, reject });
});
this.promiseMap.set(key, newPromise);

if (isFirst) {
this.enqueueDebouncedLoadJob();
}

return newPromise;
}

private runDebouncedLoad(): void {
const resolvers = [...this.resolverMap];
this.resolverMap.clear();
this.promiseMap.clear();

for (const [key, { resolve, reject }] of resolvers) {
this.loadFn(key).then(resolve, reject);
}
}

private enqueueDebouncedLoadJob(): void {
this.resolvedPromise.then(() => {
process.nextTick(() => {
this.runDebouncedLoad();
});
});
}
}
2 changes: 1 addition & 1 deletion packages/backend/src/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export function createPostgresDataSource(config: Config) {
} : false,
logging: log,
logger: log ? new MyCustomLogger() : undefined,
maxQueryExecutionTime: 300,
maxQueryExecutionTime: 10000, // 10s
entities: entities,
migrations: ['../../migration/*.js'],
});
Expand Down
18 changes: 2 additions & 16 deletions packages/backend/src/server/web/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,9 @@

//#region Detect language & fetch translations
if (!localStorage.hasOwnProperty('locale')) {
const supportedLangs = LANGS;
let lang = localStorage.getItem('lang');
if (lang == null || !supportedLangs.includes(lang)) {
if (supportedLangs.includes(navigator.language)) {
lang = navigator.language;
} else {
lang = supportedLangs.find(x => x.split('-')[0] === navigator.language);

// Fallback
if (lang == null) lang = 'en-US';
}
if (lang == null || lang.toString == null || lang.toString() === 'null') {
lang = 'ja-JP';
}

const metaRes = await window.fetch('/api/meta', {
Expand All @@ -67,12 +59,6 @@
return;
}

// for https://github.com/misskey-dev/misskey/issues/10202
if (lang == null || lang.toString == null || lang.toString() === 'null') {
console.error('invalid lang value detected!!!', typeof lang, lang);
lang = 'en-US';
}

const localRes = await window.fetch(`/assets/locales/${lang}.${v}.json`);
if (localRes.status === 200) {
localStorage.setItem('lang', lang);
Expand Down
88 changes: 88 additions & 0 deletions packages/backend/test/unit/misc/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { DebounceLoader } from '@/misc/loader.js';

class Mock {
loadCountByKey = new Map<number, number>();
load = async (key: number): Promise<number> => {
const count = this.loadCountByKey.get(key);
if (typeof count === 'undefined') {
this.loadCountByKey.set(key, 1);
} else {
this.loadCountByKey.set(key, count + 1);
}
return key * 2;
};
reset() {
this.loadCountByKey.clear();
}
}

describe(DebounceLoader, () => {
describe('single request', () => {
it('loads once', async () => {
const mock = new Mock();
const loader = new DebounceLoader(mock.load);
expect(await loader.load(7)).toBe(14);
expect(mock.loadCountByKey.size).toBe(1);
expect(mock.loadCountByKey.get(7)).toBe(1);
});
});

describe('two duplicated requests at same time', () => {
it('loads once', async () => {
const mock = new Mock();
const loader = new DebounceLoader(mock.load);
const [v1, v2] = await Promise.all([
loader.load(7),
loader.load(7),
]);
expect(v1).toBe(14);
expect(v2).toBe(14);
expect(mock.loadCountByKey.size).toBe(1);
expect(mock.loadCountByKey.get(7)).toBe(1);
});
});

describe('two different requests at same time', () => {
it('loads twice', async () => {
const mock = new Mock();
const loader = new DebounceLoader(mock.load);
const [v1, v2] = await Promise.all([
loader.load(7),
loader.load(13),
]);
expect(v1).toBe(14);
expect(v2).toBe(26);
expect(mock.loadCountByKey.size).toBe(2);
expect(mock.loadCountByKey.get(7)).toBe(1);
expect(mock.loadCountByKey.get(13)).toBe(1);
});
});

describe('non-continuous same two requests', () => {
it('loads twice', async () => {
const mock = new Mock();
const loader = new DebounceLoader(mock.load);
expect(await loader.load(7)).toBe(14);
expect(mock.loadCountByKey.size).toBe(1);
expect(mock.loadCountByKey.get(7)).toBe(1);
mock.reset();
expect(await loader.load(7)).toBe(14);
expect(mock.loadCountByKey.size).toBe(1);
expect(mock.loadCountByKey.get(7)).toBe(1);
});
});

describe('non-continuous different two requests', () => {
it('loads twice', async () => {
const mock = new Mock();
const loader = new DebounceLoader(mock.load);
expect(await loader.load(7)).toBe(14);
expect(mock.loadCountByKey.size).toBe(1);
expect(mock.loadCountByKey.get(7)).toBe(1);
mock.reset();
expect(await loader.load(13)).toBe(26);
expect(mock.loadCountByKey.size).toBe(1);
expect(mock.loadCountByKey.get(13)).toBe(1);
});
});
});
2 changes: 1 addition & 1 deletion packages/frontend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const hostname = address.hostname;
export const url = address.origin;
export const apiUrl = url + '/api';
export const wsUrl = url.replace('http://', 'ws://').replace('https://', 'wss://') + '/streaming';
export const lang = miLocalStorage.getItem('lang') ?? 'en-US';
export const lang = miLocalStorage.getItem('lang') ?? 'ja-JP';
export const langs = _LANGS_;
const preParseLocale = miLocalStorage.getItem('locale');
export let locale = preParseLocale ? JSON.parse(preParseLocale) : null;
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ export const defaultStore = markRaw(new Storage('base', {
},
useBlurEffectForModal: {
where: 'device',
default: !/mobile|iphone|android/.test(navigator.userAgent.toLowerCase()), // 循環参照するのでdevice-kind.tsは参照できない
default: false,
},
useBlurEffect: {
where: 'device',
default: !/mobile|iphone|android/.test(navigator.userAgent.toLowerCase()), // 循環参照するのでdevice-kind.tsは参照できない
default: false,
},
showFixedPostForm: {
where: 'device',
Expand Down
10 changes: 9 additions & 1 deletion packages/frontend/src/ui/_common_/stream-indicator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,30 @@ import { defaultStore } from '@/store.js';
const zIndex = os.claimZIndex('high');

let hasDisconnected = $ref(false);
let timeoutId = $ref<number>();

function onDisconnected() {
hasDisconnected = true;
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(() => {
hasDisconnected = true;
}, 1000 * 10);
}

function resetDisconnected() {
window.clearTimeout(timeoutId);
hasDisconnected = false;
}

function reload() {
location.reload();
}

useStream().on('_connected_', resetDisconnected);
useStream().on('_disconnected_', onDisconnected);

onUnmounted(() => {
window.clearTimeout(timeoutId);
useStream().off('_connected_', resetDisconnected);
useStream().off('_disconnected_', onDisconnected);
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion packages/sw/src/scripts/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SwLang {
public cacheName = `mk-cache-${_VERSION_}`;

public lang: Promise<string> = get('lang').then(async prelang => {
if (!prelang) return 'en-US';
if (!prelang) return 'ja-JP';
return prelang;
});

Expand Down

0 comments on commit 2f437e3

Please sign in to comment.