Skip to content

Commit ffecf7a

Browse files
authored
Fix/notification response (#181)
* fix(notifications): reply original tweet data * fix(notifications): reply original tweet data * fix(notifications): reply original tweet data * fix(notifications): reply original tweet data * fix(notifications): extra data * fix(notifications): extra data * fix(notifications): push notification format * fix(notifications): push notification format * fix(notifications): push notification format * fix(notifications): push notification format * fix(notifications): push notification format * fix(notifications): push notification format * fix(notifications): push notification format * fix(notifications): push notification format * fix(notifications): push notification format * fix(notifications): push notification format * fix(notifications): push notification format
1 parent c89a64b commit ffecf7a

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

dump.rdb

47.9 KB
Binary file not shown.

src/expo/expo.service.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ describe('FCMService', () => {
379379
);
380380
});
381381

382-
it('should return false when payload is invalid', async () => {
382+
it('should send notification with fallback content when payload is missing fields', async () => {
383383
const payload = {
384384
// Missing required fields
385385
tweet_id: 'tweet-123',
@@ -391,7 +391,15 @@ describe('FCMService', () => {
391391
payload
392392
);
393393

394-
expect(result).toBe(false);
394+
expect(result).toBe(true);
395+
expect(mock_expo_instance.sendPushNotificationsAsync).toHaveBeenCalledWith(
396+
expect.arrayContaining([
397+
expect.objectContaining({
398+
title: 'Liked by Someone',
399+
body: 'your post',
400+
}),
401+
])
402+
);
395403
});
396404

397405
it('should return false and warn if user has no FCM token', async () => {

src/expo/expo.service.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,49 +128,65 @@ export class FCMService {
128128
type: NotificationType,
129129
payload: any
130130
): { title: string; body: string; data?: any } {
131+
console.log(payload);
131132
switch (type) {
132133
case NotificationType.FOLLOW:
133134
return {
134135
title: 'yapper',
135-
body: `@${payload.follower_username} followed you!`,
136+
body: `@${payload.follower_username || 'Someone'} followed you!`,
136137
data: { username: payload.follower_id },
137138
};
138139
case NotificationType.MENTION:
139140
return {
140-
title: `Mentioned by ${payload.mentioned_by?.name}:`,
141-
body: payload.tweet?.content,
142-
data: { tweet_id: payload.tweet?.id },
141+
title: `Mentioned by ${payload.mentioned_by?.name || 'Someone'}:`,
142+
body: payload.tweet?.content || 'You were mentioned in a post',
143+
data: { tweet_id: payload.tweet?.id || payload.tweet?.tweet_id },
143144
};
144145
case NotificationType.REPLY:
145146
return {
146-
title: `${payload.replier?.name} replied:`,
147-
body: payload.reply_tweet?.content,
148-
data: { tweet_id: payload.reply_tweet?.id },
147+
title: `${payload.replier?.name || 'Someone'} replied:`,
148+
body: payload.reply_tweet?.content || 'replied to your post',
149+
data: { tweet_id: payload.reply_tweet?.id || payload.reply_tweet?.tweet_id },
149150
};
150151
case NotificationType.QUOTE:
151152
return {
152153
title: 'yapper',
153-
body: `@${payload.quoted_by?.username} quoted your post and said: ${
154-
payload.quote?.content || ''
154+
body: `@${payload.quoted_by?.username || 'Someone'} quoted your post${
155+
payload.quote?.content ? ` and said: ${payload.quote.content}` : ''
155156
}`,
156-
data: { tweet_id: payload.quote_tweet?.id },
157+
data: { tweet_id: payload.quote?.id || payload.quote?.tweet_id },
157158
};
158-
case NotificationType.LIKE:
159+
case NotificationType.LIKE: {
160+
// Handle both array format (likers/tweets) and singular format (liker/tweet)
161+
const liker_name = payload.liker?.name || payload.likers?.[0]?.name || 'Someone';
162+
const liked_tweet_content =
163+
payload.tweet?.content || payload.tweets?.[0]?.content || 'your post';
164+
const liked_tweet_id =
165+
payload.tweet?.tweet_id || payload.tweet?.id || payload.tweets?.[0]?.id;
159166
return {
160-
title: `Liked by ${payload.likers[0].name}`,
161-
body: payload.tweets[0].content,
162-
data: { tweet_id: payload.tweets[0].id },
167+
title: `Liked by ${liker_name}`,
168+
body: liked_tweet_content,
169+
data: { tweet_id: liked_tweet_id },
163170
};
164-
case NotificationType.REPOST:
171+
}
172+
case NotificationType.REPOST: {
173+
// Handle both array format (reposters/tweets) and singular format (reposter/tweet)
174+
const reposter_name =
175+
payload.reposter?.name || payload.reposters?.[0]?.name || 'Someone';
176+
const reposted_tweet_content =
177+
payload.tweet?.content || payload.tweets?.[0]?.content || 'your post';
178+
const reposted_tweet_id =
179+
payload.tweet?.tweet_id || payload.tweet?.id || payload.tweets?.[0]?.id;
165180
return {
166-
title: `Reposted by ${payload.reposters[0].name}:`,
167-
body: payload.tweets[0].content,
168-
data: { tweet_id: payload.tweets[0].id },
181+
title: `Reposted by ${reposter_name}:`,
182+
body: reposted_tweet_content,
183+
data: { tweet_id: reposted_tweet_id },
169184
};
185+
}
170186
case NotificationType.MESSAGE:
171187
return {
172-
title: payload.sender?.name,
173-
body: payload.message?.content,
188+
title: payload.sender?.name || 'New Message',
189+
body: payload.message?.content || 'You have a new message',
174190
};
175191
default:
176192
return {

0 commit comments

Comments
 (0)