• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

umputun / tg-spam / 20279197792

16 Dec 2025 06:52PM UTC coverage: 82.444% (+0.09%) from 82.351%
20279197792

push

github

web-flow
Merge pull request #356 from umputun/fix/rest-writeheader-renderjson

fix(webapi): use EncodeJSON to set correct Content-Type header

27 of 33 new or added lines in 1 file covered. (81.82%)

1 existing line in 1 file now uncovered.

6410 of 7775 relevant lines covered (82.44%)

275.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.58
/app/webapi/webapi.go
1
// Package webapi provides a web API spam detection service.
2
package webapi
3

4
import (
5
        "bytes"
6
        "compress/gzip"
7
        "context"
8
        "crypto/rand"
9
        "crypto/sha1" //nolint
10
        "embed"
11
        "encoding/json"
12
        "errors"
13
        "fmt"
14
        "html/template"
15
        "io"
16
        "io/fs"
17
        "math/big"
18
        "net/http"
19
        "path"
20
        "strconv"
21
        "strings"
22
        "time"
23

24
        "github.com/didip/tollbooth/v8"
25
        log "github.com/go-pkgz/lgr"
26
        "github.com/go-pkgz/rest"
27
        "github.com/go-pkgz/rest/logger"
28
        "github.com/go-pkgz/routegroup"
29

30
        "github.com/umputun/tg-spam/app/storage"
31
        "github.com/umputun/tg-spam/app/storage/engine"
32
        "github.com/umputun/tg-spam/lib/approved"
33
        "github.com/umputun/tg-spam/lib/spamcheck"
34
)
35

36
//go:generate moq --out mocks/detector.go --pkg mocks --with-resets --skip-ensure . Detector
37
//go:generate moq --out mocks/spam_filter.go --pkg mocks --with-resets --skip-ensure . SpamFilter
38
//go:generate moq --out mocks/locator.go --pkg mocks --with-resets --skip-ensure . Locator
39
//go:generate moq --out mocks/detected_spam.go --pkg mocks --with-resets --skip-ensure . DetectedSpam
40
//go:generate moq --out mocks/storage_engine.go --pkg mocks --with-resets --skip-ensure . StorageEngine
41
//go:generate moq --out mocks/dictionary.go --pkg mocks --with-resets --skip-ensure . Dictionary
42

43
//go:embed assets/* assets/components/*
44
var templateFS embed.FS
45
var tmpl = template.Must(template.ParseFS(templateFS, "assets/*.html", "assets/components/*.html"))
46

47
// startTime tracks when the server started
48
var startTime = time.Now()
49

50
// Server is a web API server.
51
type Server struct {
52
        Config
53
}
54

55
// Config defines  server parameters
56
type Config struct {
57
        Version       string        // version to show in /ping
58
        ListenAddr    string        // listen address
59
        Detector      Detector      // spam detector
60
        SpamFilter    SpamFilter    // spam filter (bot)
61
        DetectedSpam  DetectedSpam  // detected spam accessor
62
        Locator       Locator       // locator for user info
63
        Dictionary    Dictionary    // dictionary for stop phrases and ignored words
64
        StorageEngine StorageEngine // database engine access for backups
65
        AuthPasswd    string        // basic auth password for user "tg-spam"
66
        AuthHash      string        // basic auth hash for user "tg-spam". If both AuthPasswd and AuthHash are provided, AuthHash is used
67
        Dbg           bool          // debug mode
68
        Settings      Settings      // application settings
69
}
70

71
// Settings contains all application settings
72
type Settings struct {
73
        InstanceID               string        `json:"instance_id"`
74
        PrimaryGroup             string        `json:"primary_group"`
75
        AdminGroup               string        `json:"admin_group"`
76
        DisableAdminSpamForward  bool          `json:"disable_admin_spam_forward"`
77
        LoggerEnabled            bool          `json:"logger_enabled"`
78
        SuperUsers               []string      `json:"super_users"`
79
        NoSpamReply              bool          `json:"no_spam_reply"`
80
        CasEnabled               bool          `json:"cas_enabled"`
81
        MetaEnabled              bool          `json:"meta_enabled"`
82
        MetaLinksLimit           int           `json:"meta_links_limit"`
83
        MetaMentionsLimit        int           `json:"meta_mentions_limit"`
84
        MetaLinksOnly            bool          `json:"meta_links_only"`
85
        MetaImageOnly            bool          `json:"meta_image_only"`
86
        MetaVideoOnly            bool          `json:"meta_video_only"`
87
        MetaAudioOnly            bool          `json:"meta_audio_only"`
88
        MetaForwarded            bool          `json:"meta_forwarded"`
89
        MetaKeyboard             bool          `json:"meta_keyboard"`
90
        MetaContactOnly          bool          `json:"meta_contact_only"`
91
        MetaUsernameSymbols      string        `json:"meta_username_symbols"`
92
        MultiLangLimit           int           `json:"multi_lang_limit"`
93
        OpenAIEnabled            bool          `json:"openai_enabled"`
94
        OpenAIVeto               bool          `json:"openai_veto"`
95
        OpenAIHistorySize        int           `json:"openai_history_size"`
96
        OpenAIModel              string        `json:"openai_model"`
97
        OpenAICheckShortMessages bool          `json:"openai_check_short_messages"`
98
        OpenAICustomPrompts      []string      `json:"openai_custom_prompts"`
99
        LuaPluginsEnabled        bool          `json:"lua_plugins_enabled"`
100
        LuaPluginsDir            string        `json:"lua_plugins_dir"`
101
        LuaEnabledPlugins        []string      `json:"lua_enabled_plugins"`
102
        LuaDynamicReload         bool          `json:"lua_dynamic_reload"`
103
        LuaAvailablePlugins      []string      `json:"lua_available_plugins"` // the list of all available Lua plugins
104
        SamplesDataPath          string        `json:"samples_data_path"`
105
        DynamicDataPath          string        `json:"dynamic_data_path"`
106
        WatchIntervalSecs        int           `json:"watch_interval_secs"`
107
        SimilarityThreshold      float64       `json:"similarity_threshold"`
108
        MinMsgLen                int           `json:"min_msg_len"`
109
        MaxEmoji                 int           `json:"max_emoji"`
110
        MinSpamProbability       float64       `json:"min_spam_probability"`
111
        ParanoidMode             bool          `json:"paranoid_mode"`
112
        FirstMessagesCount       int           `json:"first_messages_count"`
113
        StartupMessageEnabled    bool          `json:"startup_message_enabled"`
114
        TrainingEnabled          bool          `json:"training_enabled"`
115
        StorageTimeout           time.Duration `json:"storage_timeout"`
116
        SoftBanEnabled           bool          `json:"soft_ban_enabled"`
117
        AbnormalSpacingEnabled   bool          `json:"abnormal_spacing_enabled"`
118
        HistorySize              int           `json:"history_size"`
119
        DebugModeEnabled         bool          `json:"debug_mode_enabled"`
120
        DryModeEnabled           bool          `json:"dry_mode_enabled"`
121
        TGDebugModeEnabled       bool          `json:"tg_debug_mode_enabled"`
122
}
123

124
// Detector is a spam detector interface.
125
type Detector interface {
126
        Check(req spamcheck.Request) (spam bool, cr []spamcheck.Response)
127
        ApprovedUsers() []approved.UserInfo
128
        AddApprovedUser(user approved.UserInfo) error
129
        RemoveApprovedUser(id string) error
130
        GetLuaPluginNames() []string // Returns the list of available Lua plugin names
131
}
132

133
// SpamFilter is a spam filter, bot interface.
134
type SpamFilter interface {
135
        UpdateSpam(msg string) error
136
        UpdateHam(msg string) error
137
        ReloadSamples() (err error)
138
        DynamicSamples() (spam, ham []string, err error)
139
        RemoveDynamicSpamSample(sample string) error
140
        RemoveDynamicHamSample(sample string) error
141
}
142

