login

<     >

2023-07-24 14:48:41 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

add /invites page

diff --git a/data.lua b/data.lua
index 949ce25..ce7790c 100644
--- a/data.lua
+++ b/data.lua
@@ -116,8 +116,8 @@ function Model:get_invites(user_id)
     return self.db:execute("SELECT * FROM Invite WHERE user_id = ?;", user_id)
 end
 
-function Model:del_invite(uuid)
-    self.db:execute("DELETE FROM Invite WHERE uuid = ?;", uuid)
+function Model:del_invite(user_id, uuid)
+    self.db:execute("DELETE FROM Invite WHERE user_id = ? AND uuid = ?;", user_id, uuid)
 end
 
 function Model:expire_invites()

diff --git a/skopos.lua b/skopos.lua
index 8d4ff29..0683508 100644
--- a/skopos.lua
+++ b/skopos.lua
@@ -100,6 +100,30 @@ function App:routes()
         end
         return "/login", 303
     end},
+    {"GET", "/invites",
+    function (req)
+        local user = self:get_user(req)
+        if user == nil then return "/login", 303 end
+        local invites = self.model:get_invites(user.id)
+        local env = {title=self.title, invites=invites}
+        return lud.template.render_file("view/invites.html", env)
+    end},
+    {"POST", "/create%-invite",
+    function (req)
+        local user = self:get_user(req)
+        if user == nil then return "/login", 303 end
+        self.model:create_invite(user.id)
+        self:log(LOG_INFO, "user "..user.nick.." generated a new invite")
+        return "/invites", 303
+    end},
+    {"POST", "/cancel%-invite/([%x]+)",
+    function (req, uuid)
+        local user = self:get_user(req)
+        if user == nil then return "/login", 303 end
+        self.model:del_invite(user.id, uuid)
+        self:log(LOG_INFO, "user "..user.nick.." canceled invite "..uuid)
+        return "/invites", 303
+    end},
 } end
 
 local function new_app(db_path, port, title, log_level)

diff --git a/view/form.css b/view/form.css
index 4143df8..b3c35ba 100644
--- a/view/form.css
+++ b/view/form.css
@@ -32,6 +32,3 @@
     .flat-button:hover {
       background-color: var(--color-2);
     }
-    .mono {
-      font-family: monospace;
-    }

diff --git a/view/invites.html b/view/invites.html
new file mode 100644
index 0000000..d6c2c5a
--- /dev/null
+++ b/view/invites.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>{{$title}} - invites</title>
+  <style>
+    .centered { text-align: center; }
+    .mono { font-family: monospace; }
+    % include view/table.css
+  </style>
+</head>
+<body>
+  <h1 class="centered">Invites</h1>
+  <form class="centered" action="/create-invite" method="post">
+    <button type="submit">new</button>
+  </form>
+  <br>
+  <table class="center">
+    <tr>
+      <th>uuid</th>
+      <th>expiration</th>
+      <th>cancel</th>
+    </tr>
+    % for invite in $invites do
+      <tr>
+        <td class="mono">{{$invite.uuid}}</td>
+        <td>{{os.date("%Y-%m-%d %H:%M:%S", $invite.expire)}}</td>
+        <td>
+          <form action="/cancel-invite/{{$invite.uuid}}" method="post">
+            <button type="submit">cancel</button>
+          </form>
+        </td>
+      </tr>
+    % end
+  </table>
+</body>
+</html>

diff --git a/view/join.html b/view/join.html
index 894e882..c777f5b 100644
--- a/view/join.html
+++ b/view/join.html
@@ -5,6 +5,7 @@
   <title>{{$title}} - join</title>
   <style>
     .centered { text-align: center; }
+    .mono { font-family: monospace; }
     % include view/form.css
   </style>
 </head>

diff --git a/view/table.css b/view/table.css
new file mode 100644
index 0000000..b31e871
--- /dev/null
+++ b/view/table.css
@@ -0,0 +1,13 @@
+    table {
+      border-collapse: collapse;
+    }
+    th { background-color: #EEEEEE; }
+    th, td {
+      padding: 3px 12px;
+      border: 1px solid #006;
+    }
+    .center {
+      display: flex;
+      align-items: center;
+      justify-content: center;
+    }