forked from kokonect-link/cherrypick
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
お気に入り登録クリップの一覧画面から登録解除できるように (kokonect-link#448)
- Loading branch information
1 parent
7949122
commit de66bbf
Showing
9 changed files
with
234 additions
and
122 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
18 changes: 18 additions & 0 deletions
18
packages/backend/migration/1726460877945-ClipFavoriteRemoteAuthor.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,18 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and misskey-project, yojo-art team | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
export class clipFavoriteRemoteAuthor1726460877945 { | ||
name = 'clipFavoriteRemoteAuthor1726460877945' | ||
|
||
async up(queryRunner) { | ||
await queryRunner.query(`ALTER TABLE "clip_favorite_remote" ADD "authorId" character varying(32)`); | ||
await queryRunner.query(`ALTER TABLE "clip_favorite_remote" ADD CONSTRAINT "FK_e306e7566fd6101e9767702980c" FOREIGN KEY ("authorId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); | ||
} | ||
|
||
async down(queryRunner) { | ||
await queryRunner.query(`ALTER TABLE "clip_favorite_remote" DROP CONSTRAINT "FK_e306e7566fd6101e9767702980c"`); | ||
await queryRunner.query(`ALTER TABLE "clip_favorite_remote" DROP COLUMN "authorId"`); | ||
} | ||
} |
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,129 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and misskey-project, yojo-art team | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import Redis from 'ioredis'; | ||
import got, * as Got from 'got'; | ||
import type { Config } from '@/config.js'; | ||
import { HttpRequestService } from '@/core/HttpRequestService.js'; | ||
import { MiUser } from '@/models/User.js'; | ||
|
||
export type FetchRemoteApiOpts={ | ||
/** リモートで割り当てられているid */ | ||
userId?:string, | ||
limit?:number, | ||
sinceId?:string, | ||
untilId?:string, | ||
}; | ||
|
||
export async function fetch_remote_api( | ||
config: Config, httpRequestService: HttpRequestService, host: string, endpoint: string, opts: FetchRemoteApiOpts, | ||
) { | ||
const url = 'https://' + host + endpoint; | ||
const sinceIdRemote = opts.sinceId ? opts.sinceId.split('@')[0] : undefined; | ||
const untilIdRemote = opts.untilId ? opts.untilId.split('@')[0] : undefined; | ||
const timeout = 30 * 1000; | ||
const operationTimeout = 60 * 1000; | ||
const res = got.post(url, { | ||
headers: { | ||
'User-Agent': 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: httpRequestService.httpAgent, | ||
https: httpRequestService.httpsAgent, | ||
}, | ||
http2: true, | ||
retry: { | ||
limit: 1, | ||
}, | ||
enableUnixSockets: false, | ||
body: JSON.stringify({ | ||
userId: opts.userId, | ||
limit: opts.limit, | ||
sinceId: sinceIdRemote, | ||
untilId: untilIdRemote, | ||
}), | ||
}); | ||
return await res.text(); | ||
} | ||
/** userがリモートで割り当てられているidを取得 */ | ||
export async function fetch_remote_user_id( | ||
config:Config, | ||
httpRequestService: HttpRequestService, | ||
redisForRemoteApis: Redis.Redis, | ||
user:MiUser, | ||
) { | ||
//ローカルのIDからリモートのIDを割り出す | ||
const cache_key = 'remote-userId:' + user.id; | ||
const id = await redisForRemoteApis.get(cache_key); | ||
if (id !== null) { | ||
if (id === '__NOT_MISSKEY') { | ||
return null; | ||
} | ||
if (id === '__INTERVAL') { | ||
return null; | ||
} | ||
//アクセス時に有効期限を更新 | ||
redisForRemoteApis.expire(cache_key, 7 * 24 * 60 * 60); | ||
return id; | ||
} | ||
try { | ||
const url = 'https://' + user.host + '/api/users/show'; | ||
const timeout = 30 * 1000; | ||
const operationTimeout = 60 * 1000; | ||
const res = got.post(url, { | ||
headers: { | ||
'User-Agent': 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: httpRequestService.httpAgent, | ||
https: httpRequestService.httpsAgent, | ||
}, | ||
http2: true, | ||
retry: { | ||
limit: 1, | ||
}, | ||
enableUnixSockets: false, | ||
body: JSON.stringify({ | ||
username: user.username, | ||
}), | ||
}); | ||
const text = await res.text(); | ||
const json = JSON.parse(text); | ||
if (json.id != null) { | ||
const redisPipeline = redisForRemoteApis.pipeline(); | ||
redisPipeline.set(cache_key, json.id); | ||
//キャッシュ期限1週間 | ||
redisPipeline.expire(cache_key, 7 * 24 * 60 * 60); | ||
await redisPipeline.exec(); | ||
return json.id as string; | ||
} | ||
} catch { | ||
const redisPipeline = redisForRemoteApis.pipeline(); | ||
redisPipeline.set(cache_key, '__INTERVAL'); | ||
redisPipeline.expire(cache_key, 60 * 60); | ||
await redisPipeline.exec(); | ||
} | ||
return null; | ||
} |
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.