about summary refs log tree commit diff
path: root/assets/src/models/incoming_request.ts
blob: f78454080cb729e133a2be1dff9555be30a4f974 (plain)
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
57
58
59
import type { Transfer } from "../network/transfer/transfer";
import { Writable, writable } from "svelte/store";

// Represents the current progress of every request
export enum IncomingRequestState {
    // Request was neither accepted nor declined yet
    WAITING,
    // Data is being transferred, more precise data
    // is available in the corresponding Transfer object
    IN_FLIGHT,
    // Requestor has received all data
    DONE,
    // Request was declined
    DECLINED,
}

// Identifying information about the requestor
export type IncomingRequestInfo = {
    token: string;

    ip: string;
    location: string;
    client: string;

    receivedAt: Date;
};

// The model for a request received by a sharer
// The state marks changes in the progression of the request lifecycle and can be subscribed to
export type IncomingRequest = {
    // Transfer is null while request isn't IN_FLIGHT
    transfer: Transfer | null;
    info: IncomingRequestInfo;
    state: Writable<IncomingRequestState>;
};

// Create a model for a new incoming request
export function newIncomingRequest(
    token: string,
    ip: string,
    location: string,
    client: string
): IncomingRequest {
    const info = {
        token,
        ip,
        location,
        client,

        receivedAt: new Date(),
    };

    return {
        transfer: null,
        info,
        // Each request starts out as just received and waiting for an answer
        state: writable(IncomingRequestState.WAITING),
    };
}