143
// Locator is a storage interface used to get user id by name and vice versa.
144
type Locator interface {
145
        UserIDByName(ctx context.Context, userName string) int64
146
        UserNameByID(ctx context.Context, userID int64) string
147
}
148

149
// DetectedSpam is a storage interface used to get detected spam messages and set added flag.
150
type DetectedSpam interface {
151
        Read(ctx context.Context) ([]storage.DetectedSpamInfo, error)
152
        SetAddedToSamplesFlag(ctx context.Context, id int64) error
153
        FindByUserID(ctx context.Context, userID int64) (*storage.DetectedSpamInfo, error)
154
}
155

156
// StorageEngine provides access to the database engine for operations like backup
157
type StorageEngine interface {
158
        Backup(ctx context.Context, w io.Writer) error
159
        Type() engine.Type
160
        BackupSqliteAsPostgres(ctx context.Context, w io.Writer) error
161
}
162

163
// Dictionary is a storage interface for managing stop phrases and ignored words
164
type Dictionary interface {
165
        Add(ctx context.Context, t storage.DictionaryType, data string) error
166
        Delete(ctx context.Context, id int64) error
167
        Read(ctx context.Context, t storage.DictionaryType) ([]string, error)
168
        ReadWithIDs(ctx context.Context, t storage.DictionaryType) ([]storage.DictionaryEntry, error)
169
        Stats(ctx context.Context) (*storage.DictionaryStats, error)
170
}
171

172
// NewServer creates a new web API server.
173
func NewServer(config Config) *Server {
73✔
174
        return &Server{Config: config}
73✔
175
}
73✔
176

177
// Run starts server and accepts requests checking for spam messages.
178
func (s *Server) Run(ctx context.Context) error {
3✔
179
        router := routegroup.New(http.NewServeMux())
3✔
180
        router.Use(rest.Recoverer(log.Default()))
3✔
181
        router.Use(logger.New(logger.Log(log.Default()), logger.Prefix("[DEBUG]")).Handler)
3✔
182
        router.Use(rest.Throttle(1000))
3✔
183
        router.Use(rest.AppInfo("tg-spam", "umputun", s.Version), rest.Ping)
3✔
184
        router.Use(tollbooth.HTTPMiddleware(tollbooth.NewLimiter(50, nil)))
3✔
185
        router.Use(rest.SizeLimit(1024 * 1024)) // 1M max request size
3✔
186

3✔
187
        if s.AuthPasswd != "" || s.AuthHash != "" {
6✔
188
                log.Printf("[INFO] basic auth enabled for webapi server")
3✔
189
                if s.AuthHash != "" {
4✔
190
                        router.Use(rest.BasicAuthWithBcryptHashAndPrompt("tg-spam", s.AuthHash))
1✔
191
                } else {
3✔
192
                        router.Use(rest.BasicAuthWithPrompt("tg-spam", s.AuthPasswd))
2✔
193
                }
2✔
194
        } else {
×
195
                log.Printf("[WARN] basic auth disabled, access to webapi is not protected")
×
196
        }
×
197

198
        router = s.routes(router) // setup routes
3✔
199

3✔
200
        srv := &http.Server{Addr: s.ListenAddr, Handler: router, ReadTimeout: 5 * time.Second, WriteTimeout: 5 * time.Second}
3✔
201
        go func() {
6✔
202
                <-ctx.Done()
3✔
203
                if err := srv.Shutdown(ctx); err != nil {
3✔
UNCOV
204
                        log.Printf("[WARN] failed to shutdown webapi server: %v", err)
×
205
                } else {
3✔
206
                        log.Printf("[INFO] webapi server stopped")
3✔
207
                }
3✔
208
        }()
209

210
        log.Printf("[INFO] start webapi server on %s", s.ListenAddr)
3✔
211
        if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
3✔
212
                return fmt.Errorf("failed to run server: %w", err)
×
213
        }
×
214
        return nil
3✔
215
}
216

217
func (s *Server) routes(router *routegroup.Bundle) *routegroup.Bundle {
5✔
218
        // auth api routes
5✔
219
        router.Group().Route(func(authApi *routegroup.Bundle) {
10✔
220
                authApi.Use(s.authMiddleware(rest.BasicAuthWithUserPasswd("tg-spam", s.AuthPasswd)))
5✔
221
                authApi.HandleFunc("POST /check", s.checkMsgHandler)         // check a message for spam
5✔
222
                authApi.HandleFunc("GET /check/{user_id}", s.checkIDHandler) // check user id for spam
5✔
223

5✔
224
                authApi.Mount("/update").Route(func(r *routegroup.Bundle) {
10✔
225
                        // update spam/ham samples
5✔
226
                        r.HandleFunc("POST /spam", s.updateSampleHandler(s.SpamFilter.UpdateSpam)) // update spam samples
5✔
227
                        r.HandleFunc("POST /ham", s.updateSampleHandler(s.SpamFilter.UpdateHam))   // update ham samples
5✔
228
                })
5✔
229

230
                authApi.Mount("/delete").Route(func(r *routegroup.Bundle) {
10✔
231
                        // delete spam/ham samples
5✔
232
                        r.HandleFunc("POST /spam", s.deleteSampleHandler(s.SpamFilter.RemoveDynamicSpamSample))
5✔
233
                        r.HandleFunc("POST /ham", s.deleteSampleHandler(s.SpamFilter.RemoveDynamicHamSample))
5✔
234
                })
5✔
235

236
                authApi.Mount("/download").Route(func(r *routegroup.Bundle) {
10✔
237
                        r.HandleFunc("GET /spam", s.downloadSampleHandler(func(spam, _ []string) ([]string, string) {
5✔
238
                                return spam, "spam.txt"
×
239
                        }))
×
240
                        r.HandleFunc("GET /ham", s.downloadSampleHandler(func(_, ham []string) ([]string, string) {
5✔
241
                                return ham, "ham.txt"
×
242
                        }))
×
243
                        r.HandleFunc("GET /detected_spam", s.downloadDetectedSpamHandler)
5✔
244
                        r.HandleFunc("GET /backup", s.downloadBackupHandler)
5✔
245
                        r.HandleFunc("GET /export-to-postgres", s.downloadExportToPostgresHandler)
5✔
246
                })
247

248
                authApi.HandleFunc("GET /samples", s.getDynamicSamplesHandler)    // get dynamic samples
5✔
249
                authApi.HandleFunc("PUT /samples", s.reloadDynamicSamplesHandler) // reload samples
5✔
250

5✔
251
                authApi.Mount("/users").Route(func(r *routegroup.Bundle) { // manage approved users
10✔
252
                        // add user to the approved list and storage
5✔
253
                        r.HandleFunc("POST /add", s.updateApprovedUsersHandler(s.Detector.AddApprovedUser))
5✔
254
                        // remove user from an approved list and storage
5✔
255
                        r.HandleFunc("POST /delete", s.updateApprovedUsersHandler(s.removeApprovedUser))
5✔
256
                        // get approved users
5✔
257
                        r.HandleFunc("GET /", s.getApprovedUsersHandler)
5✔
258
                })
5✔
259

260
                authApi.HandleFunc("GET /settings", s.getSettingsHandler) // get application settings
5✔
261

5✔
262
                authApi.Mount("/dictionary").Route(func(r *routegroup.Bundle) { // manage dictionary
10✔
263
                        // add stop phrase or ignored word
5✔
264
                        r.HandleFunc("POST /add", s.addDictionaryEntryHandler)
5✔
265
                        // delete entry by id
5✔
266
                        r.HandleFunc("POST /delete", s.deleteDictionaryEntryHandler)
5✔
267
                        // get all entries
5✔
268
                        r.HandleFunc("GET /", s.getDictionaryEntriesHandler)
5✔
269
                })
5✔
270
        })
271

272
        router.Group().Route(func(webUI *routegroup.Bundle) {
10✔
273
                webUI.Use(s.authMiddleware(rest.BasicAuthWithPrompt("tg-spam", s.AuthPasswd)))
5✔
274
                webUI.HandleFunc("GET /", s.htmlSpamCheckHandler)                         // serve template for webUI UI
5✔
275
                webUI.HandleFunc("GET /manage_samples", s.htmlManageSamplesHandler)       // serve manage samples page
5✔
276
                webUI.HandleFunc("GET /manage_users", s.htmlManageUsersHandler)           // serve manage users page
5✔
277
                webUI.HandleFunc("GET /manage_dictionary", s.htmlManageDictionaryHandler) // serve manage dictionary page
5✔
278
                webUI.HandleFunc("GET /detected_spam", s.htmlDetectedSpamHandler)         // serve detected spam page
5✔
279
                webUI.HandleFunc("GET /list_settings", s.htmlSettingsHandler)             // serve settings
5✔
280
                webUI.HandleFunc("POST /detected_spam/add", s.htmlAddDetectedSpamHandler) // add detected spam to samples
5✔
281

5✔
282
                // handle logout - force Basic Auth re-authentication
5✔
283
                webUI.HandleFunc("GET /logout", func(w http.ResponseWriter, _ *http.Request) {
5✔
284
                        w.Header().Set("WWW-Authenticate", `Basic realm="tg-spam"`)
×
285
                        w.WriteHeader(http.StatusUnauthorized)
×
286
                        fmt.Fprintln(w, "Logged out successfully")
×
287
                })
×
288

289
                // serve only specific static files at root level
290
                staticFiles := newStaticFS(templateFS,
5✔
291
                        staticFileMapping{urlPath: "styles.css", filesysPath: "assets/styles.css"},
5✔
292
                        staticFileMapping{urlPath: "logo.png", filesysPath: "assets/logo.png"},
5✔
293
                        staticFileMapping{urlPath: "spinner.svg", filesysPath: "assets/spinner.svg"},
5✔
294
                )
5✔
295
                webUI.HandleFiles("/", http.FS(staticFiles))
5✔
296
        })
297

298
        return router
5✔
299
}
300

