Skip to content

Commit

Permalink
[add] - チャンネル情報を検索するSocketハンドラと関数作成 (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
NfoAlex authored Aug 27, 2024
1 parent 1c0663f commit d4f9705
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/actionHandler/Channel/searchChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Database from 'better-sqlite3';
const db = new Database('./records/SERVER.db');
db.pragma('journal_mode = WAL');

import fetchUser from "../User/fetchUser";
import roleCheck from "../../util/roleCheck";

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

/**
* チャンネル情報を検索する(最大30件ずつ)
* @param _query
* @param _userId
* @param _pageIndex
* @returns
*/
export default function searchChannel(
_query: string,
_userId: string,
_pageIndex = 1

):IChannel[]|null {
try {

//検索結果のずらし分計算
const searchOffset = (_pageIndex - 1) * 30;

//チャンネルを検索する
const channelInfos = db.prepare(
"SELECT * FROM CHANNELS WHERE channelName LIKE ? LIMIT 30 OFFSET ?"
).all(`${_query}%`, searchOffset) as IChannelbeforeParsing[] | undefined;

if (channelInfos === undefined) return null;

//権限を調べるためにユーザー情報を取得
const userInfo = fetchUser(_userId, null);
if (userInfo === null) return null;

//チャンネル情報分ループしてこのユーザーが扱えるものか調べる
for (const index in channelInfos) {
//プライベートならユーザーの権限、あるいは作成者と同じか調べる
if (channelInfos[index].isPrivate) {

//チャンネル作成者と同じか、あるいはサーバー管理権限があるか調べて違うならその情報を削除する
if (
!userInfo.channelJoined.includes(channelInfos[index].channelId)
&&
!(roleCheck(_userId, "ServerManage"))
) channelInfos.splice(Number.parseInt(index), 1);
}
}

//パースしたチャンネル情報を入れる配列
const channelInfosParsed:IChannel[] = [];
//ループしてチャンネル情報をパースする
for (const index in channelInfos) {
channelInfosParsed.push({
...channelInfos[index],
isPrivate: channelInfos[index].isPrivate === 1,
speakableRole: //👇空文字列なら空配列にする
channelInfos[index].speakableRole!==""?channelInfos[index].speakableRole.split(","):[]
})
}

return channelInfosParsed;

} catch(e) {

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

}
}
23 changes: 23 additions & 0 deletions src/socketHandler/Channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import updateChannel from "../actionHandler/Channel/updateChannel";
import type IRequestSender from "../type/requestSender";
import type { IChannel } from "../type/Channel";
import recordSystemMessage from "../util/Message/recordSystemMessage";
import searchChannel from "../actionHandler/Channel/searchChannel";

module.exports = (io:Server) => {
io.on("connection", (socket:Socket) => {
Expand Down Expand Up @@ -213,6 +214,28 @@ module.exports = (io:Server) => {
}
});

//チャンネルを検索する
socket.on("searchChannelInfo", (dat:{RequestSender:IRequestSender, query:string, pageIndex:number}) => {
/* セッション認証 */
if (!(checkSession(dat.RequestSender))) {
socket.emit("RESULT::searchChannelInfo", { result:"ERROR_SESSION_ERROR", data:null });
return;
}

try {
//チャンネル情報を検索、取得する
const channelInfos = searchChannel(
dat.query,
dat.RequestSender.userId,
dat.pageIndex
);

socket.emit("RESULT::searchChannelInfo", {result:"SUCCESS", data:channelInfos });
} catch(e) {
socket.emit("RESULT::searchChannelInfo", {result:"ERROR_INTERNAL_THING", data:null });
}
});

//チャンネルを一覧で取得
socket.on("fetchChannelList", (dat:{RequestSender:IRequestSender}) => {
/* セッション認証 */
Expand Down

0 comments on commit d4f9705

Please sign in to comment.