|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { useEffect, useState } from "react"; |
| 18 | +import { M_POLL_START } from "matrix-js-sdk/src/@types/polls"; |
| 19 | +import { MatrixClient } from "matrix-js-sdk/src/client"; |
| 20 | +import { EventTimeline, EventTimelineSet, Room } from "matrix-js-sdk/src/matrix"; |
| 21 | +import { Filter, IFilterDefinition } from "matrix-js-sdk/src/filter"; |
| 22 | +import { logger } from "matrix-js-sdk/src/logger"; |
| 23 | + |
| 24 | +/** |
| 25 | + * Page timeline backwards until either: |
| 26 | + * - event older than endOfHistoryPeriodTimestamp is encountered |
| 27 | + * - end of timeline is reached |
| 28 | + * @param timelineSet - timelineset to page |
| 29 | + * @param matrixClient - client |
| 30 | + * @param endOfHistoryPeriodTimestamp - epoch timestamp to fetch until |
| 31 | + * @returns void |
| 32 | + */ |
| 33 | +const pagePolls = async ( |
| 34 | + timelineSet: EventTimelineSet, |
| 35 | + matrixClient: MatrixClient, |
| 36 | + endOfHistoryPeriodTimestamp: number, |
| 37 | +): Promise<void> => { |
| 38 | + const liveTimeline = timelineSet.getLiveTimeline(); |
| 39 | + const events = liveTimeline.getEvents(); |
| 40 | + const oldestEventTimestamp = events[0]?.getTs() || Date.now(); |
| 41 | + const hasMorePages = !!liveTimeline.getPaginationToken(EventTimeline.BACKWARDS); |
| 42 | + |
| 43 | + if (!hasMorePages || oldestEventTimestamp <= endOfHistoryPeriodTimestamp) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + await matrixClient.paginateEventTimeline(liveTimeline, { |
| 48 | + backwards: true, |
| 49 | + }); |
| 50 | + |
| 51 | + return pagePolls(timelineSet, matrixClient, endOfHistoryPeriodTimestamp); |
| 52 | +}; |
| 53 | + |
| 54 | +const ONE_DAY_MS = 60000 * 60 * 24; |
| 55 | +/** |
| 56 | + * Fetches timeline history for given number of days in past |
| 57 | + * @param timelineSet - timelineset to page |
| 58 | + * @param matrixClient - client |
| 59 | + * @param historyPeriodDays - number of days of history to fetch, from current day |
| 60 | + * @returns isLoading - true while fetching history |
| 61 | + */ |
| 62 | +const useTimelineHistory = ( |
| 63 | + timelineSet: EventTimelineSet | null, |
| 64 | + matrixClient: MatrixClient, |
| 65 | + historyPeriodDays: number, |
| 66 | +): { isLoading: boolean } => { |
| 67 | + const [isLoading, setIsLoading] = useState(true); |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + if (!timelineSet) { |
| 71 | + return; |
| 72 | + } |
| 73 | + const endOfHistoryPeriodTimestamp = Date.now() - ONE_DAY_MS * historyPeriodDays; |
| 74 | + |
| 75 | + const doFetchHistory = async (): Promise<void> => { |
| 76 | + setIsLoading(true); |
| 77 | + try { |
| 78 | + await pagePolls(timelineSet, matrixClient, endOfHistoryPeriodTimestamp); |
| 79 | + } catch (error) { |
| 80 | + logger.error("Failed to fetch room polls history", error); |
| 81 | + } finally { |
| 82 | + setIsLoading(false); |
| 83 | + } |
| 84 | + }; |
| 85 | + doFetchHistory(); |
| 86 | + }, [timelineSet, historyPeriodDays, matrixClient]); |
| 87 | + |
| 88 | + return { isLoading }; |
| 89 | +}; |
| 90 | + |
| 91 | +const filterDefinition: IFilterDefinition = { |
| 92 | + room: { |
| 93 | + timeline: { |
| 94 | + types: [M_POLL_START.name, M_POLL_START.altName], |
| 95 | + }, |
| 96 | + }, |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * Fetch poll start events in the last N days of room history |
| 101 | + * @param room - room to fetch history for |
| 102 | + * @param matrixClient - client |
| 103 | + * @param historyPeriodDays - number of days of history to fetch, from current day |
| 104 | + * @returns isLoading - true while fetching history |
| 105 | + */ |
| 106 | +export const useFetchPastPolls = ( |
| 107 | + room: Room, |
| 108 | + matrixClient: MatrixClient, |
| 109 | + historyPeriodDays = 30, |
| 110 | +): { isLoading: boolean } => { |
| 111 | + const [timelineSet, setTimelineSet] = useState<EventTimelineSet | null>(null); |
| 112 | + |
| 113 | + useEffect(() => { |
| 114 | + const filter = new Filter(matrixClient.getSafeUserId()); |
| 115 | + filter.setDefinition(filterDefinition); |
| 116 | + const getFilteredTimelineSet = async (): Promise<void> => { |
| 117 | + const filterId = await matrixClient.getOrCreateFilter(`POLL_HISTORY_FILTER_${room.roomId}}`, filter); |
| 118 | + filter.filterId = filterId; |
| 119 | + const timelineSet = room.getOrCreateFilteredTimelineSet(filter); |
| 120 | + setTimelineSet(timelineSet); |
| 121 | + }; |
| 122 | + |
| 123 | + getFilteredTimelineSet(); |
| 124 | + }, [room, matrixClient]); |
| 125 | + |
| 126 | + const { isLoading } = useTimelineHistory(timelineSet, matrixClient, historyPeriodDays); |
| 127 | + |
| 128 | + return { isLoading }; |
| 129 | +}; |
0 commit comments