301
// checkMsgHandler handles POST /check request.
302
// it gets message text and user id from request body and returns spam status and check results.
303
func (s *Server) checkMsgHandler(w http.ResponseWriter, r *http.Request) {
9✔
304
        type CheckResultDisplay struct {
9✔
305
                Spam   bool
9✔
306
                Checks []spamcheck.Response
9✔
307
        }
9✔
308

9✔
309
        isHtmxRequest := r.Header.Get("HX-Request") == "true"
9✔
310

9✔
311
        req := spamcheck.Request{CheckOnly: true}
9✔
312
        if !isHtmxRequest {
17✔
313
                // API request
8✔
314
                if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
10✔
315
                        _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "can't decode request", "details": err.Error()})
2✔
316
                        log.Printf("[WARN] can't decode request: %v", err)
2✔
317
                        return
2✔
318
                }
2✔
319
        } else {
1✔
320
                // for hx-request (HTMX) we need to get the values from the form
1✔
321
                req.UserID = r.FormValue("user_id")
1✔
322
                req.UserName = r.FormValue("user_name")
1✔
323
                req.Msg = r.FormValue("msg")
1✔
324
        }
1✔
325

326
        spam, cr := s.Detector.Check(req)
7✔
327
        if !isHtmxRequest {
13✔
328
                // for API request return JSON
6✔
329
                rest.RenderJSON(w, rest.JSON{"spam": spam, "checks": cr})
6✔
330
                return
6✔
331
        }
6✔
332

333
        if req.Msg == "" {
1✔
334
                w.Header().Set("HX-Retarget", "#error-message")
×
335
                fmt.Fprintln(w, "<div class='alert alert-danger'>Valid message required.</div>")
×
336
                return
×
337
        }
×
338

339
        // render result for HTMX request
340
        resultDisplay := CheckResultDisplay{
1✔
341
                Spam:   spam,
1✔
342
                Checks: cr,
1✔
343
        }
1✔
344

1✔
345
        if err := tmpl.ExecuteTemplate(w, "check_results", resultDisplay); err != nil {
1✔
346
                log.Printf("[WARN] can't execute result template: %v", err)
×
347
                http.Error(w, "Error rendering result", http.StatusInternalServerError)
×
348
                return
×
349
        }
×
350
}
351

352
// checkIDHandler handles GET /check/{user_id} request.
353
// it returns JSON with the status "spam" or "ham" for a given user id.
354
// if user is spammer, it also returns check results.
355
func (s *Server) checkIDHandler(w http.ResponseWriter, r *http.Request) {
4✔
356
        type info struct {
4✔
357
                UserName  string               `json:"user_name,omitempty"`
4✔
358
                Message   string               `json:"message,omitempty"`
4✔
359
                Timestamp time.Time            `json:"timestamp,omitempty"`
4✔
360
                Checks    []spamcheck.Response `json:"checks,omitempty"`
4✔
361
        }
4✔
362
        resp := struct {
4✔
363
                Status string `json:"status"`
4✔
364
                Info   *info  `json:"info,omitempty"`
4✔
365
        }{
4✔
366
                Status: "ham",
4✔
367
        }
4✔
368

4✔
369
        userID, err := strconv.ParseInt(r.PathValue("user_id"), 10, 64)
4✔
370
        if err != nil {
5✔
371
                _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "can't parse user id", "details": err.Error()})
1✔
372
                return
1✔
373
        }
1✔
374

375
        si, err := s.DetectedSpam.FindByUserID(r.Context(), userID)
3✔
376
        if err != nil {
4✔
377
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't get user info", "details": err.Error()})
1✔
378
                return
1✔
379
        }
1✔
380
        if si != nil {
3✔
381
                resp.Status = "spam"
1✔
382
                resp.Info = &info{
1✔
383
                        UserName:  si.UserName,
1✔
384
                        Message:   si.Text,
1✔
385
                        Timestamp: si.Timestamp,
1✔
386
                        Checks:    si.Checks,
1✔
387
                }
1✔
388
        }
1✔
389
        rest.RenderJSON(w, resp)
2✔
390
}
391

392
// getDynamicSamplesHandler handles GET /samples request. It returns dynamic samples both for spam and ham.
393
func (s *Server) getDynamicSamplesHandler(w http.ResponseWriter, _ *http.Request) {
2✔
394
        spam, ham, err := s.SpamFilter.DynamicSamples()
2✔
395
        if err != nil {
3✔
396
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't get dynamic samples", "details": err.Error()})
1✔
397
                return
1✔
398
        }
1✔
399
        rest.RenderJSON(w, rest.JSON{"spam": spam, "ham": ham})
1✔
400
}
401

402
// downloadSampleHandler handles GET /download/spam|ham request. It returns dynamic samples both for spam and ham.
403
func (s *Server) downloadSampleHandler(pickFn func(spam, ham []string) ([]string, string)) func(w http.ResponseWriter, r *http.Request) {
13✔
404
        return func(w http.ResponseWriter, _ *http.Request) {
16✔
405
                spam, ham, err := s.SpamFilter.DynamicSamples()
3✔
406
                if err != nil {
4✔
407
                        _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't get dynamic samples", "details": err.Error()})
1✔
408
                        return
1✔
409
                }
1✔
410
                samples, name := pickFn(spam, ham)
2✔
411
                body := strings.Join(samples, "\n")
2✔
412
                w.Header().Set("Content-Type", "text/plain; charset=utf-8")
2✔
413
                w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", name))
2✔
414
                w.Header().Set("Content-Length", strconv.Itoa(len(body)))
2✔
415
                w.WriteHeader(http.StatusOK)
2✔
416
                _, _ = w.Write([]byte(body))
2✔
417
        }
418
}
419

