-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add some improvements to thread handling #8407
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,7 +291,7 @@ export const syncThreadsIfNeeded = async (serverUrl: string, isCRTEnabled: boole | |
|
||
if (teams?.length) { | ||
for (const team of teams) { | ||
promises.push(syncTeamThreads(serverUrl, team.id, true, true)); | ||
promises.push(syncTeamThreads(serverUrl, team.id, {excludeDirect: true, fetchOnly: true})); | ||
} | ||
} | ||
|
||
|
@@ -317,7 +317,21 @@ export const syncThreadsIfNeeded = async (serverUrl: string, isCRTEnabled: boole | |
} | ||
}; | ||
|
||
export const syncTeamThreads = async (serverUrl: string, teamId: string, excludeDirect = false, fetchOnly = false) => { | ||
type SyncThreadOptions = { | ||
excludeDirect?: boolean; | ||
fetchOnly?: boolean; | ||
refresh?: boolean; | ||
} | ||
|
||
export const syncTeamThreads = async ( | ||
serverUrl: string, | ||
teamId: string, | ||
{ | ||
excludeDirect = false, | ||
fetchOnly = false, | ||
refresh = false, | ||
}: SyncThreadOptions = {}, | ||
) => { | ||
try { | ||
const {database, operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl); | ||
const syncData = await getTeamThreadsSyncData(database, teamId); | ||
|
@@ -354,36 +368,53 @@ export const syncTeamThreads = async (serverUrl: string, teamId: string, exclude | |
return {error: allUnreadThreads.error || latestThreads.error}; | ||
} | ||
|
||
const dedupe = new Set(latestThreads.threads?.map((t) => t.id)); | ||
|
||
if (latestThreads.threads?.length) { | ||
// We are fetching the threads for the first time. We get "latest" and "earliest" values. | ||
// At this point we may receive threads without replies, so we also check the post.create_at timestamp. | ||
const {earliestThread, latestThread} = getThreadsListEdges(latestThreads.threads); | ||
syncDataUpdate.latest = latestThread.last_reply_at; | ||
syncDataUpdate.earliest = earliestThread.last_reply_at; | ||
syncDataUpdate.latest = latestThread.last_reply_at || latestThread.post.create_at; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we not sync data on post edits? Or is that done per post? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thread information in the database only includes the post id, so updates in the post should not affect this piece of data (it affects the post itself, which is done somewhere else). |
||
syncDataUpdate.earliest = earliestThread.last_reply_at || earliestThread.post.create_at; | ||
|
||
threads.push(...latestThreads.threads); | ||
} | ||
if (allUnreadThreads.threads?.length) { | ||
const dedupe = new Set(latestThreads.threads?.map((t) => t.id)); | ||
const unread = allUnreadThreads.threads.filter((u) => !dedupe.has(u.id)); | ||
threads.push(...unread); | ||
} | ||
} else { | ||
const allNewThreads = await fetchThreads( | ||
serverUrl, | ||
teamId, | ||
{deleted: true, since: syncData.latest + 1, excludeDirect}, | ||
); | ||
const [allUnreadThreads, allNewThreads] = await Promise.all([ | ||
fetchThreads( | ||
serverUrl, | ||
teamId, | ||
{unread: true, excludeDirect}, | ||
Direction.Down, | ||
), | ||
fetchThreads( | ||
serverUrl, | ||
teamId, | ||
{deleted: true, since: refresh ? undefined : syncData.latest + 1, excludeDirect}, | ||
undefined, | ||
1, | ||
), | ||
]); | ||
|
||
if (allNewThreads.error) { | ||
return {error: allNewThreads.error}; | ||
} | ||
if (allNewThreads.threads?.length) { | ||
// As we are syncing, we get all new threads and we will update the "latest" value. | ||
const {latestThread} = getThreadsListEdges(allNewThreads.threads); | ||
syncDataUpdate.latest = latestThread.last_reply_at; | ||
const latestDate = latestThread.last_reply_at || latestThread.post.create_at; | ||
syncDataUpdate.latest = Math.max(syncData.latest, latestDate); | ||
|
||
threads.push(...allNewThreads.threads); | ||
} | ||
if (allUnreadThreads.threads?.length) { | ||
const dedupe = new Set(allNewThreads.threads?.map((t) => t.id)); | ||
const unread = allUnreadThreads.threads.filter((u) => !dedupe.has(u.id)); | ||
threads.push(...unread); | ||
} | ||
} | ||
|
||
const models: Model[] = []; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious - where do we sync the DM/GM threads?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other calls to
syncTeamThreads
, where we don't pass theexcludeDirect
option. Mainly on startup / reconnect, if I am not mistaken.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exclude direct is false also for the initial team, this will fetch the threads on DM's and GM's that are returned for every team if you don't set the flag as true