Skip to content

Commit

Permalink
Fix channel creation (#64)
Browse files Browse the repository at this point in the history
* [fix] - チャンネルIdの空きを調べるとき存在しないユーザーIdを使っていた

* [remove] - async

* [fix] - チャンネル作成時に使うDBが違っていた

* [remove] - 不要なimport

* [change] - チャンネル作成をawaitで待つように
  • Loading branch information
NfoAlex committed Aug 19, 2024
1 parent a81bbe2 commit 779d8aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
29 changes: 17 additions & 12 deletions src/actionHandler/Channel/createChannel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fetchChannel from "./fetchChannel";

import Database from 'better-sqlite3';
const db = new Database('./records/SERVER.db');
db.pragma('journal_mode = WAL');
const dbMsg = new Database('./records/MESSAGE.db');
dbMsg.pragma('journal_mode = WAL');

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

/**
* チャンネルを作成する
Expand All @@ -13,11 +15,11 @@ db.pragma('journal_mode = WAL');
* @returns
*/
export default async function createChannel(
_channelName:string,
_description:string,
_isPrivate:boolean,
_userId:string,
):Promise<boolean> {
_channelName:string,
_description:string,
_isPrivate:boolean,
_userId:string,
):Promise<boolean> {
try {

//空いているチャンネルIDを探して取得
Expand All @@ -26,9 +28,9 @@ export default async function createChannel(
if (channelIdGen === "") return false;

//チャンネル用履歴テーブル作成
db.exec(
dbMsg.exec(
`
create table if not exists C${channelIdGen}(
create table C${channelIdGen}(
messageId TEXT PRIMARY KEY,
channelId TEXT NOT NULL,
userId TEXT NOT NULL,
Expand Down Expand Up @@ -69,7 +71,7 @@ export default async function createChannel(
}

//チャンネルIDの空きを探す
async function getNewChannelId():Promise<string> {
function getNewChannelId():Promise<string> {
let tryCount = 0;

return new Promise<string>((resolve) => {
Expand All @@ -84,11 +86,14 @@ async function getNewChannelId():Promise<string> {
}

//チャンネル検索、データ格納
const datChannel = await fetchChannel(channelIdGen, "SYSTEM");
//const datChannel = fetchChannel(channelIdGen, "SYSTEM");
const datChannel = db.prepare(
"SELECT * FROM CHANNELS WHERE channelId=?"
).get(channelIdGen) as IChannelbeforeParsing|undefined;
console.log("createChannel :: getNewChannelId : datChannel->", datChannel);

//データ長さが0ならループ停止してIDを返す
if (datChannel === null) {
if (datChannel === undefined) {
clearInterval(checkLoop);
resolve(channelIdGen); //IDを返す
}
Expand Down
4 changes: 2 additions & 2 deletions src/socketHandler/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = (io:Server) => {
io.on("connection", (socket:Socket) => {

//チャンネル作成
socket.on("createChannel", (dat:
socket.on("createChannel", async (dat:
{
RequestSender:IRequestSender,
channelName:string,
Expand Down Expand Up @@ -52,7 +52,7 @@ module.exports = (io:Server) => {
}

//チャンネルを作成する
const createChannelResult = createChannel(
const createChannelResult = await createChannel(
dat.channelName,
dat.description,
dat.isPrivate,
Expand Down

0 comments on commit 779d8aa

Please sign in to comment.