420
// updateSampleHandler handles POST /update/spam|ham request. It updates dynamic samples both for spam and ham.
421
func (s *Server) updateSampleHandler(updFn func(msg string) error) func(w http.ResponseWriter, r *http.Request) {
15✔
422
        return func(w http.ResponseWriter, r *http.Request) {
22✔
423
                var req struct {
7✔
424
                        Msg string `json:"msg"`
7✔
425
                }
7✔
426

7✔
427
                isHtmxRequest := r.Header.Get("HX-Request") == "true"
7✔
428

7✔
429
                if isHtmxRequest {
7✔
430
                        req.Msg = r.FormValue("msg")
×
431
                } else {
7✔
432
                        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
9✔
433
                                _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "can't decode request", "details": err.Error()})
2✔
434
                                return
2✔
435
                        }
2✔
436
                }
437

438
                err := updFn(req.Msg)
5✔
439
                if err != nil {
7✔
440
                        _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't update samples", "details": err.Error()})
2✔
441
                        return
2✔
442
                }
2✔
443

444
                if isHtmxRequest {
3✔
445
                        s.renderSamples(w, "samples_list")
×
446
                } else {
3✔
447
                        rest.RenderJSON(w, rest.JSON{"updated": true, "msg": req.Msg})
3✔
448
                }
3✔
449
        }
450
}
451

452
// deleteSampleHandler handles DELETE /samples request. It deletes dynamic samples both for spam and ham.
453
func (s *Server) deleteSampleHandler(delFn func(msg string) error) func(w http.ResponseWriter, r *http.Request) {
13✔
454
        return func(w http.ResponseWriter, r *http.Request) {
18✔
455
                var req struct {
5✔
456
                        Msg string `json:"msg"`
5✔
457
                }
5✔
458
                isHtmxRequest := r.Header.Get("HX-Request") == "true"
5✔
459
                if isHtmxRequest {
6✔
460
                        req.Msg = r.FormValue("msg")
1✔
461
                } else {
5✔
462
                        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
4✔
NEW
463
                                _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "can't decode request", "details": err.Error()})
×
464
                                return
×
465
                        }
×
466
                }
467

468
                if err := delFn(req.Msg); err != nil {
6✔
469
                        _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't delete sample", "details": err.Error()})
1✔
470
                        return
1✔
471
                }
1✔
472

473
                if isHtmxRequest {
5✔
474
                        s.renderSamples(w, "samples_list")
1✔
475
                } else {
4✔
476
                        rest.RenderJSON(w, rest.JSON{"deleted": true, "msg": req.Msg, "count": 1})
3✔
477
                }
3✔
478
        }
479
}
480

481
// reloadDynamicSamplesHandler handles PUT /samples request. It reloads dynamic samples from db storage.
482
func (s *Server) reloadDynamicSamplesHandler(w http.ResponseWriter, _ *http.Request) {
2✔
483
        if err := s.SpamFilter.ReloadSamples(); err != nil {
3✔
484
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't reload samples", "details": err.Error()})
1✔
485
                return
1✔
486
        }
1✔
487
        rest.RenderJSON(w, rest.JSON{"reloaded": true})
1✔
488
}
489

490
// updateApprovedUsersHandler handles POST /users/add and /users/delete requests, it adds or removes users from approved list.
491
func (s *Server) updateApprovedUsersHandler(updFn func(ui approved.UserInfo) error) func(w http.ResponseWriter, r *http.Request) {
15✔
492
        return func(w http.ResponseWriter, r *http.Request) {
25✔
493
                req := approved.UserInfo{}
10✔
494
                isHtmxRequest := r.Header.Get("HX-Request") == "true"
10✔
495
                if isHtmxRequest {
11✔
496
                        req.UserID = r.FormValue("user_id")
1✔
497
                        req.UserName = r.FormValue("user_name")
1✔
498
                } else {
10✔
499
                        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
10✔
500
                                _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "can't decode request", "details": err.Error()})
1✔
501
                                return
1✔
502
                        }
1✔
503
                }
504

505
                // try to get userID from request and fallback to userName lookup if it's empty
506
                if req.UserID == "" {
14✔
507
                        req.UserID = strconv.FormatInt(s.Locator.UserIDByName(r.Context(), req.UserName), 10)
5✔
508
                }
5✔
509

510
                if req.UserID == "" || req.UserID == "0" {
11✔
511
                        if isHtmxRequest {
2✔
512
                                w.Header().Set("HX-Retarget", "#error-message")
×
513
                                fmt.Fprintln(w, "<div class='alert alert-danger'>Either userid or valid username required.</div>")
×
514
                                return
×
515
                        }
×
516
                        _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "user ID is required"})
2✔
517
                        return
2✔
518
                }
519

520
                // add or remove user from the approved list of detector
521
                if err := updFn(req); err != nil {
7✔
NEW
522
                        _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't update approved users", "details": err.Error()})
×
523
                        return
×
524
                }
×
525

526
                if isHtmxRequest {
8✔
527
                        users := s.Detector.ApprovedUsers()
1✔
528
                        tmplData := struct {
1✔
529
                                ApprovedUsers      []approved.UserInfo
1✔
530
                                TotalApprovedUsers int
1✔
531
                        }{
1✔
532
                                ApprovedUsers:      users,
1✔
533
                                TotalApprovedUsers: len(users),
1✔
534
                        }
1✔
535

1✔
536
                        if err := tmpl.ExecuteTemplate(w, "users_list", tmplData); err != nil {
1✔
537
                                http.Error(w, "Error executing template", http.StatusInternalServerError)
×
538
                                return
×
539
                        }
×
540

541
                } else {
6✔
542
                        rest.RenderJSON(w, rest.JSON{"updated": true, "user_id": req.UserID, "user_name": req.UserName})
6✔
543
                }
6✔
544
        }
545
}
546

547
// removeApprovedUser is adopter for updateApprovedUsersHandler updFn
548
func (s *Server) removeApprovedUser(req approved.UserInfo) error {
2✔
549
        if err := s.Detector.RemoveApprovedUser(req.UserID); err != nil {
2✔
550
                return fmt.Errorf("failed to remove approved user %s: %w", req.UserID, err)
×
551
        }
×
552
        return nil
2✔
553
}
554

555
// getApprovedUsersHandler handles GET /users request. It returns list of approved users.
556
func (s *Server) getApprovedUsersHandler(w http.ResponseWriter, _ *http.Request) {
1✔
557
        rest.RenderJSON(w, rest.JSON{"user_ids": s.Detector.ApprovedUsers()})
1✔
558
}
1✔
559

560
// getSettingsHandler returns application settings, including the list of available Lua plugins
561
func (s *Server) getSettingsHandler(w http.ResponseWriter, _ *http.Request) {
3✔
562
        // get the list of available Lua plugins before returning settings
3✔
563
        s.Settings.LuaAvailablePlugins = s.Detector.GetLuaPluginNames()
3✔
564
        rest.RenderJSON(w, s.Settings)
3✔
565
}
3✔
566

567
// getDictionaryEntriesHandler handles GET /dictionary request. It returns stop phrases and ignored words.
568
func (s *Server) getDictionaryEntriesHandler(w http.ResponseWriter, r *http.Request) {
3✔
569
        stopPhrases, err := s.Dictionary.Read(r.Context(), storage.DictionaryTypeStopPhrase)
3✔
570
        if err != nil {
4✔
571
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't get stop phrases", "details": err.Error()})
1✔
572
                return
1✔
573
        }
1✔
574

575
        ignoredWords, err := s.Dictionary.Read(r.Context(), storage.DictionaryTypeIgnoredWord)
2✔
576
        if err != nil {
3✔
577
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't get ignored words", "details": err.Error()})
1✔
578
                return
1✔
579
        }
1✔
580

581
        rest.RenderJSON(w, rest.JSON{"stop_phrases": stopPhrases, "ignored_words": ignoredWords})
1✔
582
}
583

