Skip to content

Commit

Permalink
Merge branch 'develop' into chromatic-turbosnap
Browse files Browse the repository at this point in the history
  • Loading branch information
syuilo committed Apr 7, 2023
2 parents 5c45ab2 + 191ed3c commit cce50dd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "misskey",
"version": "13.11.0-beta.5",
"version": "13.11.0-beta.6",
"codename": "nasubi",
"repository": {
"type": "git",
Expand Down
34 changes: 26 additions & 8 deletions packages/backend/src/core/FederatedInstanceService.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
import { Inject, Injectable } from '@nestjs/common';
import Redis from 'ioredis';
import type { InstancesRepository } from '@/models/index.js';
import type { Instance } from '@/models/entities/Instance.js';
import { MemoryKVCache } from '@/misc/cache.js';
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
import { IdService } from '@/core/IdService.js';
import { DI } from '@/di-symbols.js';
import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';

@Injectable()
export class FederatedInstanceService {
private cache: MemoryKVCache<Instance>;
public federatedInstanceCache: RedisKVCache<Instance | null>;

constructor(
@Inject(DI.redis)
private redisClient: Redis.Redis,

@Inject(DI.instancesRepository)
private instancesRepository: InstancesRepository,

private utilityService: UtilityService,
private idService: IdService,
) {
this.cache = new MemoryKVCache<Instance>(1000 * 60 * 60);
this.federatedInstanceCache = new RedisKVCache<Instance | null>(this.redisClient, 'federatedInstance', {
lifetime: 1000 * 60 * 60 * 24, // 24h
memoryCacheLifetime: 1000 * 60 * 30, // 30m
fetcher: (key) => this.instancesRepository.findOneBy({ host: key }),
toRedisConverter: (value) => JSON.stringify(value),
fromRedisConverter: (value) => {
const parsed = JSON.parse(value);
return {
...parsed,
firstRetrievedAt: new Date(parsed.firstRetrievedAt),
latestRequestReceivedAt: parsed.latestRequestReceivedAt ? new Date(parsed.latestRequestReceivedAt) : null,
infoUpdatedAt: parsed.infoUpdatedAt ? new Date(parsed.infoUpdatedAt) : null,
};
},
});
}

@bindThis
public async fetch(host: string): Promise<Instance> {
host = this.utilityService.toPuny(host);

const cached = this.cache.get(host);
const cached = await this.federatedInstanceCache.get(host);
if (cached) return cached;

const index = await this.instancesRepository.findOneBy({ host });
Expand All @@ -37,10 +55,10 @@ export class FederatedInstanceService {
firstRetrievedAt: new Date(),
}).then(x => this.instancesRepository.findOneByOrFail(x.identifiers[0]));

this.cache.set(host, i);
this.federatedInstanceCache.set(host, i);
return i;
} else {
this.cache.set(host, index);
this.federatedInstanceCache.set(host, index);
return index;
}
}
Expand All @@ -49,10 +67,10 @@ export class FederatedInstanceService {
public async updateCachePartial(host: string, data: Partial<Instance>): Promise<void> {
host = this.utilityService.toPuny(host);

const cached = this.cache.get(host);
const cached = await this.federatedInstanceCache.get(host);
if (cached == null) return;

this.cache.set(host, {
this.federatedInstanceCache.set(host, {
...cached,
...data,
});
Expand Down
14 changes: 4 additions & 10 deletions packages/backend/src/core/entities/UserEntityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import type { Packed } from '@/misc/json-schema.js';
import type { Promiseable } from '@/misc/prelude/await-all.js';
import { awaitAll } from '@/misc/prelude/await-all.js';
import { USER_ACTIVE_THRESHOLD, USER_ONLINE_THRESHOLD } from '@/const.js';
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
import type { Instance } from '@/models/entities/Instance.js';
import type { LocalUser, RemoteUser, User } from '@/models/entities/User.js';
import { birthdaySchema, descriptionSchema, localUsernameSchema, locationSchema, nameSchema, passwordSchema } from '@/models/entities/User.js';
import type { UsersRepository, UserSecurityKeysRepository, FollowingsRepository, FollowRequestsRepository, BlockingsRepository, MutingsRepository, DriveFilesRepository, NoteUnreadsRepository, ChannelFollowingsRepository, UserNotePiningsRepository, UserProfilesRepository, InstancesRepository, AnnouncementReadsRepository, AnnouncementsRepository, PagesRepository, UserProfile, RenoteMutingsRepository } from '@/models/index.js';
import { bindThis } from '@/decorators.js';
import { RoleService } from '@/core/RoleService.js';
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
import type { OnModuleInit } from '@nestjs/common';
import type { AntennaService } from '../AntennaService.js';
import type { CustomEmojiService } from '../CustomEmojiService.js';
Expand Down Expand Up @@ -53,7 +53,7 @@ export class UserEntityService implements OnModuleInit {
private customEmojiService: CustomEmojiService;
private antennaService: AntennaService;
private roleService: RoleService;
private userInstanceCache: RedisKVCache<Instance | null>;
private federatedInstanceService: FederatedInstanceService;

constructor(
private moduleRef: ModuleRef,
Expand Down Expand Up @@ -119,13 +119,6 @@ export class UserEntityService implements OnModuleInit {
//private antennaService: AntennaService,
//private roleService: RoleService,
) {
this.userInstanceCache = new RedisKVCache<Instance | null>(this.redisClient, 'userInstance', {
lifetime: 1000 * 60 * 60 * 24, // 24h
memoryCacheLifetime: 1000 * 60 * 30, // 30m
fetcher: (key) => this.instancesRepository.findOneBy({ host: key }),
toRedisConverter: (value) => JSON.stringify(value),
fromRedisConverter: (value) => JSON.parse(value), // TODO: date型の考慮
});
}

onModuleInit() {
Expand All @@ -135,6 +128,7 @@ export class UserEntityService implements OnModuleInit {
this.customEmojiService = this.moduleRef.get('CustomEmojiService');
this.antennaService = this.moduleRef.get('AntennaService');
this.roleService = this.moduleRef.get('RoleService');
this.federatedInstanceService = this.moduleRef.get('FederatedInstanceService');
}

//#region Validators
Expand Down Expand Up @@ -349,7 +343,7 @@ export class UserEntityService implements OnModuleInit {
avatarBlurhash: user.avatarBlurhash,
isBot: user.isBot ?? falsy,
isCat: user.isCat ?? falsy,
instance: user.host ? this.userInstanceCache.fetch(user.host).then(instance => instance ? {
instance: user.host ? this.federatedInstanceService.federatedInstanceCache.fetch(user.host).then(instance => instance ? {
name: instance.name,
softwareName: instance.softwareName,
softwareVersion: instance.softwareVersion,
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/test/e2e/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ describe('Streaming', () => {
assert.strictEqual(fired, true);
});

/* TODO
test('リモートユーザーの投稿は流れない', async () => {
const fired = await waitFire(
ayano, 'localTimeline', // ayano:Local
Expand All @@ -191,6 +192,7 @@ describe('Streaming', () => {
assert.strictEqual(fired, false);
});
*/

test('ホーム指定の投稿は流れない', async () => {
const fired = await waitFire(
Expand Down Expand Up @@ -244,6 +246,7 @@ describe('Streaming', () => {
assert.strictEqual(fired, true);
});

/* TODO
test('フォローしているリモートユーザーの投稿が流れる', async () => {
const fired = await waitFire(
ayano, 'hybridTimeline', // ayano:Hybrid
Expand All @@ -263,6 +266,7 @@ describe('Streaming', () => {
assert.strictEqual(fired, false);
});
*/

test('フォローしているユーザーのダイレクト投稿が流れる', async () => {
const fired = await waitFire(
Expand Down Expand Up @@ -316,6 +320,7 @@ describe('Streaming', () => {
assert.strictEqual(fired, true);
});

/* TODO
test('フォローしていないリモートユーザーの投稿が流れる', async () => {
const fired = await waitFire(
ayano, 'globalTimeline', // ayano:Global
Expand All @@ -325,6 +330,7 @@ describe('Streaming', () => {
assert.strictEqual(fired, true);
});
*/

test('ホーム投稿は流れない', async () => {
const fired = await waitFire(
Expand Down

1 comment on commit cce50dd

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chromatic will skip testing but you may still have to review the changes on Chromatic.

Please sign in to comment.