Skip to content

Commit

Permalink
Better read handling (#51)
Browse files Browse the repository at this point in the history
* [add] - messageReadIdからmessageReadTimeへ改名するファイル追加

* [add/change] - メッセ既読Idから時間へ変更

importも

* [change] - interface更新

* [change] - 既読時間の書き込みファイルを更新

* [change] - Socketハンドラのメッセ既読Idを時間へ更新

* [add] - 履歴をメッセージの時間基準でも取れるように

* [add] - _

* [add] - 履歴取得時の開始位置指定の判別に時間指定の探索を追加

* [change] - パッケージ情報の更新

* [change] - メッセージ既読時間格納Socketハンドラと関数の引数更新

* [add] - 既読時間設定時Date型として値を使えるかどうか検証するように

* [fix] - メッセ位置計算部分にて条件にするSQL構文が間違っていた

* [add] - 履歴取得Socketハンドラの引数更新

* [add] - 改行とログ
  • Loading branch information
NfoAlex committed Jun 24, 2024
1 parent 9f6f2ed commit 09a62ab
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 162 deletions.
69 changes: 36 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 46 additions & 9 deletions src/actionHandler/Message/fetchHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { IMessage, IMessageBeforeParsing } from "../../type/Message";
export default async function fetchHistory(
channelId: string,
fetchingPosition: {
positionMessageId: string
positionMessageId?: string
positionMessageTime?: string
includeThisPosition: boolean,
fetchDirection: "older"|"newer"
}
Expand All @@ -25,9 +26,23 @@ export default async function fetchHistory(
let positionIndex:number = 0;

//メッセージ位置の設定、指定がないなら0
if (fetchingPosition.positionMessageId !== "") {
if (
( //Idの指定があるか?
fetchingPosition.positionMessageId !== "" && fetchingPosition.positionMessageId !== undefined
)
||
( //または時間の指定があるか?
fetchingPosition.positionMessageTime !== "" && fetchingPosition.positionMessageTime !== undefined
)
) {
//メッセージのインデックス番号を計算する
const positionTemp = await calcPositionOfMessage(channelId, fetchingPosition.positionMessageId);
const positionTemp = await calcPositionOfMessage(
channelId,
{
messageId: fetchingPosition.positionMessageId,
time: fetchingPosition.positionMessageTime
}
);

//結果に応じて値設定
if (positionTemp === null) {
Expand Down Expand Up @@ -57,7 +72,7 @@ export default async function fetchHistory(
`,
(err:Error, length:[{"COUNT(*)":number}]) => {
if (err) {
console.log("fetchHistory :: db(historyLength) : エラー->", err);
console.log("fetchHistory :: db(履歴の長さ取得) : エラー->", err);
resolve(null);
return;
} else {
Expand Down Expand Up @@ -97,7 +112,7 @@ export default async function fetchHistory(
`,
(err:Error, history:IMessageBeforeParsing[]) => {
if (err) {
console.log("fetchHistory :: db : エラー->", err);
console.log("fetchHistory :: db(履歴取得) : エラー->", err);
resolve(null);
return;
} else {
Expand Down Expand Up @@ -171,11 +186,33 @@ export default async function fetchHistory(
}

//メッセージの位置を取得
async function calcPositionOfMessage(channelId:string, messageId:string)
:Promise<number|null> {
async function calcPositionOfMessage(
channelId:string,
messagePos: {
messageId?: string,
time?: string
}
):Promise<number|null> {
return await new Promise((resolve) => {
try {

//位置計算に使うメッセージ情報
let calcMode:"messageId"|"time" = "messageId";
//引数に時間があるかで使う情報切り替え
if (messagePos.time !== undefined) {
calcMode = "time";
}

//console.log("fetchHistory :: calcPositionOfMessage : calcMode->", calcMode);

//検索に使うSQL構文を選択(時間かメッセIdか)
const searchQuery = calcMode==="messageId"
?
"messageId = '" + messagePos.messageId + "'"
:
"time = '" + messagePos.time + "'"

//該当メッセージの位置取得
db.all(
`
WITH NumberedRows AS (
Expand All @@ -190,11 +227,11 @@ async function calcPositionOfMessage(channelId:string, messageId:string)
FROM
NumberedRows
WHERE
messageId = '` + messageId + `';
${searchQuery};
`,
(err:Error, messageWithIndex:any) => {
if (err) {
console.log("fetchHistory :: db : エラー->", err);
console.log("fetchHistory :: db(メッセ位置計算) : エラー->", err);
resolve(null);
return;
} else {
Expand Down
42 changes: 0 additions & 42 deletions src/actionHandler/Message/getMessageReadId.ts

This file was deleted.

42 changes: 42 additions & 0 deletions src/actionHandler/Message/getMessageReadTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import sqlite3 from "sqlite3";
import type { IMessageReadTime } from "../../type/Message";
const db = new sqlite3.Database("./records/USER.db");

//メッセージの最終既読時間をJSONで取得
export default async function getMessageReadTime(userId:string)
:Promise<IMessageReadTime|null> {
try {

return new Promise((resolve) => {
db.all(
`
SELECT messageReadTime FROM USERS_SAVES
WHERE userId='` + userId + `'
`,
(err:Error, messageReadTimeBeforeParsed:[{messageReadTime:string}]) => {
if (err) {
console.log("getMessageReadTime :: db : エラー->", err);
resolve(null);
return;
} else {
//console.log("getMessageReadTime :: db : data->", messageReadTimeBeforeParsed);
//パースして返す
const messageReadTime:IMessageReadTime =
JSON.parse(
messageReadTimeBeforeParsed[0]['messageReadTime']
);

resolve(messageReadTime);
return;
}
}
);
});

} catch(e) {

console.log("getMessageReadTime :: エラー->", e);
return null;

}
}
51 changes: 0 additions & 51 deletions src/actionHandler/Message/setMessageReadId.ts

This file was deleted.

Loading

0 comments on commit 09a62ab

Please sign in to comment.