diff --git a/api/dependecyInjection.go b/api/dependecyInjection.go index 70feb32..ba9572b 100644 --- a/api/dependecyInjection.go +++ b/api/dependecyInjection.go @@ -397,7 +397,7 @@ func getUseCases(repos *Repositories, imageStorage domainImage.Storage, settings Token: authtoken.New([]byte(settings.JWTSecret), authtoken.WithRevocationStore(sessionRevocation)), CreateArticleUseCase: useCases.NewCreateArticleUseCase(repos.Transactor, renderer, recorder, repos.SiteRepo, repos.AuthorRepo), GetArticleUseCase: useCases.NewGetArticleUseCase(repos.ArticleRepo, renderer), - ListArticlesUseCase: useCases.NewListArticlesUseCase(repos.ArticleRepo, renderer, repos.SiteRepo), + ListArticlesUseCase: useCases.NewListArticlesUseCase(repos.ArticleRepo, renderer, repos.SiteRepo, repos.LocalizationRepo), UpdateArticleUseCase: useCases.NewUpdateArticleUseCase(repos.ArticleRepo, renderer, recorder, repos.SiteRepo, repos.AuthorRepo, repos.ImageRepo, repos.LocalizationRepo, moveStory), DeleteArticleUseCase: useCases.NewDeleteArticleUseCase(repos.ArticleRepo, recorder), CreateTranslationUseCase: useCases.NewCreateTranslationUseCase(repos.Transactor, repos.ArticleRepo, repos.LocalizationRepo, renderer, recorder), diff --git a/api/handlers/article/articleHandler.go b/api/handlers/article/articleHandler.go index 429fabb..845de3c 100644 --- a/api/handlers/article/articleHandler.go +++ b/api/handlers/article/articleHandler.go @@ -7,6 +7,7 @@ import ( validator "RenewCMS/api/pkg/validator" domain "RenewCMS/internal/domain/article" "RenewCMS/internal/domain/author" + "RenewCMS/internal/domain/language" "RenewCMS/internal/infrastructure/useCases" "encoding/json" "errors" @@ -559,6 +560,29 @@ type listQuery struct { Site *string `validate:"omitempty,min=1"` SEO string `validate:"omitempty,oneof=ready suggestions hidden"` Sort string `validate:"oneof=title:asc title:desc status:asc status:desc created_at:asc created_at:desc updated_at:asc updated_at:desc"` + Group *string `validate:"required_if=Tab missing,omitempty,eq=story"` + Tab *string `validate:"omitempty,oneof=all missing"` + Lang *string `validate:"omitempty,min=1"` + Author *uint32 `validate:"omitempty,gte=1"` +} + +const groupByStory = "story" + +type storyItemResponse struct { + listItemResponse + StoryUpdatedAt time.Time `json:"story_updated_at"` + StoryAuthors []author.Byline `json:"story_authors"` + Coverage []domain.Translation `json:"coverage"` + MissingLangs []string `json:"missing_langs"` + Capabilities []string `json:"capabilities"` +} + +type storyListResponse struct { + Items []storyItemResponse `json:"items"` + Total int `json:"total"` + Page int `json:"page"` + Size int `json:"size"` + Counts domain.StoryCounts `json:"counts"` } const defaultListSort = "updated_at:desc" @@ -612,7 +636,38 @@ func readListQuery(values url.Values) (listQuery, error) { if values.Has("seo") && seoStatus == "" { return listQuery{}, errors.New("empty seo") } - return listQuery{Page: page, Size: size, Q: values.Get("q"), Status: status, Sort: sortValue, Site: siteKey, SEO: seoStatus}, nil + query := listQuery{Page: page, Size: size, Q: values.Get("q"), Status: status, Sort: sortValue, Site: siteKey, SEO: seoStatus} + if err := readLocalizedQuery(values, &query); err != nil { + return listQuery{}, err + } + return query, nil +} + +func readLocalizedQuery(values url.Values, query *listQuery) error { + if values.Has("group") { + group := values.Get("group") + query.Group = &group + } + if values.Has("tab") { + tab := values.Get("tab") + query.Tab = &tab + } + if values.Has("lang") { + tag, err := language.Normalize(values.Get("lang")) + if err != nil { + return err + } + query.Lang = &tag + } + if values.Has("author") { + id, err := strconv.ParseUint(values.Get("author"), 10, 32) + if err != nil { + return err + } + authorID := uint32(id) + query.Author = &authorID + } + return nil } func (h *Handler) ListArticles(w http.ResponseWriter, r *http.Request) { @@ -622,14 +677,21 @@ func (h *Handler) ListArticles(w http.ResponseWriter, r *http.Request) { } query, err := readListQuery(r.URL.Query()) - if err != nil || validator.Validate.Struct(query) != nil { + if err != nil || validator.Validate.Struct(query) != nil || query.Group != nil && access.CallerOf(r).UserID == 0 { http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest) return } + if stringOrEmpty(query.Group) == groupByStory { + h.listStories(w, r, format, query) + return + } + page, err := h.listUseCase.ListArticlesPage(useCases.ListArticlesCommand{ Format: format, Caller: access.CallerOf(r), + Lang: query.Lang, + Author: query.Author, Page: query.Page, Size: query.Size, Q: query.Q, @@ -653,17 +715,66 @@ func (h *Handler) ListArticles(w http.ResponseWriter, r *http.Request) { responses = append(responses, response) } - articlesJson, err := json.Marshal(listResponse{Items: responses, Total: page.Total, Page: page.Page, Size: page.Size, Counts: page.Counts}) + writeListJSON(w, listResponse{Items: responses, Total: page.Total, Page: page.Page, Size: page.Size, Counts: page.Counts}) +} + +func writeListJSON(w http.ResponseWriter, payload any) { + body, err := json.Marshal(payload) if err != nil { http.Error(w, "Failed to encode response", http.StatusInternalServerError) return } - - if _, err := w.Write(articlesJson); err != nil { + if _, err := w.Write(body); err != nil { log.Printf("Failed to write response: %v", err) } } +func (h *Handler) listStories(w http.ResponseWriter, r *http.Request, format domain.BodyFormat, query listQuery) { + caller := access.CallerOf(r) + page, err := h.listUseCase.ListStoriesPage(useCases.ListStoriesCommand{ + Format: format, + Caller: caller, + Page: query.Page, + Size: query.Size, + Q: query.Q, + Status: query.Status, + Sort: parseSort(query.Sort), + SiteKey: query.Site, + SEO: query.SEO, + Lang: stringOrEmpty(query.Lang), + Tab: stringOrEmpty(query.Tab), + Author: query.Author, + }) + if err != nil { + http.Error(w, "Failed to retrieve articles", http.StatusInternalServerError) + return + } + responses := make([]storyItemResponse, 0, len(page.Rows)) + for _, row := range page.Rows { + item, err := toListItemResponse(row.Article, row.SEO, format, caller) + if err != nil { + http.Error(w, "Failed to encode response", http.StatusInternalServerError) + return + } + responses = append(responses, storyItemResponse{ + listItemResponse: item, + StoryUpdatedAt: row.UpdatedAt, + StoryAuthors: row.Authors, + Coverage: row.Coverage, + MissingLangs: row.MissingLangs, + Capabilities: row.Capabilities, + }) + } + writeListJSON(w, storyListResponse{Items: responses, Total: page.Total, Page: page.Page, Size: page.Size, Counts: page.Counts}) +} + +func stringOrEmpty(value *string) string { + if value == nil { + return "" + } + return *value +} + func (h *Handler) DeleteArticle(w http.ResponseWriter, r *http.Request) { id, err := parseArticleId(r) if err != nil { diff --git a/docs/api.md b/docs/api.md index 8833e81..9d7a704 100644 --- a/docs/api.md +++ b/docs/api.md @@ -3,7 +3,7 @@ The full, searchable reference is the Integration page of the running app (`/integration`). It renders `web/src/data/apiCatalogue.ts`, which is the single source for this page. This file is for readers without the UI. Reading articles (pagination, `format`, body schemas) has its own page, -[api/articles.md](api/articles.md), versions, translations and the story move have [api/article-localization.md](api/article-localization.md), site languages and the one-time language setup are in [api/site-languages.md](api/site-languages.md), and API keys are covered in [api/api-keys.md](api/api-keys.md). +[api/articles.md](api/articles.md), versions, translations, the story move and the story-grouped editorial list (`GET /v1/article/?group=story`) have [api/article-localization.md](api/article-localization.md), site languages and the one-time language setup are in [api/site-languages.md](api/site-languages.md), and API keys are covered in [api/api-keys.md](api/api-keys.md). ## Base URL The origin that serves the app, for example `https://cms.example`. Every path starts with `/v1`, diff --git a/docs/api/article-localization.md b/docs/api/article-localization.md index 9d2cdee..fc8820f 100644 --- a/docs/api/article-localization.md +++ b/docs/api/article-localization.md @@ -160,6 +160,38 @@ Verified session or API key. The caller needs `content.update` on every version Everything is checked inside the transaction, under the locks of both sites, and nothing moves when something fails: every language of every version (readable or not) must be enabled at the destination (`422 language_not_enabled`, `X-Missing-Languages` limited to languages of readable versions, a language disabled at the destination counts as missing, a language disabled at the source does not matter), and no address may exist at the destination in the same language (`409 slug_conflict`, `X-Conflicting-Articles` limited to readable versions). A conflict that appears between the check and the write answers `409 slug_conflict` without the header. +## Editorial list by story: `GET /v1/article/?group=story` + +An opt-in shape of the list for a signed-in, verified caller whose role resolves. Without `group` the response keeps the flat envelope `{ items, total, page, size, counts }` of the paginated list, whose `counts` are `all`, `online`, `draft` and `suggestions`. A caller without a valid session, or with an unverified user, who sends `group` gets 400 with the body-mismatch message and nothing is read. + +| Parameter | Meaning | +| --- | --- | +| `group` | Only `story`. `group=`, any other value, or `group` from a public caller is 400. | +| `lang` | A supported language tag, normalized like the language of a version (`fr`, `pt-br` becomes `pt-BR`). Empty, malformed or unsupported is 400. Also narrows the flat list (`items`, `total`, `counts.all`, `counts.online`, `counts.draft`). Disabled languages can be selected. | +| `tab` | `all` (default) or `missing`. `review`, an empty value, any other value, and `missing` without `group=story` are 400. | +| `author` | A byline id, an integer of at least 1; anything else is 400. Also narrows the flat list. | +| `page`, `size`, `q`, `status`, `site`, `seo`, `sort` | Same values, defaults and validation as the flat list. | + +Only versions the caller can read count: they are titled, credited, listed in `coverage`, searched and counted. A hidden version never appears, and a story whose versions are all hidden has no row. + +**Rows.** With no `lang` a row is a story: `total` counts stories. With `lang` a row is the version of that language (`tab=all`), and the whole story's `coverage`, `story_authors` and `missing_langs` still ride on the row. With `tab=missing` rows are stories again: `lang=fr` lists the stories on a site that enables `fr` and that have a readable version but none in `fr`; without `lang` it lists the stories that lack at least one enabled language, and a caller who cannot create (no `content.create`) gets `total` 0. Articles without language (`lang` `""`) are one story each and are never listed as missing a language. A version of the story must satisfy `status`, `author`, `q` and `seo` together to make the story match; `q` ignores case and accents. + +**Item.** The article list item of the representative version (the one in `lang`, otherwise the site default language version, otherwise the first readable one by language tag), plus: + +| Field | Type | Meaning | +| --- | --- | --- | +| `story_updated_at` | RFC 3339 | The latest `updated_at` among the readable versions of the story | +| `story_authors` | array of bylines | The union of the bylines of the readable versions, without duplicates | +| `coverage` | array | One entry per readable version, in the order of `translations` (default language, other enabled tags, disabled tags): `{ id, lang, title, is_online, language_enabled, capabilities }` | +| `missing_langs` | array of tags | Enabled languages without a readable version, default first then by tag; `[]` when the caller cannot create | +| `capabilities` | array | The capabilities of the representative version | + +**Sort.** The whitelist is the flat one. `updated_at` uses the latest readable version of the story, `created_at` the earliest, `title` and `status` the representative; with `lang` the version itself. Ties end with `story_id` descending. + +**Counts.** `{ all, online, draft, suggestions, missing }`. They follow `lang`, `q`, `author`, `seo` and `site` but never `status` or `tab`, and count the unit of the mode (stories, or versions of the selected language). `online` and `draft` count the units with a readable online or draft version. `missing` is the `total` the not-in view would give under the same filters (0 without `lang` for a caller who cannot create). `suggestions` is the number of stories with a readable version whose `seo_status` is `suggestions`, ignoring every parameter. + +A repository failure at any step, counts included, answers 500 `Failed to retrieve articles`. + ## Mock-ready examples Statuses of the new and changed calls. Bodies are plain text unless shown as JSON; headers listed are the only extra ones. diff --git a/features/article_list_localization.feature b/features/article_list_localization.feature new file mode 100644 index 0000000..7a9ce89 --- /dev/null +++ b/features/article_list_localization.feature @@ -0,0 +1,390 @@ +Feature: Editorial article list by story and language + A signed-in editor lists the articles of the back-office either as one row per story (all languages), or per language, with coverage, a "not in this language" view, an author filter and counts. The grouped response is an explicit opt-in on the same endpoint (group=story). Without it, GET /v1/article/ keeps exactly the flat envelope of features/articles_pagination_api.feature. Only versions the caller can read contribute titles, authors, timestamps, coverage, results and counts. + + Background: + Given the storage, translation and lifecycle rules of features/article_localization_storage.feature, features/article_translation_create.feature and features/article_story_lifecycle.feature apply + And error responses are plain text, and a malformed query answers 400 with apperr.BodyErrorMessage, validated with validate tags through validator.Validate.Struct + And site "journal" has enabled languages en (default), fr, de, ar and disabled languages es, it + And the "Journal" dataset holds 8 stories and 17 versions (article id, lang, state, updated_at, byline): + | story | article ids and versions | byline | updated_at of the latest version | + | 1 | 101 en online, 102 fr draft, 103 de online, 104 ar draft | Marta Alves | 2026-10-30 10:00 | + | 2 | 201 en online, 202 fr online, 203 de online | Camille Roche | 2026-10-29 09:00 | + | 3 | 301 en online, 302 fr online | Luis Ferrer | 2026-10-28 09:00 | + | 4 | 401 en draft | Marta Alves | 2026-10-27 09:00 | + | 5 | 501 en online, 502 de draft | Luis Ferrer | 2026-10-14 09:00 | + | 6 | 601 en online, 602 fr draft | Camille Roche | 2026-10-13 09:00 | + | 7 | 701 en online, 702 de online | Marta Alves | 2026-10-12 09:00 | + | 8 | 801 es online | Luis Ferrer | 2026-10-10 09:00 | + And an "owner" holds every content permission at scope all + And a "reader" holds content.read at scope all only: drafts of other users are hidden and no create_translation capability is held + And a "restricted writer" holds content.create and content.read at scope own + And a caller is "editorial" when the jwt cookie carries a user_id claim of a verified user whose role is resolved; every other caller is "public" + And the new query parameters are: group (only "story"), lang (a supported language tag, normalized as in features/article_localization_storage.feature), tab (all, missing; review is reserved for features/ of release 2 and answers 400 until then), author (a byline id, integer >= 1) + And the existing parameters page, size, q, status, site, seo and sort keep their meaning, defaults and validation; filter changes are the client's job to page-reset + And a visible version is one the caller may read under the scopes of features/roles_permissions_content_scope.feature; a hidden version is never counted, titled, named in coverage, in missing_langs or in an error + And the repository query is portable across SQLite, MySQL 8.4, MariaDB 11 and PostgreSQL: no dialect-only construct, request text never reaches the SQL, and the access filter is applied inside the query, not after paging + + Rule: Public lists are unchanged + + Scenario: A bare request is still the flat envelope + Given the "Journal" dataset and an owner session + When the client sends GET /v1/article/ + Then the body keys are exactly items, total, page, size and counts, and each item is the article object of the version, as before this slice + And total is 17 versions' worth of visible articles, not 8 + + Scenario: lang, tab and group are not read unless asked + Given the "Journal" dataset + When a public client sends GET /v1/article/?size=5 + Then the body is identical to what it was before this slice, with lang and story_id on each item as defined in features/article_localization_storage.feature + + Scenario Outline: group=story needs an editorial caller + Given the "Journal" dataset + When sends GET /v1/article/?group=story + Then the response status is 400 and the body is "The request cannot be processed due to a mismatch in the format of the body." + And the repository was not called + + Examples: + | caller | + | a client without a jwt cookie | + | a client with a malformed or expired cookie | + | a client whose user is not verified | + + Scenario Outline: Invalid new parameters answer 400 + Given an owner session + When the client sends GET /v1/article/? + Then the response status is 400 and the body is "The request cannot be processed due to a mismatch in the format of the body." + And the repository was not called + + Examples: + | query | + | group=stories | + | group= | + | group=story&tab=review | + | group=story&tab=published | + | group=story&tab= | + | tab=missing | + | group=story&lang=english | + | group=story&lang=xx | + | group=story&lang= | + | group=story&author=0 | + | group=story&author=abc | + | group=story&author= | + | author=-1 | + + Rule: Grouped rows, one per story + + Scenario: All languages, page 1 + Given the "Journal" dataset and an owner session + When the client sends GET /v1/article/?group=story&size=5 + Then the response status is 200 and total is 8, page 1, size 5 + And the story_id of the items are 1, 2, 3, 4, 5 in that order + And counts.all is 8 + + Scenario: All languages, page 2 is the last + When the owner sends GET /v1/article/?group=story&size=5&page=2 + Then the story_id of the items are 6, 7, 8 and total is 8 + + Scenario: A page beyond the last is empty with the real total + When the owner sends GET /v1/article/?group=story&size=5&page=99 + Then items is [] and total is 8 and counts.all is 8 + + Scenario: A grouped row describes the representative version and the story + When the owner sends GET /v1/article/?group=story + Then the item of story 1 carries the fields of article 101 (id 101, lang "en", title, slug, is_online, site_id, story_id 1 and the other list fields of the flat item) + And it adds story_updated_at "2026-10-30T10:00:00Z", story_authors (the union of the bylines of the readable versions, without duplicates) and capabilities of article 101 + And it adds coverage and missing_langs + + Scenario Outline: The representative is the default-language version, else the first readable by language code + Given the "Journal" dataset and an owner session + When the client sends GET /v1/article/?group=story + Then the representative id of story is and its lang is "" + + Examples: + | story | id | lang | + | 1 | 101 | en | + | 4 | 401 | en | + | 8 | 801 | es | + + Scenario: The representative falls back when the default version is unreadable + Given story 5 holds a draft en article created by another user + When a restricted writer sends GET /v1/article/?group=story + Then the representative of story 5 is the first readable version by language code, never the hidden one + And no field of the row names the hidden one + + Scenario: story_updated_at is the latest readable version update + Given article 102 (fr draft of story 1, creator another user) was updated at 2026-11-01 08:00 and is hidden from the caller + When a restricted writer or a reader sends GET /v1/article/?group=story + Then story 1's story_updated_at ignores article 102 + When the owner sends the same request + Then story 1's story_updated_at is "2026-11-01T08:00:00Z" + + Scenario Outline: Coverage lists readable versions in the chip order, then the languages that can be created + Given the "Journal" dataset and an owner session + When the client sends GET /v1/article/?group=story + Then the coverage of story is + And each entry has id, lang, title, is_online, language_enabled and capabilities, like an entry of "translations" in features/article_story_lifecycle.feature + And missing_langs of story is + + Examples: + | story | coverage | missing | + | 1 | en, ar, de, fr | [] | + | 3 | en, fr | ["ar","de"] | + | 4 | en | ["ar","de","fr"] | + | 8 | es (language_enabled false) | ["en","ar","de","fr"] | + + Scenario: Coverage order is default, other enabled by tag, disabled by tag; missing is listed by the same rule + Given story 8 gains a readable it version, and story 8's default en version does not exist + Then the coverage of story 8 is es then it, both with language_enabled false + And missing_langs starts with the default language en + + Scenario: A caller who cannot create gets no missing_langs + When a reader sends GET /v1/article/?group=story + Then missing_langs is [] on every row + And the coverage lists only the readable versions of each story + + Scenario: The row of a story whose versions are all hidden does not exist + Given story 4's only version is a draft of another user + When a restricted writer sends GET /v1/article/?group=story + Then story 4 is in no item, in total or in counts + And total is the same as if article 401 did not exist + + Scenario: Hidden versions leave no trace + Given the "Journal" dataset and a restricted writer who cannot read the drafts 102, 104, 401, 502 and 602 + When the restricted writer sends GET /v1/article/?group=story&size=100 + Then the items, total, counts and every coverage and missing_langs equal those of a database in which those articles do not exist + And no response field or header names them + + Scenario: Unassigned articles are their own story with an empty language + Given article 30 has lang "" and its own story + When the owner sends GET /v1/article/?group=story + Then its row has lang "", coverage with its single version, and missing_langs [] + And it never appears in the "not in" view of any language + + Rule: Language selected means one row per version of that language + + Scenario: French selected + When the owner sends GET /v1/article/?group=story&lang=fr&size=5 + Then total is 4 and the story_id of the items are 1, 2, 3, 6 + And each representative is the fr version: ids 102, 202, 302, 602 + And each row still carries the whole story's coverage and story_authors + + Scenario: The row opens the selected version and sorts by its update + Given the fr versions have the updated_at values 2026-10-30, 2026-10-29, 2026-10-28 and 2026-10-13 + When the owner sends GET /v1/article/?group=story&lang=fr + Then the order is stories 1, 2, 3, 6 + + Scenario: Disabled languages can be selected + When the owner sends GET /v1/article/?group=story&lang=es + Then total is 1 and the item is story 8, representative 801 and its coverage entry has language_enabled false + + Scenario: A language nobody has gives an empty list + When the owner sends GET /v1/article/?group=story&lang=it + Then items is [] and total is 0 + + Scenario: lang also narrows the flat list + When the owner sends GET /v1/article/?lang=fr + Then the flat envelope holds the 4 fr articles, total is 4 and no item has another lang + And counts.all, counts.online and counts.draft count the fr versions only and still ignore q and status + + Rule: The not-in view lists stories without a readable version + + Scenario: Not in French + When the owner sends GET /v1/article/?group=story&lang=fr&tab=missing&size=5 + Then total is 4 and the story_id of the items are 4, 5, 7, 8 + And each representative is as in the all-languages view (401, 501, 701, 801) + And missing_langs of each row contains "fr" + + Scenario: Needs translation, no language selected + When the owner sends GET /v1/article/?group=story&tab=missing&size=5 + Then total is 7 and the stories are 2, 3, 4, 5, 6, 7, 8 (story 1 has every enabled language) + + Scenario: Missing means no readable version, not no stored version + Given a hidden fr draft exists on story 4 + When a restricted writer sends GET /v1/article/?group=story&lang=fr&tab=missing + Then story 4 is listed as not in French + And nothing in the response tells whether a fr version exists + + Scenario: Sites that do not enable the language are excluded + Given site "atelier" enables en, en-GB and fr only, with one story that has en and fr versions only + When the owner sends GET /v1/article/?group=story&lang=de&tab=missing + Then no atelier story is listed + When the owner sends GET /v1/article/?group=story&lang=es&tab=missing + Then no story is listed, because es is disabled on journal + + Scenario: Needs translation counts only languages the caller can create + When a reader sends GET /v1/article/?group=story&tab=missing + Then total is 0 and items is [] + And a reader's lang=fr tab=missing still lists the stories without a readable fr version + + Scenario: Nothing missing + Given site "press" with 3 stories, each with en and fr versions, and enabled languages en, fr + When the owner sends GET /v1/article/?group=story&tab=missing&site=press + Then items is [], total is 0 and counts.missing is 0 + + Rule: Filters apply to one readable version together + + Scenario: Status matches a readable version of the story + When the owner sends GET /v1/article/?group=story&status=draft + Then the stories are 1, 4, 5, 6 and total is 4 + + Scenario: Combined filters need a single readable version to satisfy them together + Given story 1 holds article 101 (en, online, byline Marta Alves) and article 102 (fr, draft, byline Camille Roche) + When the owner sends GET /v1/article/?group=story&status=draft&author= + Then story 1 is not listed, because no single version is both a draft and by Marta Alves + When the owner sends GET /v1/article/?group=story&status=online&author= + Then story 1 is listed + When the owner sends GET /v1/article/?group=story&status=draft&author= + Then story 1 is listed + + Scenario: story_authors is the union even when the filter matched one version + Given the same story 1 + When the owner sends GET /v1/article/?group=story&author= + Then story 1 is listed and its story_authors holds Marta Alves and Camille Roche + + Scenario: Search matches any readable title of the story + Given article 102 is titled "Comment nous chiffrons une rénovation" and article 101 "How we price a bathroom renovation" + When the owner sends GET /v1/article/?group=story&q=RENOVATION + Then story 1 is listed with the title of its representative (101) + And the search ignores case and accents like features/articles_pagination_api.feature + When the owner sends GET /v1/article/?group=story&lang=fr&q=chiffrons + Then story 1 is listed with article 102 as representative + When the owner sends GET /v1/article/?group=story&lang=en&q=chiffrons + Then items is [] + + Scenario: Search in the not-in view matches readable source titles + When the owner sends GET /v1/article/?group=story&lang=fr&tab=missing&q=Spring + Then story 4 is listed + + Scenario: Author in language mode + When the owner sends GET /v1/article/?group=story&lang=fr&author= + Then the stories are 2 and 6 (the fr version's own byline decides) + + Scenario: Site filter + When the owner sends GET /v1/article/?group=story&site=press + Then only press stories are listed, and an unknown site key gives items [] and total 0 as for the flat list + + Scenario: SEO filter on one version + Given article 502 has suggestions and article 501 has none + When the owner sends GET /v1/article/?group=story&seo=suggestions + Then story 5 is listed once, with its own representative 501 + + Scenario: All filters combine with paging and stay stable + Given 12 stories match + When the owner requests pages 1, 2 and 3 of ?group=story&size=5 with the same filters + Then the pages hold 5, 5 and 2 stories with no repeat, and total is 12 on each + + Rule: Sort applies to the whole list at story level + + Scenario Outline: Sort orders stories, not the page + Given the "Journal" dataset and an owner session + When the client sends GET /v1/article/?group=story&sort=&size=100 + Then the story order is + + Examples: + | sort | order | + | updated_at:desc | 1,2,3,4,5,6,7,8 | + | updated_at:asc | 8,7,6,5,4,3,2,1 | + | title:asc | by representative title, then story_id descending | + | title:desc | by representative title descending, then story_id descending | + | status:asc | drafts first by representative is_online, then story_id descending | + | created_at:asc | by the earliest created_at among readable versions | + + Scenario: Ties end with story_id descending + Given two stories with the same latest update + Then the one with the higher story_id comes first on every page + + Rule: Counts + + Background: + Given the grouped body is {"items","total","page","size","counts"} + And counts holds all, online, draft, suggestions and missing, in snake_case + And in grouped mode the counts respect lang, q, author, seo and site but never status or tab, so the tab counts stay comparable; they count the unit of the mode (stories, or versions of the selected language) + And online and draft count units with a readable online or draft version (the selected version in language mode) + And missing counts the units the "not in" view would list under the same lang, q, author, seo and site + And suggestions keeps its meaning (units with a readable version whose seo_status is suggestions, ignoring every other parameter) + And the flat list counts are unchanged + + Scenario: Counts of the Journal dataset, all languages + When the owner sends GET /v1/article/?group=story + Then counts is {"all": 8, "online": 7, "draft": 4, "suggestions": 0, "missing": 7} + + Scenario: Counts of the Journal dataset, French + When the owner sends GET /v1/article/?group=story&lang=fr + Then counts.all is 4, counts.online is 2, counts.draft is 2 and counts.missing is 4 + + Scenario: Counts follow the search + When the owner sends GET /v1/article/?group=story&q=zinc + Then items is [], total is 0 and counts is {"all": 0, "online": 0, "draft": 0, "suggestions": 0, "missing": 0} + And suggestions is the only count that ignores q + + Scenario: Counts follow the author filter + When the owner sends GET /v1/article/?group=story&author= + Then total is 3, the stories are 1, 4, 7 and counts.all is 3 + + Scenario: Counts do not move with status or tab + When the owner sends GET /v1/article/?group=story&lang=fr&status=draft&tab=missing + Then counts.all is 4, counts.online is 2 and counts.draft is 2, the same as without status and tab + + Scenario: A reader's counts cover readable versions only + When a reader sends GET /v1/article/?group=story + Then counts.draft is 0 and counts.all equals the number of stories with an online version + + Scenario: Total versus counts + When the owner sends GET /v1/article/?group=story&tab=missing + Then total is counts.missing and total is 7 + + Rule: Failure + + Scenario: Repository failure answers 500 + Given the repository fails on its next call + When the owner sends GET /v1/article/?group=story + Then the status is 500 and the body is "Failed to retrieve articles" + + Scenario: A failure while counting fails the request + Given the repository fails while the counts are evaluated + Then the status is 500 and the body is "Failed to retrieve articles" + + Rule: Persistence portability + + Scenario: The grouped, access-filtered query passes the same cases on every database + Given the "Journal" dataset, a hidden-version dataset and the cases above (all languages, lang, tab=missing, status+author together, q with accents, sort whitelist, paging, counts) + Then they pass on SQLite, MySQL 8.4, MariaDB 11 and PostgreSQL with identical ids, totals and counts + And the result does not depend on the database collation for the id sets + And no raw SQL uses a dialect-only construct + + Rule: The SPA, editorial list screen + + Background: + Given /article keeps its route and URL conventions of features/articles_pagination_screen.feature, and the screen follows docs/design/screens/localization-list.md group L7 + And ArticlesPage always sends group=story; the URL carries site, lang, tab (omitted when all), q, status, author, seo, page and size, defaults omitted, and any filter change drops page + And an invalid value in the URL falls back to its default and is replaced in the URL + And the unit label (UnitLabel, aria-live polite) states the unit: "8 articles", "4 French versions", "4 articles not in French", "7 articles need translation" + And the tab "Needs review" is not rendered (it ships with release 2); loc.l7.review is not part of this slice + And the tabs: with no language "All articles" and "Needs translation"; with a language "All", "Published" (status=online), "Drafts" (status=draft) and "Not in {Language}", each with its count from counts + And the row menu on every width lists Open {language}, Open another language, Add translation, Publish or Unpublish {language}, Copy API URL, Delete {language} version, each shown when its capability or condition holds (docs/design/localization-tables.md "Row actions"), and every row has the menu button + And coverage chips follow docs/design/localization-tables.md "Coverage chips for many languages": 5 shown at 1440 and 4 at 1024 and 390, then "+N" named "N more languages: ..."; dashed chips only for missing_langs and buttons on desktop only + And below md the filters open a sheet (Site, Language, Status, Author, SEO, Clear all, "Show N results") and the tabs scroll with an edge fade and "Scroll tabs left/right" buttons that keep Not in {Language} reachable + And copy comes from the design; no revision number, id or HTTP status name is shown + + Scenario Outline: The design states of group L7 are rendered from the matching response + Given the Journal dataset (and Press, Atelier or the limited reader where the state names it) mocked on GET /v1/article/ as above + When the user opens + Then the screen shows state as drawn: + + Examples: + | state | route | visible | + | loc.l7.all | /article | 5 of 8 rows, unit "8 articles", tabs "All articles" "Needs translation", pager page 1 of 2 | + | loc.l7.page2 | /article?page=2 | 3 of 8 rows, pager page 2 of 2 | + | loc.l7.fr | /article?lang=fr | unit "4 French versions", tabs All, Published, Drafts, Not in French | + | loc.l7.frMissing | /article?lang=fr&tab=missing | unit "4 articles not in French" | + | loc.l7.emptyMissing | /article?tab=missing&site=press | no summary, no pager, the nothing-missing empty state | + | loc.l7.noResults | /article?q=zinc | no-results empty state, every count 0 | + | loc.l7.frLoading | /article?lang=fr (request pending) | counts shown as "–", skeleton rows, aria-busy | + | loc.l7.listFail | /article?lang=fr (request fails) | failure state with the filters kept and a retry | + | loc.l7.limited | /article as a reader without create | no dashed chips, no Add translation item | + | loc.l7.author | /article?author= | 3 articles, filter chip and "Clear all" | + | loc.l7.langFilter | language filter opened | Enabled and Disabled groups | + | loc.l7.rowMenu | a row menu opened | the same actions on desktop and mobile | + | loc.l7.filters | /article?lang=fr&status=draft at 390 | filter sheet with Journal, French, Draft: "Show 2 results" | + | loc.l7.tabsScroll | tabs scrolled (1440, 390) | Not in French reachable; fade and chevrons | + | loc.l7.long | /article?site=atelier (8 languages) | chips collapse to +3, +4, +4; Draft and SEO filters | diff --git a/features/article_list_localization.qa.md b/features/article_list_localization.qa.md new file mode 100644 index 0000000..20ebe67 --- /dev/null +++ b/features/article_list_localization.qa.md @@ -0,0 +1,42 @@ +# QA: Editorial article list by story and language + +Order of work is part of the procedure: the repository query and its real-database tests come first (items 1 to 4), then the use case and the HTTP contract (5 to 8), then the SPA (9 onward). Backend items are Go tests in `test/`, black-box. + +## Automated, repository (SQLite file, task test) + +1. Seed the "Journal" dataset (8 stories, 17 versions, bylines, updated_at as in the feature) and the "hidden-version" variant through the real `ArticleRepository` on a temp SQLite file with `database.Migrate`. Drive the new grouped query through `article.Repository` with the new filter fields (group, lang, tab, author) and assert, table-driven over the feature's Examples: story ids per page, `total`, representative per story, coverage order, `missing_langs`, `story_updated_at`, `story_authors`, counts. +2. Access filter inside the query: for a caller with scope `own`, `all`-online and `online only`, assert the hidden-version dataset yields the same ids, totals and counts as a database where the hidden articles do not exist. Also check page boundaries (a hidden version must not shift a page: 5 per page of readable stories). +3. Filter semantics: the "single readable version satisfies all version-level filters" cases (status+author, status+q, author+seo), the "story_authors is the union" case, the "missing" cases (no readable version, site not enabling the language, disabled language, unassigned articles) and accent-insensitive q in grouped, language and not-in modes. +4. Sort whitelist and stability: each sort of the feature, ties by story_id descending, no overlap or gap between pages 1 to 3 on a 12-story set. + +## Automated, real databases (task test:mysql, task test:postgres) + +Repeat items 1 to 4 against MySQL 8.4, MariaDB 11 and PostgreSQL. Compare ids, totals and counts with SQLite, not only pass/fail. Also assert no query returns a different id set under a different collation (title sort aside). Record NOT RUN with the reason if the daemon is down, or use the podman shim (see memory). Inspect the executed SQL once (GORM debug) to confirm no dialect-only construct, no per-row query for coverage (coverage and authors are loaded in a bounded number of queries per page), and that request text is never concatenated. + +## Automated, use case (task test) + +5. `ListArticlesUseCase` over fakes: grouped command, role grants for owner, reader and restricted writer, fake `site` and `localization` repositories. Assert representative choice, `missing_langs` only when `create_translation` is held, the counts rules, and that a hidden version never reaches the result. Assert sentinel errors with `errors.Is`. Existing use case tests stay green. + +## Automated, HTTP (task test) + +6. `article.NewHandler(...)` over the real use cases and fakes, a fresh `chi.NewRouter()` mirroring `NewArticleRouter` in `api/router.go` (GET / under `jwtauth.Verifier` only), `validator.InitValidator()` first, `httptest`, jwt cookies signed with a test `jwtauth.New("HS256", ...)` secret (owner, reader, restricted writer, unverified user, expired, none). +7. "Public lists are unchanged": bare request and a public request keep the flat keys exactly (compare the key set and values with a golden produced before the slice); `lang=fr` and `author` narrow the flat list; `group=story` for the three public callers and every invalid query in the Examples answer 400 with `apperr.BodyErrorMessage` and the fake repository records no call. +8. Grouped response shape: decode and assert `items[]` keys (flat item keys plus `story_updated_at`, `story_authors`, `coverage`, `missing_langs`, `capabilities`), `total`, `page`, `size`, `counts` keys (all, online, draft, suggestions, missing), `items` `[]` not null, snake_case, errors plain text. Repository failure and count failure answer 500 "Failed to retrieve articles". Docs: `docs/` gains the grouped contract (parameters, shape, counts, errors) so the SPA mocks `/v1` from it. + +## Automated, SPA (Vitest + Vue Test Utils, task test:web) + +9. Mock `GET /v1/article/` per state with the Journal responses of the feature (and Press, Atelier and the reader). One test per design state of group L7 except `loc.l7.review`: `loc.l7.all`, `page2`, `fr`, `frMissing`, `emptyMissing`, `noResults`, `frLoading`, `listFail`, `limited`, `author`, `langFilter`, `rowMenu`, `filters`, `tabsScroll`, `long`. Assert: the request query (`group=story`, lang, tab, author, page, size; defaults omitted), the URL after a successful response, the unit label text, the tab labels and counts, "–" counts while loading, the retained filters on failure, the chip order and collapse (+N, accessible names), dashed chips only from `missing_langs`, and the identical row menu items at mobile and desktop widths. +10. URL state: invalid `lang`, `tab=review`, `author=abc`, `page=abc` fall back and are replaced; every filter change drops `page`; the tab and filter chips match the URL; "Clear all" empties all filters. +11. Regression of the amended screens: `ArticlesPage` still passes the sort, pagination, row action, delete and toast cases of `articles_list` and `articles_pagination_screen`, now on grouped rows. + +## Visual (task visual:check -- localization-list or the L7 group) + +Compare each state with its board frame at desktop (1440), 1024 where the board draws it, and mobile (390), light and dark. The gate is hard: every capture within 10 %. Mock `/v1` from `web/visual/states.ts` with the board's own data (`docs/design/source/locale-fixtures.js`: Journal 8 stories, 17 versions, page size 5; Press; Atelier; the limited scope). Titles, bylines (Marta Alves, Camille Roche, Luis Ferrer) and the language names come from the fixture. All states use relative text ("2 h ago", "Yesterday", "3 days ago"): list them as `skip: clock-sensitive` only if the SPA renders them from the current time; otherwise freeze nothing and use the board's absolute dates. `loc.l7.review` is out of scope (release 2). `loc.l7.tabsScroll` is captured at 1440 and 390 only. + +## Manual + +- Keyboard and focus: tab order through search, filters, tabs (arrow keys), rows, row menu (Enter or Down opens, Esc returns to the button), chips, pager; focus ring on the dashed chip buttons; the filter sheet traps focus and returns it to its trigger. +- Screen reader: the unit label announces politely on every filter change; chips announce "French: draft" and "3 more languages: ...". +- Hover and motion: tab fade and chevrons, skeleton pulse, sheet open and close, reduced motion. +- Real databases through a running server: open the list against a real SQLite file with several languages, a restricted account, and the same data on MySQL and PostgreSQL; compare counts with the Languages page. +- Real SMTP: not applicable. diff --git a/features/articles_list.feature b/features/articles_list.feature index 8fb2b0a..c29ba66 100644 --- a/features/articles_list.feature +++ b/features/articles_list.feature @@ -1,4 +1,5 @@ Feature: Articles list screen + Superseded in part by article_list_localization: the call is GET /v1/article/?group=story&... and rows are story rows. The row anatomy gains the coverage chips (first) and the "Authors" union. The row menu items and order are Open {language}, Open another language, Add translation, Publish or Unpublish {language}, Copy API URL, Delete {language} version (docs/design/localization-tables.md "Row actions"), each shown by its capability, replacing "Edit, Publish or Unpublish, Copy API URL, Delete…" for rows with those capabilities; the Delete… confirmation copy is unchanged here (slice 5). The tabs "All / Online / Drafts" become the two modes of the new feature: "All articles" and "Needs translation" with no language; "All", "Published", "Drafts" and "Not in {Language}" with one. The 6-article fixture is replaced by the Journal dataset for the grouped scenarios; rows with no lang assigned keep today's scenarios with empty coverage. "Copy API URL" still copies {origin}/v1/article/{id} of the version the row menu acts on. Superseded in part by roles_permissions_content_scope: the article routes are gated by the content.* permissions of the caller's role (content.create, content.update, content.delete and content.publish; a refusal is 403 "missing_permission" with the header X-Missing-Permission), and content.read decides which drafts show: every draft at all, only the caller's own drafts at own, none otherwise. A draft the caller may not read answers 404 "Article not found", and a role that cannot be resolved is treated as holding no permission. As a verified RenewCMS owner diff --git a/features/articles_list.qa.md b/features/articles_list.qa.md index 1e20cfa..6dbd2fd 100644 --- a/features/articles_list.qa.md +++ b/features/articles_list.qa.md @@ -2,6 +2,8 @@ > Superseded in part by roles_permissions_content_scope: the article routes are gated by the content.* permissions of the caller's role (content.create, content.update, content.delete and content.publish; a refusal is 403 "missing_permission" with the header X-Missing-Permission), and content.read decides which drafts show: every draft at all, only the caller's own drafts at own, none otherwise. A draft the caller may not read answers 404 "Article not found", and a role that cannot be resolved is treated as holding no permission. +> Superseded in part by article_list_localization: references to the flat list become the grouped request (GET /v1/article/?group=story&...) on story rows; the Go and Vitest cases are items 6 to 11 of features/article_list_localization.qa.md. The manual items below are kept. + ## Implementation note for coder (informational, not a QA assertion) `internal/infrastructure/persistence/repositories/articleRepository.go`'s `FindByFilters` gains diff --git a/features/articles_pagination_api.feature b/features/articles_pagination_api.feature index a58fc85..85050f3 100644 --- a/features/articles_pagination_api.feature +++ b/features/articles_pagination_api.feature @@ -1,4 +1,5 @@ Feature: Paginated article listing + Superseded in part by article_list_localization: the flat envelope below is unchanged, and the optional parameters lang and author are added to it (see the Background); the story-grouped shape is a separate opt-in. Superseded in part by roles_permissions_content_scope: the article routes are gated by the content.* permissions of the caller's role (content.create, content.update, content.delete and content.publish; a refusal is 403 "missing_permission" with the header X-Missing-Permission), and content.read decides which drafts show: every draft at all, only the caller's own drafts at own, none otherwise. A draft the caller may not read answers 404 "Article not found", and a role that cannot be resolved is treated as holding no permission. As an API consumer or the RenewCMS back-office @@ -15,6 +16,10 @@ Feature: Paginated article listing And q (optional, max 200 characters; case-insensitive, accent-insensitive, matches anywhere in the title) and status (optional, online or draft) filter the items And total is the number of visible articles matching q and status, before paging; page and size echo the values applied And counts ignores q and status, so the tab counts do not move while the user searches; it respects visibility: for a public caller all equals online and draft is 0 + And this counts statement is true for the flat shape and is explicitly NOT true for the grouped shape, where counts follow lang, q, author, seo and site (features/article_list_localization.feature) + And the flat counts.suggestions rule is unchanged + And the flat envelope above is unchanged; a client opts in to the story-grouped editorial shape with group=story (features/article_list_localization.feature); public and unverified callers cannot + And lang (flat: narrows items, total and counts.all, counts.online and counts.draft to that language) and author (a byline id, flat: narrows by byline) are additive, optional, validated parameters; an invalid value answers 400 with apperr.BodyErrorMessage And status=draft requested by a public caller matches nothing: items [], total 0 And sort (optional) is one of title:asc, title:desc, status:asc, status:desc, created_at:asc, created_at:desc, updated_at:asc, updated_at:desc; the default is updated_at:desc; status orders by is_online (asc puts drafts first); every ordering ends with id descending as the tie-break, so pages are stable and never overlap And sort is mapped to a column and direction through a fixed whitelist in the repository, so request text never reaches the SQL; the same ordering applies on the search path (q) before slicing @@ -176,6 +181,14 @@ Feature: Paginated article listing | sort=updated_at | | sort=title:asc;DROP TABLE | | sort=TITLE:ASC | + | lang=english | + | lang=xx | + | lang= | + | author=0 | + | author=abc | + | author= | + | group=stories | + | tab=missing | Scenario: Invalid format is reported first When a client sends GET /v1/article/?page=0&format=pdf diff --git a/features/articles_pagination_api.qa.md b/features/articles_pagination_api.qa.md index 2ff3baa..8dcafa3 100644 --- a/features/articles_pagination_api.qa.md +++ b/features/articles_pagination_api.qa.md @@ -2,6 +2,8 @@ > Superseded in part by roles_permissions_content_scope: the article routes are gated by the content.* permissions of the caller's role (content.create, content.update, content.delete and content.publish; a refusal is 403 "missing_permission" with the header X-Missing-Permission), and content.read decides which drafts show: every draft at all, only the caller's own drafts at own, none otherwise. A draft the caller may not read answers 404 "Article not found", and a role that cannot be resolved is treated as holding no permission. +> Superseded in part by article_list_localization: the flat envelope tested here is unchanged; the additive lang and author parameters, their 400 rows and the grouped request (group=story) are covered by items 6 to 11 of features/article_list_localization.qa.md. The manual items below are kept. + ## HTTP-level (Go, `test/`, `task test`) Build `article.NewHandler(...)` over the real `ListArticlesUseCase` and a fake `article.Repository` (extend `mockArticleRepo` in `test/article_test.go`, keeping type names unique across files). The fake implements the interface contract: paging (Page/Size 0 = unpaged), q/status filtering, ordering updated_at DESC then id DESC, total, and the visibility counts. Mount on a fresh `chi.NewRouter()` mirroring `NewArticleRouter` in `api/router.go` (GET / under `jwtauth.Verifier` only, test `jwtauth.New("HS256", ...)`). Call `validator.InitValidator()`. Use a user fake for the verified, unverified and missing cases, as in `test/public_article_listing_test.go`. No live server, no SQLite file. diff --git a/features/articles_pagination_screen.feature b/features/articles_pagination_screen.feature index 9926363..84698ae 100644 --- a/features/articles_pagination_screen.feature +++ b/features/articles_pagination_screen.feature @@ -1,4 +1,5 @@ Feature: Articles list pagination + Superseded in part by article_list_localization: the request is GET /v1/article/?group=story&format=html&...; page, size, q, status, sort, lang, tab, author, site and seo live in the URL; items are story rows (representative version plus coverage) and total is the unit total (stories, or versions of the selected language). The tab counts follow lang, q, author, seo and site (never status or tab) instead of staying fixed while searching (deliberate behavior change); "No results" shows every count at 0 (board loc.l7.noResults). A change of lang, tab or author, like any filter, drops page. The summary "Showing {from}–{to} of {total}" is unchanged and the UnitLabel sits above it. Scenarios 4, 6 and 7 keep their wording with story rows and the grouped request. As a verified RenewCMS user with many articles I want the Articles list split into pages with a page-size menu, server-side search and tabs, and shareable URLs @@ -91,15 +92,15 @@ Feature: Articles list pagination When the user types "plaster" in the search field and the debounce elapses Then GET ...&q=plaster&page=1 is sent And on success the URL is /article?q=plaster (page omitted) - And the tab counts are unchanged - And the same reset happens for a tab change, a sort change and a page-size change + And the tab counts follow the new search (lang, q, author, seo and site; never status or tab) + And the same reset happens for a tab change, a sort change, a page-size change, and a lang, author, site or seo change Scenario: 9 Empty after search - Given GET ...&q=tiles resolves 200 with items [], total 0 and counts {all 6, online 4, draft 2} + Given GET ...&q=tiles resolves 200 with items [], total 0 and counts {all 0, online 0, draft 0, suggestions 0, missing 0} When the user searches "tiles" Then the paginator and the summary are hidden And the no-results state shows "No articles match "tiles"", "Check the spelling, or search for a shorter part of the title." and "Clear search" - And the tabs still read "All 6" "Online 4" "Drafts 2" + And every tab count reads 0 (board loc.l7.noResults), because counts follow the search Scenario Outline: 10 Page size menu Given the user is on /article?page=3 @@ -130,7 +131,7 @@ Feature: Articles list pagination Given the Default list has loaded and the user is on /article?page=3 When the user selects the "Drafts" tab Then GET ...&status=draft&page=1 is sent and, on success, the URL is /article?status=draft - And the tab counts are unchanged + And the tab counts are unchanged, because counts never follow status or tab Scenario Outline: Sort header, server-side, resets to page 1 Given the user is on /article?page=7 diff --git a/features/articles_pagination_screen.qa.md b/features/articles_pagination_screen.qa.md index 5b9ee9d..91f172c 100644 --- a/features/articles_pagination_screen.qa.md +++ b/features/articles_pagination_screen.qa.md @@ -1,5 +1,7 @@ # QA: Articles list pagination (SPA) +> Superseded in part by article_list_localization: the mocked request is the grouped one (GET /v1/article/?group=story&format=html&...) on story rows, tab counts follow lang, q, author, seo and site, and the lang, tab, author, site and seo reset cases are covered by items 9 to 11 of features/article_list_localization.qa.md. The manual items below are kept. + ## Automated (Vitest + Vue Test Utils, `task test:web`) Mock `useApi` for `GET /v1/article/?format=html&...` (the route key includes the query) per case. Mount ArticlesPage inside DashboardShell with a verified session and a test router at /article, as in features/articles_list.qa.md. The fixture is 312 articles at 25 per page, built by a helper, with counts {all, online, draft}. No system date freezing. diff --git a/internal/domain/article/articleRepositoryInterface.go b/internal/domain/article/articleRepositoryInterface.go index afac7e0..f430793 100644 --- a/internal/domain/article/articleRepositoryInterface.go +++ b/internal/domain/article/articleRepositoryInterface.go @@ -37,6 +37,8 @@ type Filters struct { Size int Q string Status string + Lang *string + AuthorID *uint32 Sort Sort Unpaged bool } @@ -93,6 +95,10 @@ type Repository interface { FindPage(filters Filters) (PageResult, error) CountByVisibility(onlineOnly bool) (Counts, error) CountVisibleTo(userID uint32) (Counts, error) + CountVersions(filters Filters) (Counts, error) + FindStoryPage(filters StoryFilters) (StoryPage, error) + CountStories(filters StoryFilters) (StoryCounts, error) + ListByStories(storyIDs []uint32) ([]Article, error) Get(id uint32) (Article, error) GetByName(name string) (Article, error) GetAll() ([]Article, error) diff --git a/internal/domain/article/articleStoryList.go b/internal/domain/article/articleStoryList.go new file mode 100644 index 0000000..76d2058 --- /dev/null +++ b/internal/domain/article/articleStoryList.go @@ -0,0 +1,44 @@ +package article + +const ( + TabAll = "all" + TabMissing = "missing" +) + +type StoryFilters struct { + SkipMissing bool + OnlyOnline bool + VisibleTo *uint32 + SiteID *uint32 + Lang string + Missing bool + Status string + AuthorID *uint32 + Q string + VersionIDs *[]uint32 + Sort Sort + Page int + Size int +} + +func (f StoryFilters) PerLanguage() bool { + return f.Lang != "" && !f.Missing +} + +type StoryRef struct { + StoryID uint32 + ArticleID uint32 +} + +type StoryPage struct { + Refs []StoryRef + Total int +} + +type StoryCounts struct { + All int `json:"all"` + Online int `json:"online"` + Draft int `json:"draft"` + Suggestions int `json:"suggestions"` + Missing int `json:"missing"` +} diff --git a/internal/infrastructure/persistence/repositories/articleRepository.go b/internal/infrastructure/persistence/repositories/articleRepository.go index eb687e7..7000dc1 100644 --- a/internal/infrastructure/persistence/repositories/articleRepository.go +++ b/internal/infrastructure/persistence/repositories/articleRepository.go @@ -61,6 +61,12 @@ func (a *ArticleRepository) scoped(filters domain.Filters) *gorm.DB { case domain.StatusDraft: query = query.Where("is_online = ?", false) } + if filters.Lang != nil { + query = query.Where("lang = ?", *filters.Lang) + } + if filters.AuthorID != nil { + query = query.Where("id IN (SELECT article_id FROM article_authors WHERE author_id = ?)", *filters.AuthorID) + } return query } @@ -188,6 +194,22 @@ func (a *ArticleRepository) CountVisibleTo(userID uint32) (domain.Counts, error) return domain.Counts{All: int(online + draft), Online: int(online), Draft: int(draft)}, nil } +func (a *ArticleRepository) CountVersions(filters domain.Filters) (domain.Counts, error) { + narrowed := domain.Filters{Lang: filters.Lang} + var online, draft int64 + if err := a.scoped(narrowed).Where("is_online = ?", true).Count(&online).Error; err != nil { + return domain.Counts{}, err + } + onlineOnly := filters.IsOnline != nil && *filters.IsOnline + if !onlineOnly { + narrowed.VisibleTo = filters.VisibleTo + if err := a.scoped(narrowed).Where("is_online = ?", false).Count(&draft).Error; err != nil { + return domain.Counts{}, err + } + } + return domain.Counts{All: int(online + draft), Online: int(online), Draft: int(draft)}, nil +} + func toDomainArticles(entityArticles []entity.Article) []domain.Article { domainArticles := make([]domain.Article, 0, len(entityArticles)) for _, entityArticle := range entityArticles { diff --git a/internal/infrastructure/persistence/repositories/articleStoryRepository.go b/internal/infrastructure/persistence/repositories/articleStoryRepository.go new file mode 100644 index 0000000..c90f6b1 --- /dev/null +++ b/internal/infrastructure/persistence/repositories/articleStoryRepository.go @@ -0,0 +1,262 @@ +package persistence + +import ( + domain "RenewCMS/internal/domain/article" + entity "RenewCMS/internal/infrastructure/persistence/models" + "strings" +) + +type storySQL struct { + conditions []string + args []any +} + +func (s *storySQL) add(condition string, args ...any) { + s.conditions = append(s.conditions, condition) + s.args = append(s.args, args...) +} + +func (s storySQL) where() string { + return strings.Join(s.conditions, " AND ") +} + +func readableCondition(alias string, filters domain.StoryFilters) storySQL { + condition := storySQL{} + condition.add(alias + ".deleted_at IS NULL") + if filters.OnlyOnline { + condition.add(alias+".is_online = ?", true) + } + if filters.VisibleTo != nil { + condition.add("("+alias+".is_online = ? OR "+alias+".created_by = ?)", true, *filters.VisibleTo) + } + return condition +} + +func (s *storySQL) merge(other storySQL) { + s.conditions = append(s.conditions, other.conditions...) + s.args = append(s.args, other.args...) +} + +func lacksReadableVersionIn(languageExpression string, filters domain.StoryFilters, languageArgs ...any) storySQL { + readable := readableCondition("w", filters) + condition := storySQL{} + condition.add("NOT EXISTS (SELECT 1 FROM articles w WHERE w.story_id = q.story_id AND "+readable.where()+" AND w.lang = "+languageExpression+")", append(readable.args, languageArgs...)...) + return condition +} + +func (a *ArticleRepository) versionRestriction(filters domain.StoryFilters) ([]uint32, bool, error) { + restricted := filters.VersionIDs != nil + var allowed []uint32 + if restricted { + allowed = *filters.VersionIDs + } + query := strings.TrimSpace(filters.Q) + if query == "" { + return allowed, restricted, nil + } + readable := readableCondition("q", filters) + if filters.SiteID != nil { + readable.add("q.site_id = ?", *filters.SiteID) + } + var candidates []entity.Article + if err := a.db.Raw("SELECT q.id AS id, q.title AS title FROM articles q WHERE "+readable.where(), readable.args...).Scan(&candidates).Error; err != nil { + return nil, false, err + } + needle := foldForSearch(query) + permitted := map[uint32]bool{} + for _, id := range allowed { + permitted[id] = true + } + matching := make([]uint32, 0) + for _, candidate := range candidates { + if restricted && !permitted[candidate.ID] { + continue + } + if strings.Contains(foldForSearch(candidate.Title), needle) { + matching = append(matching, candidate.ID) + } + } + return matching, true, nil +} + +func qualifyingVersions(filters domain.StoryFilters, allowed []uint32, restricted bool) storySQL { + condition := readableCondition("q", filters) + if filters.SiteID != nil { + condition.add("q.site_id = ?", *filters.SiteID) + } + if filters.PerLanguage() { + condition.add("q.lang = ?", filters.Lang) + } + switch filters.Status { + case domain.StatusOnline: + condition.add("q.is_online = ?", true) + case domain.StatusDraft: + condition.add("q.is_online = ?", false) + } + if filters.AuthorID != nil { + condition.add("q.id IN (SELECT aa.article_id FROM article_authors aa WHERE aa.author_id = ?)", *filters.AuthorID) + } + if restricted { + if len(allowed) == 0 { + condition.add("1 = 0") + } else { + condition.add("q.id IN ?", allowed) + } + } + if filters.Missing { + condition.merge(lacksReadableVersionIn("?", filters, "")) + condition.merge(missingCondition(filters)) + } + return condition +} + +func missingCondition(filters domain.StoryFilters) storySQL { + condition := storySQL{} + if filters.Lang != "" { + condition.add("EXISTS (SELECT 1 FROM site_languages sl WHERE sl.site_id = q.site_id AND sl.lang = ? AND sl.enabled = ?)", filters.Lang, true) + condition.merge(lacksReadableVersionIn("?", filters, filters.Lang)) + return condition + } + absent := lacksReadableVersionIn("sl.lang", filters) + condition.add("EXISTS (SELECT 1 FROM site_languages sl WHERE sl.site_id = q.site_id AND sl.enabled = ? AND "+strings.Join(absent.conditions, " AND ")+")", append([]any{true}, absent.args...)...) + return condition +} + +func unitExpression(filters domain.StoryFilters) string { + if filters.PerLanguage() { + return "COUNT(*)" + } + return "COUNT(DISTINCT q.story_id)" +} + +func (a *ArticleRepository) countUnits(filters domain.StoryFilters, allowed []uint32, restricted bool) (int, error) { + qualifying := qualifyingVersions(filters, allowed, restricted) + var total int64 + err := a.db.Raw("SELECT "+unitExpression(filters)+" FROM articles q WHERE "+qualifying.where(), qualifying.args...).Scan(&total).Error + return int(total), err +} + +var storyRowOrder = map[domain.SortField]string{ + domain.SortByTitle: "s.sort_title", + domain.SortByStatus: "s.sort_online", + domain.SortByCreatedAt: "s.sort_created", + domain.SortByUpdatedAt: "s.sort_updated", +} + +var versionRowOrder = map[domain.SortField]string{ + domain.SortByTitle: "LOWER(q.title)", + domain.SortByStatus: "q.is_online", + domain.SortByCreatedAt: "q.created_at", + domain.SortByUpdatedAt: "q.updated_at", +} + +func storyOrderClause(columns map[domain.SortField]string, sort domain.Sort, storyColumn string) string { + column, ok := columns[sort.Field] + direction, directionOk := sortDirections[sort.Direction] + if !ok || !directionOk { + column, direction = columns[domain.SortByUpdatedAt], sortDirections[domain.SortDesc] + } + return column + " " + direction + ", " + storyColumn + " DESC" +} + +type storyRefRow struct { + StoryID uint32 + ArticleID uint32 +} + +func toStoryRefs(rows []storyRefRow) []domain.StoryRef { + refs := make([]domain.StoryRef, 0, len(rows)) + for _, row := range rows { + refs = append(refs, domain.StoryRef{StoryID: row.StoryID, ArticleID: row.ArticleID}) + } + return refs +} + +func (a *ArticleRepository) FindStoryPage(filters domain.StoryFilters) (domain.StoryPage, error) { + filters.Page = max(filters.Page, 1) + filters.Size = max(filters.Size, 0) + allowed, restricted, err := a.versionRestriction(filters) + if err != nil { + return domain.StoryPage{}, err + } + total, err := a.countUnits(filters, allowed, restricted) + if err != nil { + return domain.StoryPage{}, err + } + page := domain.StoryPage{Refs: []domain.StoryRef{}, Total: total} + if total == 0 { + return page, nil + } + qualifying := qualifyingVersions(filters, allowed, restricted) + offset := (filters.Page - 1) * filters.Size + var rows []storyRefRow + if filters.PerLanguage() { + query := "SELECT q.story_id AS story_id, q.id AS article_id FROM articles q WHERE " + qualifying.where() + + " ORDER BY " + storyOrderClause(versionRowOrder, filters.Sort, "q.story_id") + " LIMIT ? OFFSET ?" + err = a.db.Raw(query, append(qualifying.args, filters.Size, offset)...).Scan(&rows).Error + } else { + query, args := storyRowsQuery(filters, qualifying) + err = a.db.Raw(query, append(args, filters.Size, offset)...).Scan(&rows).Error + } + if err != nil { + return domain.StoryPage{}, err + } + page.Refs = toStoryRefs(rows) + return page, nil +} + +func storyRowsQuery(filters domain.StoryFilters, qualifying storySQL) (string, []any) { + readable := readableCondition("a", filters) + args := []any{true} + args = append(args, readable.args...) + args = append(args, qualifying.args...) + query := "SELECT s.story_id AS story_id, s.article_id AS article_id FROM (" + + "SELECT r.story_id AS story_id, r.id AS article_id, r.sort_title AS sort_title, r.sort_online AS sort_online, r.sort_updated AS sort_updated, r.sort_created AS sort_created FROM (" + + "SELECT a.id AS id, a.story_id AS story_id, LOWER(a.title) AS sort_title, a.is_online AS sort_online, " + + "MAX(a.updated_at) OVER (PARTITION BY a.story_id) AS sort_updated, " + + "MIN(a.created_at) OVER (PARTITION BY a.story_id) AS sort_created, " + + "ROW_NUMBER() OVER (PARTITION BY a.story_id ORDER BY CASE WHEN d.lang IS NULL THEN 1 ELSE 0 END, a.lang, a.id) AS row_rank " + + "FROM articles a LEFT JOIN site_languages d ON d.site_id = a.site_id AND d.lang = a.lang AND d.is_default = ? " + + "WHERE " + readable.where() + " AND a.story_id IN (SELECT q.story_id FROM articles q WHERE " + qualifying.where() + ")" + + ") r WHERE r.row_rank = 1) s ORDER BY " + storyOrderClause(storyRowOrder, filters.Sort, "s.story_id") + " LIMIT ? OFFSET ?" + return query, args +} + +func (a *ArticleRepository) CountStories(filters domain.StoryFilters) (domain.StoryCounts, error) { + filters.Status = "" + allowed, restricted, err := a.versionRestriction(filters) + if err != nil { + return domain.StoryCounts{}, err + } + counts := domain.StoryCounts{} + for _, target := range []struct { + status string + into *int + }{{"", &counts.All}, {domain.StatusOnline, &counts.Online}, {domain.StatusDraft, &counts.Draft}} { + scoped := filters + scoped.Missing = false + scoped.Status = target.status + if *target.into, err = a.countUnits(scoped, allowed, restricted); err != nil { + return domain.StoryCounts{}, err + } + } + if filters.SkipMissing { + return counts, nil + } + missing := filters + missing.Missing = true + counts.Missing, err = a.countUnits(missing, allowed, restricted) + return counts, err +} + +func (a *ArticleRepository) ListByStories(storyIDs []uint32) ([]domain.Article, error) { + var found []entity.Article + for _, chunk := range chunkIDs(storyIDs) { + var loaded []entity.Article + if err := a.db.Model(&entity.Article{}).Where("story_id IN ?", chunk).Order("id ASC").Find(&loaded).Error; err != nil { + return nil, err + } + found = append(found, loaded...) + } + return a.withAuthors(a.db, toDomainArticles(found)) +} diff --git a/internal/infrastructure/useCases/ListArticleStories.go b/internal/infrastructure/useCases/ListArticleStories.go new file mode 100644 index 0000000..4bc5390 --- /dev/null +++ b/internal/infrastructure/useCases/ListArticleStories.go @@ -0,0 +1,262 @@ +package useCases + +import ( + "RenewCMS/internal/domain/article" + "RenewCMS/internal/domain/author" + "RenewCMS/internal/domain/builtintype" + "RenewCMS/internal/domain/language" + "RenewCMS/internal/domain/localization" + "RenewCMS/internal/domain/role" + "slices" + "time" +) + +type ListStoriesCommand struct { + Format article.BodyFormat + Caller ContentCaller + Page int + Size int + Q string + Status string + Sort article.Sort + SiteKey *string + SEO string + Lang string + Tab string + Author *uint32 +} + +type StoryRow struct { + Article article.Article + SEO article.SEOSummary + UpdatedAt time.Time + Authors []author.Byline + Coverage []article.Translation + MissingLangs []string + Capabilities []string +} + +type StoryRowsPage struct { + Rows []StoryRow + Total int + Page int + Size int + Counts article.StoryCounts +} + +func (u *ListArticlesUseCase) ListStoriesPage(command ListStoriesCommand) (StoryRowsPage, error) { + visibility := visibilityFor(command.Caller) + creatable := command.Caller.Grants.HoldsFor(role.KeyContentCreate, builtintype.KeyArticle) + empty := StoryRowsPage{Rows: []StoryRow{}, Page: command.Page, Size: command.Size} + filters := storyFilters(command, visibility, command.Lang == "" && !creatable) + owners := &siteOwners{repository: u.sites} + if command.SiteKey != nil { + siteID, known, err := u.siteIDOf(*command.SiteKey) + if err != nil { + return StoryRowsPage{}, err + } + if !known { + return empty, nil + } + filters.SiteID = &siteID + } + summaries, visibleVersions, err := u.summarizeVisibleVersions(visibility, owners) + if err != nil { + return StoryRowsPage{}, err + } + if command.SEO != "" { + allowed, err := u.versionsWithSEOStatus(command, filters, summaries) + if err != nil { + return StoryRowsPage{}, err + } + filters.VersionIDs = &allowed + } + counts, err := u.articleRepository.CountStories(filters) + if err != nil { + return StoryRowsPage{}, err + } + counts.Suggestions = countSuggestedStories(visibleVersions, summaries) + empty.Counts = counts + if command.Tab == article.TabMissing && command.Lang == "" && !creatable { + return empty, nil + } + found, err := u.articleRepository.FindStoryPage(filters) + if err != nil { + return StoryRowsPage{}, err + } + empty.Total = found.Total + if len(found.Refs) == 0 { + return empty, nil + } + rows, err := u.storyRows(command, found.Refs, creatable, owners) + if err != nil { + return StoryRowsPage{}, err + } + empty.Rows = rows + return empty, nil +} + +func storyFilters(command ListStoriesCommand, visibility articleVisibility, skipMissing bool) article.StoryFilters { + return article.StoryFilters{ + SkipMissing: skipMissing, + OnlyOnline: visibility.onlineOnly, + VisibleTo: visibility.userID, + Lang: command.Lang, + Missing: command.Tab == article.TabMissing, + Status: command.Status, + AuthorID: command.Author, + Q: command.Q, + Sort: command.Sort, + Page: command.Page, + Size: command.Size, + } +} + +func (u *ListArticlesUseCase) summarizeVisibleVersions(visibility articleVisibility, owners *siteOwners) (map[uint32]article.SEOSummary, []article.Article, error) { + found, err := u.articleRepository.FindPage(visibility.apply(article.Filters{Unpaged: true})) + if err != nil { + return nil, nil, err + } + summaries, err := u.summarize(found.Items, owners) + return summaries, found.Items, err +} + +func (u *ListArticlesUseCase) versionsWithSEOStatus(command ListStoriesCommand, filters article.StoryFilters, summaries map[uint32]article.SEOSummary) ([]uint32, error) { + narrowed := article.Filters{VisibleTo: filters.VisibleTo, SiteID: filters.SiteID, AuthorID: filters.AuthorID, Q: filters.Q, Unpaged: true} + if filters.OnlyOnline { + online := true + narrowed.IsOnline = &online + } + found, err := u.articleRepository.FindPage(narrowed) + if err != nil { + return nil, err + } + allowed := []uint32{} + for _, version := range found.Items { + if summaries[version.ID].Status == command.SEO { + allowed = append(allowed, version.ID) + } + } + return allowed, nil +} + +func countSuggestedStories(versions []article.Article, summaries map[uint32]article.SEOSummary) int { + stories := map[uint32]bool{} + standalone := 0 + for _, version := range versions { + if summaries[version.ID].Status != article.SEOStatusSuggestions { + continue + } + if version.StoryID == 0 { + standalone++ + continue + } + stories[version.StoryID] = true + } + return len(stories) + standalone +} + +func (u *ListArticlesUseCase) storyRows(command ListStoriesCommand, refs []article.StoryRef, creatable bool, owners *siteOwners) ([]StoryRow, error) { + storyIDs := make([]uint32, 0, len(refs)) + for _, ref := range refs { + storyIDs = append(storyIDs, ref.StoryID) + } + loaded, err := u.articleRepository.ListByStories(storyIDs) + if err != nil { + return nil, err + } + byStory := map[uint32][]article.Article{} + for _, version := range command.Caller.readableVersions(loaded) { + byStory[version.StoryID] = append(byStory[version.StoryID], version) + } + siteRows := map[uint32][]localization.Row{} + rows := make([]StoryRow, 0, len(refs)) + representatives := make([]article.Article, 0, len(refs)) + for _, ref := range refs { + versions := byStory[ref.StoryID] + representative, found := versionByID(versions, ref.ArticleID) + if !found { + continue + } + languages, cached := siteRows[representative.SiteID] + if !cached { + if languages, err = u.localization.Rows(representative.SiteID); err != nil { + return nil, err + } + siteRows[representative.SiteID] = languages + } + rows = append(rows, buildStoryRow(command.Caller, representative, versions, languages, creatable)) + representatives = append(representatives, representative) + } + summaries, err := u.summarize(representatives, owners) + if err != nil { + return nil, err + } + rendered, err := renderArticles(u.bodyRenderer, representatives, command.Format) + if err != nil { + return nil, err + } + for index := range rows { + rows[index].Article = rendered[index] + rows[index].SEO = summaries[rendered[index].ID] + } + return rows, nil +} + +func versionByID(versions []article.Article, id uint32) (article.Article, bool) { + index := slices.IndexFunc(versions, func(version article.Article) bool { return version.ID == id }) + if index < 0 { + return article.Article{}, false + } + return versions[index], true +} + +func buildStoryRow(caller ContentCaller, representative article.Article, versions []article.Article, languages []localization.Row, creatable bool) StoryRow { + ordered := sortByLanguage(versions, languages) + coverage := make([]article.Translation, 0, len(ordered)) + updatedAt := time.Time{} + authors := []author.Byline{} + seen := map[uint32]bool{} + for _, version := range ordered { + coverage = append(coverage, article.Translation{ + ID: version.ID, + Lang: version.Lang, + Title: version.Title, + IsOnline: version.IsOnline, + LanguageEnabled: version.Lang == language.Unassigned || localization.EnabledTag(languages, version.Lang), + Capabilities: caller.capabilitiesFor(version), + }) + if version.UpdatedAt.After(updatedAt) { + updatedAt = version.UpdatedAt + } + for _, byline := range version.Authors { + if !seen[byline.ID] { + seen[byline.ID] = true + authors = append(authors, byline) + } + } + } + return StoryRow{ + UpdatedAt: updatedAt, + Authors: authors, + Coverage: coverage, + MissingLangs: missingLanguages(ordered, languages, creatable), + Capabilities: caller.capabilitiesFor(representative), + } +} + +func missingLanguages(versions []article.Article, languages []localization.Row, creatable bool) []string { + missing := []string{} + if !creatable || slices.ContainsFunc(versions, func(version article.Article) bool { return version.Lang == language.Unassigned }) { + return missing + } + present := languagesOf(versions) + for _, row := range languages { + if row.Enabled && !slices.Contains(present, row.Tag) { + missing = append(missing, row.Tag) + } + } + defaultTag := localization.DefaultTag(languages) + slices.SortFunc(missing, func(left string, right string) int { return localization.CompareTags(defaultTag, left, right) }) + return missing +} diff --git a/internal/infrastructure/useCases/ListArticles.go b/internal/infrastructure/useCases/ListArticles.go index 63bb152..adefb0a 100644 --- a/internal/infrastructure/useCases/ListArticles.go +++ b/internal/infrastructure/useCases/ListArticles.go @@ -3,6 +3,7 @@ package useCases import ( "RenewCMS/internal/domain/article" "RenewCMS/internal/domain/builtintype" + "RenewCMS/internal/domain/localization" "RenewCMS/internal/domain/role" "RenewCMS/internal/domain/site" "errors" @@ -12,10 +13,11 @@ type ListArticlesUseCase struct { articleRepository article.Repository bodyRenderer article.BodyRenderer sites site.Repository + localization localization.Repository } -func NewListArticlesUseCase(articleRepository article.Repository, bodyRenderer article.BodyRenderer, sites site.Repository) *ListArticlesUseCase { - return &ListArticlesUseCase{articleRepository: articleRepository, bodyRenderer: bodyRenderer, sites: sites} +func NewListArticlesUseCase(articleRepository article.Repository, bodyRenderer article.BodyRenderer, sites site.Repository, localizationRepository localization.Repository) *ListArticlesUseCase { + return &ListArticlesUseCase{articleRepository: articleRepository, bodyRenderer: bodyRenderer, sites: sites, localization: localizationRepository} } type ListArticlesCommand struct { @@ -28,6 +30,8 @@ type ListArticlesCommand struct { Sort article.Sort SiteKey *string SEO string + Lang *string + Author *uint32 } type ArticlePage struct { @@ -40,13 +44,17 @@ type ArticlePage struct { } func (u *ListArticlesUseCase) ListArticlesPage(command ListArticlesCommand) (ArticlePage, error) { - switch command.Caller.Grants.ScopeFor(role.KeyContentRead, builtintype.KeyArticle) { + return u.listVisibleByRepository(command, visibilityFor(command.Caller)) +} + +func visibilityFor(caller ContentCaller) articleVisibility { + switch caller.Grants.ScopeFor(role.KeyContentRead, builtintype.KeyArticle) { case role.ScopeAll: - return u.listVisibleByRepository(command, articleVisibility{}) + return articleVisibility{} case role.ScopeOwn: - return u.listVisibleByRepository(command, articleVisibility{userID: &command.Caller.UserID}) + return articleVisibility{userID: &caller.UserID} default: - return u.listVisibleByRepository(command, articleVisibility{onlineOnly: true}) + return articleVisibility{onlineOnly: true} } } @@ -64,6 +72,13 @@ func (v articleVisibility) apply(filters article.Filters) article.Filters { return filters } +func (u *ListArticlesUseCase) countFlat(command ListArticlesCommand, visibility articleVisibility) (article.Counts, error) { + if command.Lang != nil { + return u.countVisibleVersions(command, visibility) + } + return u.countVisible(visibility) +} + func (u *ListArticlesUseCase) countVisible(visibility articleVisibility) (article.Counts, error) { if visibility.userID != nil { return u.articleRepository.CountVisibleTo(*visibility.userID) @@ -73,7 +88,7 @@ func (u *ListArticlesUseCase) countVisible(visibility articleVisibility) (articl func (u *ListArticlesUseCase) listVisibleByRepository(command ListArticlesCommand, visibility articleVisibility) (ArticlePage, error) { onlineOnly := visibility.onlineOnly - counts, err := u.countVisible(visibility) + counts, err := u.countFlat(command, visibility) if err != nil { return ArticlePage{}, err } @@ -149,7 +164,12 @@ func (u *ListArticlesUseCase) summarizeVisible(visibility articleVisibility) (ma if err != nil { return nil, err } - return u.summarize(found.Items) + return u.summarize(found.Items, &siteOwners{repository: u.sites}) +} + +func (u *ListArticlesUseCase) countVisibleVersions(command ListArticlesCommand, visibility articleVisibility) (article.Counts, error) { + filters := visibility.apply(article.Filters{Lang: command.Lang}) + return u.articleRepository.CountVersions(filters) } func countSuggestions(summaries map[uint32]article.SEOSummary) int { @@ -167,19 +187,35 @@ func pageBySEOStatus(items []article.Article, summaries map[uint32]article.SEOSu return pageSlice(matching, command.Page, command.Size), len(matching) } -func (u *ListArticlesUseCase) summarize(items []article.Article) (map[uint32]article.SEOSummary, error) { +type siteOwners struct { + repository site.Repository + byID map[uint32]site.Site +} + +func (o *siteOwners) get() (map[uint32]site.Site, error) { + if o.byID != nil { + return o.byID, nil + } + known, err := o.repository.List() + if err != nil { + return nil, err + } + o.byID = make(map[uint32]site.Site, len(known)) + for _, owner := range known { + o.byID[owner.ID] = owner + } + return o.byID, nil +} + +func (u *ListArticlesUseCase) summarize(items []article.Article, lookup *siteOwners) (map[uint32]article.SEOSummary, error) { summaries := make(map[uint32]article.SEOSummary, len(items)) if len(items) == 0 { return summaries, nil } - known, err := u.sites.List() + owners, err := lookup.get() if err != nil { return nil, err } - owners := make(map[uint32]site.Site, len(known)) - for _, owner := range known { - owners[owner.ID] = owner - } for _, item := range items { summaries[item.ID] = assessSEO(item, owners[item.SiteID]).Summary() } @@ -198,5 +234,5 @@ func (u *ListArticlesUseCase) siteIDOf(key string) (uint32, bool, error) { } func pageFilters(command ListArticlesCommand) article.Filters { - return article.Filters{Page: command.Page, Size: command.Size, Q: command.Q, Status: command.Status, Sort: command.Sort} + return article.Filters{Page: command.Page, Size: command.Size, Q: command.Q, Status: command.Status, Sort: command.Sort, Lang: command.Lang, AuthorID: command.Author} } diff --git a/test/api_keys_http_test.go b/test/api_keys_http_test.go index 8ec85f2..2291f52 100644 --- a/test/api_keys_http_test.go +++ b/test/api_keys_http_test.go @@ -111,7 +111,7 @@ func newAPIKeyEnvRecording(t *testing.T, recorder audit.Recorder) *apiKeyEnv { Token: env.token, CreateArticleUseCase: useCases.NewCreateArticleUseCase(passthroughCreateTransactor(env.articles), renderer, recorder, plainSites(), plainAuthors()), GetArticleUseCase: useCases.NewGetArticleUseCase(env.articles, renderer), - ListArticlesUseCase: useCases.NewListArticlesUseCase(env.articles, renderer, plainSites()), + ListArticlesUseCase: useCases.NewListArticlesUseCase(env.articles, renderer, plainSites(), plainLocalization()), UpdateArticleUseCase: useCases.NewUpdateArticleUseCase(env.articles, renderer, recorder, plainSites(), plainAuthors(), env.images, newFakeLocalizationStore(env.articles), plainStoryMover(env.articles)), CreateTranslationUseCase: plainTranslation(env.articles), DescribeArticleUseCase: plainDescribe(env.articles), diff --git a/test/article_body_format_test.go b/test/article_body_format_test.go index e3b5322..57367f2 100644 --- a/test/article_body_format_test.go +++ b/test/article_body_format_test.go @@ -45,7 +45,7 @@ func newBodyFormatRouter(articleRepo *mockArticleRepo, userRepo *mockUserRepo, r articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), renderer, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, renderer), - useCases.NewListArticlesUseCase(articleRepo, renderer, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, renderer, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, renderer, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/article_body_unlimited_http_test.go b/test/article_body_unlimited_http_test.go index 8a4fc09..e2867e5 100644 --- a/test/article_body_unlimited_http_test.go +++ b/test/article_body_unlimited_http_test.go @@ -25,7 +25,7 @@ func newUnlimitedBodyRouter(articleRepo *mockArticleRepo, token *authtoken.Auth) articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), renderer, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, renderer), - useCases.NewListArticlesUseCase(articleRepo, renderer, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, renderer, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, renderer, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/article_created_by_http_test.go b/test/article_created_by_http_test.go index 9189a16..8d44f48 100644 --- a/test/article_created_by_http_test.go +++ b/test/article_created_by_http_test.go @@ -37,7 +37,7 @@ func newCreatedByEnv(t *testing.T) (*activityHTTPEnv, http.Handler) { AuthenticateAPIKeyUseCase: useCases.NewAuthenticateAPIKeyUseCase(e.keys, e.generator, e.users, e.clock), CreateArticleUseCase: useCases.NewCreateArticleUseCase(passthroughCreateTransactor(e.articles), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), GetArticleUseCase: useCases.NewGetArticleUseCase(e.articles, &fakeBodyRenderer{}), - ListArticlesUseCase: useCases.NewListArticlesUseCase(e.articles, &fakeBodyRenderer{}, plainSites()), + ListArticlesUseCase: useCases.NewListArticlesUseCase(e.articles, &fakeBodyRenderer{}, plainSites(), plainLocalization()), DeleteArticleUseCase: useCases.NewDeleteArticleUseCase(e.articles, discardRecorder), UpdateArticleUseCase: useCases.NewUpdateArticleUseCase(e.articles, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(e.articles), plainStoryMover(e.articles)), CreateTranslationUseCase: plainTranslation(e.articles), diff --git a/test/article_list_localization_http_test.go b/test/article_list_localization_http_test.go new file mode 100644 index 0000000..1b0f87a --- /dev/null +++ b/test/article_list_localization_http_test.go @@ -0,0 +1,713 @@ +package test + +import ( + entity "RenewCMS/internal/infrastructure/persistence/models" + "encoding/json" + "net/http" + "net/http/httptest" + "reflect" + "sort" + "strconv" + "strings" + "testing" + "time" + + validator "RenewCMS/api/pkg/validator" + + "gorm.io/gorm" +) + +const failedToRetrieveArticles = "Failed to retrieve articles\n" + +func expectIDs(t *testing.T, label string, got []int, want ...int) { + t.Helper() + if !reflect.DeepEqual(got, want) { + t.Fatalf("%s: got %v, want %v", label, got, want) + } +} + +func expectCounts(t *testing.T, got map[string]int, want map[string]int) { + t.Helper() + if !reflect.DeepEqual(got, want) { + t.Fatalf("counts: got %v, want %v", got, want) + } +} + +func bylineNames(item map[string]any, field string) []string { + names := []string{} + for _, entry := range item[field].([]any) { + names = append(names, entry.(map[string]any)["name"].(string)) + } + return names +} + +func TestArticleListLocalizationSQLite(t *testing.T) { + runArticleListLocalization(t, openContractSQLite) +} + +func TestMySQLArticleListLocalization(t *testing.T) { + runArticleListLocalization(t, openContractMySQL) +} + +func TestPostgresArticleListLocalization(t *testing.T) { + runArticleListLocalization(t, openContractPostgres) +} + +func runArticleListLocalization(t *testing.T, open func(*testing.T) *gorm.DB) { + validator.InitValidator() + fresh := func(t *testing.T) *localizationListEnv { return newLocalizationListEnv(t, open) } + shared := fresh(t) + base := func(t *testing.T) *localizationListEnv { + shared.t = t + shared.repo.calls = 0 + shared.repo.failMethod = "" + return shared + } + + t.Run("A bare request is still the flat envelope", func(t *testing.T) { + env := base(t) + rec, _ := env.get(localizationOwnerID, "") + var keys map[string]json.RawMessage + if err := json.Unmarshal(rec.Body.Bytes(), &keys); err != nil { + t.Fatal(err) + } + if len(keys) != 5 || keys["items"] == nil || keys["total"] == nil || keys["page"] == nil || keys["size"] == nil || keys["counts"] == nil { + t.Fatalf("unexpected envelope keys: %s", rec.Body.String()) + } + _, body := env.get(localizationOwnerID, "?size=100") + if body.Total != 17 || len(body.Items) != 17 { + t.Fatalf("expected 17 versions, got total %d and %d items", body.Total, len(body.Items)) + } + if _, grouped := body.Items[0]["coverage"]; grouped { + t.Fatal("the flat item must not carry grouped fields") + } + }) + + t.Run("lang, tab and group are not read unless asked", func(t *testing.T) { + env := base(t) + _, body := env.get(0, "?size=5") + if body.Total != 12 || len(body.Items) != 5 { + t.Fatalf("expected 12 online versions, got total %d and %d items", body.Total, len(body.Items)) + } + for _, item := range body.Items { + if _, ok := item["lang"]; !ok { + t.Fatal("items keep lang") + } + if _, ok := item["story_id"]; !ok { + t.Fatal("items keep story_id") + } + } + expectCounts(t, body.Counts, map[string]int{"all": 12, "online": 12, "draft": 0, "suggestions": 0}) + }) + + t.Run("group=story needs an editorial caller", func(t *testing.T) { + for name, caller := range map[string]uint32{"no cookie": 0, "malformed cookie": localizationMalformed, "unverified user": localizationUnverifiedID} { + env := base(t) + rec, _ := env.get(caller, "?group=story") + if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), paginationBodyMismatch) { + t.Fatalf("%s: expected 400, got %d %q", name, rec.Code, rec.Body.String()) + } + if env.repo.calls != 0 { + t.Fatalf("%s: the repository was called", name) + } + } + }) + + t.Run("Invalid new parameters answer 400", func(t *testing.T) { + queries := []string{ + "group=stories", "group=", "group=story&tab=review", "group=story&tab=published", "group=story&tab=", "tab=missing", + "group=story&lang=english", "group=story&lang=xx", "group=story&lang=", "group=story&author=0", "group=story&author=abc", + "group=story&author=", "author=-1", "lang=english", "lang=", "author=0", + } + for _, query := range queries { + env := base(t) + rec, _ := env.get(localizationOwnerID, "?"+query) + if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), paginationBodyMismatch) { + t.Fatalf("%s: expected 400, got %d %q", query, rec.Code, rec.Body.String()) + } + if env.repo.calls != 0 { + t.Fatalf("%s: the repository was called", query) + } + } + }) + + t.Run("All languages, page 1", func(t *testing.T) { + env := base(t) + rec, body := env.get(localizationOwnerID, "?group=story&size=5") + if rec.Code != http.StatusOK || body.Total != 8 || body.Page != 1 || body.Size != 5 { + t.Fatalf("unexpected page: %d total=%d page=%d size=%d", rec.Code, body.Total, body.Page, body.Size) + } + expectIDs(t, "stories", body.storyIDs(), 1, 2, 3, 4, 5) + if body.Counts["all"] != 8 { + t.Fatalf("counts.all = %d", body.Counts["all"]) + } + }) + + t.Run("All languages, page 2 is the last", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&size=5&page=2") + expectIDs(t, "stories", body.storyIDs(), 6, 7, 8) + if body.Total != 8 { + t.Fatalf("total = %d", body.Total) + } + }) + + t.Run("A page beyond the last is empty with the real total", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&size=5&page=99") + if len(body.Items) != 0 || body.Total != 8 || body.Counts["all"] != 8 { + t.Fatalf("unexpected page: %d items, total %d, counts %v", len(body.Items), body.Total, body.Counts) + } + }) + + t.Run("A grouped row describes the representative version and the story", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story") + item := body.item(1) + if int(item["id"].(float64)) != 101 || item["lang"] != "en" || item["site_id"] == nil || item["slug"] != "a-101" || item["is_online"] != true { + t.Fatalf("unexpected representative: %v", item) + } + if item["story_updated_at"] != "2026-10-30T10:00:00Z" { + t.Fatalf("story_updated_at = %v", item["story_updated_at"]) + } + if names := bylineNames(item, "story_authors"); !reflect.DeepEqual(names, []string{"Marta Alves"}) { + t.Fatalf("story_authors = %v", names) + } + for _, field := range []string{"coverage", "missing_langs", "capabilities"} { + if item[field] == nil { + t.Fatalf("%s is missing", field) + } + } + entry := item["coverage"].([]any)[0].(map[string]any) + for _, field := range []string{"id", "lang", "title", "is_online", "language_enabled", "capabilities"} { + if _, ok := entry[field]; !ok { + t.Fatalf("coverage entry lacks %s", field) + } + } + }) + + t.Run("The representative is the default-language version, else the first readable by language code", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story") + for story, want := range map[int]int{1: 101, 4: 401, 8: 801} { + if got := int(body.item(story)["id"].(float64)); got != want { + t.Fatalf("story %d representative = %d, want %d", story, got, want) + } + } + if body.item(8)["lang"] != "es" { + t.Fatalf("story 8 lang = %v", body.item(8)["lang"]) + } + }) + + t.Run("The representative falls back when the default version is unreadable", func(t *testing.T) { + env := fresh(t) + env.must(env.db.Model(&entity.Article{}).Where("id = ?", 502).UpdateColumn("is_online", true).Error) + env.must(env.db.Model(&entity.Article{}).Where("id = ?", 501).UpdateColumn("is_online", false).Error) + _, body := env.get(localizationWriterID, "?group=story") + item := body.item(5) + if int(item["id"].(float64)) != 502 || item["lang"] != "de" { + t.Fatalf("expected fallback to 502, got %v", item["id"]) + } + if len(item["coverage"].([]any)) != 1 { + t.Fatalf("the hidden version leaks: %v", item["coverage"]) + } + }) + + t.Run("story_updated_at is the latest readable version update", func(t *testing.T) { + env := fresh(t) + env.must(env.db.Model(&entity.Article{}).Where("id = ?", 102).UpdateColumn("updated_at", time.Date(2026, 11, 1, 8, 0, 0, 0, time.UTC)).Error) + _, writer := env.get(localizationWriterID, "?group=story") + if got := writer.item(1)["story_updated_at"]; got != "2026-10-30T09:00:00Z" { + t.Fatalf("writer story_updated_at = %v", got) + } + _, owner := env.get(localizationOwnerID, "?group=story") + if got := owner.item(1)["story_updated_at"]; got != "2026-11-01T08:00:00Z" { + t.Fatalf("owner story_updated_at = %v", got) + } + expectIDs(t, "owner order", owner.storyIDs()[:2], 1, 2) + }) + + t.Run("Coverage lists readable versions in the chip order, then the languages that can be created", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story") + cases := []struct { + story int + coverage []string + missing []string + }{ + {1, []string{"en", "ar", "de", "fr"}, []string{}}, + {3, []string{"en", "fr"}, []string{"ar", "de"}}, + {4, []string{"en"}, []string{"ar", "de", "fr"}}, + {8, []string{"es"}, []string{"en", "ar", "de", "fr"}}, + } + for _, c := range cases { + item := body.item(c.story) + if got := coverageLangs(item); !reflect.DeepEqual(got, c.coverage) { + t.Fatalf("story %d coverage = %v", c.story, got) + } + if got := stringsOf(item["missing_langs"]); !reflect.DeepEqual(got, c.missing) { + t.Fatalf("story %d missing_langs = %v", c.story, got) + } + } + if body.item(8)["coverage"].([]any)[0].(map[string]any)["language_enabled"] != false { + t.Fatal("es is disabled") + } + }) + + t.Run("Coverage order is default, other enabled by tag, disabled by tag; missing is listed by the same rule", func(t *testing.T) { + env := fresh(t) + env.addVersion(localizationVersion{id: 802, lang: "it", online: true, title: "Journal 802", updated: localizationDay(10, 8), byline: "Luis Ferrer", noindex: true, storyKey: 8}) + _, body := env.get(localizationOwnerID, "?group=story") + item := body.item(8) + if got := coverageLangs(item); !reflect.DeepEqual(got, []string{"es", "it"}) { + t.Fatalf("coverage = %v", got) + } + if got := stringsOf(item["missing_langs"]); got[0] != "en" { + t.Fatalf("missing_langs = %v", got) + } + }) + + t.Run("A caller who cannot create gets no missing_langs", func(t *testing.T) { + _, body := base(t).get(localizationReaderID, "?group=story") + for _, item := range body.Items { + if len(stringsOf(item["missing_langs"])) != 0 { + t.Fatalf("reader got missing_langs on %v", item["story_id"]) + } + } + if got := coverageLangs(body.item(1)); !reflect.DeepEqual(got, []string{"en", "de"}) { + t.Fatalf("reader coverage = %v", got) + } + }) + + t.Run("The row of a story whose versions are all hidden does not exist", func(t *testing.T) { + _, body := base(t).get(localizationWriterID, "?group=story&size=100") + if body.item(4) != nil || body.Total != 7 || body.Counts["all"] != 7 { + t.Fatalf("story 4 leaks: total=%d counts=%v", body.Total, body.Counts) + } + }) + + t.Run("Hidden versions leave no trace", func(t *testing.T) { + env := fresh(t) + _, with := env.get(localizationWriterID, "?group=story&size=100") + withRec, _ := env.get(localizationWriterID, "?group=story&size=100") + without := fresh(t) + without.must(without.db.Unscoped().Where("id IN ?", []uint32{102, 104, 401, 502, 602}).Delete(&entity.Article{}).Error) + withoutRec, _ := without.get(localizationWriterID, "?group=story&size=100") + if withRec.Body.String() != withoutRec.Body.String() { + t.Fatalf("hidden versions change the response:\n%s\n%s", withRec.Body.String(), withoutRec.Body.String()) + } + if len(with.Items) == 0 { + t.Fatal("expected rows") + } + }) + + t.Run("Unassigned articles are their own story with an empty language", func(t *testing.T) { + env := fresh(t) + env.addVersion(localizationVersion{id: 30, lang: "", online: true, title: "Journal 30", updated: localizationDay(1, 9), byline: "Luis Ferrer", noindex: true, storyKey: 30}) + _, body := env.get(localizationOwnerID, "?group=story&size=100") + item := body.item(30) + if item["lang"] != "" || len(item["coverage"].([]any)) != 1 || len(stringsOf(item["missing_langs"])) != 0 { + t.Fatalf("unexpected unassigned row: %v", item) + } + _, missing := env.get(localizationOwnerID, "?group=story&lang=fr&tab=missing&size=100") + if missing.item(30) != nil { + t.Fatal("unassigned article listed in the not-in view") + } + _, needs := env.get(localizationOwnerID, "?group=story&tab=missing&size=100") + if needs.item(30) != nil { + t.Fatal("unassigned article listed as needing translation") + } + }) + + t.Run("French selected", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&lang=fr&size=5") + if body.Total != 4 { + t.Fatalf("total = %d", body.Total) + } + expectIDs(t, "stories", body.storyIDs(), 1, 2, 3, 6) + expectIDs(t, "representatives", body.articleIDs(), 102, 202, 302, 602) + if len(body.item(1)["coverage"].([]any)) != 4 || len(bylineNames(body.item(1), "story_authors")) != 1 { + t.Fatalf("the row must carry the whole story: %v", body.item(1)) + } + }) + + t.Run("Disabled languages can be selected", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&lang=es") + expectIDs(t, "articles", body.articleIDs(), 801) + if body.Total != 1 || body.item(8)["coverage"].([]any)[0].(map[string]any)["language_enabled"] != false { + t.Fatalf("unexpected result: %v", body) + } + }) + + t.Run("A language nobody has gives an empty list", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&lang=it") + if len(body.Items) != 0 || body.Total != 0 { + t.Fatalf("expected nothing, got %d items", len(body.Items)) + } + }) + + t.Run("lang also narrows the flat list", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?lang=fr") + if body.Total != 4 || len(body.Items) != 4 { + t.Fatalf("total = %d", body.Total) + } + for _, item := range body.Items { + if item["lang"] != "fr" { + t.Fatalf("item with lang %v", item["lang"]) + } + } + if body.Counts["all"] != 4 || body.Counts["online"] != 2 || body.Counts["draft"] != 2 { + t.Fatalf("counts = %v", body.Counts) + } + _, filtered := base(t).get(localizationOwnerID, "?lang=fr&status=draft&q=zinc") + if filtered.Counts["all"] != 4 { + t.Fatalf("flat counts must ignore q and status: %v", filtered.Counts) + } + }) + + t.Run("Not in French", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&lang=fr&tab=missing&size=5") + if body.Total != 4 { + t.Fatalf("total = %d", body.Total) + } + expectIDs(t, "stories", body.storyIDs(), 4, 5, 7, 8) + expectIDs(t, "representatives", body.articleIDs(), 401, 501, 701, 801) + for _, item := range body.Items { + if !listHasTag(stringsOf(item["missing_langs"]), "fr") { + t.Fatalf("missing_langs lacks fr: %v", item["missing_langs"]) + } + } + }) + + t.Run("Needs translation, no language selected", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&tab=missing&size=5") + if body.Total != 7 { + t.Fatalf("total = %d", body.Total) + } + expectIDs(t, "stories", body.storyIDs(), 2, 3, 4, 5, 6) + }) + + t.Run("Missing means no readable version, not no stored version", func(t *testing.T) { + env := fresh(t) + env.must(env.db.Model(&entity.Article{}).Where("id = ?", 401).UpdateColumn("is_online", true).Error) + env.addVersion(localizationVersion{id: 402, lang: "fr", online: false, title: "Hidden French draft", updated: localizationDay(27, 8), byline: "Marta Alves", noindex: true, storyKey: 4}) + rec, body := env.get(localizationWriterID, "?group=story&lang=fr&tab=missing&size=100") + if body.item(4) == nil { + t.Fatal("story 4 must be listed as not in French") + } + if strings.Contains(rec.Body.String(), "402") || strings.Contains(rec.Body.String(), "Hidden French draft") { + t.Fatal("the response tells a fr version exists") + } + }) + + t.Run("Sites that do not enable the language are excluded", func(t *testing.T) { + env := fresh(t) + env.addVersion(localizationVersion{id: 5001, lang: "en", online: true, title: "Atelier one", updated: localizationDay(20, 9), byline: "Luis Ferrer", noindex: true, storyKey: 50, siteKey: "atelier"}) + env.addVersion(localizationVersion{id: 5002, lang: "fr", online: true, title: "Atelier un", updated: localizationDay(20, 8), byline: "Luis Ferrer", noindex: true, storyKey: 50, siteKey: "atelier"}) + _, german := env.get(localizationOwnerID, "?group=story&lang=de&tab=missing&size=100") + if german.item(50) != nil || german.Total == 0 { + t.Fatalf("atelier story listed or journal empty: %v", german.storyIDs()) + } + _, spanish := env.get(localizationOwnerID, "?group=story&lang=es&tab=missing") + if spanish.Total != 0 || len(spanish.Items) != 0 { + t.Fatalf("es is disabled, got %d", spanish.Total) + } + }) + + t.Run("Needs translation counts only languages the caller can create", func(t *testing.T) { + env := base(t) + _, body := env.get(localizationReaderID, "?group=story&tab=missing") + if body.Total != 0 || len(body.Items) != 0 { + t.Fatalf("expected nothing, got %d", body.Total) + } + _, french := env.get(localizationReaderID, "?group=story&lang=fr&tab=missing&size=100") + expectIDs(t, "reader stories", french.storyIDs(), 1, 5, 6, 7, 8) + }) + + t.Run("Nothing missing", func(t *testing.T) { + env := fresh(t) + for story := uint32(60); story < 63; story++ { + env.addVersion(localizationVersion{id: story * 100, lang: "en", online: true, title: "Press en", updated: localizationDay(5, 9), byline: "Luis Ferrer", noindex: true, storyKey: story, siteKey: "press"}) + env.addVersion(localizationVersion{id: story*100 + 1, lang: "fr", online: true, title: "Press fr", updated: localizationDay(5, 8), byline: "Luis Ferrer", noindex: true, storyKey: story, siteKey: "press"}) + } + _, body := env.get(localizationOwnerID, "?group=story&tab=missing&site=press") + if len(body.Items) != 0 || body.Total != 0 || body.Counts["missing"] != 0 || body.Counts["all"] != 3 { + t.Fatalf("unexpected: total=%d counts=%v", body.Total, body.Counts) + } + }) + + t.Run("Status matches a readable version of the story", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&status=draft") + expectIDs(t, "stories", body.storyIDs(), 1, 4, 5, 6) + if body.Total != 4 { + t.Fatalf("total = %d", body.Total) + } + }) + + t.Run("Combined filters need a single readable version to satisfy them together", func(t *testing.T) { + env := fresh(t) + camille := env.authors["Camille Roche"] + env.must(env.db.Model(&entity.ArticleAuthor{}).Where("article_id IN ?", []uint32{102, 104}).UpdateColumn("author_id", camille).Error) + marta := env.authors["Marta Alves"] + _, draftMarta := env.get(localizationOwnerID, "?group=story&status=draft&author="+itoa(marta)) + if draftMarta.item(1) != nil { + t.Fatal("story 1 must not be listed") + } + _, onlineMarta := env.get(localizationOwnerID, "?group=story&status=online&author="+itoa(marta)) + if onlineMarta.item(1) == nil { + t.Fatal("story 1 must be listed") + } + _, draftCamille := env.get(localizationOwnerID, "?group=story&status=draft&author="+itoa(camille)) + if draftCamille.item(1) == nil { + t.Fatal("story 1 must be listed") + } + _, union := env.get(localizationOwnerID, "?group=story&author="+itoa(camille)) + if names := bylineNames(union.item(1), "story_authors"); !reflect.DeepEqual(names, []string{"Marta Alves", "Camille Roche"}) { + t.Fatalf("story_authors = %v", names) + } + }) + + t.Run("Search matches any readable title of the story", func(t *testing.T) { + env := base(t) + _, body := env.get(localizationOwnerID, "?group=story&q=RENOVATION") + expectIDs(t, "articles", body.articleIDs(), 101) + _, french := env.get(localizationOwnerID, "?group=story&lang=fr&q=chiffrons") + expectIDs(t, "french", french.articleIDs(), 102) + _, english := env.get(localizationOwnerID, "?group=story&lang=en&q=chiffrons") + if len(english.Items) != 0 { + t.Fatal("expected nothing") + } + _, accent := env.get(localizationOwnerID, "?group=story&q=r%C3%A9novation") + expectIDs(t, "accent", accent.articleIDs(), 101) + }) + + t.Run("Search in the not-in view matches readable source titles", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&lang=fr&tab=missing&q=Spring") + expectIDs(t, "stories", body.storyIDs(), 4) + }) + + t.Run("Author in language mode", func(t *testing.T) { + env := base(t) + _, body := env.get(localizationOwnerID, "?group=story&lang=fr&author="+itoa(env.authors["Camille Roche"])) + expectIDs(t, "stories", body.storyIDs(), 2, 6) + }) + + t.Run("Site filter", func(t *testing.T) { + env := fresh(t) + env.addVersion(localizationVersion{id: 7001, lang: "en", online: true, title: "Press piece", updated: localizationDay(6, 9), byline: "Luis Ferrer", noindex: true, storyKey: 70, siteKey: "press"}) + _, body := env.get(localizationOwnerID, "?group=story&site=press") + expectIDs(t, "stories", body.storyIDs(), 70) + _, unknown := env.get(localizationOwnerID, "?group=story&site=nowhere") + if len(unknown.Items) != 0 || unknown.Total != 0 { + t.Fatal("unknown site must be empty") + } + }) + + t.Run("SEO filter on one version", func(t *testing.T) { + env := fresh(t) + env.must(env.db.Model(&entity.Article{}).Where("id = ?", 502).UpdateColumn("noindex", false).Error) + _, body := env.get(localizationOwnerID, "?group=story&seo=suggestions") + expectIDs(t, "articles", body.articleIDs(), 501) + if body.Total != 1 || body.Counts["suggestions"] != 1 { + t.Fatalf("total=%d counts=%v", body.Total, body.Counts) + } + }) + + t.Run("All filters combine with paging and stay stable", func(t *testing.T) { + env := fresh(t) + for story := uint32(21); story <= 24; story++ { + env.addVersion(localizationVersion{id: story * 10, lang: "en", online: true, title: "Journal extra", updated: localizationDay(2, 9), byline: "Luis Ferrer", noindex: true, storyKey: story}) + } + seen := map[int]bool{} + sizes := []int{} + for page := 1; page <= 3; page++ { + _, body := env.get(localizationOwnerID, "?group=story&size=5&page="+itoa(uint32(page))) + if body.Total != 12 { + t.Fatalf("page %d total = %d", page, body.Total) + } + sizes = append(sizes, len(body.Items)) + for _, id := range body.storyIDs() { + if seen[id] { + t.Fatalf("story %d repeats", id) + } + seen[id] = true + } + } + if !reflect.DeepEqual(sizes, []int{5, 5, 2}) { + t.Fatalf("page sizes = %v", sizes) + } + }) + + t.Run("Sort orders stories, not the page", func(t *testing.T) { + env := base(t) + cases := map[string][]int{ + "updated_at:desc": {1, 2, 3, 4, 5, 6, 7, 8}, + "updated_at:asc": {8, 7, 6, 5, 4, 3, 2, 1}, + "title:asc": {1, 2, 3, 5, 6, 7, 8, 4}, + "title:desc": {4, 8, 7, 6, 5, 3, 2, 1}, + "status:asc": {4, 8, 7, 6, 5, 3, 2, 1}, + "created_at:asc": {8, 7, 6, 5, 4, 3, 2, 1}, + } + for sortValue, want := range cases { + _, body := env.get(localizationOwnerID, "?group=story&size=100&sort="+sortValue) + expectIDs(t, sortValue, body.storyIDs(), want...) + } + }) + + t.Run("Ties end with story_id descending", func(t *testing.T) { + env := fresh(t) + for _, story := range []uint32{11, 12} { + env.addVersion(localizationVersion{id: story * 10, lang: "en", online: true, title: "Tied", updated: localizationDay(1, 9), byline: "Luis Ferrer", noindex: true, storyKey: story}) + } + _, body := env.get(localizationOwnerID, "?group=story&size=100") + expectIDs(t, "tail", body.storyIDs()[8:], 12, 11) + }) + + t.Run("Counts of the Journal dataset, all languages", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story") + expectCounts(t, body.Counts, map[string]int{"all": 8, "online": 7, "draft": 4, "suggestions": 0, "missing": 7}) + }) + + t.Run("Counts of the Journal dataset, French", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&lang=fr") + expectCounts(t, body.Counts, map[string]int{"all": 4, "online": 2, "draft": 2, "suggestions": 0, "missing": 4}) + }) + + t.Run("Counts follow the search", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&q=zinc") + expectCounts(t, body.Counts, map[string]int{"all": 0, "online": 0, "draft": 0, "suggestions": 0, "missing": 0}) + if len(body.Items) != 0 || body.Total != 0 { + t.Fatal("expected nothing") + } + }) + + t.Run("Counts follow the author filter", func(t *testing.T) { + env := base(t) + _, body := env.get(localizationOwnerID, "?group=story&author="+itoa(env.authors["Marta Alves"])) + expectIDs(t, "stories", body.storyIDs(), 1, 4, 7) + if body.Total != 3 || body.Counts["all"] != 3 { + t.Fatalf("total=%d counts=%v", body.Total, body.Counts) + } + }) + + t.Run("Counts do not move with status or tab", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&lang=fr&status=draft&tab=missing") + if body.Counts["all"] != 4 || body.Counts["online"] != 2 || body.Counts["draft"] != 2 { + t.Fatalf("counts = %v", body.Counts) + } + }) + + t.Run("A reader's counts cover readable versions only", func(t *testing.T) { + _, body := base(t).get(localizationReaderID, "?group=story") + if body.Counts["draft"] != 0 || body.Counts["all"] != 7 || body.Counts["missing"] != 0 { + t.Fatalf("counts = %v", body.Counts) + } + }) + + t.Run("Total versus counts", func(t *testing.T) { + _, body := base(t).get(localizationOwnerID, "?group=story&tab=missing") + if body.Total != body.Counts["missing"] || body.Total != 7 { + t.Fatalf("total=%d counts=%v", body.Total, body.Counts) + } + }) + + t.Run("Missing membership and counts agree with the missing_langs of the rows", func(t *testing.T) { + env := fresh(t) + env.addVersion(localizationVersion{id: 31, lang: "", online: true, title: "Journal 31", updated: localizationDay(2, 9), byline: "Luis Ferrer", noindex: true, storyKey: 31}) + env.addVersion(localizationVersion{id: 32, lang: "en", online: true, title: "Journal 32", updated: localizationDay(2, 8), byline: "Luis Ferrer", noindex: true, storyKey: 31}) + env.addVersion(localizationVersion{id: 33, lang: "", online: true, title: "Journal 33", updated: localizationDay(3, 9), byline: "Luis Ferrer", noindex: true, storyKey: 33}) + env.addVersion(localizationVersion{id: 34, lang: "fr", online: false, title: "Journal 34", updated: localizationDay(3, 8), byline: "Luis Ferrer", noindex: true, storyKey: 33}) + env.addVersion(localizationVersion{id: 35, lang: "", online: true, title: "Journal 35", updated: localizationDay(4, 9), byline: "Luis Ferrer", noindex: true, storyKey: 35}) + _, all := env.get(localizationOwnerID, "?group=story&size=100") + _, missing := env.get(localizationOwnerID, "?group=story&tab=missing&size=100") + wanted := []int{} + for _, item := range all.Items { + if len(stringsOf(item["missing_langs"])) > 0 { + wanted = append(wanted, int(item["story_id"].(float64))) + } + } + got := missing.storyIDs() + sort.Ints(wanted) + sort.Ints(got) + expectIDs(t, "needs translation", got, wanted...) + if missing.Total != len(wanted) || missing.Counts["missing"] != len(wanted) || all.Counts["missing"] != len(wanted) { + t.Fatalf("total=%d counts=%v, want %d", missing.Total, missing.Counts, len(wanted)) + } + for _, mixed := range []int{31, 33, 35} { + if missing.item(mixed) != nil { + t.Fatalf("story %d holding an unassigned version listed as needing translation", mixed) + } + } + for _, tag := range []string{"fr", "de", "ar", "en"} { + _, notIn := env.get(localizationOwnerID, "?group=story&lang="+tag+"&tab=missing&size=100") + expected := []int{} + for _, item := range all.Items { + if listHasTag(stringsOf(item["missing_langs"]), tag) { + expected = append(expected, int(item["story_id"].(float64))) + } + } + listed := notIn.storyIDs() + sort.Ints(expected) + sort.Ints(listed) + expectIDs(t, "not in "+tag, listed, expected...) + if notIn.Total != len(expected) || notIn.Counts["missing"] != len(expected) { + t.Fatalf("not in %s: total=%d counts=%v, want %d", tag, notIn.Total, notIn.Counts, len(expected)) + } + } + }) + + t.Run("The sites are read once per request", func(t *testing.T) { + env := base(t) + env.siteRepo.lists = 0 + _, body := env.get(localizationOwnerID, "?group=story&size=100") + if len(body.Items) == 0 || env.siteRepo.lists != 1 { + t.Fatalf("items=%d site lists=%d, want rows and 1", len(body.Items), env.siteRepo.lists) + } + }) + + t.Run("An expired token is not an editorial caller", func(t *testing.T) { + env := base(t) + expired, err := env.token.Encode(map[string]any{"user_id": localizationOwnerID, "token_version": 0, "exp": time.Now().Add(-time.Hour).Unix()}) + if err != nil { + t.Fatal(err) + } + req := httptest.NewRequest(http.MethodGet, "/v1/article/?group=story", nil) + req.AddCookie(&http.Cookie{Name: "jwt", Value: expired}) + rec := httptest.NewRecorder() + env.router.ServeHTTP(rec, req) + if rec.Code != http.StatusBadRequest || env.repo.calls != 0 { + t.Fatalf("expected 400 without repository call, got %d calls=%d", rec.Code, env.repo.calls) + } + }) + + t.Run("The grouped envelope has the documented keys and never a null list", func(t *testing.T) { + env := base(t) + rec, _ := env.get(localizationOwnerID, "?group=story&lang=fr&q=zzzznomatch") + var raw map[string]json.RawMessage + if err := json.Unmarshal(rec.Body.Bytes(), &raw); err != nil { + t.Fatal(err) + } + if len(raw) != 5 || string(raw["items"]) != "[]" { + t.Fatalf("unexpected empty envelope: %s", rec.Body.String()) + } + var counts map[string]int + if err := json.Unmarshal(raw["counts"], &counts); err != nil { + t.Fatal(err) + } + for _, key := range []string{"all", "online", "draft", "suggestions", "missing"} { + if _, ok := counts[key]; !ok { + t.Fatalf("counts lack %s: %v", key, counts) + } + } + }) + + t.Run("Repository failure answers 500", func(t *testing.T) { + for _, method := range []string{"FindStoryPage", "CountStories", "FindPage"} { + env := base(t) + env.repo.failMethod = method + rec, _ := env.get(localizationOwnerID, "?group=story") + if rec.Code != http.StatusInternalServerError || rec.Body.String() != failedToRetrieveArticles { + t.Fatalf("%s: %d %q", method, rec.Code, rec.Body.String()) + } + } + }) +} + +func itoa(value uint32) string { + return strconv.FormatUint(uint64(value), 10) +} diff --git a/test/article_list_localization_support_test.go b/test/article_list_localization_support_test.go new file mode 100644 index 0000000..5a14127 --- /dev/null +++ b/test/article_list_localization_support_test.go @@ -0,0 +1,340 @@ +package test + +import ( + "RenewCMS/api/handlers/article" + "RenewCMS/api/pkg/access" + "RenewCMS/api/pkg/authtoken" + "RenewCMS/api/pkg/claims" + domainArticle "RenewCMS/internal/domain/article" + "RenewCMS/internal/domain/role" + "RenewCMS/internal/domain/site" + entity "RenewCMS/internal/infrastructure/persistence/models" + persistence "RenewCMS/internal/infrastructure/persistence/repositories" + "RenewCMS/internal/infrastructure/useCases" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/go-chi/chi/v5" + "gorm.io/gorm" +) + +const ( + localizationOwnerID uint32 = 1 + localizationReaderID uint32 = 2 + localizationWriterID uint32 = 3 + localizationUnverifiedID uint32 = 4 + localizationOtherUserID uint32 = 9 + localizationMalformed uint32 = 999 +) + +type localizationVersion struct { + id uint32 + lang string + online bool + title string + creator uint32 + updated time.Time + byline string + siteKey string + noindex bool + storyKey uint32 +} + +type storyListingRepository struct { + *persistence.ArticleRepository + calls int + failMethod string +} + +func (r *storyListingRepository) failure(method string) error { + r.calls++ + if r.failMethod == method { + return errRepositoryFailure + } + return nil +} + +func (r *storyListingRepository) FindPage(filters domainArticle.Filters) (domainArticle.PageResult, error) { + if err := r.failure("FindPage"); err != nil { + return domainArticle.PageResult{}, err + } + return r.ArticleRepository.FindPage(filters) +} + +func (r *storyListingRepository) CountVersions(filters domainArticle.Filters) (domainArticle.Counts, error) { + if err := r.failure("CountVersions"); err != nil { + return domainArticle.Counts{}, err + } + return r.ArticleRepository.CountVersions(filters) +} + +func (r *storyListingRepository) FindStoryPage(filters domainArticle.StoryFilters) (domainArticle.StoryPage, error) { + if err := r.failure("FindStoryPage"); err != nil { + return domainArticle.StoryPage{}, err + } + return r.ArticleRepository.FindStoryPage(filters) +} + +func (r *storyListingRepository) CountStories(filters domainArticle.StoryFilters) (domainArticle.StoryCounts, error) { + if err := r.failure("CountStories"); err != nil { + return domainArticle.StoryCounts{}, err + } + return r.ArticleRepository.CountStories(filters) +} + +type listCountingSites struct { + *persistence.SiteRepository + lists int +} + +func (r *listCountingSites) List() ([]site.Site, error) { + r.lists++ + return r.SiteRepository.List() +} + +type localizationListEnv struct { + siteRepo *listCountingSites + t *testing.T + db *gorm.DB + repo *storyListingRepository + router *chi.Mux + token *authtoken.Auth + authors map[string]uint32 + sites map[string]uint32 + nextID uint32 +} + +func localizationGrants(userID uint32) (role.GrantSet, bool) { + switch userID { + case localizationOwnerID: + return role.Role{Grants: []role.Grant{ + {Key: role.KeyContentRead, Scope: role.ScopeAll}, + {Key: role.KeyContentCreate, Scope: role.ScopeAll}, + {Key: role.KeyContentUpdate, Scope: role.ScopeAll}, + {Key: role.KeyContentPublish, Scope: role.ScopeAll}, + {Key: role.KeyContentDelete, Scope: role.ScopeAll}, + }}.GrantSet(), true + case localizationReaderID: + return role.Role{}.GrantSet(), true + case localizationWriterID: + return role.Role{Grants: []role.Grant{ + {Key: role.KeyContentRead, Scope: role.ScopeOwn}, + {Key: role.KeyContentCreate, Scope: role.ScopeOwn}, + }}.GrantSet(), true + } + return role.GrantSet{}, false +} + +func localizationAccess(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if userID, ok := claims.UserId(r); ok { + if grants, known := localizationGrants(userID); known { + r = r.WithContext(access.WithCaller(r.Context(), access.Caller{UserID: userID, Grants: grants})) + } + } + next.ServeHTTP(w, r) + }) +} + +func newLocalizationListEnv(t *testing.T, open func(*testing.T) *gorm.DB) *localizationListEnv { + t.Helper() + db := open(t) + repo := &storyListingRepository{ArticleRepository: persistence.NewArticleRepository(db)} + siteRepo := &listCountingSites{SiteRepository: persistence.NewSiteRepository(db)} + list := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, siteRepo, persistence.NewLocalizationRepository(db)) + handler := article.NewHandler(nil, nil, list, nil, nil, nil, nil) + token := authtoken.New([]byte("test-secret")) + router := chi.NewRouter() + router.Route("/v1", func(r chi.Router) { + r.Route("/article", func(r chi.Router) { + r.Use(authtoken.Verifier(token)) + r.Use(localizationAccess) + r.Get("/", handler.ListArticles) + }) + }) + env := &localizationListEnv{siteRepo: siteRepo, t: t, db: db, repo: repo, router: router, token: token, authors: map[string]uint32{}, sites: map[string]uint32{}} + env.seedJournal() + return env +} + +func (e *localizationListEnv) must(err error) { + e.t.Helper() + if err != nil { + e.t.Fatalf("seed: %v", err) + } +} + +func (e *localizationListEnv) addSite(key string, defaultLang string, enabled []string, disabled []string) { + e.t.Helper() + site := entity.Site{Name: key, Key: key, BaseURL: "https://" + key + ".example.com", DefaultRobots: "index"} + e.must(e.db.Create(&site).Error) + e.sites[key] = site.ID + for _, tag := range append(append([]string{}, enabled...), disabled...) { + row := entity.SiteLanguage{SiteID: site.ID, Lang: tag, IsDefault: tag == defaultLang, Confirmed: true} + e.must(e.db.Create(&row).Error) + e.must(e.db.Model(&entity.SiteLanguage{}).Where("site_id = ? AND lang = ?", site.ID, tag).UpdateColumn("enabled", !listHasTag(disabled, tag)).Error) + } +} + +func listHasTag(tags []string, tag string) bool { + for _, candidate := range tags { + if candidate == tag { + return true + } + } + return false +} + +func (e *localizationListEnv) addAuthor(name string) { + e.t.Helper() + row := entity.Author{Name: name, Slug: fmt.Sprintf("author-%d", len(e.authors)+1), SocialLinks: "[]"} + e.must(e.db.Create(&row).Error) + e.authors[name] = row.ID +} + +func (e *localizationListEnv) addVersion(version localizationVersion) { + e.t.Helper() + siteKey := version.siteKey + if siteKey == "" { + siteKey = "journal" + } + creator := version.creator + if creator == 0 { + creator = localizationOwnerID + } + var story entity.Story + if err := e.db.First(&story, version.storyKey).Error; err != nil { + e.must(e.db.Create(&entity.Story{ID: version.storyKey}).Error) + } + row := entity.Article{ + ID: version.id, Title: version.title, Body: "

body

", IsOnline: version.online, SiteID: e.sites[siteKey], CreatedBy: creator, + Slug: fmt.Sprintf("a-%d", version.id), Noindex: version.noindex, Lang: version.lang, StoryID: version.storyKey, + CreatedAt: version.updated.Add(-24 * time.Hour), UpdatedAt: version.updated, ContentRevision: 1, EditRevision: 1, + } + e.must(e.db.Create(&row).Error) + if version.byline != "" { + e.must(e.db.Create(&entity.ArticleAuthor{ArticleID: version.id, AuthorID: e.authors[version.byline], Position: 0}).Error) + } +} + +func localizationDay(day int, hour int) time.Time { + return time.Date(2026, time.October, day, hour, 0, 0, 0, time.UTC) +} + +func (e *localizationListEnv) seedJournal() { + e.addSite("journal", "en", []string{"en", "fr", "de", "ar"}, []string{"es", "it"}) + e.addSite("press", "en", []string{"en", "fr"}, nil) + e.addSite("atelier", "en", []string{"en", "en-GB", "fr"}, nil) + for _, name := range []string{"Marta Alves", "Camille Roche", "Luis Ferrer"} { + e.addAuthor(name) + } + type journalVersion struct { + id uint32 + lang string + online bool + } + stories := []struct { + id uint32 + byline string + latest time.Time + latestID uint32 + versions []journalVersion + }{ + {1, "Marta Alves", localizationDay(30, 10), 102, []journalVersion{{101, "en", true}, {102, "fr", false}, {103, "de", true}, {104, "ar", false}}}, + {2, "Camille Roche", localizationDay(29, 9), 202, []journalVersion{{201, "en", true}, {202, "fr", true}, {203, "de", true}}}, + {3, "Luis Ferrer", localizationDay(28, 9), 302, []journalVersion{{301, "en", true}, {302, "fr", true}}}, + {4, "Marta Alves", localizationDay(27, 9), 401, []journalVersion{{401, "en", false}}}, + {5, "Luis Ferrer", localizationDay(14, 9), 502, []journalVersion{{501, "en", true}, {502, "de", false}}}, + {6, "Camille Roche", localizationDay(13, 9), 602, []journalVersion{{601, "en", true}, {602, "fr", false}}}, + {7, "Marta Alves", localizationDay(12, 9), 702, []journalVersion{{701, "en", true}, {702, "de", true}}}, + {8, "Luis Ferrer", localizationDay(10, 9), 801, []journalVersion{{801, "es", true}}}, + } + for _, story := range stories { + for _, version := range story.versions { + updated := story.latest + if version.id != story.latestID { + updated = story.latest.Add(-time.Hour) + } + e.addVersion(localizationVersion{id: version.id, lang: version.lang, online: version.online, title: fmt.Sprintf("Journal %d", version.id), updated: updated, byline: story.byline, noindex: true, storyKey: story.id}) + } + } + e.must(e.db.Model(&entity.Article{}).Where("id = ?", 101).UpdateColumn("title", "How we price a bathroom renovation").Error) + e.must(e.db.Model(&entity.Article{}).Where("id = ?", 102).UpdateColumn("title", "Comment nous chiffrons une rénovation").Error) + e.must(e.db.Model(&entity.Article{}).Where("id = ?", 401).UpdateColumn("title", "Spring maintenance checklist").Error) + e.nextID = 900 +} + +type storyListBody struct { + Items []map[string]any `json:"items"` + Total int `json:"total"` + Page int `json:"page"` + Size int `json:"size"` + Counts map[string]int `json:"counts"` +} + +func (e *localizationListEnv) get(userID uint32, query string) (*httptest.ResponseRecorder, storyListBody) { + e.t.Helper() + req := httptest.NewRequest(http.MethodGet, "/v1/article/"+query, nil) + switch userID { + case 0: + case localizationMalformed: + req.AddCookie(&http.Cookie{Name: "jwt", Value: "garbage"}) + default: + setJwtCookie(e.t, req, e.token, userID) + } + rec := httptest.NewRecorder() + e.router.ServeHTTP(rec, req) + var body storyListBody + if rec.Code == http.StatusOK { + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + e.t.Fatalf("decode %s: %v", rec.Body.String(), err) + } + } + return rec, body +} + +func (b storyListBody) storyIDs() []int { + ids := []int{} + for _, item := range b.Items { + ids = append(ids, int(item["story_id"].(float64))) + } + return ids +} + +func (b storyListBody) articleIDs() []int { + ids := []int{} + for _, item := range b.Items { + ids = append(ids, int(item["id"].(float64))) + } + return ids +} + +func (b storyListBody) item(storyID int) map[string]any { + for _, item := range b.Items { + if int(item["story_id"].(float64)) == storyID { + return item + } + } + return nil +} + +func stringsOf(value any) []string { + listed, _ := value.([]any) + out := []string{} + for _, entry := range listed { + out = append(out, entry.(string)) + } + return out +} + +func coverageLangs(item map[string]any) []string { + langs := []string{} + for _, entry := range item["coverage"].([]any) { + langs = append(langs, entry.(map[string]any)["lang"].(string)) + } + return langs +} diff --git a/test/article_list_stories_usecase_test.go b/test/article_list_stories_usecase_test.go new file mode 100644 index 0000000..6463c9f --- /dev/null +++ b/test/article_list_stories_usecase_test.go @@ -0,0 +1,68 @@ +package test + +import ( + "testing" + + "RenewCMS/internal/domain/article" + "RenewCMS/internal/infrastructure/useCases" +) + +func TestListStoriesPageSuggestionCounts(t *testing.T) { + cases := []struct { + name string + articles []article.Article + want int + }{ + {"versions of one story count once", []article.Article{ + {ID: 1, StoryID: 10, SiteID: 1, IsOnline: true}, + {ID: 2, StoryID: 10, SiteID: 1, IsOnline: true}, + }, 1}, + {"stories without an id count individually", []article.Article{ + {ID: 1, StoryID: 0, SiteID: 1, IsOnline: true}, + {ID: 2, StoryID: 0, SiteID: 1, IsOnline: true}, + {ID: 3, StoryID: 10, SiteID: 1, IsOnline: true}, + }, 3}, + {"offline versions are not visible to a reader", []article.Article{ + {ID: 1, StoryID: 10, SiteID: 1, IsOnline: false}, + }, 0}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + repo := newMockArticleRepo() + for _, a := range c.articles { + repo.articles[a.ID] = a + } + uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites(), newFakeLocalizationStore(repo)) + page, err := uc.ListStoriesPage(useCases.ListStoriesCommand{Page: 1, Size: 10}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if page.Counts.Suggestions != c.want { + t.Fatalf("suggestions = %d, want %d", page.Counts.Suggestions, c.want) + } + }) + } +} + +func TestListArticlesPageSEOFilterAndSuggestionCount(t *testing.T) { + repo := newMockArticleRepo() + for id := uint32(1); id <= 3; id++ { + repo.articles[id] = article.Article{ID: id, StoryID: id, SiteID: 1, IsOnline: true} + } + uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites(), plainLocalization()) + page, err := uc.ListArticlesPage(useCases.ListArticlesCommand{Page: 2, Size: 2, SEO: article.SEOStatusSuggestions}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if page.Total != 3 || len(page.Items) != 1 || page.Counts.Suggestions != 3 { + t.Fatalf("total=%d items=%d suggestions=%d, want 3/1/3", page.Total, len(page.Items), page.Counts.Suggestions) + } + beyond, _ := uc.ListArticlesPage(useCases.ListArticlesCommand{Page: 3, Size: 2, SEO: article.SEOStatusSuggestions}) + if len(beyond.Items) != 0 || beyond.Total != 3 { + t.Fatalf("beyond: items=%d total=%d, want 0/3", len(beyond.Items), beyond.Total) + } + none, _ := uc.ListArticlesPage(useCases.ListArticlesCommand{Page: 1, Size: 2, SEO: article.SEOStatusReady}) + if none.Total != 0 || len(none.Items) != 0 { + t.Fatalf("ready: total=%d items=%d, want 0/0", none.Total, len(none.Items)) + } +} diff --git a/test/article_publishing_http_test.go b/test/article_publishing_http_test.go index 5e9f340..720f424 100644 --- a/test/article_publishing_http_test.go +++ b/test/article_publishing_http_test.go @@ -25,7 +25,7 @@ func newArticlePublishingRouter(articleRepo *mockArticleRepo, token *authtoken.A articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/article_test.go b/test/article_test.go index 57cd69e..3b4c2ed 100644 --- a/test/article_test.go +++ b/test/article_test.go @@ -37,6 +37,7 @@ type mockArticleRepo struct { beforeMove func() onGet func(id uint32) error reverseStory bool + listCalls int } func newMockArticleRepo() *mockArticleRepo { @@ -104,6 +105,7 @@ func (m *mockArticleRepo) FindByFilters(filters article.Filters) ([]article.Arti } func (m *mockArticleRepo) FindPage(filters article.Filters) (article.PageResult, error) { + m.listCalls++ if err := m.fail.trigger("FindPage"); err != nil { return article.PageResult{}, err } @@ -124,6 +126,12 @@ func (m *mockArticleRepo) FindPage(filters article.Filters) (article.PageResult, if filters.Status == article.StatusDraft && a.IsOnline { continue } + if filters.Lang != nil && a.Lang != *filters.Lang { + continue + } + if filters.AuthorID != nil && !slices.Contains(m.credits[a.ID], *filters.AuthorID) { + continue + } if filters.Q != "" && !strings.Contains(foldArticleSearch(a.Title), foldArticleSearch(filters.Q)) { continue } @@ -158,6 +166,41 @@ func (m *mockArticleRepo) CountByVisibility(onlineOnly bool) (article.Counts, er return counts, nil } +func (m *mockArticleRepo) CountVersions(filters article.Filters) (article.Counts, error) { + m.listCalls++ + if err := m.fail.trigger("CountVersions"); err != nil { + return article.Counts{}, err + } + counts := article.Counts{} + for _, a := range m.articles { + if filters.Lang != nil && a.Lang != *filters.Lang { + continue + } + if a.IsOnline { + counts.Online++ + } else if filters.IsOnline == nil && (filters.VisibleTo == nil || a.CreatedBy == *filters.VisibleTo) { + counts.Draft++ + } + } + counts.All = counts.Online + counts.Draft + return counts, nil +} + +func (m *mockArticleRepo) FindStoryPage(filters article.StoryFilters) (article.StoryPage, error) { + m.listCalls++ + return article.StoryPage{Refs: []article.StoryRef{}}, m.fail.trigger("FindStoryPage") +} + +func (m *mockArticleRepo) CountStories(filters article.StoryFilters) (article.StoryCounts, error) { + m.listCalls++ + return article.StoryCounts{}, m.fail.trigger("CountStories") +} + +func (m *mockArticleRepo) ListByStories(storyIDs []uint32) ([]article.Article, error) { + m.listCalls++ + return []article.Article{}, m.fail.trigger("ListByStories") +} + func (m *mockArticleRepo) CountVisibleTo(userID uint32) (article.Counts, error) { if err := m.fail.trigger("CountVisibleTo"); err != nil { return article.Counts{}, err diff --git a/test/article_title_rules_test.go b/test/article_title_rules_test.go index ad348ac..4c00e41 100644 --- a/test/article_title_rules_test.go +++ b/test/article_title_rules_test.go @@ -28,7 +28,7 @@ func newArticleTitleRulesRouter(repo *mockArticleRepo, token *authtoken.Auth) *c articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(repo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(repo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(repo, discardRecorder), useCases.NewUpdateArticleUseCase(repo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(repo), plainStoryMover(repo)), plainTranslation(repo), diff --git a/test/articles_list_http_test.go b/test/articles_list_http_test.go index 0a5ca17..f15357a 100644 --- a/test/articles_list_http_test.go +++ b/test/articles_list_http_test.go @@ -22,7 +22,7 @@ func newArticlesListRouter(articleRepo *mockArticleRepo, token *authtoken.Auth) articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/articles_pagination_api_http_test.go b/test/articles_pagination_api_http_test.go index 72e71ee..8fdc9fc 100644 --- a/test/articles_pagination_api_http_test.go +++ b/test/articles_pagination_api_http_test.go @@ -578,7 +578,7 @@ func TestArticlesPaginationUseCase(t *testing.T) { t.Run("The typed sort reaches the repository and an absent sort keeps updated_at descending", func(t *testing.T) { repo := newMockArticleRepo() seedSortArticles(repo) - uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites()) + uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites(), plainLocalization()) sorted, err := uc.ListArticlesPage(useCases.ListArticlesCommand{Format: domainArticle.BodyFormatHTML, Caller: ownerCaller(1), Page: 1, Size: 25, Sort: domainArticle.Sort{Field: domainArticle.SortByTitle, Direction: domainArticle.SortAsc}}) if err != nil || sorted.Total != 3 || sorted.Counts.All != 3 { t.Fatalf("unexpected %v %+v", err, sorted) @@ -594,7 +594,7 @@ func TestArticlesPaginationUseCase(t *testing.T) { repo := newMockArticleRepo() seedPaginationArticles(repo, 3, 0) repo.fail.failNextWith(method, sentinel) - uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites()) + uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites(), plainLocalization()) _, err := uc.ListArticlesPage(useCases.ListArticlesCommand{Format: domainArticle.BodyFormatHTML, Caller: ownerCaller(1), Page: 1, Size: 25}) if !errors.Is(err, sentinel) { t.Fatalf("%s: expected sentinel, got %v", method, err) diff --git a/test/edit_article_body_http_test.go b/test/edit_article_body_http_test.go index 16284d7..5723602 100644 --- a/test/edit_article_body_http_test.go +++ b/test/edit_article_body_http_test.go @@ -26,7 +26,7 @@ func newEditArticleBodyRouter(articleRepo *mockArticleRepo, token *authtoken.Aut articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/media_env_test.go b/test/media_env_test.go index 912f31c..92b8f85 100644 --- a/test/media_env_test.go +++ b/test/media_env_test.go @@ -69,7 +69,7 @@ func newMediaEnv(t *testing.T) *mediaEnv { Token: env.token, CreateArticleUseCase: useCases.NewCreateArticleUseCase(passthroughCreateTransactor(env.articles), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), GetArticleUseCase: useCases.NewGetArticleUseCase(env.articles, &fakeBodyRenderer{}), - ListArticlesUseCase: useCases.NewListArticlesUseCase(env.articles, &fakeBodyRenderer{}, plainSites()), + ListArticlesUseCase: useCases.NewListArticlesUseCase(env.articles, &fakeBodyRenderer{}, plainSites(), plainLocalization()), UpdateArticleUseCase: useCases.NewUpdateArticleUseCase(env.articles, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), env.images, newFakeLocalizationStore(env.articles), plainStoryMover(env.articles)), CreateTranslationUseCase: plainTranslation(env.articles), DescribeArticleUseCase: plainDescribe(env.articles), diff --git a/test/not_found_errors_test.go b/test/not_found_errors_test.go index 640a616..41300ee 100644 --- a/test/not_found_errors_test.go +++ b/test/not_found_errors_test.go @@ -128,7 +128,7 @@ func newNotFoundErrorsRouter(articleRepo *mockArticleRepo, userRepo *mockUserRep articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/public_article_listing_test.go b/test/public_article_listing_test.go index 5b120d2..c5768e3 100644 --- a/test/public_article_listing_test.go +++ b/test/public_article_listing_test.go @@ -44,7 +44,7 @@ func newPublicArticleListingRouter(articleRepo *mockArticleRepo, token *authtoke articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/public_article_read_test.go b/test/public_article_read_test.go index b3e1df0..8ae5448 100644 --- a/test/public_article_read_test.go +++ b/test/public_article_read_test.go @@ -26,7 +26,7 @@ func newPublicArticleReadRouter(articleRepo *mockArticleRepo, userRepo *mockUser articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/roles_content_scope_usecases_test.go b/test/roles_content_scope_usecases_test.go index ddd7762..e92f274 100644 --- a/test/roles_content_scope_usecases_test.go +++ b/test/roles_content_scope_usecases_test.go @@ -191,7 +191,7 @@ func TestContentScopeArticleUseCases(t *testing.T) { t.Run("ListArticlesPage returns the visible set with matching counts", func(t *testing.T) { repo, sites, _, _ := newRepos() - list := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, sites) + list := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, sites, plainLocalization()) cases := []struct { name string caller useCases.ContentCaller @@ -530,7 +530,7 @@ func TestContentScopeListArticlesSiteLookupFailure(t *testing.T) { t.Run(name, func(t *testing.T) { sites := plainSites() sites.fail.failNextWith("GetByKey", boom) - list := useCases.NewListArticlesUseCase(newMockArticleRepo(), &fakeBodyRenderer{}, sites) + list := useCases.NewListArticlesUseCase(newMockArticleRepo(), &fakeBodyRenderer{}, sites, plainLocalization()) _, err := list.ListArticlesPage(useCases.ListArticlesCommand{Page: 1, Size: 10, SiteKey: &key, Caller: useCases.ContentCaller{UserID: 9, Grants: grants}}) if !errors.Is(err, boom) { t.Fatalf("expected the site lookup error, got %v", err) diff --git a/test/runtime_settings_injection_http_test.go b/test/runtime_settings_injection_http_test.go index cb3f22f..4f91298 100644 --- a/test/runtime_settings_injection_http_test.go +++ b/test/runtime_settings_injection_http_test.go @@ -132,7 +132,7 @@ func newRuntimeSettingsContainer(t *testing.T) *api.UseCases { Token: token, CreateArticleUseCase: useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), GetArticleUseCase: useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - ListArticlesUseCase: useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + ListArticlesUseCase: useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), UpdateArticleUseCase: useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), CreateTranslationUseCase: plainTranslation(articleRepo), DescribeArticleUseCase: plainDescribe(articleRepo), diff --git a/test/seo_data_support_test.go b/test/seo_data_support_test.go index 13c3e2d..bc43f07 100644 --- a/test/seo_data_support_test.go +++ b/test/seo_data_support_test.go @@ -59,7 +59,7 @@ func newSeoEnv(t *testing.T) *seoEnv { c.UpdateArticleUseCase = useCases.NewUpdateArticleUseCase(p.articles, renderer, recorder, sites, authors, p.images, newFakeLocalizationStore(p.articles), plainStoryMover(p.articles)) c.CreateTranslationUseCase = plainTranslation(p.articles) c.DescribeArticleUseCase = plainDescribe(p.articles) - c.ListArticlesUseCase = useCases.NewListArticlesUseCase(p.articles, renderer, sites) + c.ListArticlesUseCase = useCases.NewListArticlesUseCase(p.articles, renderer, sites, plainLocalization()) c.ListMediaUseCase = useCases.NewListMediaUseCase(p.images, p.articles, p.users, newMockVariantFileRepo(), sites, authors, newMockContentTypeRepo(), newMockEntryRepo()) c.DeleteMediaUseCase = useCases.NewDeleteMediaUseCase(p.images, newMockImageStorage(), newMockVariantFileRepo(), recorder, sites, authors, p.articles) c.GetMediaUsageUseCase = useCases.NewGetMediaUsageUseCase(p.images, p.articles, sites, authors, newMockContentTypeRepo(), newMockEntryRepo()) diff --git a/test/seo_filter_performance_test.go b/test/seo_filter_performance_test.go index a399205..7fa4129 100644 --- a/test/seo_filter_performance_test.go +++ b/test/seo_filter_performance_test.go @@ -38,7 +38,7 @@ func TestSeoFilterPerformance(t *testing.T) { if err := db.CreateInBatches(&rows, 100).Error; err != nil { t.Fatalf("seed: %v", err) } - list := useCases.NewListArticlesUseCase(persistence.NewArticleRepository(db), &fakeBodyRenderer{}, persistence.NewSiteRepository(db)) + list := useCases.NewListArticlesUseCase(persistence.NewArticleRepository(db), &fakeBodyRenderer{}, persistence.NewSiteRepository(db), plainLocalization()) filtered := useCases.ListArticlesCommand{Caller: ownerCaller(1), Page: 1, Size: 25, SEO: article.SEOStatusSuggestions, Sort: article.DefaultSort()} unfiltered := useCases.ListArticlesCommand{Caller: ownerCaller(1), Page: 1, Size: 25, Sort: article.DefaultSort()} diff --git a/test/seo_output_support_test.go b/test/seo_output_support_test.go index fe685da..c18b04f 100644 --- a/test/seo_output_support_test.go +++ b/test/seo_output_support_test.go @@ -132,7 +132,7 @@ func newSeoOutEnv(t *testing.T) *seoOutEnv { c.ResolveArticleSEOUseCase = useCases.NewResolveArticleSEOUseCase(counted, e.sites, e.images) c.CheckArticleSEOUseCase = useCases.NewCheckArticleSEOUseCase(counted, e.sites, e.images) c.ListSitemapUseCase = useCases.NewListSitemapUseCase(counted, e.sites) - c.ListArticlesUseCase = useCases.NewListArticlesUseCase(counted, &fakeBodyRenderer{}, e.sites) + c.ListArticlesUseCase = useCases.NewListArticlesUseCase(counted, &fakeBodyRenderer{}, e.sites, plainLocalization()) router := chi.NewRouter() router.Mount("/v1", api.InitBackendRoutes(c, e.settings)) e.router = router diff --git a/test/site_support_test.go b/test/site_support_test.go index 4905ede..cffc5ca 100644 --- a/test/site_support_test.go +++ b/test/site_support_test.go @@ -330,7 +330,7 @@ func newSiteEnv(t *testing.T) *siteEnv { c.UpdateArticleUseCase = useCases.NewUpdateArticleUseCase(p.articles, renderer, recorder, sites, plainAuthors(), p.images, newFakeLocalizationStore(p.articles), plainStoryMover(p.articles)) c.CreateTranslationUseCase = plainTranslation(p.articles) c.DescribeArticleUseCase = plainDescribe(p.articles) - c.ListArticlesUseCase = useCases.NewListArticlesUseCase(p.articles, renderer, sites) + c.ListArticlesUseCase = useCases.NewListArticlesUseCase(p.articles, renderer, sites, plainLocalization()) c.ListMediaUseCase = useCases.NewListMediaUseCase(p.images, p.articles, p.users, newMockVariantFileRepo(), sites, plainAuthors(), newMockContentTypeRepo(), newMockEntryRepo()) c.DeleteMediaUseCase = useCases.NewDeleteMediaUseCase(p.images, newMockImageStorage(), newMockVariantFileRepo(), recorder, sites, plainAuthors(), p.articles) c.MergeMediaUseCase = mergeMedia @@ -418,6 +418,10 @@ func stringPointer(value string) *string { return &value } func idPointer(value uint32) *uint32 { return &value } +func plainLocalization() *fakeLocalizationStore { + return newFakeLocalizationStore(newMockArticleRepo()) +} + func plainSites() *mockSiteRepo { sites := newMockSiteRepo(newMockArticleRepo()) sites.seed(1, "Default", "default", true) diff --git a/test/stale_token_invalidation_http_test.go b/test/stale_token_invalidation_http_test.go index 3e46e42..a767371 100644 --- a/test/stale_token_invalidation_http_test.go +++ b/test/stale_token_invalidation_http_test.go @@ -25,7 +25,7 @@ func newStaleTokenInvalidationRouter(userRepo *mockUserRepo, articleRepo *mockAr articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/story_support_test.go b/test/story_support_test.go index f3bdb0d..1424635 100644 --- a/test/story_support_test.go +++ b/test/story_support_test.go @@ -192,7 +192,7 @@ func (w *storyWorld) wire() { AuthenticateAPIKeyUseCase: useCases.NewAuthenticateAPIKeyUseCase(keys, nil, w.users, time.Now), CreateArticleUseCase: useCases.NewCreateArticleUseCase(w.transactor, renderer, w.recorder, w.sites, w.authors), GetArticleUseCase: useCases.NewGetArticleUseCase(w.articles, renderer), - ListArticlesUseCase: useCases.NewListArticlesUseCase(w.articles, renderer, w.sites), + ListArticlesUseCase: useCases.NewListArticlesUseCase(w.articles, renderer, w.sites, plainLocalization()), DeleteArticleUseCase: useCases.NewDeleteArticleUseCase(w.articles, w.recorder), UpdateArticleUseCase: useCases.NewUpdateArticleUseCase(w.articles, renderer, w.recorder, w.sites, w.authors, w.images, w.rows, moveStory), CreateTranslationUseCase: useCases.NewCreateTranslationUseCase(w.transactor, w.articles, w.rows, renderer, w.recorder), diff --git a/test/use_case_baseline_test.go b/test/use_case_baseline_test.go index 933c5e0..17f2a6b 100644 --- a/test/use_case_baseline_test.go +++ b/test/use_case_baseline_test.go @@ -199,7 +199,7 @@ func TestListArticlesFiltersByIsOnline(t *testing.T) { t.Run("ListArticlesPage unverified", func(t *testing.T) { repo := newRepo() - uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites()) + uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites(), plainLocalization()) result, err := uc.ListArticlesPage(useCases.ListArticlesCommand{Format: article.BodyFormatHTML, Page: 1, Size: 25, Sort: article.Sort{Field: article.SortByTitle, Direction: article.SortAsc}}) if err != nil { t.Fatalf("expected no error, got %v", err) @@ -209,7 +209,7 @@ func TestListArticlesFiltersByIsOnline(t *testing.T) { t.Run("ListArticlesPage verified", func(t *testing.T) { repo := newRepo() - uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites()) + uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites(), plainLocalization()) result, err := uc.ListArticlesPage(useCases.ListArticlesCommand{Format: article.BodyFormatHTML, Caller: ownerCaller(1), Page: 1, Size: 25, Sort: article.Sort{Field: article.SortByTitle, Direction: article.SortAsc}}) if err != nil { t.Fatalf("expected no error, got %v", err) @@ -220,7 +220,7 @@ func TestListArticlesFiltersByIsOnline(t *testing.T) { func TestListArticlesPageEmptyRepositoryReturnsEmptySlice(t *testing.T) { repo := newMockArticleRepo() - uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites()) + uc := useCases.NewListArticlesUseCase(repo, &fakeBodyRenderer{}, plainSites(), plainLocalization()) page, err := uc.ListArticlesPage(useCases.ListArticlesCommand{Format: article.BodyFormatHTML, Caller: ownerCaller(1), Page: 1, Size: 25}) if err != nil { diff --git a/test/verified_account_required_http_test.go b/test/verified_account_required_http_test.go index de0ca0c..f41b916 100644 --- a/test/verified_account_required_http_test.go +++ b/test/verified_account_required_http_test.go @@ -23,7 +23,7 @@ func newVerifiedAccountRequiredRouter(articleRepo *mockArticleRepo, userRepo *mo articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/test/write_not_found_errors_test.go b/test/write_not_found_errors_test.go index 620ea5d..5f63846 100644 --- a/test/write_not_found_errors_test.go +++ b/test/write_not_found_errors_test.go @@ -215,7 +215,7 @@ func newDeleteArticleRouter(articleRepo *mockArticleRepo, token *authtoken.Auth) articleHandler := article.NewHandler( useCases.NewCreateArticleUseCase(passthroughCreateTransactor(articleRepo), &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors()), useCases.NewGetArticleUseCase(articleRepo, &fakeBodyRenderer{}), - useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites()), + useCases.NewListArticlesUseCase(articleRepo, &fakeBodyRenderer{}, plainSites(), plainLocalization()), useCases.NewDeleteArticleUseCase(articleRepo, discardRecorder), useCases.NewUpdateArticleUseCase(articleRepo, &fakeBodyRenderer{}, discardRecorder, plainSites(), plainAuthors(), newMockImageRepo(), newFakeLocalizationStore(articleRepo), plainStoryMover(articleRepo)), plainTranslation(articleRepo), diff --git a/web/src/components/ArticleCardList.vue b/web/src/components/ArticleCardList.vue deleted file mode 100644 index b2da9ac..0000000 --- a/web/src/components/ArticleCardList.vue +++ /dev/null @@ -1,149 +0,0 @@ - - - diff --git a/web/src/components/ArticleFilterBar.vue b/web/src/components/ArticleFilterBar.vue new file mode 100644 index 0000000..764213f --- /dev/null +++ b/web/src/components/ArticleFilterBar.vue @@ -0,0 +1,76 @@ + + + diff --git a/web/src/components/ArticleFilterChips.vue b/web/src/components/ArticleFilterChips.vue new file mode 100644 index 0000000..28266a1 --- /dev/null +++ b/web/src/components/ArticleFilterChips.vue @@ -0,0 +1,49 @@ + + + diff --git a/web/src/components/ArticleFilterFields.vue b/web/src/components/ArticleFilterFields.vue new file mode 100644 index 0000000..c4014db --- /dev/null +++ b/web/src/components/ArticleFilterFields.vue @@ -0,0 +1,160 @@ + + + diff --git a/web/src/components/ArticleFilterSelect.vue b/web/src/components/ArticleFilterSelect.vue new file mode 100644 index 0000000..16770f1 --- /dev/null +++ b/web/src/components/ArticleFilterSelect.vue @@ -0,0 +1,43 @@ + + + diff --git a/web/src/components/ArticleFiltersSheet.vue b/web/src/components/ArticleFiltersSheet.vue new file mode 100644 index 0000000..9d89b23 --- /dev/null +++ b/web/src/components/ArticleFiltersSheet.vue @@ -0,0 +1,81 @@ + + + diff --git a/web/src/components/ArticleListEmpty.vue b/web/src/components/ArticleListEmpty.vue new file mode 100644 index 0000000..087d960 --- /dev/null +++ b/web/src/components/ArticleListEmpty.vue @@ -0,0 +1,73 @@ + + + diff --git a/web/src/components/ArticleListFailure.vue b/web/src/components/ArticleListFailure.vue new file mode 100644 index 0000000..b4e157e --- /dev/null +++ b/web/src/components/ArticleListFailure.vue @@ -0,0 +1,33 @@ + + + diff --git a/web/src/components/ArticleListSkeleton.vue b/web/src/components/ArticleListSkeleton.vue new file mode 100644 index 0000000..c3831f7 --- /dev/null +++ b/web/src/components/ArticleListSkeleton.vue @@ -0,0 +1,17 @@ + + + diff --git a/web/src/components/ArticleListTabs.vue b/web/src/components/ArticleListTabs.vue new file mode 100644 index 0000000..1d9f10e --- /dev/null +++ b/web/src/components/ArticleListTabs.vue @@ -0,0 +1,99 @@ + + + diff --git a/web/src/components/ArticleListToolbar.vue b/web/src/components/ArticleListToolbar.vue deleted file mode 100644 index 82a52b5..0000000 --- a/web/src/components/ArticleListToolbar.vue +++ /dev/null @@ -1,98 +0,0 @@ - - - diff --git a/web/src/components/ArticleSiteFilter.vue b/web/src/components/ArticleSiteFilter.vue deleted file mode 100644 index e9348a5..0000000 --- a/web/src/components/ArticleSiteFilter.vue +++ /dev/null @@ -1,46 +0,0 @@ - - - diff --git a/web/src/components/ArticleTable.vue b/web/src/components/ArticleTable.vue deleted file mode 100644 index 1be3bd7..0000000 --- a/web/src/components/ArticleTable.vue +++ /dev/null @@ -1,381 +0,0 @@ - - - diff --git a/web/src/components/NeedsAttentionToggle.vue b/web/src/components/NeedsAttentionToggle.vue deleted file mode 100644 index c09bf5b..0000000 --- a/web/src/components/NeedsAttentionToggle.vue +++ /dev/null @@ -1,31 +0,0 @@ - - - diff --git a/web/src/components/SeoStatusChip.vue b/web/src/components/SeoStatusChip.vue deleted file mode 100644 index 760a652..0000000 --- a/web/src/components/SeoStatusChip.vue +++ /dev/null @@ -1,52 +0,0 @@ - - - diff --git a/web/src/components/StoryCardList.vue b/web/src/components/StoryCardList.vue new file mode 100644 index 0000000..c902e5e --- /dev/null +++ b/web/src/components/StoryCardList.vue @@ -0,0 +1,120 @@ + + + diff --git a/web/src/components/StoryCoverage.vue b/web/src/components/StoryCoverage.vue new file mode 100644 index 0000000..9916849 --- /dev/null +++ b/web/src/components/StoryCoverage.vue @@ -0,0 +1,134 @@ + + + diff --git a/web/src/components/StoryRowMenu.vue b/web/src/components/StoryRowMenu.vue new file mode 100644 index 0000000..ea4a1ae --- /dev/null +++ b/web/src/components/StoryRowMenu.vue @@ -0,0 +1,138 @@ + + + diff --git a/web/src/components/StoryTable.vue b/web/src/components/StoryTable.vue new file mode 100644 index 0000000..45693f0 --- /dev/null +++ b/web/src/components/StoryTable.vue @@ -0,0 +1,227 @@ + + + diff --git a/web/src/composables/__tests__/useArticles.spec.ts b/web/src/composables/__tests__/useArticles.spec.ts index 08fe1ff..4d808e3 100644 --- a/web/src/composables/__tests__/useArticles.spec.ts +++ b/web/src/composables/__tests__/useArticles.spec.ts @@ -69,4 +69,24 @@ describe('useArticles', () => { expect(result).toEqual(envelope) }) + + it('asks for the grouped list and sends lang, tab and author only when set', async () => { + await useArticles().listStories({ page: 1, size: 5 }) + await useArticles().listStories({ + page: 2, + size: 5, + lang: 'fr', + tab: 'missing', + author: 7, + }) + + expect(request).toHaveBeenNthCalledWith( + 1, + '/v1/article/?format=html&page=1&size=5&group=story', + ) + expect(request).toHaveBeenNthCalledWith( + 2, + '/v1/article/?format=html&page=2&size=5&group=story&lang=fr&tab=missing&author=7', + ) + }) }) diff --git a/web/src/composables/useArticleFilterOptions.ts b/web/src/composables/useArticleFilterOptions.ts new file mode 100644 index 0000000..280fdcc --- /dev/null +++ b/web/src/composables/useArticleFilterOptions.ts @@ -0,0 +1,47 @@ +import { ref, watch } from 'vue' +import { useAuthors, type Author } from '@/composables/useAuthors' +import { useSiteLanguages } from '@/composables/useSiteLanguages' +import type { SiteLanguage } from '@/lib/siteLanguages' + +export function useArticleFilterOptions(siteKey: () => string) { + const { getLanguages } = useSiteLanguages() + const { listAllAuthors } = useAuthors() + const languages = ref([]) + const languagesKnown = ref(false) + const authors = ref([]) + let token = 0 + + async function loadLanguages(key: string) { + const current = ++token + languagesKnown.value = false + if (!key) { + languages.value = [] + return + } + try { + const response = await getLanguages(key) + if (current !== token) { + return + } + languages.value = response.languages + languagesKnown.value = true + } catch { + if (current === token) { + languages.value = [] + } + } + } + + async function loadAuthors() { + try { + authors.value = await listAllAuthors() + } catch { + authors.value = [] + } + } + + watch(siteKey, loadLanguages, { immediate: true }) + loadAuthors() + + return { languages, languagesKnown, authors } +} diff --git a/web/src/composables/useArticleList.ts b/web/src/composables/useArticleList.ts index 6d8c670..3049a3c 100644 --- a/web/src/composables/useArticleList.ts +++ b/web/src/composables/useArticleList.ts @@ -1,11 +1,6 @@ import { computed, ref } from 'vue' import { useRoute } from 'vue-router' -import { - useArticles, - type Article, - type ArticleCounts, - type ArticleStatus, -} from '@/composables/useArticles' +import { useArticles, type StoryCounts, type StoryItem } from '@/composables/useArticles' import { usePagedQuery, useSearchInput, @@ -21,7 +16,9 @@ import { toRouteQuery, type ArticleListQuery, type ArticleSortField, + type ArticleStatus, } from '@/lib/articleListQuery' +import { activeTabKey, queryForTab, type ListTabKey } from '@/lib/storyList' import { PAGE_SIZES } from '@/lib/pageQuery' import { ARTICLES_PAGE_SIZE_KEY, readStoredPageSize, storePageSize } from '@/lib/pageSizeMemory' @@ -37,7 +34,7 @@ interface ArticleListOptions { export function useArticleList({ sitesSettled, usableSite }: ArticleListOptions) { const route = useRoute() - const { listArticles } = useArticles() + const { listStories } = useArticles() function parseWithStoredSize(query: typeof route.query): ArticleListQuery { const parsed = parseArticleQuery(query) @@ -48,13 +45,13 @@ export function useArticleList({ sitesSettled, usableSite }: ArticleListOptions) return stored === null ? parsed : { ...parsed, size: stored } } - const items = ref([]) - const counts = ref({ all: 0, online: 0, draft: 0 }) + const items = ref([]) + const counts = ref(null) const loadFailed = ref(false) async function request(query: ArticleListQuery, context: { loaded: boolean }) { try { - const response = await listArticles(query) + const response = await listStories(query) return { kind: 'loaded', total: response.total, @@ -97,7 +94,7 @@ export function useArticleList({ sitesSettled, usableSite }: ArticleListOptions) (q) => load({ ...desired.value, q, page: 1 }, 'replace'), ) - const needsAttentionCount = computed(() => counts.value.suggestions ?? null) + const needsAttentionCount = computed(() => counts.value?.suggestions ?? null) const settled = computed(() => hasLoaded.value && !loadFailed.value) const showPaginator = computed(() => settled.value && total.value > 0) @@ -110,14 +107,57 @@ export function useArticleList({ sitesSettled, usableSite }: ArticleListOptions) if (status === desired.value.status) { return } - load({ ...desired.value, status, page: 1 }, 'push') + load({ ...desired.value, status, tab: '', page: 1 }, 'push') } function changeSite(site: string) { if (site === desired.value.site) { return } - load({ ...desired.value, site, page: 1 }, 'push') + load({ ...desired.value, site, lang: '', page: 1 }, 'push') + } + + function changeLang(lang: string) { + if (lang === desired.value.lang) { + return + } + load({ ...desired.value, lang, page: 1 }, 'push') + } + + function changeTab(key: ListTabKey) { + if (key === activeTabKey(desired.value)) { + return + } + load(queryForTab(desired.value, key), 'push') + } + + function changeAuthor(author: number) { + if (author === desired.value.author) { + return + } + load({ ...desired.value, author, page: 1 }, 'push') + } + + function clearFilters() { + load( + { + ...desired.value, + site: '', + lang: '', + tab: '', + status: '', + author: 0, + seo: '', + page: 1, + }, + 'push', + ) + } + + function dropUnknownLanguage(known: string[]) { + if (desired.value.lang && !known.includes(desired.value.lang)) { + load({ ...desired.value, lang: '', page: 1 }, 'replace') + } } function changeSeo(on: boolean) { @@ -163,6 +203,11 @@ export function useArticleList({ sitesSettled, usableSite }: ArticleListOptions) changeStatus, changeSite, changeSeo, + changeLang, + changeTab, + changeAuthor, + clearFilters, + dropUnknownLanguage, changeSort, changeSearch: search.changeSearch, clearSearch: search.clearSearch, diff --git a/web/src/composables/useArticleMenu.ts b/web/src/composables/useArticleMenu.ts deleted file mode 100644 index 769af84..0000000 --- a/web/src/composables/useArticleMenu.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { t } from '@/i18n' -import { useContentAccess } from '@/composables/useContentAccess' -import type { Article } from '@/composables/useArticles' -import { ARTICLE_TYPE_KEY } from '@/lib/routeAccess' - -interface ArticleMenuHandlers { - isBusy: (article: Article) => boolean - edit: (article: Article) => void - publish: (id: number) => void - unpublish: (id: number) => void - copyUrl: (id: number) => void - remove: (article: Article) => void -} - -export function useArticleMenu(handlers: ArticleMenuHandlers) { - const access = useContentAccess(ARTICLE_TYPE_KEY, 'articles') - - function publishHint(article: Article) { - return !article.is_online && !access.canPublish(article.created_by) - ? t('Ask an editor to publish.') - : '' - } - - function actionsFor(article: Article) { - const row = access.rowAccess(article.created_by) - const toggle = access.canPublish(article.created_by) - ? [ - { - get label() { - return article.is_online ? t('Unpublish') : t('Publish') - }, - icon: article.is_online ? 'i-lucide-globe-lock' : 'i-lucide-globe', - disabled: handlers.isBusy(article), - onSelect: () => { - if (article.is_online) { - handlers.unpublish(article.id) - } else { - handlers.publish(article.id) - } - }, - }, - ] - : [] - return [ - [ - { - get label() { - return t('Edit') - }, - icon: 'i-lucide-pencil', - disabled: !row.canEdit, - description: row.canEdit ? undefined : row.editReason, - onSelect: () => handlers.edit(article), - }, - ...toggle, - { - get label() { - return t('Copy API URL') - }, - icon: 'i-lucide-link', - onSelect: () => handlers.copyUrl(article.id), - }, - ], - [ - { - get label() { - return t('Delete…') - }, - icon: 'i-lucide-trash-2', - color: 'error' as const, - disabled: !row.canDelete, - description: row.canDelete ? undefined : row.deleteReason, - onSelect: () => handlers.remove(article), - }, - ], - ] - } - - return { actionsFor, publishHint, access } -} diff --git a/web/src/composables/useArticleRowActions.ts b/web/src/composables/useArticleRowActions.ts index 7e77732..522d860 100644 --- a/web/src/composables/useArticleRowActions.ts +++ b/web/src/composables/useArticleRowActions.ts @@ -2,25 +2,16 @@ import { nextTick, ref, type Ref } from 'vue' import { t } from '@/i18n' import { useLocalizedToast } from '@/i18n/presentation' import { useArticleLifecycle } from '@/composables/useArticleLifecycle' -import type { Article, ArticleCounts } from '@/composables/useArticles' +import type { Article, StoryItem } from '@/composables/useArticles' import type { ArticleListQuery } from '@/lib/articleListQuery' -import { pageCountFor } from '@/lib/pagination' interface ArticleRowActionsState { - items: Ref - total: Ref - counts: Ref + items: Ref shown: Ref reload: (query: ArticleListQuery, mode: 'push' | 'replace') => void } -export function useArticleRowActions({ - items, - total, - counts, - shown, - reload, -}: ArticleRowActionsState) { +export function useArticleRowActions({ items, shown, reload }: ArticleRowActionsState) { const lifecycle = useArticleLifecycle() const toast = useLocalizedToast() const busyIds = ref>(new Set()) @@ -35,48 +26,12 @@ export function useArticleRowActions({ busyIds.value = next } - function shiftCounts(article: Article, delta: number) { - const key = article.is_online ? 'online' : 'draft' - counts.value = { - ...counts.value, - all: Math.max(0, counts.value.all + delta), - [key]: Math.max(0, counts.value[key] + delta), - } - } - - function detachRow(id: number) { - if (!items.value.some((article) => article.id === id)) { - return - } - items.value = items.value.filter((article) => article.id !== id) - total.value = Math.max(0, total.value - 1) - if (items.value.length === 0 && total.value > 0) { - const page = Math.min(shown.value.page, pageCountFor(total.value, shown.value.size)) - reload({ ...shown.value, page }, 'replace') - } - } - - function removeArticle(article: Article) { - shiftCounts(article, -1) - detachRow(article.id) - } - - function replaceArticle(updated: Article, previous: Article) { - items.value = items.value.map((existing) => - existing.id === updated.id ? updated : existing, + function refresh(removed = false) { + const emptied = removed && items.value.length === 1 && shown.value.page > 1 + reload( + { ...shown.value, page: emptied ? shown.value.page - 1 : shown.value.page }, + 'replace', ) - if (updated.is_online === previous.is_online) { - return - } - counts.value = { - ...counts.value, - online: counts.value.online + (updated.is_online ? 1 : -1), - draft: counts.value.draft + (updated.is_online ? -1 : 1), - } - const filter = shown.value.status - if (filter && (filter === 'online') !== updated.is_online) { - detachRow(updated.id) - } } function notFoundToast(article: Article) { @@ -96,34 +51,21 @@ export function useArticleRowActions({ function setOnline(article: Article, makeOnline: boolean) { return lifecycle.changeOnline(article, makeOnline, { pending: (busy) => setBusy(article.id, busy), - onUpdated: (updated) => replaceArticle(updated, article), + onUpdated: () => refresh(), onGone: () => goneFromList(article), }) } - function findItem(id: number) { - return items.value.find((existing) => existing.id === id) + function handlePublish(article: Article) { + return setOnline(article, true) } - function handlePublish(id: number) { - const article = findItem(id) - if (article) { - setOnline(article, true) - } + function handleUnpublish(article: Article) { + return setOnline(article, false) } - function handleUnpublish(id: number) { - const article = findItem(id) - if (article) { - setOnline(article, false) - } - } - - async function handleCopyUrl(id: number) { - const article = findItem(id) - if (!article) { - return - } + async function handleCopyUrl(article: Article) { + const id = article.id const url = `${window.location.origin}/v1/article/${id}` try { await navigator.clipboard.writeText(url) @@ -153,8 +95,8 @@ export function useArticleRowActions({ } function goneFromList(article: Article) { - removeArticle(article) toast.add(() => notFoundToast(article)) + refresh(true) } async function focusRowOrHeading(idsAtOpen: number[], deletedId: number) { @@ -171,7 +113,7 @@ export function useArticleRowActions({ async function handleDelete(article: Article) { const idsAtOpen = items.value.map((existing) => existing.id) const confirmed = await lifecycle.confirmAndDelete(article, { - onDeleted: () => removeArticle(article), + onDeleted: () => refresh(true), onGone: () => goneFromList(article), }) if (confirmed) { diff --git a/web/src/composables/useArticles.ts b/web/src/composables/useArticles.ts index 51ae00e..7a859cd 100644 --- a/web/src/composables/useArticles.ts +++ b/web/src/composables/useArticles.ts @@ -2,7 +2,7 @@ import type { SuggestionCode } from '@/lib/seoTypes' import type { ArticleSeo } from '@/lib/articleSeo' import { useApi } from '@/composables/useApi' import type { Byline } from '@/lib/articleTypes' -import type { ArticleStatus, SEO_FILTER } from '@/lib/articleListQuery' +import type { ArticleStatus, ArticleTab, SEO_FILTER } from '@/lib/articleListQuery' export type { Byline } @@ -49,32 +49,89 @@ export interface ListArticlesParams { seo?: '' | typeof SEO_FILTER } +export type ArticleCapability = 'read' | 'edit' | 'publish' | 'delete' | 'create_translation' + +export interface CoverageEntry { + id: number + lang: string + title: string + is_online: boolean + language_enabled: boolean + capabilities: ArticleCapability[] +} + +export interface StoryItem extends Article { + story_id?: number + story_updated_at: string + story_authors: Byline[] + coverage: CoverageEntry[] + missing_langs: string[] + capabilities: ArticleCapability[] +} + +export interface StoryCounts extends ArticleCounts { + missing: number +} + +export interface StoryPage { + items: StoryItem[] + total: number + page: number + size: number + counts: StoryCounts +} + +export interface ListStoriesParams extends ListArticlesParams { + lang?: string + tab?: ArticleTab + author?: number +} + +function listQuery(params: ListArticlesParams) { + const query = new URLSearchParams({ + format: 'html', + page: String(params.page), + size: String(params.size), + }) + if (params.q) { + query.set('q', params.q) + } + if (params.status) { + query.set('status', params.status) + } + if (params.sort) { + query.set('sort', params.sort) + } + if (params.site) { + query.set('site', params.site) + } + if (params.seo) { + query.set('seo', params.seo) + } + return query +} + export function useArticles() { const { request } = useApi() function listArticles(params: ListArticlesParams): Promise { - const query = new URLSearchParams({ - format: 'html', - page: String(params.page), - size: String(params.size), - }) - if (params.q) { - query.set('q', params.q) - } - if (params.status) { - query.set('status', params.status) - } - if (params.sort) { - query.set('sort', params.sort) - } - if (params.site) { - query.set('site', params.site) - } - if (params.seo) { - query.set('seo', params.seo) - } - return request(`/v1/article/?${query.toString()}`) + return request(`/v1/article/?${listQuery(params).toString()}`) } - return { listArticles } + function listStories(params: ListStoriesParams): Promise { + const query = listQuery(params) + query.set('group', 'story') + if (params.lang) { + query.set('lang', params.lang) + } + if (params.tab) { + query.set('tab', params.tab) + } + if (params.author) { + query.set('author', String(params.author)) + } + return request(`/v1/article/?${query.toString()}`) + } + + return { listArticles, listStories } } diff --git a/web/src/composables/useIsDesktop.ts b/web/src/composables/useIsDesktop.ts index 3edde53..af1116e 100644 --- a/web/src/composables/useIsDesktop.ts +++ b/web/src/composables/useIsDesktop.ts @@ -1,6 +1,6 @@ import { useMediaQuery } from '@vueuse/core' -export type DesktopBreakpoint = '40rem' | '48rem' | '64rem' +export type DesktopBreakpoint = '40rem' | '48rem' | '64rem' | '80rem' export function useIsDesktop(minWidth: DesktopBreakpoint = '48rem') { return useMediaQuery(`(min-width: ${minWidth})`) diff --git a/web/src/i18n/articleListMessages.spec.ts b/web/src/i18n/articleListMessages.spec.ts new file mode 100644 index 0000000..0b0d5dc --- /dev/null +++ b/web/src/i18n/articleListMessages.spec.ts @@ -0,0 +1,77 @@ +import { readFileSync } from 'node:fs' +import { join } from 'node:path' +import { describe, expect, it } from 'vitest' +import { articleListMessages } from './articleListMessages' +import { messages } from './messages' + +const SOURCES = [ + 'components/ArticleFilterBar.vue', + 'components/ArticleFilterChips.vue', + 'components/ArticleFilterFields.vue', + 'components/ArticleFiltersSheet.vue', + 'components/ArticleListEmpty.vue', + 'components/ArticleListFailure.vue', + 'components/ArticleListTabs.vue', + 'components/StoryCardList.vue', + 'components/StoryCoverage.vue', + 'components/StoryRowMenu.vue', + 'components/StoryTable.vue', + 'lib/articleFilterChips.ts', + 'lib/storyList.ts', + 'lib/storyMenu.ts', + 'views/ArticleView.vue', +] + +const SAME_IN_FRENCH = new Set([ + 'SEO', + 'Site', + 'Article', + 'Articles', + 'Actions', + '1 version', + '{0} versions', + '1 article', + '{0} articles', +]) +const LITERAL = /\bt\(\s*(?:'((?:\\.|[^'\\])*)'|"((?:\\.|[^"\\])*)")/g +const NUMBERED = /\{\d+\}/g + +function keysIn(source: string) { + return [...source.matchAll(LITERAL)].map((match) => + (match[1] ?? match[2] ?? '').replace(/\\(['"\\])/g, '$1'), + ) +} + +describe('Article list messages', () => { + const keys = SOURCES.flatMap((file) => + keysIn(readFileSync(join(import.meta.dirname, '..', file), 'utf8')).map((key) => ({ + file, + key, + })), + ) + + it('finds the strings of the list screen', () => { + expect(keys.length).toBeGreaterThan(60) + }) + + it.each(keys.map(({ file, key }) => [file, key]))( + '%s: "%s" has a French entry', + (_file, key) => { + const french = (messages as Record)[key as string] + + expect(french).toMatch(/\S/) + if (!SAME_IN_FRENCH.has(key as string)) { + expect(french).not.toBe(key) + } + }, + ) + + it.each(Object.entries(articleListMessages))( + 'keeps the placeholders of "%s" in French', + (english, french) => { + expect([...french.matchAll(NUMBERED)].map((m) => m[0]).sort()).toEqual( + [...english.matchAll(NUMBERED)].map((m) => m[0]).sort(), + ) + }, + ) +}) diff --git a/web/src/i18n/articleListMessages.ts b/web/src/i18n/articleListMessages.ts new file mode 100644 index 0000000..63b09ef --- /dev/null +++ b/web/src/i18n/articleListMessages.ts @@ -0,0 +1,80 @@ +export const articleListMessages = { + 'Needs translation': 'À traduire', + All: 'Tous', + Published: 'Publiés', + Drafts: 'Brouillons', + 'Not in {0}': 'Pas en {0}', + 'Article views': 'Vues des articles', + 'Scroll tabs left': 'Faire défiler les onglets vers la gauche', + 'Scroll tabs right': 'Faire défiler les onglets vers la droite', + '{0} {1} versions': '{0} versions en {1}', + '1 {0} version': '1 version en {0}', + '{0} {1} drafts': '{0} brouillons en {1}', + '1 {0} draft': '1 brouillon en {0}', + '{0} published {1} versions': '{0} versions publiées en {1}', + '1 published {0} version': '1 version publiée en {0}', + '{0} articles not in {1}': '{0} articles sans version en {1}', + '1 article not in {0}': '1 article sans version en {0}', + '{0} articles need translation': '{0} articles à traduire', + '1 article needs translation': '1 article à traduire', + 'Search titles in every language': 'Rechercher dans les titres de toutes les langues', + 'Search {0} titles': 'Rechercher dans les titres en {0}', + 'All languages': 'Toutes les langues', + 'Any status': 'Tout statut', + 'Any author': 'Tout auteur', + 'Any SEO state': 'Tout état SEO', + 'Filters · {0}': 'Filtres · {0}', + 'Filters, {0} active': 'Filtres, {0} actifs', + 'Close filters': 'Fermer les filtres', + 'Site: {0}': 'Site : {0}', + 'Status: {0}': 'Statut : {0}', + 'Author: {0}': 'Auteur : {0}', + 'SEO: Needs attention': 'SEO : À examiner', + 'Disabled languages keep their versions. They can’t get new ones.': + 'Les langues désactivées gardent leurs versions. Elles n’en reçoivent pas de nouvelles.', + '{0} versions': '{0} versions', + '1 version': '1 version', + Authors: 'Auteurs', + SEO: 'SEO', + Actions: 'Actions', + 'No articles match these filters': 'Aucun article ne correspond à ces filtres', + 'Remove a filter to see more articles.': 'Retirez un filtre pour voir plus d’articles.', + 'Shown in {0}': 'Affiché en {0}', + 'Shown in {0} · language disabled': 'Affiché en {0} · langue désactivée', + 'Opens {0} · published': 'Ouvre {0} · publié', + 'Opens {0} · draft': 'Ouvre {0} · brouillon', + 'Opens {0} · published · language disabled': 'Ouvre {0} · publié · langue désactivée', + 'Opens {0} · draft · language disabled': 'Ouvre {0} · brouillon · langue désactivée', + 'Add {0} translation': 'Ajouter une traduction en {0}', + 'Add {0} translation to {1}': 'Ajouter une traduction en {0} à {1}', + 'Open {0}': 'Ouvrir {0}', + 'Open another language…': 'Ouvrir une autre langue…', + 'Add translation…': 'Ajouter une traduction…', + 'Publish {0}': 'Publier {0}', + 'Unpublish {0}': 'Dépublier {0}', + 'Delete {0} version': 'Supprimer la version {0}', + 'Open this version': 'Ouvrir cette version', + published: 'publié', + draft: 'brouillon', + 'no version': 'aucune version', + 'language disabled': 'langue désactivée', + '{0}: {1}': '{0} : {1}', + '{0} more languages: {1}': '{0} autres langues : {1}', + '1 more language: {0}': '1 autre langue : {0}', + 'Every article you can see has a {0} version': + 'Tous les articles que vous voyez ont une version en {0}', + 'Articles without a {0} version will appear here.': + 'Les articles sans version en {0} apparaîtront ici.', + 'Every article you can see is fully translated': + 'Tous les articles que vous voyez sont entièrement traduits', + 'Articles that need a translation will appear here.': + 'Les articles à traduire apparaîtront ici.', + 'Search covers titles in every language you can read.': + 'La recherche porte sur les titres de toutes les langues que vous pouvez lire.', + "Couldn't load articles": 'Impossible de charger les articles', + 'Your filters and page are kept. Try again.': + 'Vos filtres et votre page sont conservés. Réessayez.', + 'Showing only what you can read': 'Seul ce que vous pouvez lire s’affiche', + "Languages, counts and missing-translation results reflect the versions you have permission to see. You can't create translations on this site.": + 'Les langues, les totaux et les traductions manquantes reflètent les versions que vous avez le droit de voir. Vous ne pouvez pas créer de traductions sur ce site.', +} diff --git a/web/src/i18n/messages.ts b/web/src/i18n/messages.ts index b9955be..661841b 100644 --- a/web/src/i18n/messages.ts +++ b/web/src/i18n/messages.ts @@ -1,7 +1,9 @@ +import { articleListMessages } from './articleListMessages' import { localizationMessages } from './localizationMessages' export const messages = { ...localizationMessages, + ...articleListMessages, 'Not found': 'Introuvable', 'Edit article': 'Modifier l’article', unselected: 'désélectionnée', diff --git a/web/src/i18n/metaMessages.spec.ts b/web/src/i18n/metaMessages.spec.ts index c834039..30f7c32 100644 --- a/web/src/i18n/metaMessages.spec.ts +++ b/web/src/i18n/metaMessages.spec.ts @@ -5,11 +5,7 @@ import { API_GROUPS, SESSION_GROUPS } from '@/data/apiCatalogue' import { messages } from './messages' const SOURCES = [ - 'components/ArticleCardList.vue', 'components/ArticleListNotice.vue', - 'components/ArticleListToolbar.vue', - 'components/ArticleSiteFilter.vue', - 'components/ArticleTable.vue', 'components/AuthorList.vue', 'components/AuthorProfileCard.vue', 'components/AuthorSelect.vue', diff --git a/web/src/i18n/seoMessages.spec.ts b/web/src/i18n/seoMessages.spec.ts index 73f70f1..da053c8 100644 --- a/web/src/i18n/seoMessages.spec.ts +++ b/web/src/i18n/seoMessages.spec.ts @@ -10,20 +10,17 @@ import type { ResolvedSeo, SeoSuggestion, SuggestionCode } from '@/lib/seoTypes' const SOURCES = [ 'components/ApiPreviewDialog.vue', 'components/ArticleListNotice.vue', - 'components/ArticleListToolbar.vue', 'components/EditorHeader.vue', 'components/EditorMobileBar.vue', 'components/EditorSkeleton.vue', 'components/EmptyState.vue', 'components/FallbackChip.vue', - 'components/NeedsAttentionToggle.vue', 'components/SearchPreview.vue', 'components/SearchSharingSection.vue', 'components/SeoAdvancedSection.vue', 'components/SeoDisclosure.vue', 'components/SeoPanel.vue', 'components/SeoSocialSection.vue', - 'components/SeoStatusChip.vue', 'components/SocialCardPreview.vue', 'components/SoftCounter.vue', 'components/SuggestionList.vue', diff --git a/web/src/i18n/spa.spec.ts b/web/src/i18n/spa.spec.ts index fcc3d0e..920a304 100644 --- a/web/src/i18n/spa.spec.ts +++ b/web/src/i18n/spa.spec.ts @@ -13,7 +13,7 @@ import { initializeLocale, setLocale, t } from './index' import { messages } from './messages' import { formatAbsoluteDateFromIso } from '@/lib/relativeDate' import { formatSize, pluralArticles } from '@/lib/mediaFormat' -import { articleEnvelope, articleListUrl } from '@/views/__tests__/articleEnvelope' +import { storyEnvelope, storyListUrl } from '@/views/__tests__/articleEnvelope' import { accessFor, ownerAccess } from '@/test/access' vi.mock('@/composables/useApi', async () => { @@ -371,9 +371,13 @@ describe('Hardener localization outcomes', () => { updated_at: '2025-02-14T12:00:00Z', } request.mockImplementation(async (url: string) => - articleEnvelope([article], { - q: new URL(url, 'http://localhost').searchParams.get('q') ?? '', - }), + url.startsWith('/v1/article/') + ? storyEnvelope([article], { + q: new URL(url, 'http://localhost').searchParams.get('q') ?? '', + }) + : url.startsWith('/v1/author/') + ? { items: [], total: 0, page: 1, size: 100 } + : [], ) setLocale('fr') await mountRoute('/article?q=absent') @@ -438,7 +442,13 @@ describe('Hardener localization outcomes', () => { updated_at: '2025-02-14T12:00:00Z', } request.mockImplementation(async (url: string) => - url === articleListUrl() ? articleEnvelope([article]) : article, + url === storyListUrl() + ? storyEnvelope([article]) + : url.startsWith('/v1/site/') || url.startsWith('/v1/author/') + ? url.startsWith('/v1/author/') + ? { items: [], total: 0, page: 1, size: 100 } + : [] + : article, ) await mountRoute(path) if (path === '/article') { diff --git a/web/src/lib/__tests__/articleListQuery.spec.ts b/web/src/lib/__tests__/articleListQuery.spec.ts index 1fcef40..68770a4 100644 --- a/web/src/lib/__tests__/articleListQuery.spec.ts +++ b/web/src/lib/__tests__/articleListQuery.spec.ts @@ -11,6 +11,9 @@ describe('parseArticleQuery', () => { sort: '', site: '', seo: '', + lang: '', + tab: '', + author: 0, }) }) @@ -31,6 +34,9 @@ describe('parseArticleQuery', () => { sort: 'title:asc', site: '', seo: '', + lang: '', + tab: '', + author: 0, }) }) @@ -55,6 +61,51 @@ describe('parseArticleQuery', () => { }) }) +describe('localized list parameters', () => { + it('reads lang, tab and author', () => { + expect(parseArticleQuery({ lang: 'fr', tab: 'missing', author: '12' })).toMatchObject({ + lang: 'fr', + tab: 'missing', + author: 12, + }) + }) + + it.each(['english', 'x', '', 'fr_FR', '12'])('falls back from the lang "%s"', (lang) => { + expect(parseArticleQuery({ lang }).lang).toBe('') + }) + + it.each(['review', 'all', 'published', ''])('falls back from the tab "%s"', (tab) => { + expect(parseArticleQuery({ tab }).tab).toBe('') + }) + + it.each(['abc', '0', '-1', '', '1.5'])('falls back from the author "%s"', (author) => { + expect(parseArticleQuery({ author }).author).toBe(0) + }) + + it('drops the status when the tab is Not in language', () => { + expect(parseArticleQuery({ tab: 'missing', status: 'draft' }).status).toBe('') + }) + + it('writes lang, tab and author and omits their defaults', () => { + expect( + toRouteQuery({ + ...parseArticleQuery({}), + lang: 'fr', + tab: 'missing', + author: 4, + }), + ).toEqual({ lang: 'fr', tab: 'missing', author: '4' }) + expect(toRouteQuery(parseArticleQuery({}))).toEqual({}) + }) + + it('tells queries apart by lang, tab and author', () => { + const base = parseArticleQuery({}) + expect(sameArticleQuery(base, { ...base, lang: 'fr' })).toBe(false) + expect(sameArticleQuery(base, { ...base, tab: 'missing' })).toBe(false) + expect(sameArticleQuery(base, { ...base, author: 3 })).toBe(false) + }) +}) + describe('sort parameter', () => { it.each(['title:asc', 'status:desc', 'created_at:asc', 'created_at:desc'])( 'keeps %s', diff --git a/web/src/lib/__tests__/storyList.spec.ts b/web/src/lib/__tests__/storyList.spec.ts new file mode 100644 index 0000000..316a4c1 --- /dev/null +++ b/web/src/lib/__tests__/storyList.spec.ts @@ -0,0 +1,125 @@ +import { afterEach, describe, expect, it } from 'vitest' +import { setLocale } from '@/i18n' +import { parseArticleQuery } from '@/lib/articleListQuery' +import { + activeTabKey, + coverageChips, + listTabs, + overflowLabel, + queryForTab, + unitLabel, +} from '@/lib/storyList' +import type { CoverageEntry } from '@/composables/useArticles' + +const counts = { all: 4, online: 2, draft: 2, suggestions: 0, missing: 4 } + +function query(raw: Record = {}) { + return parseArticleQuery(raw) +} + +function entry(lang: string, isOnline: boolean, enabled = true): CoverageEntry { + return { + id: 1, + lang, + title: lang, + is_online: isOnline, + language_enabled: enabled, + capabilities: ['read'], + } +} + +afterEach(() => setLocale('en')) + +describe('listTabs', () => { + it('offers All articles and Needs translation when no language is selected', () => { + expect( + listTabs(query(), { ...counts, all: 8, missing: 7 }).map((tab) => [ + tab.label, + tab.count, + ]), + ).toEqual([ + ['All articles', 8], + ['Needs translation', 7], + ]) + }) + + it('offers All, Published, Drafts and Not in French for a language', () => { + expect( + listTabs(query({ lang: 'fr' }), counts).map((tab) => [tab.label, tab.count]), + ).toEqual([ + ['All', 4], + ['Published', 2], + ['Drafts', 2], + ['Not in French', 4], + ]) + }) + + it('has no count before the first response', () => { + expect(listTabs(query(), null).map((tab) => tab.count)).toEqual([null, null]) + }) + + it('names the language in French lowercase', () => { + setLocale('fr') + + expect(listTabs(query({ lang: 'de' }), counts)[3]!.label).toBe('Pas en allemand') + }) +}) + +describe('tab selection', () => { + it('reads the active tab from tab and status', () => { + expect(activeTabKey(query())).toBe('all') + expect(activeTabKey(query({ lang: 'fr', status: 'draft' }))).toBe('draft') + expect(activeTabKey(query({ lang: 'fr', tab: 'missing' }))).toBe('missing') + expect(activeTabKey(query({ status: 'draft' }))).toBe('all') + }) + + it('resets page and the other tab filters when a tab is chosen', () => { + const base = { ...query({ lang: 'fr', status: 'draft' }), page: 3 } + + expect(queryForTab(base, 'missing')).toMatchObject({ tab: 'missing', status: '', page: 1 }) + expect(queryForTab(base, 'online')).toMatchObject({ tab: '', status: 'online', page: 1 }) + expect(queryForTab(base, 'all')).toMatchObject({ tab: '', status: '', page: 1 }) + }) +}) + +describe('unitLabel', () => { + it.each([ + [{}, 8, '8 articles'], + [{}, 1, '1 article'], + [{ lang: 'fr' }, 4, '4 French versions'], + [{ lang: 'fr' }, 1, '1 French version'], + [{ lang: 'fr', tab: 'missing' }, 4, '4 articles not in French'], + [{ tab: 'missing' }, 7, '7 articles need translation'], + [{ tab: 'missing' }, 1, '1 article needs translation'], + [{ lang: 'fr', status: 'draft' }, 2, '2 French drafts'], + [{ lang: 'fr', status: 'online' }, 2, '2 published French versions'], + ])('%j with %i rows reads "%s"', (raw, total, label) => { + expect(unitLabel(query(raw), total)).toBe(label) + }) +}) + +describe('coverageChips', () => { + it('keeps the response order, then the dashed missing languages', () => { + const chips = coverageChips([entry('en', true), entry('es', true, false)], ['fr', 'de']) + + expect(chips.map((chip) => [chip.code, chip.status, chip.disabled])).toEqual([ + ['EN', 'published', false], + ['ES', 'published', true], + ['FR', 'missing', false], + ['DE', 'missing', false], + ]) + expect(chips.map((chip) => chip.label)).toEqual([ + 'English: published', + 'Spanish: published, language disabled', + 'French: no version', + 'German: no version', + ]) + }) + + it('names the hidden languages of the overflow chip', () => { + const chips = coverageChips([], ['fr', 'de', 'ar']) + + expect(overflowLabel(chips)).toBe('3 more languages: French, German, Arabic') + expect(overflowLabel(chips.slice(0, 1))).toBe('1 more language: French') + }) +}) diff --git a/web/src/lib/articleFilterChips.ts b/web/src/lib/articleFilterChips.ts new file mode 100644 index 0000000..9c63152 --- /dev/null +++ b/web/src/lib/articleFilterChips.ts @@ -0,0 +1,50 @@ +import { locale, t } from '@/i18n' +import type { Author } from '@/composables/useAuthors' +import type { Site } from '@/composables/useSites' +import type { ArticleListQuery } from '@/lib/articleListQuery' +import { languageName } from '@/lib/siteLanguages' + +export type FilterChipKey = 'site' | 'lang' | 'status' | 'author' | 'seo' + +export interface FilterChip { + key: FilterChipKey + label: string +} + +export function filterChips( + query: ArticleListQuery, + sites: Site[], + authors: Author[], +): FilterChip[] { + if (!query.status && !query.author && !query.seo) { + return [] + } + const chips: FilterChip[] = [] + const site = sites.find((entry) => entry.key === query.site) + if (query.site) { + chips.push({ key: 'site', label: t('Site: {0}', [site?.name ?? query.site]) }) + } + if (query.lang) { + chips.push({ + key: 'lang', + label: t('Language: {0}', [languageName(query.lang, locale.value)]), + }) + } + if (query.status) { + chips.push({ + key: 'status', + label: t('Status: {0}', [query.status === 'draft' ? t('Draft') : t('Published')]), + }) + } + if (query.author) { + const author = authors.find((entry) => entry.id === query.author) + chips.push({ + key: 'author', + label: t('Author: {0}', [author?.name ?? String(query.author)]), + }) + } + if (query.seo) { + chips.push({ key: 'seo', label: t('SEO: Needs attention') }) + } + return chips +} diff --git a/web/src/lib/articleFilterFields.ts b/web/src/lib/articleFilterFields.ts new file mode 100644 index 0000000..1076811 --- /dev/null +++ b/web/src/lib/articleFilterFields.ts @@ -0,0 +1,17 @@ +import type { Author } from '@/composables/useAuthors' +import type { Site } from '@/composables/useSites' +import type { ArticleStatus, SEO_FILTER } from '@/lib/articleListQuery' +import type { SiteLanguage } from '@/lib/siteLanguages' + +export interface ArticleFilterFieldProps { + site: string + sites: Site[] + severalSites: boolean + lang: string + languages: SiteLanguage[] + status: '' | ArticleStatus + author: number + authors: Author[] + seo: '' | typeof SEO_FILTER + suggestions: number | null +} diff --git a/web/src/lib/articleListQuery.ts b/web/src/lib/articleListQuery.ts index 02acb9e..4da463b 100644 --- a/web/src/lib/articleListQuery.ts +++ b/web/src/lib/articleListQuery.ts @@ -9,6 +9,9 @@ import { } from '@/lib/pageQuery' export type ArticleStatus = 'online' | 'draft' +export type ArticleTab = '' | 'missing' + +const LANGUAGE_TAG = /^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8})*$/ export const DEFAULT_ARTICLE_SORT = 'updated_at:desc' export const ARTICLE_SORT_FIELDS = ['title', 'status', 'created_at', 'updated_at'] as const @@ -24,6 +27,9 @@ export interface ArticleListQuery { sort: string site: string seo: '' | typeof SEO_FILTER + lang: string + tab: ArticleTab + author: number } const SORT_PATTERN = new RegExp(`^(${ARTICLE_SORT_FIELDS.join('|')}):(asc|desc)$`) @@ -44,15 +50,20 @@ export function nextSort(current: string, field: ArticleSortField) { export function parseArticleQuery(query: LocationQuery): ArticleListQuery { const page = firstQueryValue(query.page) const size = Number(firstQueryValue(query.size)) + const tab = firstQueryValue(query.tab) === 'missing' ? 'missing' : '' const status = firstQueryValue(query.status) + const lang = firstQueryValue(query.lang) return { page: parsePositiveInteger(page) ?? 1, size: PAGE_SIZES.includes(size) ? size : DEFAULT_PAGE_SIZE, q: firstQueryValue(query.q).slice(0, SEARCH_MAX_LENGTH), - status: status === 'online' || status === 'draft' ? status : '', + status: tab === '' && (status === 'online' || status === 'draft') ? status : '', sort: validSort(firstQueryValue(query.sort)), site: firstQueryValue(query.site).slice(0, SITE_KEY_MAX), seo: firstQueryValue(query.seo) === SEO_FILTER ? SEO_FILTER : '', + lang: LANGUAGE_TAG.test(lang) ? lang : '', + tab, + author: parsePositiveInteger(firstQueryValue(query.author)) ?? 0, } } @@ -79,6 +90,15 @@ export function toRouteQuery(query: ArticleListQuery) { if (query.seo) { route.seo = query.seo } + if (query.lang) { + route.lang = query.lang + } + if (query.tab) { + route.tab = query.tab + } + if (query.author) { + route.author = String(query.author) + } return route } @@ -90,6 +110,9 @@ export function sameArticleQuery(left: ArticleListQuery, right: ArticleListQuery left.status === right.status && left.site === right.site && left.seo === right.seo && + left.lang === right.lang && + left.tab === right.tab && + left.author === right.author && effectiveSort(left.sort) === effectiveSort(right.sort) ) } diff --git a/web/src/lib/articleSeoStatus.ts b/web/src/lib/articleSeoStatus.ts deleted file mode 100644 index 3cc1a0d..0000000 --- a/web/src/lib/articleSeoStatus.ts +++ /dev/null @@ -1,17 +0,0 @@ -export type SeoChip = { kind: 'suggestions'; count: number } | { kind: 'hidden' } - -interface SeoStatusSource { - seo_status?: unknown - seo_suggestions?: unknown -} - -export function seoChipOf(article: SeoStatusSource): SeoChip | null { - if (article.seo_status === 'hidden') { - return { kind: 'hidden' } - } - const count = article.seo_suggestions - if (article.seo_status === 'suggestions' && typeof count === 'number' && count > 0) { - return { kind: 'suggestions', count } - } - return null -} diff --git a/web/src/lib/storyList.ts b/web/src/lib/storyList.ts new file mode 100644 index 0000000..24fa782 --- /dev/null +++ b/web/src/lib/storyList.ts @@ -0,0 +1,196 @@ +import { locale, t } from '@/i18n' +import type { + ArticleCapability, + CoverageEntry, + StoryCounts, + StoryItem, +} from '@/composables/useArticles' +import type { ArticleListQuery, ArticleStatus } from '@/lib/articleListQuery' +import { authorsSummary } from '@/lib/articleMeta' +import { formatAbsoluteDate, formatRelativeDate } from '@/lib/relativeDate' +import { languageName } from '@/lib/siteLanguages' + +export type ListTabKey = 'all' | 'online' | 'draft' | 'missing' + +export interface ListTab { + key: ListTabKey + label: string + count: number | null +} + +export interface CoverageChip { + lang: string + code: string + status: 'published' | 'draft' | 'missing' + disabled: boolean + label: string + entry: CoverageEntry | null +} + +export const COVERAGE_LIMIT_DESKTOP = 5 +export const COVERAGE_LIMIT_NARROW = 4 + +export function nameInSentence(tag: string) { + const name = languageName(tag, locale.value) + return locale.value === 'fr' ? name.toLocaleLowerCase('fr') : name +} + +export function activeTabKey(query: ArticleListQuery): ListTabKey { + if (query.tab === 'missing') { + return 'missing' + } + if (query.lang && query.status) { + return query.status + } + return 'all' +} + +export function listTabs(query: ArticleListQuery, counts: StoryCounts | null): ListTab[] { + const count = (key: keyof StoryCounts) => (counts ? (counts[key] ?? 0) : null) + if (!query.lang) { + return [ + { key: 'all', label: t('All articles'), count: count('all') }, + { key: 'missing', label: t('Needs translation'), count: count('missing') }, + ] + } + return [ + { key: 'all', label: t('All'), count: count('all') }, + { key: 'online', label: t('Published'), count: count('online') }, + { key: 'draft', label: t('Drafts'), count: count('draft') }, + { + key: 'missing', + label: t('Not in {0}', [nameInSentence(query.lang)]), + count: count('missing'), + }, + ] +} + +export function queryForTab(query: ArticleListQuery, key: ListTabKey): ArticleListQuery { + const status: '' | ArticleStatus = key === 'online' || key === 'draft' ? key : '' + return { ...query, tab: key === 'missing' ? 'missing' : '', status, page: 1 } +} + +export function unitLabel(query: ArticleListQuery, total: number) { + const plural = total !== 1 + if (!query.lang) { + if (query.tab === 'missing') { + return plural + ? t('{0} articles need translation', [total]) + : t('1 article needs translation') + } + return plural ? t('{0} articles', [total]) : t('1 article') + } + const name = nameInSentence(query.lang) + if (query.tab === 'missing') { + return plural + ? t('{0} articles not in {1}', [total, name]) + : t('1 article not in {0}', [name]) + } + if (query.status === 'draft') { + return plural ? t('{0} {1} drafts', [total, name]) : t('1 {0} draft', [name]) + } + if (query.status === 'online') { + return plural + ? t('{0} published {1} versions', [total, name]) + : t('1 published {0} version', [name]) + } + return plural ? t('{0} {1} versions', [total, name]) : t('1 {0} version', [name]) +} + +function chipLabel(chip: Omit) { + const status = + chip.status === 'published' + ? t('published') + : chip.status === 'draft' + ? t('draft') + : t('no version') + const suffix = chip.disabled ? `, ${t('language disabled')}` : '' + return `${t('{0}: {1}', [languageName(chip.lang, locale.value), status])}${suffix}` +} + +export function coverageChips(coverage: CoverageEntry[], missing: string[]): CoverageChip[] { + const existing = coverage.map((entry) => { + const chip = { + lang: entry.lang, + code: entry.lang.toUpperCase(), + status: entry.is_online ? ('published' as const) : ('draft' as const), + disabled: !entry.language_enabled, + entry, + } + return { ...chip, label: chipLabel(chip) } + }) + const absent = missing.map((lang) => { + const chip = { + lang, + code: lang.toUpperCase(), + status: 'missing' as const, + disabled: false, + entry: null, + } + return { ...chip, label: chipLabel(chip) } + }) + return [...existing, ...absent] +} + +export function overflowLabel(hidden: CoverageChip[]) { + const names = hidden.map((chip) => languageName(chip.lang, locale.value)).join(', ') + return hidden.length === 1 + ? t('1 more language: {0}', [names]) + : t('{0} more languages: {1}', [hidden.length, names]) +} + +export function can(capabilities: ArticleCapability[] | undefined, capability: ArticleCapability) { + return (capabilities ?? []).includes(capability) +} + +export type ListMode = 'story' | 'version' | 'missing' + +export function listMode(query: ArticleListQuery): ListMode { + if (query.lang) { + return query.tab === 'missing' ? 'missing' : 'version' + } + return 'story' +} + +export function shownInLine(item: StoryItem, defaultLang: string) { + if (!item.lang || !defaultLang || item.lang === defaultLang) { + return '' + } + const name = languageName(item.lang, locale.value) + const disabled = + item.coverage?.find((entry) => entry.id === item.id)?.language_enabled === false + return disabled ? t('Shown in {0} · language disabled', [name]) : t('Shown in {0}', [name]) +} + +export function opensLine(item: StoryItem) { + const name = languageName(item.lang ?? '', locale.value) + const disabled = + item.coverage?.find((entry) => entry.id === item.id)?.language_enabled === false + if (disabled) { + return item.is_online + ? t('Opens {0} · published · language disabled', [name]) + : t('Opens {0} · draft · language disabled', [name]) + } + return item.is_online ? t('Opens {0} · published', [name]) : t('Opens {0} · draft', [name]) +} + +export function storyUpdated(iso: string, now: Date = new Date()) { + const text = formatRelativeDate(iso, now) + const date = new Date(iso) + if (text !== formatAbsoluteDate(date) || date.getFullYear() !== now.getFullYear()) { + return text + } + return formatAbsoluteDate(date).replace(/\s*\d{4}$/, '') +} + +export function editPath(item: StoryItem) { + return `/article/${item.id}/edit` +} + +export function rowAuthors(item: StoryItem, mode: ListMode) { + return authorsSummary((mode === 'story' ? item.story_authors : item.authors) ?? []) +} + +export function rowUpdated(item: StoryItem, mode: ListMode, now?: Date) { + return storyUpdated(mode === 'story' ? item.story_updated_at : item.updated_at, now) +} diff --git a/web/src/lib/storyMenu.ts b/web/src/lib/storyMenu.ts new file mode 100644 index 0000000..ce2926f --- /dev/null +++ b/web/src/lib/storyMenu.ts @@ -0,0 +1,94 @@ +import { locale, t } from '@/i18n' +import type { StoryItem } from '@/composables/useArticles' +import { can } from '@/lib/storyList' +import { languageName } from '@/lib/siteLanguages' + +export interface StoryMenuItem { + key: string + label: string + icon: string + destructive?: boolean + children?: StoryMenuItem[] + onSelect: () => void +} + +export interface StoryMenuHandlers { + open: (id: number) => void + addTranslation: (item: StoryItem) => void + publish: (item: StoryItem) => void + unpublish: (item: StoryItem) => void + copyUrl: (item: StoryItem) => void + remove: (item: StoryItem) => void +} + +export function versionName(item: Pick) { + return item.lang ? languageName(item.lang, locale.value) : '' +} + +export function storyMenuItems(item: StoryItem, handlers: StoryMenuHandlers): StoryMenuItem[] { + const name = versionName(item) + const others = (item.coverage ?? []).filter((entry) => entry.id !== item.id) + const items: StoryMenuItem[] = [ + { + key: 'open', + label: name ? t('Open {0}', [name]) : t('Edit'), + icon: 'i-lucide-file-pen', + onSelect: () => handlers.open(item.id), + }, + ] + if ((item.coverage ?? []).length > 1) { + items.push({ + key: 'open-another', + label: t('Open another language…'), + icon: 'i-lucide-languages', + onSelect: () => undefined, + children: others.map((entry) => ({ + key: `open-${entry.id}`, + label: languageName(entry.lang, locale.value), + icon: 'i-lucide-file-pen', + onSelect: () => handlers.open(entry.id), + })), + }) + } + if (can(item.capabilities, 'create_translation') && (item.missing_langs ?? []).length > 0) { + items.push({ + key: 'add-translation', + label: t('Add translation…'), + icon: 'i-lucide-plus', + onSelect: () => handlers.addTranslation(item), + }) + } + if (can(item.capabilities, 'publish')) { + items.push( + item.is_online + ? { + key: 'unpublish', + label: name ? t('Unpublish {0}', [name]) : t('Unpublish'), + icon: 'i-lucide-eye-off', + onSelect: () => handlers.unpublish(item), + } + : { + key: 'publish', + label: name ? t('Publish {0}', [name]) : t('Publish'), + icon: 'i-lucide-eye', + onSelect: () => handlers.publish(item), + }, + ) + } + items.push({ + key: 'copy-url', + label: t('Copy API URL'), + icon: 'i-lucide-copy', + onSelect: () => handlers.copyUrl(item), + }) + if (can(item.capabilities, 'delete')) { + items.push({ + key: 'delete', + label: name ? t('Delete {0} version', [name]) : t('Delete…'), + icon: 'i-lucide-trash-2', + destructive: true, + onSelect: () => handlers.remove(item), + }) + } + return items +} diff --git a/web/src/test/storyLists.ts b/web/src/test/storyLists.ts new file mode 100644 index 0000000..a9bf299 --- /dev/null +++ b/web/src/test/storyLists.ts @@ -0,0 +1,492 @@ +type ArticleCapability = 'read' | 'edit' | 'publish' | 'delete' | 'create_translation' + +interface Byline { + id: number + name: string + slug: string +} + +interface CoverageEntry { + id: number + lang: string + title: string + is_online: boolean + language_enabled: boolean + capabilities: ArticleCapability[] +} + +interface StoryItem { + id: number + title: string + body: string + is_online: boolean + lang: string + site_id: number + story_id: number + created_by: number + authors: Byline[] + created_at: string + updated_at: string + story_updated_at: string + story_authors: Byline[] + coverage: CoverageEntry[] + missing_langs: string[] + capabilities: ArticleCapability[] +} + +interface StoryCounts { + all: number + online: number + draft: number + suggestions: number + missing: number +} + +interface StoryPage { + items: StoryItem[] + total: number + page: number + size: number + counts: StoryCounts +} + +type VersionState = [status: 'pub' | 'draft', needsReview: 0 | 1] + +interface FixtureStory { + n: number + title: string + versions: Record + titles: Record + by: string + updatedAt: string + seo?: boolean +} + +export interface StorySite { + key: string + id: number + langs: string[] + disabled: string[] + def: string + stories: FixtureStory[] +} + +export interface StoryViewer { + read?: string[] + create?: boolean + scope?: (story: FixtureStory) => boolean +} + +export interface StoryRequest { + site: string + lang: string + tab: string + status: string + author: string + seo: string + q: string + page: number + size: number +} + +export const BYLINES: Record = { + 'Marta Alves': { id: 1, name: 'Marta Alves', slug: 'marta-alves' }, + 'Camille Roche': { id: 2, name: 'Camille Roche', slug: 'camille-roche' }, + 'Luis Ferrer': { id: 3, name: 'Luis Ferrer', slug: 'luis-ferrer' }, +} + +function story( + n: number, + title: string, + versions: Record, + titles: Record, + by: string, + updatedAt: string, + seo = false, +): FixtureStory { + return { n, title, versions, titles, by, updatedAt, seo } +} + +const JOURNAL_STORIES = [ + story( + 1, + 'How we price a bathroom renovation', + { en: ['pub', 0], fr: ['draft', 1], de: ['pub', 0], ar: ['draft', 1] }, + { + fr: 'Comment nous chiffrons une rénovation de salle de bain', + de: 'So kalkulieren wir eine Badsanierung', + ar: 'كيف نسعّر تجديد الحمّام', + }, + 'Marta Alves', + '2026-10-30T10:00:00Z', + ), + story( + 2, + 'A guide to lime plaster', + { en: ['pub', 0], fr: ['pub', 0], de: ['pub', 0] }, + { fr: "Guide de l'enduit à la chaux", de: 'Ein Leitfaden zu Kalkputz' }, + 'Camille Roche', + '2026-10-29T09:00:00Z', + ), + story( + 3, + 'Winter checklist for timber windows', + { en: ['pub', 0], fr: ['pub', 1] }, + { fr: "Liste d'hiver pour les fenêtres en bois" }, + 'Luis Ferrer', + '2026-10-28T09:00:00Z', + ), + story( + 4, + 'Spring newsletter', + { en: ['draft', 0] }, + {}, + 'Marta Alves', + '2026-10-27T09:00:00Z', + true, + ), + story( + 5, + 'Roof inspection checklist', + { en: ['pub', 0], de: ['draft', 0] }, + { de: 'Checkliste für die Dachprüfung' }, + 'Luis Ferrer', + '2026-10-14T09:00:00Z', + true, + ), + story( + 6, + 'Why we reuse original floorboards', + { en: ['pub', 0], fr: ['draft', 0] }, + { fr: "Pourquoi nous réutilisons les lames de parquet d'origine" }, + 'Camille Roche', + '2026-10-13T09:00:00Z', + ), + story( + 7, + 'Our first passive-house retrofit', + { en: ['pub', 0], de: ['pub', 0] }, + { de: 'Unsere erste Passivhaus-Sanierung' }, + 'Marta Alves', + '2026-10-12T09:00:00Z', + ), + story( + 8, + 'Cómo elegir un contratista', + { es: ['pub', 0] }, + {}, + 'Luis Ferrer', + '2026-10-10T09:00:00Z', + ), +] + +const PRESS_STORIES = [ + story( + 11, + 'Opening hours over the holidays', + { en: ['pub', 0], fr: ['pub', 0] }, + { fr: 'Horaires pendant les fêtes' }, + 'Marta Alves', + '2026-10-31T09:00:00Z', + ), + story( + 12, + 'Meet the new site manager', + { en: ['pub', 0], fr: ['draft', 1] }, + { fr: 'Rencontrez la nouvelle cheffe de chantier' }, + 'Luis Ferrer', + '2026-10-29T09:00:00Z', + ), + story( + 13, + 'Annual report', + { en: ['draft', 0], fr: ['pub', 0] }, + { fr: 'Rapport annuel' }, + 'Marta Alves', + '2026-10-14T09:00:00Z', + ), +] + +const ATELIER_STORIES = [ + story( + 21, + 'A complete guide to renovating a ground-floor kitchen and bathroom in a listed building, with every cost itemised for owners and tenants', + { + en: ['pub', 0], + 'en-GB': ['draft', 0], + fr: ['draft', 1], + de: ['pub', 0], + 'pt-BR': ['draft', 0], + 'zh-Hans': ['pub', 0], + ja: ['pub', 0], + ar: ['draft', 0], + }, + {}, + 'Marta Alves', + '2026-10-30T10:00:00Z', + true, + ), + story( + 22, + 'Heat pumps for stone farmhouses', + { en: ['pub', 0], 'en-GB': ['pub', 0], fr: ['draft', 0], de: ['pub', 0], ja: ['draft', 0] }, + {}, + 'Luis Ferrer', + '2026-10-29T09:00:00Z', + ), + story( + 23, + 'Sourcing reclaimed oak beams', + { en: ['draft', 0], 'en-GB': ['draft', 0] }, + {}, + 'Camille Roche', + '2026-10-14T09:00:00Z', + true, + ), +] + +export const STORY_SITES: Record = { + journal: { + key: 'journal', + id: 1, + langs: ['en', 'fr', 'de', 'ar'], + disabled: ['es', 'it'], + def: 'en', + stories: JOURNAL_STORIES, + }, + press: { + key: 'press', + id: 6, + langs: ['en', 'fr'], + disabled: [], + def: 'en', + stories: PRESS_STORIES, + }, + atelier: { + key: 'atelier', + id: 2, + langs: ['en', 'en-GB', 'fr', 'de', 'pt-BR', 'zh-Hans', 'ja', 'ar'], + disabled: [], + def: 'en', + stories: ATELIER_STORIES, + }, +} + +export const LIMITED_VIEWER: StoryViewer = { + read: ['en'], + create: false, + scope: (entry) => ['Camille Roche', 'Luis Ferrer'].includes(entry.by), +} + +const FULL: ArticleCapability[] = ['read', 'edit', 'publish', 'delete', 'create_translation'] +const READ_ONLY: ArticleCapability[] = ['read'] + +export function requestOf(url: URL): StoryRequest { + const params = url.searchParams + return { + site: params.get('site') ?? 'journal', + lang: params.get('lang') ?? '', + tab: params.get('tab') ?? '', + status: params.get('status') ?? '', + author: params.get('author') ?? '', + seo: params.get('seo') ?? '', + q: (params.get('q') ?? '').toLowerCase(), + page: Number(params.get('page') ?? 1), + size: Number(params.get('size') ?? 25), + } +} + +function titleIn(entry: FixtureStory, lang: string) { + return entry.titles[lang] ?? entry.title +} + +function idOf(entry: FixtureStory, lang: string) { + return entry.n * 100 + Object.keys(entry.versions).indexOf(lang) + 1 +} + +function representative(entry: FixtureStory, def: string) { + return entry.versions[def] ? def : Object.keys(entry.versions).sort()[0]! +} + +function authorId(name: string) { + return BYLINES[name]?.id ?? 0 +} + +function visibleVersions(entry: FixtureStory, viewer: StoryViewer) { + return Object.fromEntries( + Object.entries(entry.versions).filter( + ([lang]) => !viewer.read || viewer.read.includes(lang), + ), + ) +} + +function coverageOf(entry: FixtureStory, site: StorySite, viewer: StoryViewer): CoverageEntry[] { + const own = visibleVersions(entry, viewer) + const ordered = [site.def, ...site.langs.filter((lang) => lang !== site.def)] + const langs = [ + ...ordered.filter((lang) => own[lang]), + ...Object.keys(own).filter((lang) => !site.langs.includes(lang)), + ] + return langs.map((lang) => ({ + id: idOf(entry, lang), + lang, + title: titleIn(entry, lang), + is_online: own[lang]![0] === 'pub', + language_enabled: site.langs.includes(lang), + capabilities: viewer.create === false ? READ_ONLY : FULL, + })) +} + +function missingOf(entry: FixtureStory, site: StorySite, viewer: StoryViewer) { + if (viewer.create === false) { + return [] + } + const own = visibleVersions(entry, viewer) + return site.langs.filter((lang) => !own[lang]) +} + +function itemOf( + entry: FixtureStory, + lang: string, + site: StorySite, + viewer: StoryViewer, +): StoryItem { + const own = visibleVersions(entry, viewer) + const byline = BYLINES[entry.by]! + return { + id: idOf(entry, lang), + title: titleIn(entry, lang), + body: '', + is_online: own[lang]![0] === 'pub', + lang, + site_id: site.id, + story_id: entry.n, + created_by: authorId(entry.by), + authors: [byline], + created_at: entry.updatedAt, + updated_at: entry.updatedAt, + story_updated_at: entry.updatedAt, + story_authors: [byline], + coverage: coverageOf(entry, site, viewer), + missing_langs: missingOf(entry, site, viewer), + capabilities: viewer.create === false ? READ_ONLY : FULL, + } +} + +export function storyPage(request: StoryRequest, viewer: StoryViewer = {}): StoryPage { + const site = STORY_SITES[request.site] ?? STORY_SITES.journal! + const author = request.author ? Number(request.author) : 0 + const status = request.status === 'online' ? 'pub' : request.status + const readable = site.stories + .map((entry) => ({ entry, own: visibleVersions(entry, viewer) })) + .filter( + ({ entry, own }) => + Object.keys(own).length > 0 && (!viewer.scope || viewer.scope(entry)), + ) + const passes = ( + entry: FixtureStory, + versions: VersionState[], + langs: string[], + useStatus: boolean, + ) => + (!useStatus || !status || versions.some((version) => version[0] === status)) && + (!author || authorId(entry.by) === author) && + (!request.seo || entry.seo) && + (!request.q || langs.some((lang) => titleIn(entry, lang).toLowerCase().includes(request.q))) + const anyVersion = ( + entry: FixtureStory, + own: Record, + useStatus: boolean, + ) => passes(entry, Object.values(own), Object.keys(own), useStatus) + const create = viewer.create !== false + const rows = (useStatus: boolean) => { + if (request.lang) { + const lang = request.lang + const inLang = readable.filter( + ({ entry, own }) => own[lang] && passes(entry, [own[lang]!], [lang], useStatus), + ) + const missing = readable.filter( + ({ entry, own }) => !own[lang] && anyVersion(entry, own, useStatus), + ) + return { inLang, missing } + } + const base = readable.filter(({ entry, own }) => anyVersion(entry, own, useStatus)) + const missing = base.filter(({ own }) => create && site.langs.some((lang) => !own[lang])) + return { inLang: base, missing } + } + const listed = rows(true) + const unfiltered = rows(false) + const chosen = request.tab === 'missing' ? listed.missing : listed.inLang + const start = (request.page - 1) * request.size + const items = chosen.slice(start, start + request.size).map(({ entry, own }) => { + const lang = + request.lang && request.tab !== 'missing' + ? request.lang + : representative(entry, site.def) + return itemOf(entry, own[lang] ? lang : representative(entry, site.def), site, viewer) + }) + const stateOf = (entry: FixtureStory, own: Record) => + request.lang ? [own[request.lang]!] : Object.values(own) + const counts: StoryCounts = { + all: unfiltered.inLang.length, + online: unfiltered.inLang.filter(({ entry, own }) => + stateOf(entry, own).some((version) => version[0] === 'pub'), + ).length, + draft: unfiltered.inLang.filter(({ entry, own }) => + stateOf(entry, own).some((version) => version[0] === 'draft'), + ).length, + suggestions: site.stories.filter((entry) => entry.seo).length, + missing: unfiltered.missing.length, + } + return { items, total: chosen.length, page: request.page, size: request.size, counts } +} + +export function storyPageFor(url: URL, viewer: StoryViewer = {}) { + return storyPage(requestOf(url), viewer) +} + +export function siteLanguagesBody(siteKey: string) { + const site = STORY_SITES[siteKey] ?? STORY_SITES.journal! + const count = (lang: string) => + site.stories.filter((entry) => entry.versions[lang] !== undefined).length + const published = (lang: string) => + site.stories.filter((entry) => entry.versions[lang]?.[0] === 'pub').length + const row = (lang: string, enabled: boolean) => ({ + lang, + enabled, + is_default: lang === site.def, + version_count: count(lang), + published_count: published(lang), + social_locale: null, + social_locale_state: 'not_set', + }) + return { + revision: `${site.key}-1`, + default: site.def, + needs_setup: false, + languages: [ + ...site.langs.map((lang) => row(lang, true)), + ...site.disabled.map((lang) => row(lang, false)), + ], + available: [], + } +} + +export function storyAuthorsBody() { + const counts = [3, 2, 3] + return { + items: Object.values(BYLINES).map((byline, index) => ({ + ...byline, + bio: '', + avatar_id: null, + website: null, + social_links: [], + user_id: null, + is_linked: false, + article_count: counts[index], + })), + total: 3, + page: 1, + size: 100, + } +} diff --git a/web/src/views/ArticleView.vue b/web/src/views/ArticleView.vue index 4713c4d..9a7abf0 100644 --- a/web/src/views/ArticleView.vue +++ b/web/src/views/ArticleView.vue @@ -1,39 +1,52 @@