Browse Source

debounce tribe push notifications

feature/dockerfile-arm
Evan Feenstra 4 years ago
parent
commit
41634095f0
  1. 36
      api/hub.ts
  2. 39
      dist/api/hub.js
  3. 2
      dist/api/hub.js.map

36
api/hub.ts

@ -223,12 +223,6 @@ const sendNotification = async (chat, name, type) => {
const unseenMessages = await models.Message.count({ where: { sender: { [Op.ne]: owner.id }, seen: false } })
// if(type==='message' && chat.type==constants.chat_types.tribe){
// debounce(()=>{
// },)
// }
const params = {
device_id: owner.deviceId,
notification: {
@ -238,6 +232,23 @@ const sendNotification = async (chat, name, type) => {
}
}
if(type==='message' && chat.type==constants.chat_types.tribe){
debounce(()=>{
const count = tribeCounts[chat.id]?tribeCounts[chat.id]+' ':''
triggerNotification({
device_id: owner.deviceId,
notification: {
chat_id: chat.id, badge: unseenMessages,
message: `You have ${count}new messages in ${chat.name}`
}
})
}, chat.id, 30000)
} else {
triggerNotification(params)
}
}
function triggerNotification(params){
fetch("https://hub.sphinx.chat/api/v1/nodes/notify", {
method: 'POST' ,
body: JSON.stringify(params),
@ -267,3 +278,16 @@ export {
// clearTimeout(inDebounce)
// inDebounce = setTimeout(() => func.apply(context, args), delay)
// }
const bounceTimeouts={}
const tribeCounts = {}
function debounce(func, id, delay) {
const context = this
const args = arguments
if(bounceTimeouts[id]) clearTimeout(bounceTimeouts[id])
if(tribeCounts[id]||tribeCounts[id]===0) tribeCounts[id]+=1
bounceTimeouts[id] = setTimeout(() => {
tribeCounts[id]=0
func.apply(context, args)
}, delay)
}

39
dist/api/hub.js

@ -214,10 +214,6 @@ const sendNotification = (chat, name, type) => __awaiter(void 0, void 0, void 0,
return;
}
const unseenMessages = yield models_1.models.Message.count({ where: { sender: { [sequelize_1.Op.ne]: owner.id }, seen: false } });
// if(type==='message' && chat.type==constants.chat_types.tribe){
// debounce(()=>{
// },)
// }
const params = {
device_id: owner.deviceId,
notification: {
@ -226,6 +222,24 @@ const sendNotification = (chat, name, type) => __awaiter(void 0, void 0, void 0,
badge: unseenMessages
}
};
if (type === 'message' && chat.type == constants.chat_types.tribe) {
debounce(() => {
const count = tribeCounts[chat.id] ? tribeCounts[chat.id] + ' ' : '';
triggerNotification({
device_id: owner.deviceId,
notification: {
chat_id: chat.id, badge: unseenMessages,
message: `You have ${count}new messages in ${chat.name}`
}
});
}, chat.id, 30000);
}
else {
triggerNotification(params);
}
});
exports.sendNotification = sendNotification;
function triggerNotification(params) {
fetch("https://hub.sphinx.chat/api/v1/nodes/notify", {
method: 'POST',
body: JSON.stringify(params),
@ -235,8 +249,7 @@ const sendNotification = (chat, name, type) => __awaiter(void 0, void 0, void 0,
.then(json => {
// console.log('[hub notification]', json)
});
});
exports.sendNotification = sendNotification;
}
// let inDebounce
// function debounce(func, delay) {
// const context = this
@ -244,4 +257,18 @@ exports.sendNotification = sendNotification;
// clearTimeout(inDebounce)
// inDebounce = setTimeout(() => func.apply(context, args), delay)
// }
const bounceTimeouts = {};
const tribeCounts = {};
function debounce(func, id, delay) {
const context = this;
const args = arguments;
if (bounceTimeouts[id])
clearTimeout(bounceTimeouts[id]);
if (tribeCounts[id] || tribeCounts[id] === 0)
tribeCounts[id] += 1;
bounceTimeouts[id] = setTimeout(() => {
tribeCounts[id] = 0;
func.apply(context, args);
}, delay);
}
//# sourceMappingURL=hub.js.map

2
dist/api/hub.js.map

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save