584
// addDictionaryEntryHandler handles POST /dictionary/add request. It adds a stop phrase or ignored word.
585
func (s *Server) addDictionaryEntryHandler(w http.ResponseWriter, r *http.Request) {
11✔
586
        var req struct {
11✔
587
                Type string `json:"type"`
11✔
588
                Data string `json:"data"`
11✔
589
        }
11✔
590

11✔
591
        isHtmxRequest := r.Header.Get("HX-Request") == "true"
11✔
592

11✔
593
        if isHtmxRequest {
15✔
594
                req.Type = r.FormValue("type")
4✔
595
                req.Data = r.FormValue("data")
4✔
596
        } else {
11✔
597
                if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
8✔
598
                        _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "can't decode request", "details": err.Error()})
1✔
599
                        return
1✔
600
                }
1✔
601
        }
602

603
        if req.Data == "" {
13✔
604
                if isHtmxRequest {
4✔
605
                        w.Header().Set("HX-Retarget", "#error-message")
1✔
606
                        fmt.Fprintln(w, "<div class='alert alert-danger'>Data cannot be empty.</div>")
1✔
607
                        return
1✔
608
                }
1✔
609
                _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "data cannot be empty"})
2✔
610
                return
2✔
611
        }
612

613
        dictType := storage.DictionaryType(req.Type)
7✔
614
        if err := dictType.Validate(); err != nil {
9✔
615
                if isHtmxRequest {
3✔
616
                        w.Header().Set("HX-Retarget", "#error-message")
1✔
617
                        fmt.Fprintf(w, "<div class='alert alert-danger'>Invalid type: %v</div>", err)
1✔
618
                        return
1✔
619
                }
1✔
620
                _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "invalid type", "details": err.Error()})
1✔
621
                return
1✔
622
        }
623

624
        if err := s.Dictionary.Add(r.Context(), dictType, req.Data); err != nil {
6✔
625
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't add entry", "details": err.Error()})
1✔
626
                return
1✔
627
        }
1✔
628

629
        // reload samples to apply dictionary changes immediately
630
        if err := s.SpamFilter.ReloadSamples(); err != nil {
6✔
631
                log.Printf("[WARN] failed to reload samples after dictionary add: %v", err)
2✔
632
                if !isHtmxRequest {
3✔
633
                        _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "entry added but reload failed", "details": err.Error()})
1✔
634
                        return
1✔
635
                }
1✔
636
                // for HTMX, log but continue rendering (entry was added successfully)
637
        }
638

639
        if isHtmxRequest {
5✔
640
                s.renderDictionary(r.Context(), w, "dictionary_list")
2✔
641
        } else {
3✔
642
                rest.RenderJSON(w, rest.JSON{"added": true, "type": req.Type, "data": req.Data})
1✔
643
        }
1✔
644
}
645

646
// deleteDictionaryEntryHandler handles POST /dictionary/delete request. It deletes an entry by data.
647
func (s *Server) deleteDictionaryEntryHandler(w http.ResponseWriter, r *http.Request) {
7✔
648
        var req struct {
7✔
649
                ID int64 `json:"id"`
7✔
650
        }
7✔
651

7✔
652
        isHtmxRequest := r.Header.Get("HX-Request") == "true"
7✔
653

7✔
654
        if isHtmxRequest {
10✔
655
                idStr := r.FormValue("id")
3✔
656
                var err error
3✔
657
                req.ID, err = strconv.ParseInt(idStr, 10, 64)
3✔
658
                if err != nil {
4✔
659
                        w.Header().Set("HX-Retarget", "#error-message")
1✔
660
                        fmt.Fprintf(w, "<div class='alert alert-danger'>Invalid ID: %v</div>", err)
1✔
661
                        return
1✔
662
                }
1✔
663
        } else {
4✔
664
                if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
5✔
665
                        _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "can't decode request", "details": err.Error()})
1✔
666
                        return
1✔
667
                }
1✔
668
        }
669

670
        if err := s.Dictionary.Delete(r.Context(), req.ID); err != nil {
6✔
671
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't delete entry", "details": err.Error()})
1✔
672
                return
1✔
673
        }
1✔
674

675
        // reload samples to apply dictionary changes immediately
676
        if err := s.SpamFilter.ReloadSamples(); err != nil {
6✔
677
                log.Printf("[WARN] failed to reload samples after dictionary delete: %v", err)
2✔
678
                if !isHtmxRequest {
3✔
679
                        _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "entry deleted but reload failed", "details": err.Error()})
1✔
680
                        return
1✔
681
                }
1✔
682
                // for HTMX, log but continue rendering (entry was deleted successfully)
683
        }
684

685
        if isHtmxRequest {
5✔
686
                s.renderDictionary(r.Context(), w, "dictionary_list")
2✔
687
        } else {
3✔
688
                rest.RenderJSON(w, rest.JSON{"deleted": true, "id": req.ID})
1✔
689
        }
1✔
690
}
691

692
// htmlSpamCheckHandler handles GET / request.
693
// It returns rendered spam_check.html template with all the components.
694
func (s *Server) htmlSpamCheckHandler(w http.ResponseWriter, _ *http.Request) {
3✔
695
        tmplData := struct {
3✔
696
                Version string
3✔
697
        }{
3✔
698
                Version: s.Version,
3✔
699
        }
3✔
700

3✔
701
        if err := tmpl.ExecuteTemplate(w, "spam_check.html", tmplData); err != nil {
4✔
702
                log.Printf("[WARN] can't execute template: %v", err)
1✔
703
                http.Error(w, "Error executing template", http.StatusInternalServerError)
1✔
704
                return
1✔
705
        }
1✔
706
}
707

708
// htmlManageSamplesHandler handles GET /manage_samples request.
709
// It returns rendered manage_samples.html template with all the components.
710
func (s *Server) htmlManageSamplesHandler(w http.ResponseWriter, _ *http.Request) {
1✔
711
        s.renderSamples(w, "manage_samples.html")
1✔
712
}
1✔
713

714
func (s *Server) htmlManageUsersHandler(w http.ResponseWriter, _ *http.Request) {
3✔
715
        users := s.Detector.ApprovedUsers()
3✔
716
        tmplData := struct {
3✔
717
                ApprovedUsers      []approved.UserInfo
3✔
718
                TotalApprovedUsers int
3✔
719
        }{
3✔
720
                ApprovedUsers:      users,
3✔
721
                TotalApprovedUsers: len(users),
3✔
722
        }
3✔
723
        tmplData.TotalApprovedUsers = len(tmplData.ApprovedUsers)
3✔
724

3✔
725
        if err := tmpl.ExecuteTemplate(w, "manage_users.html", tmplData); err != nil {
4✔
726
                log.Printf("[WARN] can't execute template: %v", err)
1✔
727
                http.Error(w, "Error executing template", http.StatusInternalServerError)
1✔
728
                return
1✔
729
        }
1✔
730
}
731

732
func (s *Server) htmlManageDictionaryHandler(w http.ResponseWriter, r *http.Request) {
×
733
        s.renderDictionary(r.Context(), w, "manage_dictionary.html")
×
734
}
×
735

