Only send messages newer than last seen id

This commit is contained in:
Pavel Djundik 2017-08-28 23:14:01 +03:00
parent 05fc00d9be
commit cffa957e34

View file

@ -236,7 +236,7 @@ function index(req, res, next) {
res.render("index", data);
}
function initializeClient(socket, client, token) {
function initializeClient(socket, client, token, lastMessage) {
socket.emit("authorized");
socket.on("disconnect", function() {
@ -400,11 +400,24 @@ function initializeClient(socket, client, token) {
socket.join(client.id);
const sendInitEvent = (tokenToSend) => {
let networks = client.networks;
if (lastMessage > -1) {
// We need a deep cloned object because we are going to remove unneeded messages
networks = _.cloneDeep(networks);
networks.forEach((network) => {
network.channels.forEach((channel) => {
channel.messages = channel.messages.filter((m) => m.id > lastMessage);
});
});
}
socket.emit("init", {
applicationServerKey: manager.webPush.vapidKeys.publicKey,
pushSubscription: client.config.sessions[token],
active: client.lastActiveChannel,
networks: client.networks,
networks: networks,
token: tokenToSend
});
};
@ -434,7 +447,7 @@ function performAuthentication(data) {
const socket = this;
let client;
const finalInit = () => initializeClient(socket, client, data.token || null);
const finalInit = () => initializeClient(socket, client, data.token || null, data.lastMessage || -1);
const initClient = () => {
client.ip = getClientIp(socket.request);