2022-12-26 13:41:45 -05:00
|
|
|
package glist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-12-26 18:09:06 -05:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2022-12-26 21:48:01 -05:00
|
|
|
"strings"
|
2022-12-26 13:41:45 -05:00
|
|
|
"sync"
|
2022-12-26 18:09:06 -05:00
|
|
|
"time"
|
2022-12-26 13:41:45 -05:00
|
|
|
)
|
|
|
|
|
2022-12-26 18:09:06 -05:00
|
|
|
var DataPath string
|
|
|
|
|
2022-12-26 13:41:45 -05:00
|
|
|
type Entry struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
Checked bool `json:"checked"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GList struct {
|
|
|
|
AllMsgCounter int `json:"-"`
|
|
|
|
Mutex sync.Mutex `json:"-"`
|
|
|
|
ChatID int `json:"chat_id"`
|
|
|
|
MessageID *int `json:"message_id"`
|
|
|
|
Items []Entry `json:"items"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGList(chatID int, items ...string) *GList {
|
|
|
|
g := GList{ChatID: chatID}
|
|
|
|
for _, text := range items {
|
|
|
|
g.Items = append(g.Items, Entry{text, false})
|
|
|
|
}
|
|
|
|
return &g
|
|
|
|
}
|
|
|
|
|
2022-12-26 18:09:06 -05:00
|
|
|
var reqs chan *GList
|
|
|
|
|
|
|
|
func DoPersist() {
|
|
|
|
reqs = make(chan *GList, 50)
|
|
|
|
go func() {
|
|
|
|
for range time.Tick(5 * time.Second) {
|
|
|
|
lists := map[*GList]struct{}{}
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case g := <-reqs:
|
|
|
|
lists[g] = struct{}{}
|
|
|
|
default:
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for g := range lists {
|
|
|
|
g.persist()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GList) persist() {
|
|
|
|
g.Mutex.Lock()
|
|
|
|
defer g.Mutex.Unlock()
|
|
|
|
data, err := json.Marshal(g)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln("failed to marshal")
|
|
|
|
}
|
|
|
|
filename := path.Join(DataPath, fmt.Sprintf("chkchat%d", g.ChatID))
|
|
|
|
if err := os.WriteFile(filename, data, 0644); err != nil {
|
|
|
|
log.Panicln("failed to write to file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-26 21:48:01 -05:00
|
|
|
func (g *GList) Add(t string) {
|
|
|
|
outer:
|
|
|
|
for _, text := range strings.Split(t, "\n") {
|
|
|
|
for _, item := range g.Items {
|
|
|
|
if item.Text == text {
|
|
|
|
continue outer
|
|
|
|
}
|
2022-12-26 13:41:45 -05:00
|
|
|
}
|
2022-12-26 21:48:01 -05:00
|
|
|
g.Items = append(g.Items, Entry{text, false})
|
2022-12-26 13:41:45 -05:00
|
|
|
}
|
2022-12-26 18:09:06 -05:00
|
|
|
reqs <- g
|
2022-12-26 13:41:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GList) Toggle(text string) error {
|
|
|
|
for i, item := range g.Items {
|
|
|
|
if item.Text == text {
|
|
|
|
g.Items[i].Checked = !g.Items[i].Checked
|
2022-12-26 18:09:06 -05:00
|
|
|
reqs <- g
|
2022-12-26 13:41:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("not found:%s", text)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GList) ClearChecked() {
|
|
|
|
var remaining []Entry
|
|
|
|
for _, item := range g.Items {
|
|
|
|
if !item.Checked {
|
|
|
|
remaining = append(remaining, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.Items = remaining
|
2022-12-26 18:09:06 -05:00
|
|
|
reqs <- g
|
2022-12-26 13:41:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type button struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
CallbackData string `json:"callback_data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type newListReq struct {
|
|
|
|
ChatID int `json:"chat_id"`
|
|
|
|
MessageID *int `json:"message_id,omitempty"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
ReplyMarkup struct {
|
|
|
|
InlineKeyboard [][]button `json:"inline_keyboard"`
|
|
|
|
} `json:"reply_markup"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeButton(e Entry) button {
|
|
|
|
b := button{Text: e.Text, CallbackData: e.Text}
|
|
|
|
if e.Checked {
|
|
|
|
b.Text = fmt.Sprintf("✓ %s", e.Text)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeButtons(items []Entry) [][]button {
|
|
|
|
var buttons [][]button
|
|
|
|
var current []button
|
|
|
|
for _, item := range items {
|
|
|
|
current = append(current, makeButton(item))
|
|
|
|
if len(current) == 2 {
|
|
|
|
buttons = append(buttons, current)
|
|
|
|
current = []button{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(current) != 0 {
|
|
|
|
buttons = append(buttons, current)
|
|
|
|
}
|
|
|
|
return buttons
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GList) GenSendListReq() ([]byte, error) {
|
2022-12-26 13:46:45 -05:00
|
|
|
req := newListReq{ChatID: g.ChatID, MessageID: g.MessageID, Text: "List:"}
|
2022-12-26 13:41:45 -05:00
|
|
|
itemButtons := makeButtons(g.Items)
|
|
|
|
controlButtons := []button{{"clear checked", "clear"}}
|
|
|
|
req.ReplyMarkup.InlineKeyboard = append(itemButtons, controlButtons)
|
|
|
|
data, err := json.Marshal(req)
|
|
|
|
return data, err
|
|
|
|
}
|