This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 828
Poll history: fetch last 30 days of polls #10157
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9e18d85
use timeline pagination
f18e37c
fetch last 30 days of poll history
6ac1543
Merge branch 'develop' into psg-1046/poll-history-fetch
9dde734
add comments, tidy
cc0228e
more comments
7716187
finish comment
58ee2d4
wait for responses to resolve before displaying in list
4f1d063
dont use state for list
7dd2074
return unsubscribe
09e7195
strict fixes
03fa15b
Merge branch 'develop' into psg-1046/poll-history-fetch
4753b4a
unnecessary event type in filter
19b2331
Merge branch 'develop' into psg-1046/poll-history-fetch
bfc85a8
add catch
c84fae8
Merge branch 'develop' into psg-1046/poll-history-fetch
4dde9ca
Merge branch 'develop' into psg-1046/poll-history-fetch
df592d9
Merge branch 'develop' into psg-1046/poll-history-fetch
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { useEffect, useState } from "react"; | ||
import { M_POLL_START } from "matrix-js-sdk/src/@types/polls"; | ||
import { MatrixClient } from "matrix-js-sdk/src/client"; | ||
import { EventTimeline, EventTimelineSet, Room } from "matrix-js-sdk/src/matrix"; | ||
import { Filter, IFilterDefinition } from "matrix-js-sdk/src/filter"; | ||
|
||
/** | ||
* Page timeline backwards until either: | ||
* - event older than endOfHistoryPeriodTimestamp is encountered | ||
* - end of timeline is reached | ||
* @param timelineSet - timelineset to page | ||
* @param matrixClient - client | ||
* @param endOfHistoryPeriodTimestamp - epoch timestamp to fetch until | ||
* @returns void | ||
*/ | ||
const pagePolls = async ( | ||
timelineSet: EventTimelineSet, | ||
matrixClient: MatrixClient, | ||
endOfHistoryPeriodTimestamp: number, | ||
): Promise<void> => { | ||
const liveTimeline = timelineSet.getLiveTimeline(); | ||
const events = liveTimeline.getEvents(); | ||
const oldestEventTimestamp = events[0]?.getTs() || Date.now(); | ||
const hasMorePages = !!liveTimeline.getPaginationToken(EventTimeline.BACKWARDS); | ||
|
||
if (!hasMorePages || oldestEventTimestamp <= endOfHistoryPeriodTimestamp) { | ||
return; | ||
} | ||
|
||
await matrixClient.paginateEventTimeline(liveTimeline, { | ||
backwards: true, | ||
}); | ||
|
||
return pagePolls(timelineSet, matrixClient, endOfHistoryPeriodTimestamp); | ||
}; | ||
|
||
const ONE_DAY_MS = 60000 * 60 * 24; | ||
/** | ||
* Fetches timeline history for given number of days in past | ||
* @param timelineSet - timelineset to page | ||
* @param matrixClient - client | ||
* @param historyPeriodDays - number of days of history to fetch, from current day | ||
* @returns isLoading - true while fetching history | ||
*/ | ||
const useTimelineHistory = ( | ||
timelineSet: EventTimelineSet | null, | ||
matrixClient: MatrixClient, | ||
historyPeriodDays: number, | ||
): { isLoading: boolean } => { | ||
const [isLoading, setIsLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
if (!timelineSet) { | ||
return; | ||
} | ||
const endOfHistoryPeriodTimestamp = Date.now() - ONE_DAY_MS * historyPeriodDays; | ||
|
||
const doFetchHistory = async (): Promise<void> => { | ||
setIsLoading(true); | ||
try { | ||
await pagePolls(timelineSet, matrixClient, endOfHistoryPeriodTimestamp); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
doFetchHistory(); | ||
}, [timelineSet, historyPeriodDays, matrixClient]); | ||
|
||
return { isLoading }; | ||
}; | ||
|
||
const filterDefinition: IFilterDefinition = { | ||
room: { | ||
timeline: { | ||
types: [M_POLL_START.name, M_POLL_START.altName], | ||
}, | ||
}, | ||
}; | ||
|
||
/** | ||
* Fetch poll start events in the last N days of room history | ||
* @param room - room to fetch history for | ||
* @param matrixClient - client | ||
* @param historyPeriodDays - number of days of history to fetch, from current day | ||
* @returns isLoading - true while fetching history | ||
*/ | ||
export const useFetchPastPolls = ( | ||
room: Room, | ||
matrixClient: MatrixClient, | ||
historyPeriodDays = 30, | ||
): { isLoading: boolean } => { | ||
const [timelineSet, setTimelineSet] = useState<EventTimelineSet | null>(null); | ||
|
||
useEffect(() => { | ||
const filter = new Filter(matrixClient.getSafeUserId()); | ||
filter.setDefinition(filterDefinition); | ||
const getFilteredTimelineSet = async (): Promise<void> => { | ||
const filterId = await matrixClient.getOrCreateFilter(`POLL_HISTORY_FILTER_${room.roomId}}`, filter); | ||
filter.filterId = filterId; | ||
const timelineSet = room.getOrCreateFilteredTimelineSet(filter); | ||
setTimelineSet(timelineSet); | ||
}; | ||
|
||
getFilteredTimelineSet(); | ||
}, [room, matrixClient]); | ||
|
||
const { isLoading } = useTimelineHistory(timelineSet, matrixClient, historyPeriodDays); | ||
|
||
return { isLoading }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need a catch here? If not, could we not remove the try and finally blocks (keeping the code they contain) without any effect on how this works?