feat: add chat for backend

This commit is contained in:
2026-06-25 23:34:41 +03:30
parent 9a86d2bd4d
commit d290ba86bb
7 changed files with 406 additions and 0 deletions
+102
View File
@@ -51,6 +51,11 @@ func (h *Handler) Routes(user, pass string) http.Handler {
r.Post("/users/coins", h.adjustCoins)
r.Post("/users/grant", h.grantToUser)
r.Post("/season/reset", h.resetSeason)
r.Get("/chat", h.chatAdmin)
r.Post("/chat/pack/add", h.chatPackAdd)
r.Post("/chat/pack/delete", h.chatPackDelete)
r.Post("/chat/message/add", h.chatMessageAdd)
r.Post("/chat/message/delete", h.chatMessageDelete)
return r
}
@@ -438,6 +443,103 @@ func (h *Handler) grantToUser(w http.ResponseWriter, r *http.Request) {
}
// rows یک کوئری را به []map[string]any تبدیل می‌کند (برای رندرِ عمومیِ جدول‌ها).
// --- مدیریتِ بسته‌های چت و پیام‌ها ---
func (h *Handler) chatAdmin(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
packs := h.rows(ctx,
`SELECT id,title,kind,price_coins,vip,sort,enabled FROM chat_packs ORDER BY sort`,
"id", "title", "kind", "price_coins", "vip", "sort", "enabled")
// پیام‌های هر بسته را زیرِ همان بسته قرار می‌دهیم.
for _, p := range packs {
id, _ := p["id"].(string)
p["messages"] = h.messagesOf(ctx, id)
}
h.render(w, "chat.html", map[string]any{
"Nav": "chat",
"Packs": packs,
"Saved": r.URL.Query().Get("saved") == "1",
})
}
func (h *Handler) messagesOf(ctx context.Context, packID string) []map[string]any {
rs, err := h.db.QueryContext(ctx,
`SELECT id,body,sort FROM chat_messages WHERE pack_id=? ORDER BY sort,id`, packID)
if err != nil {
return nil
}
defer rs.Close()
var out []map[string]any
for rs.Next() {
var id, sort int
var body string
if err := rs.Scan(&id, &body, &sort); err != nil {
return out
}
out = append(out, map[string]any{"id": id, "body": body, "sort": sort})
}
return out
}
func (h *Handler) chatPackAdd(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
id := r.FormValue("id")
if id == "" {
http.Error(w, "id required", http.StatusBadRequest)
return
}
num := func(k string) int { v, _ := strconv.Atoi(r.FormValue(k)); return v }
vip := 0
if r.FormValue("vip") == "on" {
vip = 1
}
kind := r.FormValue("kind")
if kind != "emoji" {
kind = "text"
}
_, err := h.db.ExecContext(r.Context(),
`INSERT OR REPLACE INTO chat_packs (id,title,kind,price_coins,vip,sort,enabled) VALUES (?,?,?,?,?,?,1)`,
id, r.FormValue("title"), kind, num("price_coins"), vip, num("sort"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/chat?saved=1", http.StatusSeeOther)
}
func (h *Handler) chatPackDelete(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
id := r.FormValue("id")
_, _ = h.db.ExecContext(r.Context(), `DELETE FROM chat_messages WHERE pack_id=?`, id)
_, _ = h.db.ExecContext(r.Context(), `DELETE FROM chat_packs WHERE id=?`, id)
http.Redirect(w, r, "/admin/chat?saved=1", http.StatusSeeOther)
}
func (h *Handler) chatMessageAdd(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
pack := r.FormValue("pack_id")
body := r.FormValue("body")
if pack == "" || body == "" {
http.Redirect(w, r, "/admin/chat", http.StatusSeeOther)
return
}
sort, _ := strconv.Atoi(r.FormValue("sort"))
_, err := h.db.ExecContext(r.Context(),
`INSERT INTO chat_messages (pack_id,body,sort) VALUES (?,?,?)`, pack, body, sort)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/chat?saved=1", http.StatusSeeOther)
}
func (h *Handler) chatMessageDelete(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
id, _ := strconv.Atoi(r.FormValue("id"))
_, _ = h.db.ExecContext(r.Context(), `DELETE FROM chat_messages WHERE id=?`, id)
http.Redirect(w, r, "/admin/chat?saved=1", http.StatusSeeOther)
}
func (h *Handler) rows(ctx context.Context, query string, cols ...string) []map[string]any {
rs, err := h.db.QueryContext(ctx, query)
if err != nil {