|
| 1 | +import axios from 'axios'; |
| 2 | + |
| 3 | +import type { |
| 4 | + StrapiLibrariesResponse, |
| 5 | + StrapiLibraryEntry, |
| 6 | + StrapiSingleLibraryResponse, |
| 7 | +} from '@local-types/library/library'; |
| 8 | +import type { IObject } from '@local-types/library/object'; |
| 9 | +import type { LibraryTag } from '@local-types/library/tag'; |
| 10 | + |
| 11 | +import { librarySeo } from '@lib/library/seo'; |
| 12 | + |
| 13 | +/** |
| 14 | + * The library as a reader arrives at it, read on the server. |
| 15 | + * |
| 16 | + * A library used to reach the browser as a shell: the title, the description |
| 17 | + * and the schema.org list were in the first response, and every word a person |
| 18 | + * (or a crawler that does not run scripts) could actually read was fetched |
| 19 | + * afterwards by script. This is the same read the page makes, made once, on |
| 20 | + * the request, so the shelves, the books and the About text are in the HTML |
| 21 | + * that leaves the server. |
| 22 | + * |
| 23 | + * Always anonymous, including on an owner's own request: what the server |
| 24 | + * paints is what a visitor sees, and the owner's own view arrives a moment |
| 25 | + * later from their own session. A private shelf therefore never reaches the |
| 26 | + * page source. |
| 27 | + */ |
| 28 | + |
| 29 | +export interface PublicLibraryView { |
| 30 | + seo: ReturnType<typeof librarySeo>; |
| 31 | + /** The library trimmed to what the first paint draws, or null. */ |
| 32 | + library: StrapiLibraryEntry | null; |
| 33 | + /** The library's tags, as the right panel lists them. */ |
| 34 | + tags: LibraryTag[]; |
| 35 | +} |
| 36 | + |
| 37 | +const client = () => |
| 38 | + axios.create({ |
| 39 | + baseURL: process.env.NEXT_PUBLIC_STRAPI, |
| 40 | + timeout: 5000, |
| 41 | + }); |
| 42 | + |
| 43 | +const listPopulate = { |
| 44 | + 'populate[avatar]': true, |
| 45 | + 'populate[user]': true, |
| 46 | + 'populate[libraryDetails]': true, |
| 47 | + 'populate[singleShelves][populate][objects][populate][coverImage]': true, |
| 48 | + 'populate[singleShelves][populate][objects][populate][tags]': true, |
| 49 | + 'populate[singleShelves][sort][0]': 'order:asc', |
| 50 | + 'populate[singleShelves][populate][objects][sort][0]': 'order:asc', |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * A cover carries every rendition Strapi cut for it, which is six sizes of |
| 55 | + * metadata the page never reads: the card asks for `url` and nothing else. |
| 56 | + * Dropping the rest keeps a 165-book library from shipping a third of a |
| 57 | + * megabyte of image bookkeeping in the page source. |
| 58 | + */ |
| 59 | +const trimCover = (cover: IObject['attributes']['coverImage']) => { |
| 60 | + const media = cover?.data; |
| 61 | + |
| 62 | + if (!media) return cover ?? { data: null }; |
| 63 | + |
| 64 | + const { url, name, mime, size } = media.attributes; |
| 65 | + |
| 66 | + return { data: { id: media.id, attributes: { url, name, mime, size } } }; |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * Notes are the longest thing a library holds and the shelves do not draw |
| 71 | + * them: a note is read on the book's own page, which is where it is written |
| 72 | + * into the HTML in full. The one book the URL names keeps its note here so |
| 73 | + * that page is complete on arrival. |
| 74 | + */ |
| 75 | +const trimObject = (object: IObject, keepNoteFor: number | null): IObject => { |
| 76 | + // The key is dropped rather than emptied: Next refuses to serialize an |
| 77 | + // `undefined` into the page's own data, and an empty string would read as a |
| 78 | + // note the owner had cleared. |
| 79 | + const { description, ...attributes } = object.attributes; |
| 80 | + |
| 81 | + return { |
| 82 | + ...object, |
| 83 | + attributes: { |
| 84 | + ...attributes, |
| 85 | + ...(object.id === keepNoteFor && description !== undefined |
| 86 | + ? { description } |
| 87 | + : {}), |
| 88 | + coverImage: trimCover(object.attributes.coverImage), |
| 89 | + }, |
| 90 | + }; |
| 91 | +}; |
| 92 | + |
| 93 | +const forFirstPaint = ( |
| 94 | + entry: StrapiLibraryEntry, |
| 95 | + keepNoteFor: number | null, |
| 96 | +): StrapiLibraryEntry => ({ |
| 97 | + ...entry, |
| 98 | + attributes: { |
| 99 | + ...entry.attributes, |
| 100 | + singleShelves: { |
| 101 | + data: (entry.attributes.singleShelves?.data ?? []) |
| 102 | + // A visitor's reading of the shelf list, decided here rather than in |
| 103 | + // the components: a private shelf must not travel to the browser at |
| 104 | + // all, drawn or not. |
| 105 | + .filter(shelf => shelf.attributes.visibility !== 'private') |
| 106 | + .map(shelf => ({ |
| 107 | + ...shelf, |
| 108 | + attributes: { |
| 109 | + ...shelf.attributes, |
| 110 | + objects: { |
| 111 | + data: (shelf.attributes.objects?.data ?? []).map(object => |
| 112 | + trimObject(object, keepNoteFor), |
| 113 | + ), |
| 114 | + }, |
| 115 | + }, |
| 116 | + })), |
| 117 | + }, |
| 118 | + }, |
| 119 | +}); |
| 120 | + |
| 121 | +/** The numeric id of the library a username owns, or null. */ |
| 122 | +const findLibraryId = async (username: string): Promise<number | null> => { |
| 123 | + const wanted = username.trim().toLowerCase(); |
| 124 | + |
| 125 | + if (!wanted) return null; |
| 126 | + |
| 127 | + let page = 1; |
| 128 | + let pageCount = 1; |
| 129 | + |
| 130 | + do { |
| 131 | + const { data } = await client().get<StrapiLibrariesResponse>( |
| 132 | + '/api/libraries', |
| 133 | + { |
| 134 | + params: { |
| 135 | + 'pagination[page]': page, |
| 136 | + 'pagination[pageSize]': 100, |
| 137 | + 'sort[0]': 'id:asc', |
| 138 | + }, |
| 139 | + }, |
| 140 | + ); |
| 141 | + |
| 142 | + const entry = data.data.find( |
| 143 | + item => |
| 144 | + item.attributes.user?.data?.attributes.username?.toLowerCase() === |
| 145 | + wanted, |
| 146 | + ); |
| 147 | + |
| 148 | + if (entry) return entry.id; |
| 149 | + |
| 150 | + pageCount = data.meta.pagination.pageCount; |
| 151 | + page += 1; |
| 152 | + } while (page <= pageCount); |
| 153 | + |
| 154 | + return null; |
| 155 | +}; |
| 156 | + |
| 157 | +export async function getPublicLibraryView( |
| 158 | + username: string, |
| 159 | + /** The object the URL names, so its note travels with the first paint. */ |
| 160 | + keepNoteFor: number | null = null, |
| 161 | +): Promise<PublicLibraryView> { |
| 162 | + const id = await findLibraryId(username); |
| 163 | + |
| 164 | + if (id == null) return { seo: librarySeo(), library: null, tags: [] }; |
| 165 | + |
| 166 | + const { data } = await client().get<StrapiSingleLibraryResponse>( |
| 167 | + `/api/libraries/${id}`, |
| 168 | + { params: listPopulate }, |
| 169 | + ); |
| 170 | + |
| 171 | + const entry = data.data; |
| 172 | + |
| 173 | + if (!entry) return { seo: librarySeo(), library: null, tags: [] }; |
| 174 | + |
| 175 | + const seo = librarySeo(entry); |
| 176 | + const library = forFirstPaint(entry, keepNoteFor); |
| 177 | + |
| 178 | + let tags: LibraryTag[] = []; |
| 179 | + |
| 180 | + try { |
| 181 | + const answer = await client().get<{ |
| 182 | + data: { |
| 183 | + id: number; |
| 184 | + attributes: { |
| 185 | + name: string; |
| 186 | + color: string; |
| 187 | + description?: string; |
| 188 | + slug: string; |
| 189 | + objects?: number[]; |
| 190 | + }; |
| 191 | + }[]; |
| 192 | + }>('/api/tags', { params: { libraryId: id } }); |
| 193 | + |
| 194 | + tags = (answer.data?.data ?? []).map(tag => ({ |
| 195 | + id: tag.id, |
| 196 | + name: tag.attributes.name, |
| 197 | + color: tag.attributes.color, |
| 198 | + // Omitted rather than undefined: the page's own data is JSON. |
| 199 | + ...(tag.attributes.description |
| 200 | + ? { description: tag.attributes.description } |
| 201 | + : {}), |
| 202 | + slug: tag.attributes.slug, |
| 203 | + objects: tag.attributes.objects ?? [], |
| 204 | + })); |
| 205 | + } catch { |
| 206 | + // The shelves stand without the filter panel; the browser re-reads it. |
| 207 | + console.error('Library tags unavailable'); |
| 208 | + } |
| 209 | + |
| 210 | + return { seo, library, tags }; |
| 211 | +} |
0 commit comments