From 3769837765f6e4b17ad7f7fbedcec50b5c81e096 Mon Sep 17 00:00:00 2001 From: Tom Whale Date: Tue, 3 Apr 2018 21:25:23 +0100 Subject: [PATCH 1/9] #2645: Only update the threadLast Seen if its later than the current one --- athena/queues/track-user-thread-last-seen.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/athena/queues/track-user-thread-last-seen.js b/athena/queues/track-user-thread-last-seen.js index ebef7951b0..336092c976 100644 --- a/athena/queues/track-user-thread-last-seen.js +++ b/athena/queues/track-user-thread-last-seen.js @@ -29,6 +29,17 @@ export default async (job: Job) => { const record = await getUsersThread(userId, threadId); if (record) { + if (record.lastSeen > date) { + debug( + `old lastSeen ${ + record.lastSeen + } is later than new lastSeen ${date}, not running job:\nuserId: ${userId}\nthreadId: ${threadId}\ntimestamp: ${new Date( + timestamp + ).toString()}` + ); + return; + } + debug( `existing usersThread, updating usersThreads#${record.id} with lastSeen` ); From 9237ee4446fe8de47db8bd6900dc1498eaa24d03 Mon Sep 17 00:00:00 2001 From: Tom Whale Date: Tue, 3 Apr 2018 22:05:16 +0100 Subject: [PATCH 2/9] Fix flow --- athena/queues/track-user-thread-last-seen.js | 4 ++-- shared/types.js | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/athena/queues/track-user-thread-last-seen.js b/athena/queues/track-user-thread-last-seen.js index 336092c976..5153c0a464 100644 --- a/athena/queues/track-user-thread-last-seen.js +++ b/athena/queues/track-user-thread-last-seen.js @@ -33,13 +33,13 @@ export default async (job: Job) => { debug( `old lastSeen ${ record.lastSeen - } is later than new lastSeen ${date}, not running job:\nuserId: ${userId}\nthreadId: ${threadId}\ntimestamp: ${new Date( + } is later than new lastSeen ${date.toString()}, not running job:\nuserId: ${userId}\nthreadId: ${threadId}\ntimestamp: ${new Date( timestamp ).toString()}` ); return; } - + debug( `existing usersThread, updating usersThreads#${record.id} with lastSeen` ); diff --git a/shared/types.js b/shared/types.js index a027a9fbf4..006e74a168 100644 --- a/shared/types.js +++ b/shared/types.js @@ -328,6 +328,7 @@ export type DBUsersThreads = { receiveNotifications: boolean, threadId: string, userId: string, + lastSeen: Date, }; export type Mention = { From c739a6b8074492fcfe4ad69abd948b4be3339982 Mon Sep 17 00:00:00 2001 From: Tom Whale Date: Wed, 4 Apr 2018 10:29:28 +0100 Subject: [PATCH 3/9] PR Changes: Change type of lastSeen & convert to Date object --- athena/queues/track-user-thread-last-seen.js | 2 +- shared/types.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/athena/queues/track-user-thread-last-seen.js b/athena/queues/track-user-thread-last-seen.js index 5153c0a464..d622cda2ed 100644 --- a/athena/queues/track-user-thread-last-seen.js +++ b/athena/queues/track-user-thread-last-seen.js @@ -29,7 +29,7 @@ export default async (job: Job) => { const record = await getUsersThread(userId, threadId); if (record) { - if (record.lastSeen > date) { + if (new Date(record.lastSeen) > new Date(date)) { debug( `old lastSeen ${ record.lastSeen diff --git a/shared/types.js b/shared/types.js index 006e74a168..0b7e9da416 100644 --- a/shared/types.js +++ b/shared/types.js @@ -328,7 +328,7 @@ export type DBUsersThreads = { receiveNotifications: boolean, threadId: string, userId: string, - lastSeen: Date, + lastSeen: Date | number, }; export type Mention = { From 2afdc51c924b6bb7f91da1b8857f546eb6357c37 Mon Sep 17 00:00:00 2001 From: Tom Whale Date: Wed, 4 Apr 2018 10:32:37 +0100 Subject: [PATCH 4/9] PR Changes: Change type of lastSeen & convert to Date object --- athena/queues/track-user-thread-last-seen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/athena/queues/track-user-thread-last-seen.js b/athena/queues/track-user-thread-last-seen.js index d622cda2ed..3b07684c67 100644 --- a/athena/queues/track-user-thread-last-seen.js +++ b/athena/queues/track-user-thread-last-seen.js @@ -29,7 +29,7 @@ export default async (job: Job) => { const record = await getUsersThread(userId, threadId); if (record) { - if (new Date(record.lastSeen) > new Date(date)) { + if (new Date(record.lastSeen).getTime() > new Date(date).getTime()) { debug( `old lastSeen ${ record.lastSeen From 678fd5f7eb903a1d6331c2083eea7a656ec14fde Mon Sep 17 00:00:00 2001 From: Tom Whale Date: Wed, 4 Apr 2018 11:15:29 +0100 Subject: [PATCH 5/9] Nullable lastSeen --- shared/types.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/types.js b/shared/types.js index 0b7e9da416..8d9d043769 100644 --- a/shared/types.js +++ b/shared/types.js @@ -328,7 +328,7 @@ export type DBUsersThreads = { receiveNotifications: boolean, threadId: string, userId: string, - lastSeen: Date | number, + lastSeen?: Date | number, }; export type Mention = { From 8139445daea01f939d3162d3dd1a9a13ffec1b11 Mon Sep 17 00:00:00 2001 From: Tom Whale Date: Wed, 4 Apr 2018 11:50:53 +0100 Subject: [PATCH 6/9] Fix flow issues --- athena/queues/track-user-thread-last-seen.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/athena/queues/track-user-thread-last-seen.js b/athena/queues/track-user-thread-last-seen.js index 3b07684c67..525eecfe5a 100644 --- a/athena/queues/track-user-thread-last-seen.js +++ b/athena/queues/track-user-thread-last-seen.js @@ -29,7 +29,10 @@ export default async (job: Job) => { const record = await getUsersThread(userId, threadId); if (record) { - if (new Date(record.lastSeen).getTime() > new Date(date).getTime()) { + if ( + record.lastSeen && + new Date(record.lastSeen).getTime() > new Date(date).getTime() + ) { debug( `old lastSeen ${ record.lastSeen From 0fd74838aa40ee7ea7fa04489f466c33289ee27d Mon Sep 17 00:00:00 2001 From: Tom Whale Date: Wed, 4 Apr 2018 11:59:24 +0100 Subject: [PATCH 7/9] Fix flow issues --- athena/models/usersThreads.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/athena/models/usersThreads.js b/athena/models/usersThreads.js index 52e1f49bc1..3eb2083db2 100644 --- a/athena/models/usersThreads.js +++ b/athena/models/usersThreads.js @@ -18,7 +18,7 @@ export const getThreadNotificationUsers = ( export const getUsersThread = ( userId: string, threadId: string -): Promise => { +): Promise => { return db .table('usersThreads') .getAll(userId, { index: 'userId' }) From 0815fea45859b8e4bd2fb2d704c78bfd17c34fe2 Mon Sep 17 00:00:00 2001 From: Tom Whale Date: Wed, 4 Apr 2018 20:46:05 +0100 Subject: [PATCH 8/9] Fix flow issues --- athena/models/usersThreads.js | 2 +- athena/queues/track-user-thread-last-seen.js | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/athena/models/usersThreads.js b/athena/models/usersThreads.js index 3eb2083db2..52e1f49bc1 100644 --- a/athena/models/usersThreads.js +++ b/athena/models/usersThreads.js @@ -18,7 +18,7 @@ export const getThreadNotificationUsers = ( export const getUsersThread = ( userId: string, threadId: string -): Promise => { +): Promise => { return db .table('usersThreads') .getAll(userId, { index: 'userId' }) diff --git a/athena/queues/track-user-thread-last-seen.js b/athena/queues/track-user-thread-last-seen.js index 525eecfe5a..f404e4db2c 100644 --- a/athena/queues/track-user-thread-last-seen.js +++ b/athena/queues/track-user-thread-last-seen.js @@ -26,7 +26,7 @@ export default async (job: Job) => { ).toString()}` ); - const record = await getUsersThread(userId, threadId); + const record: ?DBUsersThreads = await getUsersThread(userId, threadId); if (record) { if ( @@ -34,9 +34,7 @@ export default async (job: Job) => { new Date(record.lastSeen).getTime() > new Date(date).getTime() ) { debug( - `old lastSeen ${ - record.lastSeen - } is later than new lastSeen ${date.toString()}, not running job:\nuserId: ${userId}\nthreadId: ${threadId}\ntimestamp: ${new Date( + `old lastSeen ${record.lastSeen.toString()} is later than new lastSeen ${date.toString()}, not running job:\nuserId: ${userId}\nthreadId: ${threadId}\ntimestamp: ${new Date( timestamp ).toString()}` ); From f46920b7275926a1c7c1f4891e142a202b67abf3 Mon Sep 17 00:00:00 2001 From: Tom Whale Date: Wed, 4 Apr 2018 20:53:47 +0100 Subject: [PATCH 9/9] Import type --- athena/queues/track-user-thread-last-seen.js | 1 + 1 file changed, 1 insertion(+) diff --git a/athena/queues/track-user-thread-last-seen.js b/athena/queues/track-user-thread-last-seen.js index f404e4db2c..18e331763e 100644 --- a/athena/queues/track-user-thread-last-seen.js +++ b/athena/queues/track-user-thread-last-seen.js @@ -7,6 +7,7 @@ import { createUserThread, } from '../models/usersThreads'; import type { Job, UserThreadLastSeenJobData } from 'shared/bull/types'; +import type { DBUsersThreads } from 'shared/types'; export default async (job: Job) => { const { userId, threadId, timestamp } = job.data;