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

umputun / tg-spam / 20468833846

23 Dec 2025 06:41PM UTC coverage: 82.447% (-0.01%) from 82.46%
20468833846

push

github

umputun
feat: add e2e UI tests with Playwright

Add comprehensive end-to-end tests for the web UI using playwright-go:

- Page load tests for all 6 pages (checker, samples, users, dictionary, detected spam, settings)
- Navbar navigation tests
- Spam checker form submission (empty and with content)
- Samples management (add spam/ham, delete spam)
- Users management (add and delete)
- Dictionary management (add stop phrase, add ignored word)

Infrastructure:
- e2e-ui/ directory with test file using //go:build e2e tag
- Makefile targets: e2e-ui-setup, e2e-ui (headless), e2e-ui-debug (visible browser)
- GitHub workflow with Playwright browser caching

6449 of 7822 relevant lines covered (82.45%)

274.46 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
        MetaGiveaway             bool          `json:"meta_giveaway"`
93
        MultiLangLimit           int           `json:"multi_lang_limit"`
94
        OpenAIEnabled            bool          `json:"openai_enabled"`
95
        OpenAIVeto               bool          `json:"openai_veto"`
96
        OpenAIHistorySize        int           `json:"openai_history_size"`
97
        OpenAIModel              string        `json:"openai_model"`
98
        OpenAICheckShortMessages bool          `json:"openai_check_short_messages"`
99
        OpenAICustomPrompts      []string      `json:"openai_custom_prompts"`
100
        LuaPluginsEnabled        bool          `json:"lua_plugins_enabled"`
101
        LuaPluginsDir            string        `json:"lua_plugins_dir"`
102
        LuaEnabledPlugins        []string      `json:"lua_enabled_plugins"`
103
        LuaDynamicReload         bool          `json:"lua_dynamic_reload"`
104
        LuaAvailablePlugins      []string      `json:"lua_available_plugins"` // the list of all available Lua plugins
105
        SamplesDataPath          string        `json:"samples_data_path"`
106
        DynamicDataPath          string        `json:"dynamic_data_path"`
107
        WatchIntervalSecs        int           `json:"watch_interval_secs"`
108
        SimilarityThreshold      float64       `json:"similarity_threshold"`
109
        MinMsgLen                int           `json:"min_msg_len"`
110
        MaxEmoji                 int           `json:"max_emoji"`
111
        MinSpamProbability       float64       `json:"min_spam_probability"`
112
        ParanoidMode             bool          `json:"paranoid_mode"`
113
        FirstMessagesCount       int           `json:"first_messages_count"`
114
        StartupMessageEnabled    bool          `json:"startup_message_enabled"`
115
        TrainingEnabled          bool          `json:"training_enabled"`
116
        StorageTimeout           time.Duration `json:"storage_timeout"`
117
        SoftBanEnabled           bool          `json:"soft_ban_enabled"`
118
        AbnormalSpacingEnabled   bool          `json:"abnormal_spacing_enabled"`
119
        HistorySize              int           `json:"history_size"`
120
        DebugModeEnabled         bool          `json:"debug_mode_enabled"`
121
        DryModeEnabled           bool          `json:"dry_mode_enabled"`
122
        TGDebugModeEnabled       bool          `json:"tg_debug_mode_enabled"`
123
}
124

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

299
        return router
5✔
300
}
301

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2✔
1255
        result := make([]byte, length)
2✔
1256
        for i := 0; i < length; i++ {
66✔
1257
                n, err := rand.Int(rand.Reader, big.NewInt(charsetLen))
64✔
1258
                if err != nil {
64✔
1259
                        return "", fmt.Errorf("failed to generate random number: %w", err)
×
1260
                }
×
1261
                result[i] = charset[n.Int64()]
64✔
1262
        }
1263
        return string(result), nil
2✔
1264
}
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