Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #71 from NfoAlex/PinnedMessages
Browse files Browse the repository at this point in the history
Pinned messages
  • Loading branch information
NfoAlex authored Dec 3, 2023
2 parents d1f077a + 1759413 commit de85a46
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 12 deletions.
73 changes: 73 additions & 0 deletions Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ let getMessage = function getMessage(channelid, messageid) {
"userid": "不明なユーザー",
"channelid": channelid,
"time": "20010101000000",
"pinned": false,
"content": "消去されたメッセージ",
"replyData": {
"isReplying": false,
Expand Down Expand Up @@ -443,6 +444,7 @@ let getMessage = function getMessage(channelid, messageid) {
"userid": "不明なユーザー",
"channelid": channelid,
"time": "20010101000000",
"pinned": false,
"content": "消去されたメッセージ",
"replyData": {
"isReplying": false,
Expand Down Expand Up @@ -471,6 +473,75 @@ let getMessage = function getMessage(channelid, messageid) {

}

//メッセージをチャンネルへピン留めする
let msgPin = function msgPin(dat) {
/*
dat
{
action: "pin",
channelid: channelid,
messageid: msgId,
reqSender: {...}
}
*/

//メッセージIDとチャンネルIDを抽出
let messageid = dat.messageid;
let channelid = dat.channelid;

//メッセージIDから送信日付を取得してパスを割り出す
let fulldate = messageid.slice(0,4) + "_" + messageid.slice(4,6) + "_" + messageid.slice(6,8);
let pathOfJson = "./record/" + channelid + "/" + fulldate + ".json";

//チャンネルデータにpinsないなら作成
if ( db.dataServer.channels[channelid].pins === undefined ) {
db.dataServer.channels[channelid].pins = [];

}

//すでにピン留めされていたら削除、そうでないなら追加
if ( db.dataServer.channels[channelid].pins.indexOf(messageid) !== -1 ) {
//配列上のピンの位置
let pinIndex = db.dataServer.channels[channelid].pins.indexOf(messageid);
//ピン削除
db.dataServer.channels[channelid].pins.splice(pinIndex, 1);
} else {
//ピン追加
db.dataServer.channels[channelid].pins.push(messageid);
}

//チャンネルの更新のためにJSONへ書き込み
fs.writeFileSync("./server.json", JSON.stringify(db.dataServer, null, 4));

//データ取り出し、更新
try{
dataHistory = JSON.parse(fs.readFileSync(pathOfJson, 'utf-8')); //メッセージデータのJSON読み込み
//もしデータが正常にとれるならそのままピン留め
if ( dataHistory[messageid] !== undefined ) {
//ピン留めを更新してJSONへ書き込み
dataHistory[messageid].pinned = !dataHistory[messageid].pinned;
fs.writeFileSync(pathOfJson, JSON.stringify(dataHistory, null, 4));

//返す
return {
action:"pin",
channelid: dataHistory[messageid].channelid,
messageid: dataHistory[messageid].messageid,
pinned: dataHistory[messageid].pinned,
};

} else { //undefinedなら削除された体で返す
return -1;

}
}
catch(e) { //エラーなら中止
console.log("Message :: msgPin : エラー->", e);
return -1;
}

}

//メッセージの削除
let msgDelete = function msgDelete(dat) {
/*
Expand Down Expand Up @@ -818,6 +889,7 @@ let msgRecord = function msgRecord(json) {
userid: json.userid,
channelid: json.channelid,
time: receivedTime,
pinned: false,
content: json.content,
isSystemMessage: json.isSystemMessage,
replyData: json.replyData,
Expand Down Expand Up @@ -929,6 +1001,7 @@ exports.msgMix = msgMix; //メッセージ送受信
exports.addUrlPreview = addUrlPreview; //URLプレビュー設定
exports.msgRecord = msgRecord; //履歴に記録
exports.getMessage = getMessage; //メッセージの単体取得
exports.msgPin = msgPin; //メッセージをチャンネルへピン留めする
exports.msgDelete = msgDelete; //メッセージ削除
exports.msgReaction = msgReaction; //メッセージにリアクションする
exports.msgEdit = msgEdit; //メッセージの編集
Expand Down
16 changes: 11 additions & 5 deletions dbControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ let dataServerInitText = `
"0001": {
"name": "random",
"description": "なんでも雑談",
"scope": "public"
"pins": [],
"scope": "public",
"canTalk": "Member"
}
}
}`;
Expand Down Expand Up @@ -299,9 +301,10 @@ let getInfoChannel = function getInfoChannel(dat) {
infoParsed = {
channelid: dat.targetid,
channelname: "存在しないチャンネル",
channelid: dat.targetid,
description: "このチャンネルの情報がありません。これが見えていたらおかしいよ。",
scope: "deleted"
pins: [],
scope: "deleted",
canTalk: "Member"
};

return infoParsed;
Expand All @@ -313,17 +316,20 @@ let getInfoChannel = function getInfoChannel(dat) {
channelname: dataServer.channels[dat.targetid].name,
channelid: dat.targetid,
description: dataServer.channels[dat.targetid].description,
pins: dataServer.channels[dat.targetid].pins,
scope: dataServer.channels[dat.targetid].scope,
canTalk: dataServer.channels[dat.targetid].canTalk
}
}
catch(e) {
//読み取れなかったら
infoParsed = {
channelname: "存在しないチャンネル",
channelid: dat.targetid,
channelname: "存在しないチャンネル",
description: "このチャンネルの情報がありません。これが見えていたらおかしいよ。",
scope: "deleted"
pins: [],
scope: "deleted",
canTalk: "Member"
};
console.log("dbControl :: getInfoChannel : エラー->", e);
}
Expand Down
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1702,10 +1702,7 @@ io.on("connection", (socket) => {
socket.on("getMessageSingle", (req) => {
/*
{
reqSender: {
userid: userinfo.userid,
sessionid: userinfo.sessionid
},
reqSender: {...},
channelid: channelid,
messageid: msgId
}
Expand Down Expand Up @@ -1738,7 +1735,6 @@ io.on("connection", (socket) => {
}
}
*/
//msg.msgDelete(dat);

let result = -1; //結果用変数

Expand All @@ -1747,13 +1743,25 @@ io.on("connection", (socket) => {
"channelid",
"messageid",
];

if ( !checkDataIntegrality(dat, paramRequire, "actMessage") ) {
return -1;

}

//行動内容によって処理を変える
switch( dat.action ) {
case "pin":
result = msg.msgPin(dat);
//チャンネル情報の更新もしてるからデータを送信
//現在のチャンネルの情報を取得、送信
let info = db.getInfoChannel({
targetid: dat.channelid,
reqSender: dat.reqSender
});
console.log("index :: actMessage : channeldata->", info);
io.to("loggedin").emit("infoChannel", info);
break;

case "delete":
//削除、そして更新するメッージのIDなどを取り込む
result = msg.msgDelete(dat);
Expand Down
4 changes: 3 additions & 1 deletion infoUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,9 @@ let channelCreate = async function channelCreate(dat) {
db.dataServer.channels[newChannelId] = {
name: dat.channelname,
description: dat.description,
scope: dat.scope
pins: [],
scope: dat.scope,
canTalk: "Member"
};

//チャンネル作成者をそのまま参加させる
Expand Down

0 comments on commit de85a46

Please sign in to comment.