mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
feat: convert src/messaging/* to async/await (#7778)
* feat: src/messaging/create.js, #7743 * feat: src/messaging/data.js, #7743 * feat: src/messaging/delete.js, #7743 * feat: src/messaging/edit.js, index.js, #7743 * fix: added in missing awaits * feat: wrapped up src/messaging/* rewrite * refactor: messaging delete/restore similar blocks of code
This commit is contained in:
@@ -1,53 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
|
||||
var db = require('../database');
|
||||
var sockets = require('../socket.io');
|
||||
|
||||
module.exports = function (Messaging) {
|
||||
Messaging.getUnreadCount = function (uid, callback) {
|
||||
Messaging.getUnreadCount = async (uid) => {
|
||||
if (parseInt(uid, 10) <= 0) {
|
||||
return setImmediate(callback, null, 0);
|
||||
return 0;
|
||||
}
|
||||
db.sortedSetCard('uid:' + uid + ':chat:rooms:unread', callback);
|
||||
|
||||
return await db.sortedSetCard('uid:' + uid + ':chat:rooms:unread');
|
||||
};
|
||||
|
||||
Messaging.pushUnreadCount = function (uid) {
|
||||
Messaging.pushUnreadCount = async (uid) => {
|
||||
if (parseInt(uid, 10) <= 0) {
|
||||
return;
|
||||
}
|
||||
Messaging.getUnreadCount(uid, function (err, unreadCount) {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
sockets.in('uid_' + uid).emit('event:unread.updateChatCount', unreadCount);
|
||||
const unreadCount = await Messaging.getUnreadCount(uid);
|
||||
sockets.in('uid_' + uid).emit('event:unread.updateChatCount', unreadCount);
|
||||
};
|
||||
|
||||
Messaging.markRead = async (uid, roomId) => db.sortedSetRemove('uid:' + uid + ':chat:rooms:unread', roomId);
|
||||
Messaging.markAllRead = async uid => db.delete('uid:' + uid + ':chat:rooms:unread');
|
||||
|
||||
Messaging.markUnread = async (uids, roomId) => {
|
||||
const exists = await Messaging.roomExists(roomId);
|
||||
if (!exists) {
|
||||
throw new Error('[[error:chat-room-does-not-exist]]');
|
||||
}
|
||||
var keys = uids.map(function (uid) {
|
||||
return 'uid:' + uid + ':chat:rooms:unread';
|
||||
});
|
||||
};
|
||||
|
||||
Messaging.markRead = function (uid, roomId, callback) {
|
||||
db.sortedSetRemove('uid:' + uid + ':chat:rooms:unread', roomId, callback);
|
||||
};
|
||||
|
||||
Messaging.markAllRead = function (uid, callback) {
|
||||
db.delete('uid:' + uid + ':chat:rooms:unread', callback);
|
||||
};
|
||||
|
||||
Messaging.markUnread = function (uids, roomId, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.roomExists(roomId, next);
|
||||
},
|
||||
function (exists, next) {
|
||||
if (!exists) {
|
||||
return next(new Error('[[error:chat-room-does-not-exist]]'));
|
||||
}
|
||||
var keys = uids.map(function (uid) {
|
||||
return 'uid:' + uid + ':chat:rooms:unread';
|
||||
});
|
||||
|
||||
db.sortedSetsAdd(keys, Date.now(), roomId, next);
|
||||
},
|
||||
], callback);
|
||||
return await db.sortedSetsAdd(keys, Date.now(), roomId);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user