From 2ef1a0ff0617599f9332cf33cdd873598efc62c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9lian=20Riboulet?= Date: Wed, 7 Sep 2022 21:43:32 +0200 Subject: [PATCH] Fix app crash & background ping notification (#139) * Fix app crash & background ping notification * Update src/notification.ts Co-authored-by: Killian Mannarelli <83612329+killian-mannarelli@users.noreply.github.com> Co-authored-by: Killian Mannarelli <83612329+killian-mannarelli@users.noreply.github.com> --- src/interfaces/dropy.interface.ts | 5 ++- src/notification.ts | 29 +++++++++++------ src/services/api/services/dropy.service.ts | 30 +++++++++++------ src/services/api/services/users.service.ts | 6 ++-- .../socket/services/chat.socket.service.ts | 32 ++++++++++++++++--- src/utils/geolocation.utils.ts | 3 +- 6 files changed, 75 insertions(+), 30 deletions(-) diff --git a/src/interfaces/dropy.interface.ts b/src/interfaces/dropy.interface.ts index d459561..f317ae9 100644 --- a/src/interfaces/dropy.interface.ts +++ b/src/interfaces/dropy.interface.ts @@ -3,7 +3,10 @@ import { SimplifiedUser } from './user.interface'; export type DropyAround = Pick; -export type SimplifiedDropy = Pick; +export type SimplifiedDropy = Pick & { + emitter: { id: number }; + retriever: { id: number }; +}; export type DropyWithUsers = SimplifiedDropy & { emitter: SimplifiedUser; diff --git a/src/notification.ts b/src/notification.ts index adfc5e2..a4dc10a 100644 --- a/src/notification.ts +++ b/src/notification.ts @@ -54,14 +54,23 @@ export async function sendPushNotification(notification: Notification | BatchedN tokens.push(single.user.deviceToken); } - const results = await push.send(tokens, { - topic: 'com.dropy.project', - title: notification.title, - body: notification.body, - sound: notification.sound ?? 'default', - badge: notification.badge, - contentAvailable: true, - clickAction: notification.payload ? JSON.stringify(notification.payload) : undefined, - }); - return results; + try { + return await push.send(tokens, { + topic: 'com.dropy.project', + title: notification.title, + body: notification.body, + sound: notification.sound ?? 'default', + badge: notification.badge, + contentAvailable: true, + clickAction: notification.payload ? JSON.stringify(notification.payload) : undefined, + }); + } catch (error) { + console.error('PUSH NOTIFICATION ERROR', error, { + production: process.env.NODE_ENV === 'production', + apnKey: apnKey != undefined, + apnKeyId: process.env.APN_KEYID != undefined, + apnTeamId: process.env.APN_TEAMID != undefined, + fcmKey: process.env.FCM_KEY != undefined, + }); + } } diff --git a/src/services/api/services/dropy.service.ts b/src/services/api/services/dropy.service.ts index 7c6c5f8..85f85fe 100644 --- a/src/services/api/services/dropy.service.ts +++ b/src/services/api/services/dropy.service.ts @@ -2,12 +2,7 @@ import client from '@/client'; import { HttpException } from '@exceptions/HttpException'; import { Dropy, MediaType, User } from '@prisma/client'; import { deleteContent } from '@/utils/content.utils'; -import { DropyWithUsers, SimplifiedDropy } from '@/interfaces/dropy.interface'; - -export async function createDropy(user: User, latitude, longitude): Promise { - const dropy = client.dropy.create({ data: { emitterId: user.id, latitude, longitude } }); - return dropy; -} +import { DropyWithUsers } from '@/interfaces/dropy.interface'; export async function getDropyById(dropyId: number): Promise { const dropy = await client.dropy.findUnique({ where: { id: dropyId } }); @@ -53,13 +48,14 @@ export async function getDropy(dropyId: number): Promise { }; } -export async function userEmittedDropies(user: User): Promise { +export async function userEmittedDropies(user: User): Promise { const userDropies = await client.dropy.findMany({ where: { emitterId: user.id }, orderBy: { creationDate: 'desc' }, + include: { emitter: true, retriever: true }, }); - const simplifiedDropies = userDropies.map(dropy => { + return userDropies.map(dropy => { return { id: dropy.id, mediaType: dropy.mediaType, @@ -68,10 +64,24 @@ export async function userEmittedDropies(user: User): Promise retrieveDate: dropy.retrieveDate, latitude: dropy.latitude, longitude: dropy.longitude, + conversationId: dropy.chatConversationId, + emitter: { + id: user.id, + username: user.username, + avatarUrl: user.avatarUrl, + displayName: user.displayName, + }, + retriever: + dropy.retrieverId != null + ? { + id: dropy.retrieverId, + username: dropy.retriever.username, + avatarUrl: dropy.retriever.avatarUrl, + displayName: dropy.retriever.displayName, + } + : null, }; }); - - return simplifiedDropies; } export async function userRetrievedDropies(user: User): Promise { diff --git a/src/services/api/services/users.service.ts b/src/services/api/services/users.service.ts index fc71037..2c030a7 100644 --- a/src/services/api/services/users.service.ts +++ b/src/services/api/services/users.service.ts @@ -35,7 +35,7 @@ export async function backgroundGeolocationPing(user: User, latitude: number, lo return; } - const dropiesAround = await findDropiesByGeohash(user, [pingGeohash]); + const dropiesAround = await findDropiesByGeohash(user, [pingGeohash], true); const canSendNotification = dropiesAround.length > 0; @@ -48,9 +48,9 @@ export async function backgroundGeolocationPing(user: User, latitude: number, lo console.log(`Send notification : ${canSendNotification}`); console.log('-------------------'); - await sendPushNotification({ + sendPushNotification({ user, - title: `${canSendNotification} Drops found near your position!`, + title: `${dropiesAround.length} Drops found near your position!`, body: `Open the app to see ${dropiesAround.length > 1 ? 'them' : 'it'}!`, sound: 'dropy_sound.mp3', }); diff --git a/src/services/socket/services/chat.socket.service.ts b/src/services/socket/services/chat.socket.service.ts index 3c4f553..6264ba2 100644 --- a/src/services/socket/services/chat.socket.service.ts +++ b/src/services/socket/services/chat.socket.service.ts @@ -38,7 +38,17 @@ export async function getMessages(conversationId: number, offset: number, limit: }); return chatMessages.reverse().map(message => ({ - content: message.content ?? message.dropy, + content: message.content ?? { + id: message.dropy.id, + latitude: message.dropy.latitude, + longitude: message.dropy.longitude, + mediaUrl: message.dropy.mediaUrl, + retrieveDate: message.dropy.retrieveDate, + mediaType: message.dropy.mediaType, + creationDate: message.dropy.creationDate, + emitter: { id: message.dropy.emitterId }, + retriever: { id: message.dropy.retrieverId }, + }, date: message.date, read: message.read, id: message.id, @@ -172,7 +182,17 @@ export async function getLastMessage(conversationId: number): Promise ({ content: message.content ?? { id: message.dropy.id, - mediaUrl: message.dropy.mediaUrl, - mediaType: message.dropy.mediaType, - creationDate: message.dropy.creationDate, latitude: message.dropy.latitude, longitude: message.dropy.longitude, + mediaUrl: message.dropy.mediaUrl, retrieveDate: message.dropy.retrieveDate, + mediaType: message.dropy.mediaType, + creationDate: message.dropy.creationDate, + emitter: { id: message.dropy.emitterId }, + retriever: { id: message.dropy.retrieverId }, }, date: message.date, read: message.read, diff --git a/src/utils/geolocation.utils.ts b/src/utils/geolocation.utils.ts index ddc64de..067b5e9 100644 --- a/src/utils/geolocation.utils.ts +++ b/src/utils/geolocation.utils.ts @@ -4,11 +4,12 @@ import { DropyAround } from '@/interfaces/dropy.interface'; export const GEOHASH_SIZE = 32; -export async function findDropiesByGeohash(user: User, zones: string[]): Promise { +export async function findDropiesByGeohash(user: User, zones: string[], excludeUserDropies = false): Promise { const dropiesByGeohash = await client.dropy.findMany({ where: { geohash: { in: zones }, retrieverId: null, + emitterId: excludeUserDropies ? { not: user.id } : undefined, }, include: { emitter: true }, });