···3535import { NamedGroup, parseRegex } from "@foxxmd/regex-buddy-core";
3636import { Duration } from "dayjs/plugin/duration.js";
3737import { SourceType } from "../common/infrastructure/config/source/sources.js";
3838+import { Logger } from "@foxxmd/logging";
3939+import { loggerNoop } from "../common/MaybeLogger.js";
38403941//dayjs.extend(isToday);
4042···7375 fuzzyDuration?: boolean,
7476 fuzzyDiffThreshold?: number
7577 duringReferences?: AcceptableTemporalDuringReference
7878+ logger?: Logger
7679}
77807881export const comparePlayTemporally = (existingPlay: PlayObject, candidatePlay: PlayObject, options: TemporalPlayComparisonOptions = {}): TemporalPlayComparison => {
···113116 diffThreshold = getTemporalAccuracyCloseVal(source as SourceType),
114117 fuzzyDuration = false,
115118 fuzzyDiffThreshold = 10,
116116- duringReferences = ['range']
119119+ duringReferences = ['range'],
120120+ logger = loggerNoop
117121 } = options;
118122119123 const result: TemporalPlayComparison = {
···151155152156 if(duringReferences.length > 0) {
153157154154- if (duringReferences.includes('range') && existingRanges !== undefined) {
155155- // since we know when the existing track was listened to
156156- // we can check if the new track play date took place while the existing one was being listened to
157157- // which would indicate (assuming same source) the new track is a duplicate
158158- for (const range of existingRanges) {
159159- if (candidateTsSOCDate.isBetween(range.start.timestamp, range.end.timestamp)) {
160160- result.range = {
161161- type: 'range',
162162- timestamps: [range.start.timestamp, range.end.timestamp]
158158+ // guard against old, badly cached/marshalled range data:
159159+ // this should not be an issue in 0.14.0+ since listenprogress has been updated to be a plain object
160160+ // but for folks migrating with very old cached data it could end up being an issue
161161+ try {
162162+ if (duringReferences.includes('range') && existingRanges !== undefined) {
163163+ // since we know when the existing track was listened to
164164+ // we can check if the new track play date took place while the existing one was being listened to
165165+ // which would indicate (assuming same source) the new track is a duplicate
166166+ for (const range of existingRanges) {
167167+168168+ if (candidateTsSOCDate.isBetween(range.start.timestamp, range.end.timestamp)) {
169169+ result.range = {
170170+ type: 'range',
171171+ timestamps: [range.start.timestamp, range.end.timestamp]
172172+ }
173173+ result.match = TA_DURING;
174174+ return result;
163175 }
164164- result.match = TA_DURING;
165165- return result;
166176 }
167177 }
178178+ } catch (e) {
179179+ logger.warn(new Error('Failed to compare plays based on range but will continue', {cause: e}));
168180 }
169181170182 if(duringReferences.includes('listenedFor') && existingPlay.data.listenedFor !== undefined) {