Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve user details #43

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(core): create distance utils
pchmn committed Jul 23, 2023
commit 8049dc32bce9df0166a06cc88dc324b9859ab592
6 changes: 2 additions & 4 deletions apps/functions/src/i18n/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const translations = {
en: {
from: (distanceInKm: number) =>
`From ${distanceInKm < 1 ? Math.round(distanceInKm * 1000) + 'm' : distanceInKm + 'km'} away`,
from: (distance: string) => `From ${distance} away`,
},
fr: {
from: (distanceInKm: number) =>
`À ${distanceInKm < 1 ? Math.round(distanceInKm * 1000) + 'm' : distanceInKm + 'km'}`,
from: (distance: string) => `À ${distance}`,
},
};

21 changes: 13 additions & 8 deletions apps/functions/src/sendMessage/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Devices, FunctionName, FunctionParams, FunctionValidation, Message, User } from '@kuzpot/core';
import {
Devices,
distanceBetween,
formatDistance,
FunctionName,
FunctionParams,
FunctionValidation,
Message,
User,
} from '@kuzpot/core';
import { Logtail } from '@logtail/node';
import { initializeApp } from 'firebase-admin/app';
import { FieldValue, getFirestore } from 'firebase-admin/firestore';
import { getMessaging, TokenMessage } from 'firebase-admin/messaging';
import { HttpsError } from 'firebase-functions/v2/https';
import { CallableRequest } from 'firebase-functions/v2/https';
import { distanceBetween } from 'geofire-common';

import { getLocaleWithouRegionCode, i18n } from '../i18n';

@@ -30,10 +38,7 @@ export async function sendMessage(req: CallableRequest<FunctionParams['sendMessa
const receiverRef = db.collection('users').doc(to);
const receiver = (await receiverRef.get()).data() as User;

const distance = distanceBetween(
[sender.geopoint.latitude, sender.geopoint.longitude],
[receiver.geopoint.latitude, receiver.geopoint.longitude]
);
const distance = distanceBetween(sender.geopoint, receiver.geopoint);

const pushNotificationsResponse = await sendPushNotifications(to, {
message: { ...message, key: messageKey },
@@ -152,7 +157,7 @@ async function sendPushNotifications(
)) {
const locale = recipientDevices[installationId].language;
const title = `${message.emoji} ${getMessageTranslation(locale, message)}`;
const body = i18n(locale).from(distance);
const body = i18n(locale).from(formatDistance(distance));
const token = recipientDevices[installationId].pushToken;

if (!token) {
@@ -165,7 +170,7 @@ async function sendPushNotifications(
pushMessages.push({
data: {
message: message.key,
distance: distance.toString(),
distance: formatDistance(distance),
},
notification: {
title,
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './cloud-functions';
export * from './models';
export * from './utils';
11 changes: 7 additions & 4 deletions packages/core/src/models/user.ts
Original file line number Diff line number Diff line change
@@ -22,12 +22,15 @@ export interface MessageReceived extends MessageHistory {
export interface MessageSent extends MessageHistory {
to: string;
}

export interface GeoPoint {
latitude: number;
longitude: number;
}
export interface User {
geohash: string;
geopoint: {
latitude: number;
longitude: number;
};
geopoint: GeoPoint;
name: string;
status: 'online' | 'offline';
messageStatistics: {
receivedCount: {
17 changes: 17 additions & 0 deletions packages/core/src/utils/distance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { distanceBetween as geofireDistanceBetween } from 'geofire-common';

import { GeoPoint } from '../models';

export function distanceBetween(from: GeoPoint, to: GeoPoint) {
return geofireDistanceBetween([from.latitude, from.longitude], [to.latitude, to.longitude]);
}

export function formatDistance(distance: number) {
return distance < 1 ? Math.round(distance * 1000) + 'm' : Math.round(distance) + 'km';
}

export function formatDistanceBetween(from: GeoPoint, to: GeoPoint) {
const distance = distanceBetween(from, to);

return formatDistance(distance);
}
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './distance';