From 5b5d2939526c851eeee1b6a21a55db72c047ab41 Mon Sep 17 00:00:00 2001 From: satori Date: Tue, 8 Feb 2022 03:57:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3=20like?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++-- index.js | 33 ++++++++++++++++++++++++++------- sdk.js | 18 ++++++++++++++++++ 3 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 sdk.js diff --git a/README.md b/README.md index b8ba8d9..5347643 100644 --- a/README.md +++ b/README.md @@ -293,14 +293,20 @@ fetch('/post?attach=post&aid=spNkjLA', { 受益于评论的实现结构, 也可以对二级评论作点赞操作, 也可以对任意对象点赞操作 ```javascript -fetch('/like?attach=post&aid=spNkjLA', { +fetch('/like', { method: 'POST', + body: JSON.stringify({ + attach: "post", + aid: "spNkjLA" + }), }).then(Response => Response.json()).then(data => { console.log(data) }) { - _id: 'SOAPSAdaw' + _id: 'SOAPSAdaw', + attach: "post", + aid: "spNkjLA" } ``` diff --git a/index.js b/index.js index ba98ff6..9fd6d54 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ import HUB from './fmhub.js' const databases = new Map() // 所有数据库 const FM = new HUB() // 频道消息分发器 +const messagelist = new Map() // 消息队列的nedb存储 const db = (name) => (databases.get(name) || function () { let database = new nedb({ filename: `./data/db/${name}.db`, autoload: true, timestampData: true }) @@ -43,6 +44,7 @@ const user_load = async (_id) => await new Promise(resolve => db('user').findOne if (!doc) return resolve(doc) // let { salt, password, mobile, email, ...user } = doc // 这里只应提取有限简略信息附给列表, 因为个人信息中可能含有大量私有字段 + // 但是..nedb 全在内存, 效率略略略... let { _id, gid, name, avatar } = doc return resolve({ _id, gid, name, avatar }) })) @@ -74,6 +76,10 @@ function websocketer(ws, req) { if (typeof (msg) !== "string") return console.log("消息不是字符串") let { fm, data } = JSON.parse(msg) FM.发送消息(fm, uid, data) + // 基于链表实现消息记录(对所有频道加入查询) + // ws收到的消息不一定是向频道推送, 还可能是消息查询(因为系统推送的消息也不一定是聊天室) + // 因而要为消息加上 id, 并为其附上双向链表字段, 使用 nedb 存储吗.. + // 因此 格式为 { fm, id, direction: before||after, } }) // 关闭连接时 @@ -280,12 +286,6 @@ const object_create = async function (req, res) { }) } -//import memory from "./memory.js" -//function object_patch(req, res, next) { -// let obj = new memory({ name: req.params.name, _id: req.params._id }) -// return res.send(obj.PATCH({ account: req.session.account, data: req.body })) -//} - // 修改对象 function object_patch(req, res, next) { return db(req.params.name).findOne({ _id: req.params._id }, function (err, doc) { @@ -439,13 +439,32 @@ const db_compact = function (req, res) { return res.send("ok") } +// 读取对象列表 +function index_get(req, res) { + // 返回对象列表 + let list = [] + databases.forEach((value, key) => { + list.push(key) + }) + res.json(list) +} + +// 锁定对象列表 +function index_patch(req, res) { + // 指定设定的目标类型 +} + +// 锁定后手动创建列表(管理员) { +// +//} + const app = expressWs(express()).app app.use(express.json()) app.use(express.urlencoded({ extended: false })) app.use(session({ secret: 'kana', name: 'sid', resave: false, saveUninitialized: false, cookie: { maxAge: 180 * 24 * 3600000 }, store: session_store })) app.use('/data/file/', express.static('data/file')) app.ws('/', websocketer) -app.route('/').get((req, res) => res.send(`

Hello World

`)) +app.route('/').get(index_get) // (req, res) => res.send(`

Hello World

`) app.route('/account').get(profile).post(online, uploadavatar) app.route('/session').get(online, session_list).post(session_create).delete(online, sessionDeleteSelf) app.route('/session/:sid').delete(online, session_delete) diff --git a/sdk.js b/sdk.js new file mode 100644 index 0000000..94c8fda --- /dev/null +++ b/sdk.js @@ -0,0 +1,18 @@ +export default class { + constructor() { + // 初始化状态, 连接, 确认登录状态 + this.title = "Kana" + this.info = "站点信息" + this.account = 'account' + } + // 对像的增删改查 +} + +class account { + constructor() { + this.name = "Last" + this.avatar = "xxxxxx" + this.createdAt = "是创建时间" + this.createdAt_fmt = "是格式化时间, 有很多格式" + } +}