736
func (s *Server) htmlDetectedSpamHandler(w http.ResponseWriter, r *http.Request) {
2✔
737
        ds, err := s.DetectedSpam.Read(r.Context())
2✔
738
        if err != nil {
3✔
739
                log.Printf("[ERROR] Failed to fetch detected spam: %v", err)
1✔
740
                http.Error(w, "Internal Server Error", http.StatusInternalServerError)
1✔
741
                return
1✔
742
        }
1✔
743

744
        // clean up detected spam entries
745
        for i, d := range ds {
3✔
746
                d.Text = strings.ReplaceAll(d.Text, "'", " ")
2✔
747
                d.Text = strings.ReplaceAll(d.Text, "\n", " ")
2✔
748
                d.Text = strings.ReplaceAll(d.Text, "\r", " ")
2✔
749
                d.Text = strings.ReplaceAll(d.Text, "\t", " ")
2✔
750
                d.Text = strings.ReplaceAll(d.Text, "\"", " ")
2✔
751
                d.Text = strings.ReplaceAll(d.Text, "\\", " ")
2✔
752
                ds[i] = d
2✔
753
        }
2✔
754

755
        // get filter from query param, default to "all"
756
        filter := r.URL.Query().Get("filter")
1✔
757
        if filter == "" {
2✔
758
                filter = "all"
1✔
759
        }
1✔
760

761
        // apply filtering
762
        var filteredDS []storage.DetectedSpamInfo
1✔
763
        switch filter {
1✔
764
        case "non-classified":
×
765
                for _, entry := range ds {
×
766
                        hasClassifierHam := false
×
767
                        for _, check := range entry.Checks {
×
768
                                if check.Name == "classifier" && !check.Spam {
×
769
                                        hasClassifierHam = true
×
770
                                        break
×
771
                                }
772
                        }
773
                        if hasClassifierHam {
×
774
                                filteredDS = append(filteredDS, entry)
×
775
                        }
×
776
                }
777
        case "openai":
×
778
                for _, entry := range ds {
×
779
                        hasOpenAI := false
×
780
                        for _, check := range entry.Checks {
×
781
                                if check.Name == "openai" {
×
782
                                        hasOpenAI = true
×
783
                                        break
×
784
                                }
785
                        }
786
                        if hasOpenAI {
×
787
                                filteredDS = append(filteredDS, entry)
×
788
                        }
×
789
                }
790
        default: // "all" or any other value
1✔
791
                filteredDS = ds
1✔
792
        }
793

794
        tmplData := struct {
1✔
795
                DetectedSpamEntries []storage.DetectedSpamInfo
1✔
796
                TotalDetectedSpam   int
1✔
797
                FilteredCount       int
1✔
798
                Filter              string
1✔
799
                OpenAIEnabled       bool
1✔
800
        }{
1✔
801
                DetectedSpamEntries: filteredDS,
1✔
802
                TotalDetectedSpam:   len(ds),
1✔
803
                FilteredCount:       len(filteredDS),
1✔
804
                Filter:              filter,
1✔
805
                OpenAIEnabled:       s.Settings.OpenAIEnabled,
1✔
806
        }
1✔
807

1✔
808
        // if it's an HTMX request, render both content and count display for OOB swap
1✔
809
        if r.Header.Get("HX-Request") == "true" {
1✔
810
                var buf bytes.Buffer
×
811

×
812
                // first render the content template
×
813
                if err := tmpl.ExecuteTemplate(&buf, "detected_spam_content", tmplData); err != nil {
×
814
                        log.Printf("[WARN] can't execute content template: %v", err)
×
815
                        http.Error(w, "Error executing template", http.StatusInternalServerError)
×
816
                        return
×
817
                }
×
818

819
                // then append OOB swap for the count display
820
                countHTML := ""
×
821
                if filter != "all" {
×
822
                        countHTML = fmt.Sprintf("(%d/%d)", len(filteredDS), len(ds))
×
823
                } else {
×
824
                        countHTML = fmt.Sprintf("(%d)", len(ds))
×
825
                }
×
826

827
                buf.WriteString(fmt.Sprintf(`<span id="count-display" hx-swap-oob="true">%s</span>`, countHTML))
×
828

×
829
                // write the combined response
×
830
                if _, err := buf.WriteTo(w); err != nil {
×
831
                        log.Printf("[WARN] failed to write response: %v", err)
×
832
                }
×
833
                return
×
834
        }
835

836
        // full page render for normal requests
837
        if err := tmpl.ExecuteTemplate(w, "detected_spam.html", tmplData); err != nil {
1✔
838
                log.Printf("[WARN] can't execute template: %v", err)
×
839
                http.Error(w, "Error executing template", http.StatusInternalServerError)
×
840
                return
×
841
        }
×
842
}
843

844
func (s *Server) htmlAddDetectedSpamHandler(w http.ResponseWriter, r *http.Request) {
5✔
845
        reportErr := func(err error, _ int) {
9✔
846
                w.Header().Set("HX-Retarget", "#error-message")
4✔
847
                fmt.Fprintf(w, "<div class='alert alert-danger'>%s</div>", err)
4✔
848
        }
4✔
849
        msg := r.FormValue("msg")
5✔
850

5✔
851
        id, err := strconv.ParseInt(r.FormValue("id"), 10, 64)
5✔
852
        if err != nil || msg == "" {
7✔
853
                log.Printf("[WARN] bad request: %v", err)
2✔
854
                reportErr(fmt.Errorf("bad request: %v", err), http.StatusBadRequest)
2✔
855
                return
2✔
856
        }
2✔
857

858
        if err := s.SpamFilter.UpdateSpam(msg); err != nil {
4✔
859
                log.Printf("[WARN] failed to update spam samples: %v", err)
1✔
860
                reportErr(fmt.Errorf("can't update spam samples: %v", err), http.StatusInternalServerError)
1✔
861
                return
1✔
862

1✔
863
        }
1✔
864
        if err := s.DetectedSpam.SetAddedToSamplesFlag(r.Context(), id); err != nil {
3✔
865
                log.Printf("[WARN] failed to update detected spam: %v", err)
1✔
866
                reportErr(fmt.Errorf("can't update detected spam: %v", err), http.StatusInternalServerError)
1✔
867
                return
1✔
868
        }
1✔
869
        w.WriteHeader(http.StatusOK)
1✔
870
}
871

872
func (s *Server) htmlSettingsHandler(w http.ResponseWriter, _ *http.Request) {
4✔
873
        // get database information if StorageEngine is available
4✔
874
        var dbInfo struct {
4✔
875
                DatabaseType   string `json:"database_type"`
4✔
876
                GID            string `json:"gid"`
4✔
877
                DatabaseStatus string `json:"database_status"`
4✔
878
        }
4✔
879

4✔
880
        if s.StorageEngine != nil {
6✔
881
                // try to cast to SQL engine to get type information
2✔
882
                if sqlEngine, ok := s.StorageEngine.(*engine.SQL); ok {
2✔
883
                        dbInfo.DatabaseType = string(sqlEngine.Type())
×
884
                        dbInfo.GID = sqlEngine.GID()
×
885
                        dbInfo.DatabaseStatus = "Connected"
×
886
                } else {
2✔
887
                        dbInfo.DatabaseType = "Unknown"
2✔
888
                        dbInfo.DatabaseStatus = "Connected (unknown type)"
2✔
889
                }
2✔
890
        } else {
2✔
891
                dbInfo.DatabaseStatus = "Not connected"
2✔
892
        }
2✔
893

894
        // get backup information
895
        backupURL := "/download/backup"
4✔
896
        backupFilename := fmt.Sprintf("tg-spam-backup-%s-%s.sql.gz", dbInfo.DatabaseType, time.Now().Format("20060102-150405"))
4✔
897

4✔
898
        // get system info - uptime since server start
4✔
899
        uptime := time.Since(startTime)
4✔
900

4✔
901
        // get the list of available Lua plugins
4✔
902
        s.Settings.LuaAvailablePlugins = s.Detector.GetLuaPluginNames()
4✔
903

4✔
904
        data := struct {
4✔
905
                Settings
4✔
906
                Version  string
4✔
907
                Database struct {
4✔
908
                        Type   string
4✔
909
                        GID    string
4✔
910
                        Status string
4✔
911
                }
4✔
912
                Backup struct {
4✔
913
                        URL      string
4✔
914
                        Filename string
4✔
915
                }
4✔
916
                System struct {
4✔
917
                        Uptime string
4✔
918
                }
4✔
919
        }{
4✔
920
                Settings: s.Settings,
4✔
921
                Version:  s.Version,
4✔
922
                Database: struct {
4✔
923
                        Type   string
4✔
924
                        GID    string
4✔
925
                        Status string
4✔
926
                }{
4✔
927
                        Type:   dbInfo.DatabaseType,
4✔
928
                        GID:    dbInfo.GID,
4✔
929
                        Status: dbInfo.DatabaseStatus,
4✔
930
                },
4✔
931
                Backup: struct {
4✔
932
                        URL      string
4✔
933
                        Filename string
4✔
934
                }{
4✔
935
                        URL:      backupURL,
4✔
936
                        Filename: backupFilename,
4✔
937
                },
4✔
938
                System: struct {
4✔
939
                        Uptime string
4✔
940
                }{
4✔
941
                        Uptime: formatDuration(uptime),
4✔
942
                },
4✔
943
        }
4✔
944

4✔
945
        if err := tmpl.ExecuteTemplate(w, "settings.html", data); err != nil {
5✔
946
                log.Printf("[WARN] can't execute template: %v", err)
1✔
947
                http.Error(w, "Error executing template", http.StatusInternalServerError)
1✔
948
                return
1✔
949
        }
1✔
950
}
951

