Repos / pytaku / ba57956778
commit ba57956778b69163d26a0f692f42adfcdf4debce
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Sun Aug 23 21:49:14 2020 +0700

    mark as read when user clicks "next"

diff --git a/src/pytaku/main.py b/src/pytaku/main.py
index 14e9630..d934a5e 100644
--- a/src/pytaku/main.py
+++ b/src/pytaku/main.py
@@ -30,10 +30,12 @@
     import_follows,
     load_chapter,
     load_title,
+    read,
     register_user,
     save_chapter,
     save_title,
     unfollow,
+    unread,
     verify_username_password,
 )
 from .source_sites import (
@@ -529,3 +531,25 @@ def api_follow():
         unfollow(request.user_id, site, title_id)
 
     return jsonify({"follow": should_follow})
+
+
+@app.route("/api/read", methods=["POST"])
+@process_token(required=True)
+def api_read():
+    reads = request.json.get("read") or []
+    unreads = request.json.get("unread") or []
+    assert reads or unreads
+
+    if reads:
+        for r in reads:
+            read(
+                request.user_id, r["site"], r["title_id"], r["chapter_id"],
+            )
+    if unreads:
+        for u in unreads:
+            read(
+                request.user_id, u["site"], u["title_id"], u["chapter_id"],
+            )
+    # TODO: rewrite read/unread to do bulk updates instead of n+1 queries like these.
+    # ... Or maybe not. SQLite doesn't mind.
+    return {}
diff --git a/src/pytaku/static/js/routes/chapter.js b/src/pytaku/static/js/routes/chapter.js
index e188822..b22a2d7 100644
--- a/src/pytaku/static/js/routes/chapter.js
+++ b/src/pytaku/static/js/routes/chapter.js
@@ -103,20 +103,35 @@ function Chapter(initialVNode) {
           },
           [m("i.icon.icon-list"), m("span", " chapter list")]
         ),
-        next
-          ? m(
-              m.route.Link,
-              {
-                class: "touch-friendly",
-                href: `/m/${site}/${titleId}/${next.id}`,
-              },
-              [m("span", "next"), m("i.icon.icon-chevrons-right")]
-            )
-          : m(Button, {
-              text: "next",
-              icon: "chevrons-right",
-              disabled: true,
-            }),
+        m(
+          m.route.Link,
+          {
+            class: "touch-friendly",
+            href: next
+              ? `/m/${site}/${titleId}/${next.id}`
+              : `/m/${site}/${titleId}`,
+            onclick: (ev) => {
+              Auth.request({
+                method: "POST",
+                url: "/api/read",
+                body: {
+                  read: [
+                    {
+                      site,
+                      title_id: titleId,
+                      chapter_id: chapter.id,
+                    },
+                  ],
+                },
+              });
+              return true;
+            },
+          },
+          [
+            m("span", next ? "next" : "finish"),
+            m("i.icon.icon-" + (next ? "chevrons-right" : "check-circle")),
+          ]
+        ),
       ]);
       return m("div.chapter.content", [
         m("h1", fullChapterName(chapter)),
diff --git a/src/pytaku/static/js/utils.js b/src/pytaku/static/js/utils.js
index 46b877d..d8f1f12 100644
--- a/src/pytaku/static/js/utils.js
+++ b/src/pytaku/static/js/utils.js
@@ -19,7 +19,7 @@ const Button = {
 
 const Chapter = {
   view: (vnode) =>
-    m("div.utils--chapter", [
+    m("div.utils--chapter" + (vnode.attrs.chapter.is_read ? " read" : ""), [
       m(
         m.route.Link,
         {
diff --git a/src/pytaku/static/spa.css b/src/pytaku/static/spa.css
index 71978df..9d7bc59 100644
--- a/src/pytaku/static/spa.css
+++ b/src/pytaku/static/spa.css
@@ -324,6 +324,12 @@ .chapter--buttons > button:last-child {
 .utils--chapter {
   margin-bottom: 0.5rem;
 }
+.utils--chapter.read > a:before {
+  content: "🗹";
+  color: green;
+  margin-right: 0.2rem;
+  font-weight: bold;
+}
 .utils--chapter > a {
   padding-left: 0.5rem;
   padding-right: 0.5rem;