Tidy stale comments and duplicated helpers
Comments carried over from web_server (CVLayout.vue, messageResolver, the Spotify token code) and ones outdated by later changes (the email window now anchors on ReceivedAt, failed emails are retried) are corrected. truncateRunes had a copy in handlers and in services; there is now one, services.TruncateRunes. cleanEmailBody's regexes are compiled once, a status check that could never fire is gone, and api.js loses its unused put/patch helpers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -74,8 +74,6 @@ type EmailSyncService struct {
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// Microsoft Graph API response types
|
||||
|
||||
// Microsoft Graph API response types. The IMAP backend parses raw RFC822
|
||||
// messages into these same structs (see parseRawEmail) so that everything
|
||||
// downstream of fetchEmails is backend-agnostic.
|
||||
@@ -177,8 +175,8 @@ var statusOrder = map[string]int{
|
||||
// Token persistence
|
||||
|
||||
// SaveMSGraphToken persists an OAuth token to disk with 0600 permissions.
|
||||
// Like the Spotify equivalent it copies into a local struct so the on-disk
|
||||
// shape does not depend on the oauth2 package's own JSON tags.
|
||||
// It copies into a local struct so the on-disk shape does not depend on the
|
||||
// oauth2 package's own JSON tags.
|
||||
func SaveMSGraphToken(path string, tok *oauth2.Token) error {
|
||||
data := struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
@@ -450,7 +448,6 @@ func (s *EmailSyncService) SyncEmails(ctx context.Context) error {
|
||||
since = oldestRetryable.ReceivedAt
|
||||
}
|
||||
|
||||
// Fetch emails from Microsoft Graph
|
||||
emails, err := s.fetchEmails(ctx, since)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fetching emails: %w", err)
|
||||
@@ -767,10 +764,6 @@ func (s *EmailSyncService) createJobApplication(analysis *EmailAnalysis) (string
|
||||
}
|
||||
}
|
||||
|
||||
if app.Status == "" {
|
||||
app.Status = "applied"
|
||||
}
|
||||
|
||||
if err := s.DB.Create(&app).Error; err != nil {
|
||||
return "", nil, fmt.Errorf("creating job application: %w", err)
|
||||
}
|
||||
@@ -827,6 +820,13 @@ func (s *EmailSyncService) updateJobApplication(existing *models.JobApplication,
|
||||
return "updated", &existing.ID, nil
|
||||
}
|
||||
|
||||
// Whitespace patterns for cleanEmailBody. Tags are stripped with htmlAnyTag
|
||||
// (job_sources.go).
|
||||
var (
|
||||
emailSpaces = regexp.MustCompile(`[ \t]+`)
|
||||
emailBlankRuns = regexp.MustCompile(`\n{3,}`)
|
||||
)
|
||||
|
||||
// cleanEmailBody reduces an email body to plain-ish text before it is sent to
|
||||
// the classifier, cutting token cost and noise.
|
||||
//
|
||||
@@ -839,8 +839,7 @@ func cleanEmailBody(content string, contentType string) string {
|
||||
|
||||
if strings.EqualFold(contentType, "html") {
|
||||
// Strip HTML tags
|
||||
tagRegex := regexp.MustCompile(`<[^>]*>`)
|
||||
text = tagRegex.ReplaceAllString(text, " ")
|
||||
text = htmlAnyTag.ReplaceAllString(text, " ")
|
||||
|
||||
// Decode common HTML entities
|
||||
replacer := strings.NewReplacer(
|
||||
@@ -856,10 +855,8 @@ func cleanEmailBody(content string, contentType string) string {
|
||||
}
|
||||
|
||||
// Collapse whitespace
|
||||
spaceRegex := regexp.MustCompile(`[ \t]+`)
|
||||
text = spaceRegex.ReplaceAllString(text, " ")
|
||||
nlRegex := regexp.MustCompile(`\n{3,}`)
|
||||
text = nlRegex.ReplaceAllString(text, "\n\n")
|
||||
text = emailSpaces.ReplaceAllString(text, " ")
|
||||
text = emailBlankRuns.ReplaceAllString(text, "\n\n")
|
||||
text = strings.TrimSpace(text)
|
||||
|
||||
// Cut everything from the first signature marker onwards. The idx > 0
|
||||
@@ -953,8 +950,7 @@ func parseReceivedAt(raw string) time.Time {
|
||||
|
||||
// saveProcessed writes the outcome of an attempt: updating the existing row
|
||||
// when this was a retry, inserting when it is the first attempt. Update is
|
||||
// used rather than Save so the row keeps its original CreatedAt, which the
|
||||
// fetch window depends on.
|
||||
// used rather than Save so the row keeps its original CreatedAt.
|
||||
func (s *EmailSyncService) saveProcessed(found bool, existing, record *models.ProcessedEmail) {
|
||||
var err error
|
||||
if found {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"adam-french.co.uk/job-tracker/internal/claude"
|
||||
"adam-french.co.uk/job-tracker/internal/models"
|
||||
@@ -432,7 +433,7 @@ func (s *JobLeadService) storeLeads(ctx context.Context, src models.JobSource, j
|
||||
// snippet in place rather than losing the lead.
|
||||
if job.detailID != "" && lead.Matched && lead.State == LeadNew {
|
||||
if full, err := s.fetchReedDescription(ctx, job.detailID); err == nil && full != "" {
|
||||
lead.Description = truncateRunes(full, maxLeadRunes)
|
||||
lead.Description = TruncateRunes(full, maxLeadRunes)
|
||||
}
|
||||
}
|
||||
fresh = append(fresh, lead)
|
||||
@@ -473,7 +474,7 @@ func newLead(src models.JobSource, job fetchedJob, filter leadFilter, tracked ma
|
||||
Company: company,
|
||||
Location: strings.TrimSpace(job.Location),
|
||||
URL: strings.TrimSpace(job.URL),
|
||||
Description: truncateRunes(job.Description, maxLeadRunes),
|
||||
Description: TruncateRunes(job.Description, maxLeadRunes),
|
||||
PostedAt: job.PostedAt,
|
||||
LastSeenAt: now,
|
||||
State: LeadNew,
|
||||
@@ -686,10 +687,10 @@ func containsWord(text, word string) bool {
|
||||
}
|
||||
start := from + i
|
||||
end := start + len(word)
|
||||
before := []rune(text[:start])
|
||||
after := []rune(strings.TrimPrefix(text[end:], "s"))
|
||||
if (len(before) == 0 || !isWordRune(before[len(before)-1])) &&
|
||||
(len(after) == 0 || !isWordRune(after[0])) {
|
||||
before, _ := utf8.DecodeLastRuneInString(text[:start])
|
||||
after, _ := utf8.DecodeRuneInString(strings.TrimPrefix(text[end:], "s"))
|
||||
// RuneError is what an empty side decodes to, and is not a word rune.
|
||||
if !isWordRune(before) && !isWordRune(after) {
|
||||
return true
|
||||
}
|
||||
from = start + 1
|
||||
@@ -777,7 +778,7 @@ func (s *JobLeadService) scoreLeads(ctx context.Context, profile *models.JobSear
|
||||
}
|
||||
|
||||
func (s *JobLeadService) scoreLead(ctx context.Context, profile string, lead *models.JobLead) (LeadScore, error) {
|
||||
return s.Scorer.ScorePosting(ctx, truncateRunes(profile, maxProfileRunes),
|
||||
return s.Scorer.ScorePosting(ctx, TruncateRunes(profile, maxProfileRunes),
|
||||
lead.JobTitle, lead.Company, lead.Location, lead.Description)
|
||||
}
|
||||
|
||||
@@ -802,7 +803,7 @@ func LoadJobSearchProfile(db *gorm.DB) (*models.JobSearchProfile, error) {
|
||||
// SaveJobSearchProfile stores an edited profile, bounding the free text.
|
||||
func SaveJobSearchProfile(db *gorm.DB, profile *models.JobSearchProfile) error {
|
||||
profile.ID = jobSearchProfileID
|
||||
profile.Profile = truncateRunes(strings.TrimSpace(profile.Profile), maxProfileRunes)
|
||||
profile.Profile = TruncateRunes(strings.TrimSpace(profile.Profile), maxProfileRunes)
|
||||
profile.MinScore = min(max(profile.MinScore, 0), 100)
|
||||
return db.Save(profile).Error
|
||||
}
|
||||
|
||||
@@ -491,9 +491,9 @@ func htmlToText(content string) string {
|
||||
return strings.TrimSpace(strings.Join(lines, "\n"))
|
||||
}
|
||||
|
||||
// truncateRunes cuts s to at most max runes, on a rune boundary so the result
|
||||
// TruncateRunes cuts s to at most max runes, on a rune boundary so the result
|
||||
// is still valid UTF-8 for Postgres and for JSON.
|
||||
func truncateRunes(s string, max int) string {
|
||||
func TruncateRunes(s string, max int) string {
|
||||
runes := []rune(s)
|
||||
if len(runes) <= max {
|
||||
return s
|
||||
|
||||
@@ -279,3 +279,12 @@ func TestWithWorkplace(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncateRunes(t *testing.T) {
|
||||
if got := TruncateRunes("££££", 2); got != "££" {
|
||||
t.Fatalf("got %q; the cut must fall on a rune boundary", got)
|
||||
}
|
||||
if got := TruncateRunes("short", 10); got != "short" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ func firstApplication(query *gorm.DB) (*models.JobApplication, error) {
|
||||
}
|
||||
|
||||
// JobCaptureInput is a posting to add to the tracker. Its JSON form is the
|
||||
// body of POST /jobs/capture: the popup's fields.
|
||||
// body of POST /jobs/api/capture: the popup's fields.
|
||||
type JobCaptureInput struct {
|
||||
JobTitle string `json:"jobTitle"`
|
||||
Company string `json:"company"`
|
||||
|
||||
Reference in New Issue
Block a user