952
// formatDuration formats a duration in a human-readable way
953
func formatDuration(d time.Duration) string {
12✔
954
        days := int(d.Hours() / 24)
12✔
955
        hours := int(d.Hours()) % 24
12✔
956
        minutes := int(d.Minutes()) % 60
12✔
957

12✔
958
        if days > 0 {
15✔
959
                return fmt.Sprintf("%dd %dh %dm", days, hours, minutes)
3✔
960
        }
3✔
961

962
        if hours > 0 {
11✔
963
                return fmt.Sprintf("%dh %dm", hours, minutes)
2✔
964
        }
2✔
965

966
        return fmt.Sprintf("%dm", minutes)
7✔
967
}
968

969
func (s *Server) downloadDetectedSpamHandler(w http.ResponseWriter, r *http.Request) {
3✔
970
        ctx := r.Context()
3✔
971
        spam, err := s.DetectedSpam.Read(ctx)
3✔
972
        if err != nil {
4✔
973
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't get detected spam", "details": err.Error()})
1✔
974
                return
1✔
975
        }
1✔
976

977
        type jsonSpamInfo struct {
2✔
978
                ID        int64                `json:"id"`
2✔
979
                GID       string               `json:"gid"`
2✔
980
                Text      string               `json:"text"`
2✔
981
                UserID    int64                `json:"user_id"`
2✔
982
                UserName  string               `json:"user_name"`
2✔
983
                Timestamp time.Time            `json:"timestamp"`
2✔
984
                Added     bool                 `json:"added"`
2✔
985
                Checks    []spamcheck.Response `json:"checks"`
2✔
986
        }
2✔
987

2✔
988
        // convert entries to jsonl format with lowercase fields
2✔
989
        lines := make([]string, 0, len(spam))
2✔
990
        for _, entry := range spam {
5✔
991
                data, err := json.Marshal(jsonSpamInfo{
3✔
992
                        ID:        entry.ID,
3✔
993
                        GID:       entry.GID,
3✔
994
                        Text:      entry.Text,
3✔
995
                        UserID:    entry.UserID,
3✔
996
                        UserName:  entry.UserName,
3✔
997
                        Timestamp: entry.Timestamp,
3✔
998
                        Added:     entry.Added,
3✔
999
                        Checks:    entry.Checks,
3✔
1000
                })
3✔
1001
                if err != nil {
3✔
NEW
1002
                        _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't marshal entry", "details": err.Error()})
×
1003
                        return
×
1004
                }
×
1005
                lines = append(lines, string(data))
3✔
1006
        }
1007

1008
        body := strings.Join(lines, "\n")
2✔
1009
        w.Header().Set("Content-Type", "application/x-jsonlines")
2✔
1010
        w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", "detected_spam.jsonl"))
2✔
1011
        w.Header().Set("Content-Length", strconv.Itoa(len(body)))
2✔
1012
        w.WriteHeader(http.StatusOK)
2✔
1013
        _, _ = w.Write([]byte(body))
2✔
1014
}
1015

1016
// downloadBackupHandler streams a database backup as an SQL file with gzip compression
1017
// Files are always compressed and always have .gz extension to ensure consistency
1018
func (s *Server) downloadBackupHandler(w http.ResponseWriter, r *http.Request) {
2✔
1019
        if s.StorageEngine == nil {
3✔
1020
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "storage engine not available"})
1✔
1021
                return
1✔
1022
        }
1✔
1023

1024
        // set filename based on database type and timestamp
1025
        dbType := "db"
1✔
1026
        sqlEng, ok := s.StorageEngine.(*engine.SQL)
1✔
1027
        if ok {
1✔
1028
                dbType = string(sqlEng.Type())
×
1029
        }
×
1030
        timestamp := time.Now().Format("20060102-150405")
1✔
1031

1✔
1032
        // always use a .gz extension as the content is always compressed
1✔
1033
        filename := fmt.Sprintf("tg-spam-backup-%s-%s.sql.gz", dbType, timestamp)
1✔
1034

1✔
1035
        // set headers for file download - note we're using application/octet-stream
1✔
1036
        // instead of application/sql to prevent browsers from trying to interpret the file
1✔
1037
        w.Header().Set("Content-Type", "application/octet-stream")
1✔
1038
        w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
1✔
1039
        w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
1✔
1040
        w.Header().Set("Pragma", "no-cache")
1✔
1041
        w.Header().Set("Expires", "0")
1✔
1042

1✔
1043
        // create a gzip writer that streams to response
1✔
1044
        gzipWriter := gzip.NewWriter(w)
1✔
1045
        defer func() {
2✔
1046
                if err := gzipWriter.Close(); err != nil {
1✔
1047
                        log.Printf("[ERROR] failed to close gzip writer: %v", err)
×
1048
                }
×
1049
        }()
1050

1051
        // stream backup directly to response through gzip
1052
        if err := s.StorageEngine.Backup(r.Context(), gzipWriter); err != nil {
1✔
1053
                log.Printf("[ERROR] failed to create backup: %v", err)
×
1054
                // we've already started writing the response, so we can't send a proper error response
×
1055
                return
×
1056
        }
×
1057

1058
        // flush the gzip writer to ensure all data is written
1059
        if err := gzipWriter.Flush(); err != nil {
1✔
1060
                log.Printf("[ERROR] failed to flush gzip writer: %v", err)
×
1061
        }
×
1062
}
1063

1064
// downloadExportToPostgresHandler streams a PostgreSQL-compatible export from a SQLite database
1065
func (s *Server) downloadExportToPostgresHandler(w http.ResponseWriter, r *http.Request) {
3✔
1066
        if s.StorageEngine == nil {
4✔
1067
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "storage engine not available"})
1✔
1068
                return
1✔
1069
        }
1✔
1070

1071
        // check if the database is SQLite
1072
        if s.StorageEngine.Type() != engine.Sqlite {
3✔
1073
                _ = rest.EncodeJSON(w, http.StatusBadRequest, rest.JSON{"error": "source database must be SQLite"})
1✔
1074
                return
1✔
1075
        }
1✔
1076

1077
        // set filename based on timestamp
1078
        timestamp := time.Now().Format("20060102-150405")
1✔
1079
        filename := fmt.Sprintf("tg-spam-sqlite-to-postgres-%s.sql.gz", timestamp)
1✔
1080

1✔
1081
        // set headers for file download
1✔
1082
        w.Header().Set("Content-Type", "application/octet-stream")
1✔
1083
        w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
1✔
1084
        w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
1✔
1085
        w.Header().Set("Pragma", "no-cache")
1✔
1086
        w.Header().Set("Expires", "0")
1✔
1087

1✔
1088
        // create a gzip writer that streams to response
1✔
1089
        gzipWriter := gzip.NewWriter(w)
1✔
1090
        defer func() {
2✔
1091
                if err := gzipWriter.Close(); err != nil {
1✔
1092
                        log.Printf("[ERROR] failed to close gzip writer: %v", err)
×
1093
                }
×
1094
        }()
