forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeferList.ts
More file actions
56 lines (50 loc) · 2.02 KB
/
deferList.ts
File metadata and controls
56 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { differenceInHours } from 'date-fns';
import { StorageParams } from '../storage/storage-params';
import { isDomainEquals } from '../utils/common';
import { Settings } from './settings';
import { Deffering } from '../entity/deffering';
import { injecStorage } from '../storage/inject-storage';
import { MINUTE } from '../utils/time';
import { log } from '../utils/logger';
export async function isInDeferList(url: string): Promise<boolean> {
const deferList = (await Settings.getInstance().getSetting(
StorageParams.BLOCK_DEFERRAL_TIME,
)) as Deffering[];
const array = Object.values(deferList);
const item = array?.find(x => isDomainEquals(x.domain, url));
if (item != undefined)
log(
`Deferring time ${url} ${new Date(item.time)} diff ${differenceInHours(
new Date(item.time),
new Date(),
)}`,
);
return item != undefined && item.time > Date.now();
}
export async function canDefering(url: string): Promise<boolean> {
const deferList = (await Settings.getInstance().getSetting(
StorageParams.BLOCK_DEFERRAL_TIME,
)) as Deffering[];
const array = Object.values(deferList);
const item = array?.find(x => isDomainEquals(x.domain, url));
if (item != undefined)
log(
`Deferring time ${url} ${new Date(item.time)} diff ${differenceInHours(
new Date(item.time),
new Date(),
)}`,
);
if (item == undefined) return true;
return item != undefined && differenceInHours(new Date(item.time), new Date()) > 24;
}
export async function defering(url: string, timeInMinutes: number): Promise<void> {
const settingsStorage = injecStorage();
const deferList = (await Settings.getInstance().getSetting(
StorageParams.BLOCK_DEFERRAL_TIME,
)) as Deffering[];
const array = Object.values(deferList);
const item = array?.find(x => isDomainEquals(x.domain, url));
if (item != undefined) item.time = Date.now() + timeInMinutes * MINUTE;
else array.push(new Deffering(url, 5));
await settingsStorage.saveValue(StorageParams.BLOCK_DEFERRAL_TIME, array);
}