Fix: 絵文字が正しくカウントされない問題 / Fix: 投稿できない問題 / Feat: 投稿の待機時間を表示
This commit is contained in:
+31
-17
@@ -17,7 +17,16 @@ type SuccessPosted<T = string> = {
|
||||
};
|
||||
}
|
||||
|
||||
const BASE_URL = "https://collapse.jp/api/v3/bot/";
|
||||
const getRealLength = (str: string): number => {
|
||||
const segmenter = new Intl.Segmenter("ja", { granularity: "grapheme" });
|
||||
return [...segmenter.segment(str)].length;
|
||||
};
|
||||
|
||||
const realSlice = (str: string, limit: number): string => {
|
||||
const segmenter = new Intl.Segmenter("ja", { granularity: "grapheme" });
|
||||
const segments = [...segmenter.segment(str)];
|
||||
return segments.slice(0, limit).map(s => s.segment).join("");
|
||||
};
|
||||
|
||||
export const createPost = async (data: {
|
||||
text: string;
|
||||
@@ -28,6 +37,9 @@ export const createPost = async (data: {
|
||||
}, title: string) => {
|
||||
const excessedMessage = "**👉返信に続きがあります。**";
|
||||
|
||||
const excessedMessageLength = getRealLength(excessedMessage);
|
||||
const eolLength = getRealLength(EOL);
|
||||
|
||||
let lines = data.text.split(EOL);
|
||||
let firstUniqid = "";
|
||||
let count = 0;
|
||||
@@ -40,23 +52,25 @@ export const createPost = async (data: {
|
||||
|
||||
const maxLength = config.post.maxLength;
|
||||
|
||||
const limit = maxLength - (excessedMessage.length + EOL.length * 2);
|
||||
const limit = maxLength - (excessedMessageLength + eolLength * 2);
|
||||
|
||||
while (lines.length > 0) {
|
||||
const nextLine = lines[0];
|
||||
if (nextLine === undefined)
|
||||
break;
|
||||
|
||||
if (nextLine.length > limit && currentText === "") {
|
||||
const nextLineLength = getRealLength(nextLine);
|
||||
if (nextLineLength > limit && currentText === "") {
|
||||
const targetLine = lines.shift() || "";
|
||||
currentText = targetLine.slice(0, limit);
|
||||
|
||||
lines.unshift(targetLine.slice(limit));
|
||||
currentText = realSlice(targetLine, limit);
|
||||
lines.unshift(realSlice(targetLine, limit * -1));
|
||||
break;
|
||||
}
|
||||
|
||||
const potentialText = currentText ? currentText + EOL + nextLine : nextLine;
|
||||
if (potentialText.length <= limit) {
|
||||
const potentialText = currentText
|
||||
? currentText + EOL + nextLine
|
||||
: nextLine;
|
||||
if (getRealLength(potentialText) <= limit) {
|
||||
currentText = potentialText;
|
||||
lines.shift();
|
||||
} else {
|
||||
@@ -79,25 +93,24 @@ export const createPost = async (data: {
|
||||
? firstUniqid
|
||||
: data.replyid;
|
||||
|
||||
const body = new FormData();
|
||||
body.append("content", data.text);
|
||||
body.append("zone", data.zone ?? "normal");
|
||||
body.append("reply_restriction", data.replyTarget ?? "all");
|
||||
const formData = new FormData();
|
||||
formData.append("content", currentText);
|
||||
formData.append("zone", data.zone ?? "normal");
|
||||
formData.append("reply_restriction", data.replyTarget ?? "all");
|
||||
if (replyid) {
|
||||
body.append("reply_to_id", replyid);
|
||||
formData.append("reply_to_id", replyid);
|
||||
}
|
||||
if (data.images) {
|
||||
data.images.forEach(img => body.append("images", img[0], img[1]));
|
||||
data.images.forEach(img => formData.append("images", img[0], img[1]));
|
||||
}
|
||||
|
||||
const req = await fetch(new URL("posts", BASE_URL), {
|
||||
const req = await fetch(`https://collapse.jp/api/v3/bot/posts`, {
|
||||
method: "POST",
|
||||
body,
|
||||
headers: {
|
||||
"Authorization": `Bearer ${config.mtweet.token}`,
|
||||
"Content-Type": "application/json",
|
||||
"X-Idempotency-Key": String(process.hrtime.bigint()),
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
|
||||
const res = await req.json();
|
||||
@@ -114,6 +127,7 @@ export const createPost = async (data: {
|
||||
const interval = res.error.details?.retry_after
|
||||
? res.error.details.retry_after * 1000
|
||||
: config.post.retryInterval;
|
||||
console.log(`${interval}ms待機します...`);
|
||||
await new Promise(resolve => setTimeout(resolve, interval));
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user