1095

1096
        // stream export directly to response through gzip
1097
        if err := s.StorageEngine.BackupSqliteAsPostgres(r.Context(), gzipWriter); err != nil {
1✔
1098
                log.Printf("[ERROR] failed to create export: %v", err)
×
1099
                // we've already started writing the response, so we can't send a proper error response
×
1100
                return
×
1101
        }
×
1102

1103
        // flush the gzip writer to ensure all data is written
1104
        if err := gzipWriter.Flush(); err != nil {
1✔
1105
                log.Printf("[ERROR] failed to flush gzip writer: %v", err)
×
1106
        }
×
1107
}
1108

1109
func (s *Server) renderSamples(w http.ResponseWriter, tmplName string) {
6✔
1110
        spam, ham, err := s.SpamFilter.DynamicSamples()
6✔
1111
        if err != nil {
7✔
1112
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't fetch samples", "details": err.Error()})
1✔
1113
                return
1✔
1114
        }
1✔
1115

1116
        spam, ham = s.reverseSamples(spam, ham)
5✔
1117

5✔
1118
        type smpleWithID struct {
5✔
1119
                ID     string
5✔
1120
                Sample string
5✔
1121
        }
5✔
1122

5✔
1123
        makeID := func(s string) string {
19✔
1124
                hash := sha1.New() //nolint
14✔
1125
                if _, err := hash.Write([]byte(s)); err != nil {
14✔
1126
                        return fmt.Sprintf("%x", s)
×
1127
                }
×
1128
                return fmt.Sprintf("%x", hash.Sum(nil))
14✔
1129
        }
1130

1131
        tmplData := struct {
5✔
1132
                SpamSamples      []smpleWithID
5✔
1133
                HamSamples       []smpleWithID
5✔
1134
                TotalHamSamples  int
5✔
1135
                TotalSpamSamples int
5✔
1136
        }{
5✔
1137
                TotalHamSamples:  len(ham),
5✔
1138
                TotalSpamSamples: len(spam),
5✔
1139
        }
5✔
1140
        for _, s := range spam {
12✔
1141
                tmplData.SpamSamples = append(tmplData.SpamSamples, smpleWithID{ID: makeID(s), Sample: s})
7✔
1142
        }
7✔
1143
        for _, h := range ham {
12✔
1144
                tmplData.HamSamples = append(tmplData.HamSamples, smpleWithID{ID: makeID(h), Sample: h})
7✔
1145
        }
7✔
1146

1147
        if err := tmpl.ExecuteTemplate(w, tmplName, tmplData); err != nil {
6✔
1148
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't execute template", "details": err.Error()})
1✔
1149
                return
1✔
1150
        }
1✔
1151
}
1152

1153
func (s *Server) authMiddleware(mw func(next http.Handler) http.Handler) func(next http.Handler) http.Handler {
10✔
1154
        if s.AuthPasswd == "" {
16✔
1155
                return func(next http.Handler) http.Handler {
93✔
1156
                        return next
87✔
1157
                }
87✔
1158
        }
1159
        return func(next http.Handler) http.Handler {
62✔
1160
                return mw(next)
58✔
1161
        }
58✔
1162
}
1163

1164
// reverseSamples returns reversed lists of spam and ham samples
1165
func (s *Server) reverseSamples(spam, ham []string) (revSpam, revHam []string) {
8✔
1166
        revSpam = make([]string, len(spam))
8✔
1167
        revHam = make([]string, len(ham))
8✔
1168

8✔
1169
        for i, j := 0, len(spam)-1; i < len(spam); i, j = i+1, j-1 {
19✔
1170
                revSpam[i] = spam[j]
11✔
1171
        }
11✔
1172
        for i, j := 0, len(ham)-1; i < len(ham); i, j = i+1, j-1 {
19✔
1173
                revHam[i] = ham[j]
11✔
1174
        }
11✔
1175
        return revSpam, revHam
8✔
1176
}
1177

1178
// renderDictionary renders dictionary entries for HTMX or full page request
1179
func (s *Server) renderDictionary(ctx context.Context, w http.ResponseWriter, tmplName string) {
4✔
1180
        stopPhrases, err := s.Dictionary.ReadWithIDs(ctx, storage.DictionaryTypeStopPhrase)
4✔
1181
        if err != nil {
4✔
NEW
1182
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't fetch stop phrases", "details": err.Error()})
×
1183
                return
×
1184
        }
×
1185

1186
        ignoredWords, err := s.Dictionary.ReadWithIDs(ctx, storage.DictionaryTypeIgnoredWord)
4✔
1187
        if err != nil {
4✔
NEW
1188
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't fetch ignored words", "details": err.Error()})
×
1189
                return
×
1190
        }
×
1191

1192
        tmplData := struct {
4✔
1193
                StopPhrases       []storage.DictionaryEntry
4✔
1194
                IgnoredWords      []storage.DictionaryEntry
4✔
1195
                TotalStopPhrases  int
4✔
1196
                TotalIgnoredWords int
4✔
1197
        }{
4✔
1198
                StopPhrases:       stopPhrases,
4✔
1199
                IgnoredWords:      ignoredWords,
4✔
1200
                TotalStopPhrases:  len(stopPhrases),
4✔
1201
                TotalIgnoredWords: len(ignoredWords),
4✔
1202
        }
4✔
1203

4✔
1204
        if err := tmpl.ExecuteTemplate(w, tmplName, tmplData); err != nil {
4✔
NEW
1205
                _ = rest.EncodeJSON(w, http.StatusInternalServerError, rest.JSON{"error": "can't execute template", "details": err.Error()})
×
1206
                return
×
1207
        }
×
1208
}
1209

1210
// staticFS is a filtered filesystem that only exposes specific static files
1211
type staticFS struct {
1212
        fs        fs.FS
1213
        urlToPath map[string]string
1214
}
1215

1216
// staticFileMapping defines a mapping between URL path and filesystem path
1217
type staticFileMapping struct {
1218
        urlPath     string
1219
        filesysPath string
1220
}
1221

1222
func newStaticFS(fsys fs.FS, files ...staticFileMapping) *staticFS {
5✔
1223
        urlToPath := make(map[string]string)
5✔
1224
        for _, f := range files {
20✔
1225
                urlToPath[f.urlPath] = f.filesysPath
15✔
1226
        }
15✔
1227

1228
        return &staticFS{
5✔
1229
                fs:        fsys,
5✔
1230
                urlToPath: urlToPath,
5✔
1231
        }
5✔
1232
}
1233

1234
func (sfs *staticFS) Open(name string) (fs.File, error) {
5✔
1235
        cleanName := path.Clean("/" + name)[1:]
5✔
1236

5✔
1237
        fsPath, ok := sfs.urlToPath[cleanName]
5✔
1238
        if !ok {
7✔
1239
                return nil, fs.ErrNotExist
2✔
1240
        }
2✔
1241

1242
        file, err := sfs.fs.Open(fsPath)
3✔
1243
        if err != nil {
3✔
1244
                return nil, fmt.Errorf("failed to open static file %s: %w", fsPath, err)
×
1245
        }
×
1246
        return file, nil
3✔
1247
}
1248

1249
// GenerateRandomPassword generates a random password of a given length
1250
func GenerateRandomPassword(length int) (string, error) {
2✔
1251
        const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+"
2✔
1252
        const charsetLen = int64(len(charset))
2✔
1253

2✔
1254
        result := make([]byte, length)
2✔
1255
        for i := 0; i < length; i++ {
66✔
1256
                n, err := rand.Int(rand.Reader, big.NewInt(charsetLen))
64✔
1257
                if err != nil {
64✔
1258
                        return "", fmt.Errorf("failed to generate random number: %w", err)
×
1259
                }
×
1260
                result[i] = charset[n.Int64()]
64✔
1261
        }
1262
        return string(result), nil
2✔
1263
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc