Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/knowledge/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ sources:
- resource: https://github.com/strelov1/freehire/blob/c0fc3518d5f25c66f39b069c943fe7a1fab840b9/internal/platform/AGENTS.md
- resource: ../../internal/ai/assistant/AGENT-REFERENCE.md
- resource: ../../internal/candidate/cv/AGENT-REFERENCE.md
- resource: ../../internal/api/handler/feed.go
- resource: ../../internal/platform/db/queries/feed_state.sql
---

Freehire aggregates IT jobs through a Go API and workers, with PostgreSQL,
Expand All @@ -35,6 +37,11 @@ mapping is enforced by depguard and `internal/platform/arch/layering`.

For deployment ownership, follow [operations](operations.md).

The Matches API resolves the caller's active source before querying the job index.
PostgreSQL stores the active choice separately from the per-source New watermark.
The API resolves saved searches by owner and leaves a deleted selection in place so
Matches can offer a return to profile matching.

Assistant merge consent is checked by the owner-scoped handler against the transcript;
requirement report normalization belongs to the CV store. Follow the current
[assistant contract](../../internal/ai/assistant/AGENT-REFERENCE.md) and
Expand Down
297 changes: 187 additions & 110 deletions internal/api/handler/feed.go

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions internal/api/handler/feed_cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/strelov1/freehire/internal/search/feed"
)

const feedSourceProfile = "profile"

// profileVersion is the filter version a cursor binds: the profile's UpdatedAt, which
// also moves when excluded_companies does.
func profileVersion(p userprofile.Profile) int64 {
Expand All @@ -20,8 +22,7 @@ func profileVersion(p userprofile.Profile) int64 {

// pageCursor reads ?cursor=. Without one it starts a session: it reads (or creates) the
// source's watermark, fixes the snapshot below the drain high-water, and begins in the new
// phase when anything can be new. source is the resolved source key: profile or search:<id>.
// A bad, expired or foreign cursor, or one for another source, is a 400 and the client
// phase when anything can be new. A bad, expired or foreign cursor is a 400 and the client
// restarts from page 1.
func (h *feedHandlers) pageCursor(c *fiber.Ctx, userID int64, source string) (feed.Cursor, error) {
raw := c.Query("cursor")
Expand Down Expand Up @@ -53,17 +54,18 @@ func (h *feedHandlers) pageCursor(c *fiber.Ctx, userID int64, source string) (fe
}
return feed.Cursor{UserID: userID, Source: source, SnapshotAt: snap, SeenAt: seenAt, ActedBefore: acted, Phase: phase, ExpiresAt: now.Add(feed.CursorTTL).Unix()}, nil
}
cur, err := h.verifyCursor(raw, userID)
// A cursor for another source (the choice changed) restarts the client.
if err != nil || cur.Source != source || (cur.Phase != feed.PhaseNew && cur.Phase != feed.PhaseRest) {
cur, err := h.verifyCursor(raw, userID, source)
if err != nil || (cur.Phase != feed.PhaseNew && cur.Phase != feed.PhaseRest) {
return feed.Cursor{}, fiber.NewError(fiber.StatusBadRequest, "invalid cursor")
}
return cur, nil
}

func (h *feedHandlers) verifyCursor(raw string, userID int64) (feed.Cursor, error) {
// verifyCursor checks a cursor's signature, owner and source. An empty source accepts any:
// POST /me/feed/seen advances whichever source its cursor was signed for.
func (h *feedHandlers) verifyCursor(raw string, userID int64, source string) (feed.Cursor, error) {
cur, err := h.cursors.Verify(raw, time.Now())
if err != nil || cur.UserID != userID {
if err != nil || cur.UserID != userID || (source != "" && cur.Source != source) {
return feed.Cursor{}, fiber.NewError(fiber.StatusBadRequest, "invalid cursor")
}
return cur, nil
Expand Down
63 changes: 63 additions & 0 deletions internal/api/handler/feed_source_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//go:build integration

package handler

import (
"context"
"fmt"
"testing"

"github.com/gofiber/fiber/v2"

"github.com/strelov1/freehire/internal/platform/db"
"github.com/strelov1/freehire/internal/platform/testdb"
)

func TestFeedSourceRejectsIneligibleSavedSearches(t *testing.T) {
ctx := context.Background()
pool := testdb.Pool(t)
q := db.New(pool)

newUser := func(email string) int64 {
t.Helper()
var id int64
if err := pool.QueryRow(ctx, "INSERT INTO users (email) VALUES ($1) RETURNING id", email).Scan(&id); err != nil {
t.Fatal(err)
}
return id
}
owner := newUser("feed-owner@example.test")
other := newUser("feed-other@example.test")

createSearch := func(userID int64, name string, derived bool) int64 {
t.Helper()
row, err := q.CreateSavedSearch(ctx, db.CreateSavedSearchParams{
UserID: userID, Name: name, Query: "q=go", DerivedFromProfile: derived,
})
if err != nil {
t.Fatal(err)
}
return row.ID
}
active := createSearch(owner, "Active", false)
foreign := createSearch(other, "Foreign", false)
derived := createSearch(owner, "Derived", true)
app, issuer := feedStateApp(oneJob(), &feedExcludedAt{}, feedUpdated, feedStateDB{q: q})
pick := func(id int64) int {
return putFeedSource(t, app, issuer, owner, fmt.Sprintf(`{"source":"saved_search","saved_search_id":%d}`, id))
}
if status := pick(active); status != fiber.StatusNoContent {
t.Fatalf("pick owned search: status = %d", status)
}
for name, id := range map[string]int64{"foreign": foreign, "profile-derived": derived} {
t.Run(name, func(t *testing.T) {
if status := pick(id); status != fiber.StatusNotFound {
t.Errorf("status = %d, want 404", status)
}
chosen, err := q.GetFeedSource(ctx, owner)
if err != nil || !chosen.Valid || chosen.Int64 != active {
t.Fatalf("chosen source = %+v, %v; want %d", chosen, err, active)
}
})
}
}
Loading
Loading