about summary refs log tree commit diff stats
path: root/classes/Handler/LastreadHandler.php
blob: 638db258c8adc54fb8f713017bfc8c9810177337 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php

namespace Handler;

use AuthRequired;
use ORM;
use Request;
use RequestHandlerInterface;
use Response;

class LastreadHandler implements RequestHandlerInterface, AuthRequired
{
    private function handleGet(int $bookid): Response
    {
        if ($bookid) {
            $book = ORM::for_table('epube_books')
                ->where('bookid', $bookid)
                ->where('owner', $_SESSION["owner"] ?? "")
                ->find_one();

            if ($book) {
                return (new Response())->withBody(json_encode([
                    "page" => (int)$book->lastread,
                    "cfi" => $book->lastcfi,
                    "total" => 100,
                    "timestamp" => (int)$book->lastts
                ]));
            }
        }

        return (new Response())
            ->withStatus(404)
            ->withBody(json_encode(["error" => "Not found."]));
    }

    private function handleStore(int $bookid, int $page, string $cfi, int $timestamp): Response
    {
        if ($bookid) {
            $book = ORM::for_table('epube_books')
                ->where('bookid', $bookid)
                ->where('owner', $_SESSION["owner"] ?? "")
                ->find_one();

            if ($book) {
                if (($timestamp >= $book->lastts) && ($page >= $book->lastread || $page == PAGE_RESET_PROGRESS)) {
                    $book->set([
                        'lastread' => $page,
                        'lastcfi' => $cfi,
                        'lastts' => $timestamp,
                    ]);
                }
            } else {
                $book = ORM::for_table('epube_books')->create();
                $book->set([
                    'bookid' => $bookid,
                    'owner' => $_SESSION["owner"] ?? "",
                    'lastread' => $page,
                    'lastcfi' => $cfi,
                    'lastts' => $timestamp,
                ]);
            }

            $book->save();
        }

        return (new Response())->withBody(json_encode(["page" => $page, "cfi" => $cfi]));
    }

    public function handle(Request $request): Response
    {
        $subOp = $request->getString('op', '');
        $bookid = $request->getInt('id');

        return match ($subOp) {
            'getlastread'    => $this->handleGet($bookid),
            'storelastread'  => $this->handleStore(
                $bookid,
                $request->getInt('page'),
                $request->getString('cfi', ''),
                $request->getInt('timestamp'),
            ),
            default          => (new Response())->withStatus(404)->withBody(json_encode(["error" => "Not found."])),
        };
    }
}