aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/phonesync/DocPlan.kt
blob: 1a2a359514221f3610a6d2e1d84ec7b319a2784a (plain) (blame)
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
package org.meshbay.client.phonesync

import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone

/** One file a run sends, and what is recorded once the node has it. */
data class Export(
    val file: File,
    val name: String,
    val mime: String,
    /** Recorded only when the node took the file: what the next export starts from. */
    val marker: String,
    /** Contacts or messages in it, for the page to say. */
    val count: Int,
    /** A folder under the kind's own, such as the year, or empty. */
    val subdir: String = "",
)

/**
 * One kind of the phone's data (the contacts, the messages) written out as
 * a file of what is new since `marker`, the value the last file sent left.
 */
interface DocSource {
    /** The runtime permissions it reads with. */
    val permissions: Array<String>
    /** Where its files go, under the chosen folder: `<account>-<suffix>`. */
    val suffix: String
    /** A file of what is new since `marker` in `dir`, or null when nothing is. */
    fun export(marker: String?, dir: File, now: Long, zone: TimeZone): Export?
}

/** The names a run gives what it sends. Pure, so the JVM tests hold them. */
object DocPlan {
    /** A run is due once a day, counted from the last one that finished. */
    const val DAY_MS = 24L * 3600 * 1000

    /** A username as the hub allows it; it names a folder on the node. */
    val SAFE_ACCOUNT = Regex("^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$")

    /** `<folder>/<account>-<suffix>`: everything of one kind, from one account, in one place. */
    fun dirFor(destination: Destination, suffix: String): String =
        "${destination.folder}/${destination.account}-$suffix"

    /**
     * `<stem>-YYYY-MM-DD-HHmm.<ext>`, in the phone's time: a run never replaces
     * an earlier file, and the name says when it was taken.
     */
    fun yearOf(now: Long, zone: TimeZone): String =
        SimpleDateFormat("yyyy", Locale.ROOT).apply { timeZone = zone }.format(Date(now))

    fun nameFor(stem: String, ext: String, now: Long, zone: TimeZone): String =
        "$stem-" + SimpleDateFormat("yyyy-MM-dd-HHmm", Locale.ROOT).apply { timeZone = zone }.format(Date(now)) + ".$ext"
}