-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64c50d7
commit 67e077c
Showing
21 changed files
with
534 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/backend/migration/1726452644817-FlashLikeRemote.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and misskey-project, yojo-art team | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
export class flashLikeRemote1726452644817 { | ||
name = 'flashLikeRemote1726452644817' | ||
|
||
async up(queryRunner) { | ||
await queryRunner.query(`CREATE TABLE "flash_like_remote" ("id" character varying(32) NOT NULL, "userId" character varying(32) NOT NULL, "flashId" character varying(32) NOT NULL, "host" character varying(128) NOT NULL, "authorId" character varying(32) NOT NULL, CONSTRAINT "PK_840a074b84bd1663054e020e43" PRIMARY KEY ("id"))`); | ||
await queryRunner.query(`CREATE INDEX "IDX_ade312aad367a2902ed415abbc" ON "flash_like_remote" ("userId") `); | ||
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_f7c8a8fd916efed73a05bc1ea0" ON "flash_like_remote" ("userId", "flashId","host") `); | ||
await queryRunner.query(`ALTER TABLE "flash_like_remote" ADD CONSTRAINT "FK_8c14417c4cc57f04b4d7376707a" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); | ||
await queryRunner.query(`ALTER TABLE "flash_like_remote" ADD CONSTRAINT "FK_75f247337676468f6bd6f22eb24" FOREIGN KEY ("authorId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); | ||
} | ||
|
||
async down(queryRunner) { | ||
await queryRunner.query(`ALTER TABLE "flash_like_remote" DROP CONSTRAINT "FK_75f247337676468f6bd6f22eb24"`); | ||
await queryRunner.query(`ALTER TABLE "flash_like_remote" DROP CONSTRAINT "FK_8c14417c4cc57f04b4d7376707a"`); | ||
await queryRunner.query(`DROP INDEX "public"."IDX_f7c8a8fd916efed73a05bc1ea0"`); | ||
await queryRunner.query(`DROP INDEX "public"."IDX_ade312aad367a2902ed415abbc"`); | ||
await queryRunner.query(`DROP TABLE "flash_like_remote"`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and misskey-project, yojo-art team | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { Inject, Injectable } from '@nestjs/common'; | ||
import got, * as Got from 'got'; | ||
import * as Redis from 'ioredis'; | ||
import type { Config } from '@/config.js'; | ||
import { HttpRequestService } from '@/core/HttpRequestService.js'; | ||
import { UserEntityService } from '@/core/entities/UserEntityService.js'; | ||
import { awaitAll } from '@/misc/prelude/await-all.js'; | ||
import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js'; | ||
import { DI } from '@/di-symbols.js'; | ||
import type { ClipsRepository, ClipNotesRepository, NotesRepository, MiUser } from '@/models/_.js'; | ||
import { bindThis } from '@/decorators.js'; | ||
import { RoleService } from '@/core/RoleService.js'; | ||
import { IdService } from '@/core/IdService.js'; | ||
import { Packed } from '@/misc/json-schema.js'; | ||
import { emojis } from '@/misc/remote-api-utils.js'; | ||
|
||
@Injectable() | ||
export class FlashService { | ||
public static FailedToResolveRemoteUserError = class extends Error {}; | ||
|
||
constructor( | ||
@Inject(DI.config) | ||
private config: Config, | ||
@Inject(DI.redisForRemoteApis) | ||
private redisForRemoteApis: Redis.Redis, | ||
@Inject(DI.clipsRepository) | ||
private clipsRepository: ClipsRepository, | ||
|
||
@Inject(DI.clipNotesRepository) | ||
private clipNotesRepository: ClipNotesRepository, | ||
|
||
@Inject(DI.notesRepository) | ||
private notesRepository: NotesRepository, | ||
|
||
private httpRequestService: HttpRequestService, | ||
private userEntityService: UserEntityService, | ||
private remoteUserResolveService: RemoteUserResolveService, | ||
private roleService: RoleService, | ||
private idService: IdService, | ||
) { | ||
} | ||
@bindThis | ||
async showRemoteOrDummy( | ||
flashId: string, | ||
author: MiUser|null, | ||
fetch_emoji = false, | ||
) : Promise<Packed<'Flash'>> { | ||
if (author == null) { | ||
throw new Error(); | ||
} | ||
try { | ||
if (author.host == null) { | ||
throw new Error(); | ||
} | ||
return await this.showRemote(flashId, author.host, fetch_emoji); | ||
} catch { | ||
return await awaitAll({ | ||
id: flashId + '@' + (author.host ? author.host : ''), | ||
createdAt: new Date(0).toISOString(), | ||
updatedAt: new Date(0).toISOString(), | ||
userId: author.id, | ||
user: this.userEntityService.pack(author), | ||
title: 'Unavailable', | ||
summary: '', | ||
script: '', | ||
favoritedCount: 0, | ||
visibility: 'public', | ||
likedCount: 0, | ||
isLiked: false, //後でLike対応する | ||
}); | ||
} | ||
} | ||
@bindThis | ||
public async showRemote( | ||
flashId:string, | ||
host:string, | ||
fetch_emoji = false, | ||
) : Promise<Packed<'Flash'>> { | ||
const cache_key = 'flash:show:' + flashId + '@' + host; | ||
const cache_value = await this.redisForRemoteApis.get(cache_key); | ||
let remote_json = null; | ||
if (cache_value === null) { | ||
const timeout = 30 * 1000; | ||
const operationTimeout = 60 * 1000; | ||
const url = 'https://' + host + '/api/flash/show'; | ||
const res = got.post(url, { | ||
headers: { | ||
'User-Agent': this.config.userAgent, | ||
'Content-Type': 'application/json; charset=utf-8', | ||
}, | ||
timeout: { | ||
lookup: timeout, | ||
connect: timeout, | ||
secureConnect: timeout, | ||
socket: timeout, // read timeout | ||
response: timeout, | ||
send: timeout, | ||
request: operationTimeout, // whole operation timeout | ||
}, | ||
agent: { | ||
http: this.httpRequestService.httpAgent, | ||
https: this.httpRequestService.httpsAgent, | ||
}, | ||
http2: true, | ||
retry: { | ||
limit: 1, | ||
}, | ||
enableUnixSockets: false, | ||
body: JSON.stringify({ | ||
flashId, | ||
}), | ||
}); | ||
remote_json = await res.text(); | ||
const redisPipeline = this.redisForRemoteApis.pipeline(); | ||
redisPipeline.set(cache_key, remote_json); | ||
redisPipeline.expire(cache_key, 10 * 60); | ||
await redisPipeline.exec(); | ||
} else { | ||
remote_json = cache_value; | ||
} | ||
const remote = JSON.parse(remote_json); | ||
if (remote.user == null || remote.user.username == null) { | ||
throw new FlashService.FailedToResolveRemoteUserError(); | ||
} | ||
const user = await this.remoteUserResolveService.resolveUser(remote.user.username, host).catch(err => { | ||
throw new FlashService.FailedToResolveRemoteUserError(); | ||
}); | ||
return await awaitAll({ | ||
id: flashId + '@' + host, | ||
createdAt: remote.createdAt ? new Date(remote.createdAt).toISOString() : new Date(0).toISOString(), | ||
updatedAt: remote.updatedAt ? new Date(remote.updatedAt).toISOString() : new Date(0).toISOString(), | ||
userId: user.id, | ||
user: this.userEntityService.pack(user), | ||
title: String(remote.title), | ||
summary: String(remote.summary), | ||
script: String(remote.script), | ||
favoritedCount: remote.favoritedCount, | ||
visibility: remote.visibility ?? 'public', | ||
likedCount: remote.likedCount ?? 0, | ||
isLiked: false, //後でLike対応する | ||
emojis: (remote.summary && fetch_emoji) ? emojis(this.config, this.httpRequestService, this.redisForRemoteApis, host, remote.summary) : {}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and misskey-project, yojo-art team | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { PrimaryColumn, Entity, Index, JoinColumn, Column, ManyToOne } from 'typeorm'; | ||
import { id } from './util/id.js'; | ||
import { MiUser } from './User.js'; | ||
|
||
@Entity('flash_like_remote') | ||
@Index(['userId', 'flashId', 'host'], { unique: true }) | ||
export class MiFlashLikeRemote { | ||
@PrimaryColumn(id()) | ||
public id: string; | ||
|
||
@Index() | ||
@Column(id()) | ||
public userId: MiUser['id']; | ||
|
||
@ManyToOne(type => MiUser, { | ||
onDelete: 'CASCADE', | ||
}) | ||
@JoinColumn() | ||
public user: MiUser | null; | ||
|
||
@Column(id()) | ||
public authorId: MiUser['id']; | ||
@ManyToOne(type => MiUser, { | ||
onDelete: 'CASCADE', | ||
}) | ||
@JoinColumn() | ||
public author: MiUser | null; | ||
|
||
@Column('varchar', { | ||
length: 32, | ||
}) | ||
public flashId: string; | ||
|
||
@Column('varchar', { | ||
length: 128, | ||
}) | ||
public host: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.