From 30b7fddf1859b980e437b878a9e45b3ddfc4df41 Mon Sep 17 00:00:00 2001 From: fade Date: Tue, 6 Sep 2022 05:31:05 -0400 Subject: [PATCH] add order limit --- README.md | 1 + bot.go | 28 ++++++---- config.go | 3 +- config.json | 1 + limits.go | 143 +++++++++++++++++++++++++++++++++------------------- 5 files changed, 113 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index a0f76a9..c173bd4 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ The bot is configured in a JSON file that looks like this: "Max_toots": 2, "Toots_interval": 12, "Duplicate_buf": 10, + "Order_limit": 1, "Admins": ["admin@example.com"] } ``` diff --git a/bot.go b/bot.go index 4ebb806..ccae133 100644 --- a/bot.go +++ b/bot.go @@ -63,7 +63,7 @@ func RunBot() { add_to_db(acct) InfoLogger.Printf("%s added to database", acct) - var message = fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct) + message := fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct) err := postToot(message, "public") if err != nil { ErrorLogger.Println("Post welcome message") @@ -101,14 +101,22 @@ func RunBot() { InfoLogger.Printf("%s added to database", acct) } - // Message limit - if check_ticket(acct) > 0 { - take_ticket(acct) - InfoLogger.Printf("Ticket of %s was taken", acct) - c.Reblog(ctx, notif.Status.ID) - InfoLogger.Printf("Toot %s of %s was rebloged", tooturl, acct) + // Message order + if check_order(acct) < Conf.Order_limit { + if check_ticket(acct) > 0 { // Message limit + take_ticket(acct) + InfoLogger.Printf("Ticket of %s was taken", acct) + + count_order(acct) + InfoLogger.Printf("Order of %s was counted", acct) + + c.Reblog(ctx, notif.Status.ID) + InfoLogger.Printf("Toot %s of %s was rebloged", tooturl, acct) + } else { + WarnLogger.Printf("%s haven't tickets", acct) + } } else { - WarnLogger.Printf("%s haven't tickets", acct) + WarnLogger.Printf("%s order limit", acct) } } else { WarnLogger.Printf("%s is reply and not boosted", tooturl) @@ -125,10 +133,10 @@ func RunBot() { switch args[1] { case "unboost": c.Unreblog(ctx, mID) - WarnLogger.Printf("%s was unrebloged", mID) + WarnLogger.Printf("%s was unrebloged", mID) case "delete": c.DeleteStatus(ctx, mID) - WarnLogger.Printf("%s was deleted", mID) + WarnLogger.Printf("%s was deleted", mID) } } } else { diff --git a/config.go b/config.go index 712bc39..f58e399 100644 --- a/config.go +++ b/config.go @@ -23,7 +23,8 @@ type Config struct { WelcomeMessage string `json:"WelcomeMessage"` Max_toots uint16 `json:"Max_toots"` Toots_interval uint16 `json:"Toots_interval"` - Duplicate_buf int `json:"Duplicate_buf"` + Duplicate_buf uint16 `json:"Duplicate_buf"` + Order_limit uint16 `json:"Order_limit"` Admins []string `json:"Admins"` } diff --git a/config.json b/config.json index d9c3c43..878869c 100644 --- a/config.json +++ b/config.json @@ -7,5 +7,6 @@ "Max_toots": 2, "Toots_interval": 12, "Duplicate_buf": 10, + "Order_limit": 1, "Admins": ["admin@example.com"] } \ No newline at end of file diff --git a/limits.go b/limits.go index 407e1d4..9b69748 100644 --- a/limits.go +++ b/limits.go @@ -16,18 +16,18 @@ func init_limit_db() *sql.DB { ErrorLogger.Println("Open database") } - cmd1 := `CREATE TABLE IF NOT EXISTS Limits (id INTEGER PRIMARY KEY AUTOINCREMENT, acct TEXT, ticket INTEGER, time TEXT)` + cmd1 := `CREATE TABLE IF NOT EXISTS Limits (id INTEGER PRIMARY KEY AUTOINCREMENT, acct TEXT, ticket INTEGER, order_msg INTEGER, time TEXT)` cmd2 := `CREATE TABLE IF NOT EXISTS MsgHashs (message_hash TEXT)` stat1, err := db.Prepare(cmd1) if err != nil { - ErrorLogger.Println("Create database") + ErrorLogger.Println("Create database and table Limits") } stat1.Exec() stat2, err := db.Prepare(cmd2) if err != nil { - ErrorLogger.Println("Create database") + ErrorLogger.Println("Create database and table MsgHashs") } stat2.Exec() @@ -37,43 +37,12 @@ func init_limit_db() *sql.DB { // Add account to database func add_to_db(acct string) { db := init_limit_db() - cmd := `INSERT INTO Limits (acct, ticket) VALUES (?, ?)` + cmd := `INSERT INTO Limits (acct, ticket, order_msg) VALUES (?, ?, ?)` stat, err := db.Prepare(cmd) if err != nil { ErrorLogger.Println("Add account to databse") } - stat.Exec(acct, Conf.Max_toots) -} - -// Save message hash -func save_msg_hash(hash string) { - db := init_limit_db() - - cmd1 := `SELECT COUNT(*) FROM MsgHashs` - cmd2 := `DELETE FROM MsgHashs WHERE ROWID IN (SELECT ROWID FROM MsgHashs LIMIT 1)` - cmd3 := `INSERT INTO MsgHashs (message_hash) VALUES (?)` - - var rows int - - db.QueryRow(cmd1).Scan(&rows) - - if rows >= Conf.Duplicate_buf { - superfluous := rows - Conf.Duplicate_buf - - for i := 0; i <= superfluous; i++ { - stat2, err := db.Prepare(cmd2) - if err != nil { - ErrorLogger.Println("Delete message hash from database") - } - stat2.Exec() - } - } - - stat1, err := db.Prepare(cmd3) - if err != nil { - ErrorLogger.Println("Add message hash to database") - } - stat1.Exec(hash) + stat.Exec(acct, Conf.Max_toots, 0) } // Check followed once @@ -92,22 +61,6 @@ func followed(acct string) bool { return true } -// Check message hash -func check_msg_hash(hash string) bool { - db := init_limit_db() - cmd := `SELECT message_hash FROM MsgHashs WHERE message_hash = ?` - err := db.QueryRow(cmd, hash).Scan(&hash) - if err != nil { - if err != sql.ErrNoRows { - InfoLogger.Println("Check message hash in database") - } - - return false - } - - return true -} - // Take ticket for tooting func take_ticket(acct string) { db := init_limit_db() @@ -162,3 +115,89 @@ func check_ticket(acct string) uint16 { return tickets } + +// Save message hash +func save_msg_hash(hash string) { + db := init_limit_db() + + cmd1 := `SELECT COUNT(*) FROM MsgHashs` + cmd2 := `DELETE FROM MsgHashs WHERE ROWID IN (SELECT ROWID FROM MsgHashs LIMIT 1)` + cmd3 := `INSERT INTO MsgHashs (message_hash) VALUES (?)` + + var rows uint16 + + db.QueryRow(cmd1).Scan(&rows) + + if rows >= Conf.Duplicate_buf { + superfluous := rows - Conf.Duplicate_buf + + for i := uint16(0); i <= superfluous; i++ { + stat2, err := db.Prepare(cmd2) + if err != nil { + ErrorLogger.Println("Delete message hash from database") + } + stat2.Exec() + } + } + + stat1, err := db.Prepare(cmd3) + if err != nil { + ErrorLogger.Println("Add message hash to database") + } + stat1.Exec(hash) +} + +// Check message hash +func check_msg_hash(hash string) bool { + db := init_limit_db() + cmd := `SELECT message_hash FROM MsgHashs WHERE message_hash = ?` + err := db.QueryRow(cmd, hash).Scan(&hash) + if err != nil { + if err != sql.ErrNoRows { + InfoLogger.Println("Check message hash in database") + } + + return false + } + + return true +} + +// Count order +func count_order(acct string) { + db := init_limit_db() + cmd1 := `UPDATE Limits SET order_msg = ? WHERE acct != ?` + cmd2 := `SELECT order_msg FROM Limits WHERE acct = ?` + cmd3 := `UPDATE Limits SET order_msg = ? WHERE acct = ?` + + stat1, err := db.Prepare(cmd1) + if err != nil { + ErrorLogger.Println("Count order to zero") + } + + stat1.Exec(0, acct) + + var order uint16 + db.QueryRow(cmd2, acct).Scan(&order) + if order < Conf.Order_limit { + order = order + 1 + } + + stat2, err := db.Prepare(cmd3) + if err != nil { + ErrorLogger.Println("Count order") + } + + stat2.Exec(order, acct) +} + +// Check order +func check_order(acct string) uint16 { + db := init_limit_db() + cmd := `SELECT order_msg FROM Limits WHERE acct = ?` + + var order uint16 + db.QueryRow(cmd, acct).Scan(&order) + + return order +}