Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Fix app crash & background ping notification (#139)
Browse files Browse the repository at this point in the history
* Fix app crash & background ping notification

* Update src/notification.ts

Co-authored-by: Killian Mannarelli <[email protected]>

Co-authored-by: Killian Mannarelli <[email protected]>
  • Loading branch information
celian-rib and killian-mannarelli authored Sep 7, 2022
1 parent 154ddc5 commit 2ef1a0f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 30 deletions.
5 changes: 4 additions & 1 deletion src/interfaces/dropy.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { SimplifiedUser } from './user.interface';

export type DropyAround = Pick<Dropy, 'id' | 'creationDate' | 'latitude' | 'longitude'>;

export type SimplifiedDropy = Pick<Dropy, 'id' | 'mediaUrl' | 'mediaType' | 'latitude' | 'longitude' | 'creationDate' | 'retrieveDate'>;
export type SimplifiedDropy = Pick<Dropy, 'id' | 'mediaUrl' | 'mediaType' | 'latitude' | 'longitude' | 'creationDate' | 'retrieveDate'> & {
emitter: { id: number };
retriever: { id: number };
};

export type DropyWithUsers = SimplifiedDropy & {
emitter: SimplifiedUser;
Expand Down
29 changes: 19 additions & 10 deletions src/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
30 changes: 20 additions & 10 deletions src/services/api/services/dropy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dropy> {
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<Dropy> {
const dropy = await client.dropy.findUnique({ where: { id: dropyId } });
Expand Down Expand Up @@ -53,13 +48,14 @@ export async function getDropy(dropyId: number): Promise<DropyWithUsers> {
};
}

export async function userEmittedDropies(user: User): Promise<SimplifiedDropy[]> {
export async function userEmittedDropies(user: User): Promise<DropyWithUsers[]> {
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,
Expand All @@ -68,10 +64,24 @@ export async function userEmittedDropies(user: User): Promise<SimplifiedDropy[]>
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<DropyWithUsers[]> {
Expand Down
6 changes: 3 additions & 3 deletions src/services/api/services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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',
});
Expand Down
32 changes: 27 additions & 5 deletions src/services/socket/services/chat.socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -172,7 +182,17 @@ export async function getLastMessage(conversationId: number): Promise<UserMessag
});

return {
content: lastMessage.content ?? lastMessage.dropy,
content: lastMessage.content ?? {
id: lastMessage.dropy.id,
latitude: lastMessage.dropy.latitude,
longitude: lastMessage.dropy.longitude,
mediaUrl: lastMessage.dropy.mediaUrl,
retrieveDate: lastMessage.dropy.retrieveDate,
mediaType: lastMessage.dropy.mediaType,
creationDate: lastMessage.dropy.creationDate,
emitter: { id: lastMessage.dropy.emitterId },
retriever: { id: lastMessage.dropy.retrieverId },
},
date: lastMessage.date,
id: lastMessage.id,
read: lastMessage.read,
Expand Down Expand Up @@ -203,12 +223,14 @@ export async function getAllMessages(conversationId: number): Promise<UserMessag
return chatMessages.map(message => ({
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,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/geolocation.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { DropyAround } from '@/interfaces/dropy.interface';

export const GEOHASH_SIZE = 32;

export async function findDropiesByGeohash(user: User, zones: string[]): Promise<DropyAround[]> {
export async function findDropiesByGeohash(user: User, zones: string[], excludeUserDropies = false): Promise<DropyAround[]> {
const dropiesByGeohash = await client.dropy.findMany({
where: {
geohash: { in: zones },
retrieverId: null,
emitterId: excludeUserDropies ? { not: user.id } : undefined,
},
include: { emitter: true },
});
Expand Down

0 comments on commit 2ef1a0f

Please sign in to comment.