Skip to content

Commit 30d894e

Browse files
authored
Fix/notification response (#193)
* 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 * 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): not important * fix(notifications): send quote notification only if tweet owner is already mentioned in it * fix(notifications): fix mention in push notification * fix(notifications): fix mention in push notification * fix(notifications): fix mention in push notification * fix(notifications): fix mention in push notification
1 parent 550b4c1 commit 30d894e

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

src/expo/expo.service.spec.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ describe('FCMService', () => {
3838
(Expo as unknown as jest.Mock).mockImplementation(() => mock_expo_instance);
3939
(Expo.isExpoPushToken as unknown as jest.Mock) = jest.fn().mockReturnValue(true);
4040

41+
const mock_query_builder = {
42+
where: jest.fn().mockReturnThis(),
43+
select: jest.fn().mockReturnThis(),
44+
getOne: jest.fn().mockResolvedValue(mock_user),
45+
};
46+
4147
mock_user_repository = {
4248
findOne: jest.fn().mockResolvedValue(mock_user),
4349
update: jest.fn().mockResolvedValue({ affected: 1 }),
50+
createQueryBuilder: jest.fn().mockReturnValue(mock_query_builder),
4451
};
4552

4653
const module: TestingModule = await Test.createTestingModule({
@@ -87,7 +94,6 @@ describe('FCMService', () => {
8794
sound: 'default',
8895
title: notification.title,
8996
body: notification.body,
90-
subtitle: notification.body,
9197
data: data,
9298
},
9399
]);
@@ -227,18 +233,14 @@ describe('FCMService', () => {
227233
payload
228234
);
229235

230-
expect(mock_user_repository.findOne).toHaveBeenCalledWith({
231-
where: { id: 'user-123' },
232-
select: ['fcm_token'],
233-
});
236+
expect(mock_user_repository.createQueryBuilder).toHaveBeenCalledWith('user');
234237

235238
expect(mock_expo_instance.sendPushNotificationsAsync).toHaveBeenCalledWith([
236239
{
237240
to: 'ExponentPushToken[mock-token-123]',
238241
sound: 'default',
239242
title: 'Liked by John Doe',
240243
body: 'Tweet content',
241-
subtitle: 'Tweet content',
242244
data: {
243245
tweet_id: 'tweet-123',
244246
},
@@ -306,7 +308,7 @@ describe('FCMService', () => {
306308
expect(mock_expo_instance.sendPushNotificationsAsync).toHaveBeenCalledWith(
307309
expect.arrayContaining([
308310
expect.objectContaining({
309-
title: 'yapper',
311+
title: 'Yapper',
310312
body: '@alice quoted your post and said: Quote content',
311313
}),
312314
])
@@ -373,7 +375,7 @@ describe('FCMService', () => {
373375
expect(mock_expo_instance.sendPushNotificationsAsync).toHaveBeenCalledWith(
374376
expect.arrayContaining([
375377
expect.objectContaining({
376-
title: 'yapper',
378+
title: 'Yapper',
377379
body: '@emma followed you!',
378380
}),
379381
])
@@ -404,7 +406,12 @@ describe('FCMService', () => {
404406
});
405407

406408
it('should return false and warn if user has no FCM token', async () => {
407-
mock_user_repository.findOne.mockResolvedValue({ id: 'user-123', fcm_token: null });
409+
const mock_query_builder = {
410+
where: jest.fn().mockReturnThis(),
411+
select: jest.fn().mockReturnThis(),
412+
getOne: jest.fn().mockResolvedValue({ id: 'user-123', fcm_token: null }),
413+
};
414+
mock_user_repository.createQueryBuilder.mockReturnValue(mock_query_builder);
408415

409416
const logger_spy = jest.spyOn(service['logger'], 'warn');
410417

@@ -420,7 +427,12 @@ describe('FCMService', () => {
420427
});
421428

422429
it('should return false and warn if user not found', async () => {
423-
mock_user_repository.findOne.mockResolvedValue(null);
430+
const mock_query_builder = {
431+
where: jest.fn().mockReturnThis(),
432+
select: jest.fn().mockReturnThis(),
433+
getOne: jest.fn().mockResolvedValue(null),
434+
};
435+
mock_user_repository.createQueryBuilder.mockReturnValue(mock_query_builder);
424436

425437
const logger_spy = jest.spyOn(service['logger'], 'warn');
426438

src/expo/expo.service.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export class FCMService {
3737
sound: 'default',
3838
title: notification?.title,
3939
body: notification?.body,
40-
subtitle: notification?.body,
4140
data: data,
4241
};
4342

@@ -91,10 +90,11 @@ export class FCMService {
9190
payload: any
9291
): Promise<boolean> {
9392
try {
94-
const user = await this.user_repository.findOne({
95-
where: { id: user_id },
96-
select: ['fcm_token'],
97-
});
93+
const user = await this.user_repository
94+
.createQueryBuilder('user')
95+
.where('user.id = :id', { id: user_id })
96+
.select(['user.fcm_token'])
97+
.getOne();
9898

9999
if (!user?.fcm_token) {
100100
this.logger.warn(`No FCM token found for user ${user_id}`);
@@ -126,16 +126,23 @@ export class FCMService {
126126
switch (type) {
127127
case NotificationType.FOLLOW:
128128
return {
129-
title: 'yapper',
129+
title: 'Yapper',
130130
body: `@${payload.follower_username || 'Someone'} followed you!`,
131131
data: { user_id: payload.follower_id },
132132
};
133-
case NotificationType.MENTION:
133+
case NotificationType.MENTION: {
134+
let content = payload.tweet?.content;
135+
const mentions = payload.tweet?.mentions;
136+
if (content && mentions)
137+
mentions.forEach((mention, index) => {
138+
content = content.replace(`\u200B$(${index})\u200C`, `@${mention}`);
139+
});
134140
return {
135141
title: `Mentioned by ${payload.mentioned_by?.name || 'Someone'}:`,
136-
body: payload.tweet?.content || 'You were mentioned in a post',
142+
body: content || 'You were mentioned in a post',
137143
data: { tweet_id: payload.tweet?.id || payload.tweet?.tweet_id },
138144
};
145+
}
139146
case NotificationType.REPLY:
140147
return {
141148
title: `${payload.replier?.name || 'Someone'} replied:`,
@@ -144,7 +151,7 @@ export class FCMService {
144151
};
145152
case NotificationType.QUOTE:
146153
return {
147-
title: 'yapper',
154+
title: 'Yapper',
148155
body: `@${payload.quoted_by?.username || 'Someone'} quoted your post${
149156
payload.quote?.content ? ` and said: ${payload.quote.content}` : ''
150157
}`,
@@ -181,7 +188,7 @@ export class FCMService {
181188
};
182189
default:
183190
return {
184-
title: 'yapper',
191+
title: 'Yapper',
185192
body: 'You have a new notification',
186193
data: {},
187194
};

src/user/entities/user.entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class User {
8888
@Column({ type: 'int', default: 0 })
8989
following: number;
9090

91-
@Column({ name: 'fcm_token', type: 'varchar', unique: true, nullable: true })
91+
@Column({ name: 'fcm_token', type: 'varchar', unique: true, nullable: true, select: false })
9292
fcm_token?: string | null;
9393

9494
@OneToMany(() => Hashtag, (hashtags) => hashtags.created_by, { onDelete: 'CASCADE' })

0 commit comments

Comments
 (0)