changeset 42:a9858e4be3d6

Move some code around; add orig_msg_id to reminder table
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 10 Dec 2016 16:04:43 +0100
parents 7d363fef93a8
children ad3a8f150464
files handlers.go http.go pull.go queries.sql schema.sql sql/storage.go
diffstat 6 files changed, 51 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/handlers.go	Sat Dec 10 16:03:43 2016 +0100
+++ b/handlers.go	Sat Dec 10 16:04:43 2016 +0100
@@ -108,11 +108,12 @@
 	}
 
 	reply := sendMessage{
-		Chat_ID:             chatID,
-		Parse_Mode:          "Markdown",
-		Text:                rp.text,
-		Reply_Markup:        inlineKeyboardMarkup{Inline_Keyboard: [][]inlineKeyboardButton{}},
-		Reply_To_Message_Id: upd.Message.Message_ID,
+		Chat_ID:      chatID,
+		Parse_Mode:   "Markdown",
+		Text:         rp.text,
+		Reply_Markup: inlineKeyboardMarkup{Inline_Keyboard: [][]inlineKeyboardButton{}},
+		// We could do this, but it doesn't look nice
+		//Reply_To_Message_Id: upd.Message.Message_ID,
 	}
 
 	if rp.buttons.Inline_Keyboard != nil {
--- a/http.go	Sat Dec 10 16:03:43 2016 +0100
+++ b/http.go	Sat Dec 10 16:04:43 2016 +0100
@@ -1,7 +1,9 @@
 package main
 
 import (
+	"bytes"
 	"context"
+	"encoding/json"
 	"fmt"
 	"io/ioutil"
 	"log"
@@ -69,3 +71,31 @@
 	rp.WriteHeader(http.StatusOK)
 	rp.Write([]byte(fmt.Sprint(reply)))
 }
+
+func sendChatMessage(msg sendMessage) error {
+	body, err := json.Marshal(msg)
+
+	if err != nil {
+		log.Println(err)
+		return err
+	}
+
+	log.Println("Msg response:", string(body))
+
+	url := buildURL(sendMessageMethod)
+
+	srvStatus.apiCalls++
+
+	rp, err := defaultClient.Post(url, jsonBodyType, bytes.NewBuffer(body))
+
+	if err != nil {
+		srvStatus.apiErrors++
+		log.Println(err)
+		return err
+	} else if rp.StatusCode != http.StatusOK {
+		srvStatus.apiErrors++
+		log.Println("Error:", rp.StatusCode, rp.Status)
+	}
+
+	return nil
+}
--- a/pull.go	Sat Dec 10 16:03:43 2016 +0100
+++ b/pull.go	Sat Dec 10 16:04:43 2016 +0100
@@ -6,7 +6,6 @@
 	"encoding/json"
 	"io/ioutil"
 	"log"
-	"net/http"
 	"time"
 )
 
@@ -61,7 +60,7 @@
 			continue
 		}
 
-		err = sendReply(sM)
+		err = sendChatMessage(sM)
 
 		if err != nil {
 			srvStatus.apiErrors++
@@ -73,31 +72,3 @@
 	}
 	return off, nil
 }
-
-func sendReply(msg sendMessage) error {
-	body, err := json.Marshal(msg)
-
-	if err != nil {
-		log.Println(err)
-		return err
-	}
-
-	log.Println("Msg response:", string(body))
-
-	url := buildURL(sendMessageMethod)
-
-	srvStatus.apiCalls++
-
-	rp, err := defaultClient.Post(url, jsonBodyType, bytes.NewBuffer(body))
-
-	if err != nil {
-		srvStatus.apiErrors++
-		log.Println(err)
-		return err
-	} else if rp.StatusCode != http.StatusOK {
-		srvStatus.apiErrors++
-		log.Println("Error:", rp.StatusCode, rp.Status)
-	}
-
-	return nil
-}
--- a/queries.sql	Sat Dec 10 16:03:43 2016 +0100
+++ b/queries.sql	Sat Dec 10 16:04:43 2016 +0100
@@ -23,3 +23,10 @@
 SELECT id, text, owner FROM todo WHERE NOT done AND chat_id = $0 AND list = '' ORDER BY ts;
 -- Mark item as done. Returns '0 rows affected' if marking an already finished list item.
 UPDATE todo SET done = true WHERE id = $0 AND NOT done;
+
+-- Add reminder
+INSERT INTO reminders (created, due, description, chat_id) VALUES (now(), $0, $1, $2);
+-- Get reminders past due
+SELECT id, due, description, chat_id FROM reminders WHERE NOT done AND now() > due ORDER BY due ASC;
+-- Remove obsolete reminder
+UPDATE reminders SET done = true WHERE id = $0;
--- a/schema.sql	Sat Dec 10 16:03:43 2016 +0100
+++ b/schema.sql	Sat Dec 10 16:04:43 2016 +0100
@@ -111,9 +111,11 @@
     id integer NOT NULL,
     created timestamp without time zone,
     due timestamp without time zone,
+    owner text,
     description text,
     done boolean,
-    chat_id integer
+    chat_id integer,
+    orig_msg_id integer
 );
 
 
--- a/sql/storage.go	Sat Dec 10 16:03:43 2016 +0100
+++ b/sql/storage.go	Sat Dec 10 16:04:43 2016 +0100
@@ -43,6 +43,10 @@
 	return newTodo(s, chatID)
 }
 
+func (s *Storage) Reminders() (Reminders, error) {
+	return newReminders(s)
+}
+
 // Prepare the given queries. Most objects (Todo, Fortunes, ...) require that their queries
 // be prepared.
 func (s *Storage) prewarm(queries []string) error {