Skip to content

Commit

Permalink
Channel order (#46)
Browse files Browse the repository at this point in the history
* [add] - チャンネル順序を取得できるSocketハンドラと関数作成

* [add] - チャンネル順序用のinterface追加

* [change] - コメントとログ無効化

* [fix] - リンクデータパース時のデータ判別式が間違い

* [add] - チャンネル順序の上書き関数とSocketハンドラを作成
  • Loading branch information
NfoAlex committed Jun 11, 2024
1 parent 1fc7de3 commit 0fef173
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/actionHandler/Message/fetchHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default async function fetchHistory(
for (let index in history) {
//リンクプレビューのJSONパース、nullかundefinedなら空JSONに
const linkDataParsed:IMessage["linkData"] =
(history[index].linkData!==null || history[index].linkData!==undefined)
(history[index].linkData!==null && history[index].linkData!==undefined)
?
JSON.parse(history[index].linkData)
:
Expand Down
29 changes: 29 additions & 0 deletions src/actionHandler/User/fetchUserChannelOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sqlite3 from "sqlite3";
const db = new sqlite3.Database("./records/USER.db");

export default async function fetchUserChannelOrder(userId: string) {
try {

return new Promise((resolve) => {
db.all(
`
SELECT channelOrder FROM USERS_SAVES
WHERE userId=?
`,
userId,
(err:Error, channelOrderData:[{channelOrder: string}]) => {
//console.log("fetchUserChannelOrder :: db : channelOrder->", channelOrderData);
//文字列をJSONにしてから返す
resolve(JSON.parse(channelOrderData[0].channelOrder));
return;
}
)
});

} catch(e) {

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

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

import type { IChannelOrder } from "../../type/Channel";

export default async function saveUserChannelOrder(userId:string, channelOrder:IChannelOrder)
:Promise<boolean> {
return new Promise((resolve) => {
try {

//設定データを書き込み更新
db.run(
`
UPDATE USERS_SAVES SET channelOrder=?
WHERE userId=?
`
,
[JSON.stringify(channelOrder), userId],
(err) => {
if (err) {
console.log("saveUserChannelOrder :: エラー->", err);
//失敗と返す
resolve(false);
return;
} else {
//成功と返す
resolve(true);
return;
}
});

} catch(e) {

console.log("saveUserChannelOrder :: エラー->", e);
resolve(false);
return;

}
});
}
2 changes: 1 addition & 1 deletion src/actionHandler/User/saveUserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default async function saveUserConfig(userId:string, datConfig:IUserConfi

} catch(e) {

console.log("saveUserConfig :: saveUserConfig : エラー->", e);
console.log("saveUserConfig :: エラー->", e);
resolve(false);
return;

Expand Down
59 changes: 59 additions & 0 deletions src/socketHandler/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import fetchUserAll from "../actionHandler/User/fetchUserAll";
import banUser from "../actionHandler/User/banUser";
import pardonUser from "../actionHandler/User/pardonUser";
import roleCheck from "../util/roleCheck";
import fetchUserChannelOrder from "../actionHandler/User/fetchUserChannelOrder";
import saveUserChannelOrder from "../actionHandler/User/saveUserChannelOrder";

import type IRequestSender from "../type/requestSender";
import type { IUserConfig } from "../type/User";
import type { IChannelOrder } from "../type/Channel";

module.exports = (io:Server) => {
io.on("connection", (socket:Socket) => {
Expand Down Expand Up @@ -42,6 +45,33 @@ module.exports = (io:Server) => {
socket.emit("RESULT::fetchUserConfig", { result:"ERROR_DB_THING", data:null });
}
});

//ユーザーごとのチャンネル順序を取得
socket.on("fetchUserChannelOrder", async (dat:{RequestSender:IRequestSender}) => {
/*
返し : {
result: "SUCCESS"|"ERROR_DB_THING"|"ERROR_SESSION_ERROR",
data: ServerInfoLimited<IServerInfo>
}
*/

/* セッション認証 */
if (!(await checkSession(dat.RequestSender))) {
socket.emit("RESULT::fetchUserChannelOrder", { result:"ERROR_SESSION_ERROR", data:null });
return;
}

try {
//チャンネル順序の読み取り
const resultChannelOrder = await fetchUserChannelOrder(dat.RequestSender.userId);

//返す
socket.emit("RESULT::fetchUserChannelOrder", { result:"SUCCESS", data:resultChannelOrder });
} catch(e) {
//返す
socket.emit("RESULT::fetchUserChannelOrder", { result:"ERROR_DB_THING", data:null });
}
});

//設定データを保存する
socket.on("saveUserConfig", async (dat:{RequestSender:IRequestSender, config:IUserConfig}) => {
Expand Down Expand Up @@ -72,6 +102,35 @@ module.exports = (io:Server) => {
}
});

//ユーザーごとのチャンネル順序を保存
socket.on("saveUserChannelOrder", async (dat:{RequestSender:IRequestSender, channelOrder:IChannelOrder}) => {
/*
返し : {
result: "SUCCESS"|"ERROR_DB_THING"|"ERROR_SESSION_ERROR",
data: null
}
*/

//セッション確認
if (!(await checkSession(dat.RequestSender))) {
socket.emit("RESULT::saveUserChannelOrder", { result:"ERROR_SESSION_ERROR", data:null });
return;
}

try {
//書き込み、結果受け取り
const resultSaveUserChannelOrder:boolean = await saveUserChannelOrder(dat.RequestSender.userId, dat.channelOrder);
//結果に応じて結果と設定データを返す
if (resultSaveUserChannelOrder) {
socket.emit("RESULT::saveUserChannelOrder", { result:"SUCCESS", data:null});
} else {
socket.emit("RESULT::saveUserChannelOrder", { result:"ERROR_DB_THING", data:null});
}
} catch(e) {
socket.emit("RESULT::saveUserChannelOrder", { result:"ERROR_DB_THING", data:null });
}
});

//一人分のユーザー情報取得(ユーザーIDから)
socket.on("fetchUserInfo", async (dat:{RequestSender:IRequestSender, userId:string}) => {
/*
Expand Down
7 changes: 7 additions & 0 deletions src/type/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ export interface IChannel {
isPrivate: boolean,
speakableRole: string[]
}

export interface IChannelOrder {
channelId: string,
isThread: boolean,
isFolder: boolean,
child?: []
}

0 comments on commit 0fef173

Please sign in to comment.