Browse Source

fix badge count

push-params
Evan Feenstra 5 years ago
parent
commit
f7d7e53292
  1. 1
      api/helpers.ts
  2. 20
      api/hub.ts
  3. 1
      dist/api/helpers.js
  4. 2
      dist/api/helpers.js.map
  5. 17
      dist/api/hub.js
  6. 2
      dist/api/hub.js.map

1
api/helpers.ts

@ -163,7 +163,6 @@ export async function parseReceiveParams(payload) {
const owner = await models.Contact.findOne({ where: { isOwner: true } })
if (isConversation) {
sender = await findOrCreateContactByPubkey(sender_pub_key)
console.log("CREATED CONTACT AUTOMATICALLY",sender&&sender.id)
chat = await findOrCreateChatByUUID(
chat_uuid, [parseInt(owner.id), parseInt(sender.id)]
)

20
api/hub.ts

@ -204,8 +204,6 @@ const sendNotification = async (chat, name, type:NotificationType) => {
message += ` on ${chat.name}`
}
console.log('[send notification]', { chat_id:chat.id, message })
const owner = await models.Contact.findOne({ where: { isOwner: true }})
if (!owner.deviceId) {
@ -215,13 +213,10 @@ const sendNotification = async (chat, name, type:NotificationType) => {
const device_id = owner.deviceId
const isIOS = device_id.length===64
const isAndroid = !isIOS
let unseenMessages=await models.Message.count({ where: { sender: { [Op.ne]: owner.id }, seen: false } })
const params:{[k:string]:any} = {device_id}
const notification:{[k:string]:any} = {
chat_id: chat.id,
badge: unseenMessages,
sound: ''
}
if(type!=='badge' && !chat.isMuted) {
@ -236,14 +231,23 @@ const sendNotification = async (chat, name, type:NotificationType) => {
debounce(()=>{
const count = tribeCounts[chat.id]?tribeCounts[chat.id]+' ':''
params.notification.message = `You have ${count}new messages in ${chat.name}`
triggerNotification(params)
finalNotification(owner.id, params)
}, chat.id, 30000)
} else {
triggerNotification(params)
finalNotification(owner.id, params)
}
}
async function finalNotification(ownerID: number, params:{[k:string]:any}){
if(params.notification.message) {
console.log('[send notification]', params.notification)
}
let unseenMessages = await models.Message.count({ where: { sender: { [Op.ne]: ownerID }, seen: false } })
params.notification.badge = unseenMessages
triggerNotification(params)
}
function triggerNotification(params){
function triggerNotification(params:{[k:string]:any}){
fetch("https://hub.sphinx.chat/api/v1/nodes/notify", {
method: 'POST' ,
body: JSON.stringify(params),

1
dist/api/helpers.js

@ -175,7 +175,6 @@ function parseReceiveParams(payload) {
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
if (isConversation) {
sender = yield findOrCreateContactByPubkey(sender_pub_key);
console.log("CREATED CONTACT AUTOMATICALLY", sender && sender.id);
chat = yield findOrCreateChatByUUID(chat_uuid, [parseInt(owner.id), parseInt(sender.id)]);
if (sender.fromGroup) { // if a private msg received, update the contact
yield sender.update({ fromGroup: false });

2
dist/api/helpers.js.map

File diff suppressed because one or more lines are too long

17
dist/api/hub.js

@ -198,7 +198,6 @@ const sendNotification = (chat, name, type) => __awaiter(void 0, void 0, void 0,
if (type === 'message' && chat.type == constants.chat_types.group && chat.name && chat.name.length) {
message += ` on ${chat.name}`;
}
console.log('[send notification]', { chat_id: chat.id, message });
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
if (!owner.deviceId) {
console.log('[send notification] skipping. owner.deviceId not set.');
@ -207,11 +206,9 @@ const sendNotification = (chat, name, type) => __awaiter(void 0, void 0, void 0,
const device_id = owner.deviceId;
const isIOS = device_id.length === 64;
const isAndroid = !isIOS;
let unseenMessages = yield models_1.models.Message.count({ where: { sender: { [sequelize_1.Op.ne]: owner.id }, seen: false } });
const params = { device_id };
const notification = {
chat_id: chat.id,
badge: unseenMessages,
sound: ''
};
if (type !== 'badge' && !chat.isMuted) {
@ -227,14 +224,24 @@ const sendNotification = (chat, name, type) => __awaiter(void 0, void 0, void 0,
debounce(() => {
const count = tribeCounts[chat.id] ? tribeCounts[chat.id] + ' ' : '';
params.notification.message = `You have ${count}new messages in ${chat.name}`;
triggerNotification(params);
finalNotification(owner.id, params);
}, chat.id, 30000);
}
else {
triggerNotification(params);
finalNotification(owner.id, params);
}
});
exports.sendNotification = sendNotification;
function finalNotification(ownerID, params) {
return __awaiter(this, void 0, void 0, function* () {
if (params.notification.message) {
console.log('[send notification]', params.notification);
}
let unseenMessages = yield models_1.models.Message.count({ where: { sender: { [sequelize_1.Op.ne]: ownerID }, seen: false } });
params.notification.badge = unseenMessages;
triggerNotification(params);
});
}
function triggerNotification(params) {
fetch("https://hub.sphinx.chat/api/v1/nodes/notify", {
method: 'POST',

2
dist/api/hub.js.map

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