Fix: 通知のページネーションで2つ以上読み込めなくなることがある問題 (#15277)
* fix: notifications-groupedのinclude/exclude typesに:groupedを指定できてしまう問題 * refactor: 通知の取得処理を Notification Service に移動 * feat: add function to parse additional part of id * fix: 通知のページネーションが正しく動かない問題 Redisにのページネーションで使用する時間及びidとRedis上のものが混同されていたので、Misskeyが生成するものに寄せました。 * pnpm run build-misskey-js-with-types * chore: XADDをretryするように * fix: notifications-groupedでxrevrangeしているのを消し忘れていた
This commit is contained in:
parent
3ec5bf114b
commit
55d835ad51
12 changed files with 248 additions and 96 deletions
40
packages/backend/src/misc/bigint.ts
Normal file
40
packages/backend/src/misc/bigint.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
function parseBigIntChunked(str: string, base: number, chunkSize: number, powerOfChunkSize: bigint): bigint {
|
||||
const chunks = [];
|
||||
while (str.length > 0) {
|
||||
chunks.unshift(str.slice(-chunkSize));
|
||||
str = str.slice(0, -chunkSize);
|
||||
}
|
||||
let result = 0n;
|
||||
for (const chunk of chunks) {
|
||||
result *= powerOfChunkSize;
|
||||
const int = parseInt(chunk, base);
|
||||
if (Number.isNaN(int)) {
|
||||
throw new Error('Invalid base36 string');
|
||||
}
|
||||
result += BigInt(int);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function parseBigInt36(str: string): bigint {
|
||||
// log_36(Number.MAX_SAFE_INTEGER) => 10.251599391715352
|
||||
// so we process 10 chars at once
|
||||
return parseBigIntChunked(str, 36, 10, 36n ** 10n);
|
||||
}
|
||||
|
||||
export function parseBigInt16(str: string): bigint {
|
||||
// log_16(Number.MAX_SAFE_INTEGER) => 13.25
|
||||
// so we process 13 chars at once
|
||||
return parseBigIntChunked(str, 16, 13, 16n ** 13n);
|
||||
}
|
||||
|
||||
export function parseBigInt32(str: string): bigint {
|
||||
// log_32(Number.MAX_SAFE_INTEGER) => 10.6
|
||||
// so we process 10 chars at once
|
||||
return parseBigIntChunked(str, 32, 10, 32n ** 10n);
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@
|
|||
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列]
|
||||
|
||||
import * as crypto from 'node:crypto';
|
||||
import { parseBigInt36 } from '@/misc/bigint.js';
|
||||
|
||||
export const aidRegExp = /^[0-9a-z]{10}$/;
|
||||
|
||||
|
|
@ -35,6 +36,12 @@ export function parseAid(id: string): { date: Date; } {
|
|||
return { date: new Date(time) };
|
||||
}
|
||||
|
||||
export function parseAidFull(id: string): { date: number; additional: bigint; } {
|
||||
const date = parseInt(id.slice(0, 8), 36) + TIME2000;
|
||||
const additional = parseBigInt36(id.slice(8, 10));
|
||||
return { date, additional };
|
||||
}
|
||||
|
||||
export function isSafeAidT(t: number): boolean {
|
||||
return t > TIME2000;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
// https://misskey.m544.net/notes/71899acdcc9859ec5708ac24
|
||||
|
||||
import { customAlphabet } from 'nanoid';
|
||||
import { parseBigInt36 } from '@/misc/bigint.js';
|
||||
|
||||
export const aidxRegExp = /^[0-9a-z]{16}$/;
|
||||
|
||||
|
|
@ -16,6 +17,7 @@ const TIME2000 = 946684800000;
|
|||
const TIME_LENGTH = 8;
|
||||
const NODE_LENGTH = 4;
|
||||
const NOISE_LENGTH = 4;
|
||||
const AIDX_LENGTH = TIME_LENGTH + NODE_LENGTH + NOISE_LENGTH;
|
||||
|
||||
const nodeId = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', NODE_LENGTH)();
|
||||
let counter = 0;
|
||||
|
|
@ -42,6 +44,12 @@ export function parseAidx(id: string): { date: Date; } {
|
|||
return { date: new Date(time) };
|
||||
}
|
||||
|
||||
export function parseAidxFull(id: string): { date: number; additional: bigint; } {
|
||||
const date = parseInt(id.slice(0, TIME_LENGTH), 36) + TIME2000;
|
||||
const additional = parseBigInt36(id.slice(TIME_LENGTH, AIDX_LENGTH));
|
||||
return { date, additional };
|
||||
}
|
||||
|
||||
export function isSafeAidxT(t: number): boolean {
|
||||
return t > TIME2000;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { parseBigInt16 } from '@/misc/bigint.js';
|
||||
|
||||
const CHARS = '0123456789abcdef';
|
||||
|
||||
// same as object-id
|
||||
|
|
@ -39,6 +41,13 @@ export function parseMeid(id: string): { date: Date; } {
|
|||
};
|
||||
}
|
||||
|
||||
export function parseMeidFull(id: string): { date: number; additional: bigint; } {
|
||||
return {
|
||||
date: parseInt(id.slice(0, 12), 16) - 0x800000000000,
|
||||
additional: parseBigInt16(id.slice(12, 24)),
|
||||
};
|
||||
}
|
||||
|
||||
export function isSafeMeidT(t: number): boolean {
|
||||
return t > 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { parseBigInt16 } from '@/misc/bigint.js';
|
||||
|
||||
const CHARS = '0123456789abcdef';
|
||||
|
||||
// 4bit Fixed hex value 'g'
|
||||
|
|
@ -39,6 +41,13 @@ export function parseMeidg(id: string): { date: Date; } {
|
|||
};
|
||||
}
|
||||
|
||||
export function parseMeidgFull(id: string): { date: number; additional: bigint; } {
|
||||
return {
|
||||
date: parseInt(id.slice(1, 12), 16),
|
||||
additional: parseBigInt16(id.slice(12, 24)),
|
||||
};
|
||||
}
|
||||
|
||||
export function isSafeMeidgT(t: number): boolean {
|
||||
return t > 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { parseBigInt16 } from '@/misc/bigint.js';
|
||||
|
||||
const CHARS = '0123456789abcdef';
|
||||
|
||||
// same as meid
|
||||
|
|
@ -39,6 +41,13 @@ export function parseObjectId(id: string): { date: Date; } {
|
|||
};
|
||||
}
|
||||
|
||||
export function parseObjectIdFull(id: string): { date: number; additional: bigint; } {
|
||||
return {
|
||||
date: parseInt(id.slice(0, 8), 16) * 1000,
|
||||
additional: parseBigInt16(id.slice(8, 24)),
|
||||
};
|
||||
}
|
||||
|
||||
export function isSafeObjectIdT(t: number): boolean {
|
||||
return t > 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,15 +5,27 @@
|
|||
|
||||
// Crockford's Base32
|
||||
// https://github.com/ulid/spec#encoding
|
||||
import { parseBigInt32 } from '@/misc/bigint.js';
|
||||
|
||||
const CHARS = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
||||
|
||||
export const ulidRegExp = /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/;
|
||||
|
||||
export function parseUlid(id: string): { date: Date; } {
|
||||
const timestamp = id.slice(0, 10);
|
||||
function parseBase32(timestamp: string) {
|
||||
let time = 0;
|
||||
for (let i = 0; i < 10; i++) {
|
||||
for (let i = 0; i < timestamp.length; i++) {
|
||||
time = time * 32 + CHARS.indexOf(timestamp[i]);
|
||||
}
|
||||
return { date: new Date(time) };
|
||||
return time;
|
||||
}
|
||||
|
||||
export function parseUlid(id: string): { date: Date; } {
|
||||
return { date: new Date(parseBase32(id.slice(0, 10))) };
|
||||
}
|
||||
|
||||
export function parseUlidFull(id: string): { date: number; additional: bigint; } {
|
||||
return {
|
||||
date: parseBase32(id.slice(0, 10)),
|
||||
additional: parseBigInt32(id.slice(10, 26)),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue