a tool for shared writing and social publishing

simplify updatePublication

+74 -66
+74 -66
app/lish/createPub/updatePublication.ts
··· 1 1 "use server"; 2 - import { TID } from "@atproto/common"; 3 2 import { 4 3 AtpBaseClient, 5 4 PubLeafletPublication, ··· 42 41 */ 43 42 async function withPublicationUpdate( 44 43 uri: string, 45 - buildRecord: RecordBuilder, 44 + recordBuilder: RecordBuilder, 46 45 ): Promise<UpdatePublicationResult> { 47 46 // Get identity and validate authentication 48 47 const identity = await getIdentityData(); ··· 87 86 : undefined; 88 87 89 88 // Build the updated record 90 - const record = await buildRecord({ 89 + const record = await recordBuilder({ 91 90 normalizedPub, 92 91 existingBasePath, 93 92 publicationType, ··· 117 116 return { success: true, publication }; 118 117 } 119 118 119 + /** Fields that can be overridden when building a record */ 120 + interface RecordOverrides { 121 + name?: string; 122 + description?: string; 123 + icon?: any; 124 + theme?: any; 125 + basicTheme?: NormalizedPublication["basicTheme"]; 126 + preferences?: NormalizedPublication["preferences"]; 127 + basePath?: string; 128 + } 129 + 130 + /** Merges override with existing value, respecting explicit undefined */ 131 + function resolveField<T>(override: T | undefined, existing: T | undefined, hasOverride: boolean): T | undefined { 132 + return hasOverride ? override : existing; 133 + } 134 + 120 135 /** 121 - * Helper to build preferences object with correct $type based on publication type. 122 - * site.standard.publication preferences is a simple object type, no $type needed. 123 - * pub.leaflet.publication preferences requires $type. 136 + * Builds a pub.leaflet.publication record. 137 + * Uses base_path for the URL path component. 124 138 */ 125 - function buildPreferences( 126 - preferencesData: NormalizedPublication["preferences"] | undefined, 127 - publicationType: PublicationType, 128 - ): SiteStandardPublication.Preferences | PubLeafletPublication.Preferences | undefined { 129 - if (!preferencesData) return undefined; 139 + function buildLeafletRecord( 140 + normalizedPub: NormalizedPublication | null, 141 + existingBasePath: string | undefined, 142 + overrides: RecordOverrides, 143 + ): PubLeafletPublication.Record { 144 + const preferences = overrides.preferences ?? normalizedPub?.preferences; 130 145 131 - const basePreferences = { 132 - showInDiscover: preferencesData.showInDiscover, 133 - showComments: preferencesData.showComments, 134 - showMentions: preferencesData.showMentions, 135 - showPrevNext: preferencesData.showPrevNext, 146 + return { 147 + $type: "pub.leaflet.publication", 148 + name: overrides.name ?? normalizedPub?.name ?? "", 149 + description: resolveField(overrides.description, normalizedPub?.description, "description" in overrides), 150 + icon: resolveField(overrides.icon, normalizedPub?.icon, "icon" in overrides), 151 + theme: resolveField(overrides.theme, normalizedPub?.theme, "theme" in overrides), 152 + base_path: overrides.basePath ?? existingBasePath, 153 + preferences: preferences ? { 154 + $type: "pub.leaflet.publication#preferences", 155 + showInDiscover: preferences.showInDiscover, 156 + showComments: preferences.showComments, 157 + showMentions: preferences.showMentions, 158 + showPrevNext: preferences.showPrevNext, 159 + } : undefined, 136 160 }; 161 + } 137 162 138 - if (publicationType === "site.standard.publication") { 139 - return basePreferences; 140 - } 163 + /** 164 + * Builds a site.standard.publication record. 165 + * Uses url for the full URL. Also supports basicTheme. 166 + */ 167 + function buildStandardRecord( 168 + normalizedPub: NormalizedPublication | null, 169 + existingBasePath: string | undefined, 170 + overrides: RecordOverrides, 171 + ): SiteStandardPublication.Record { 172 + const preferences = overrides.preferences ?? normalizedPub?.preferences; 173 + const basePath = overrides.basePath ?? existingBasePath; 141 174 142 175 return { 143 - $type: "pub.leaflet.publication#preferences" as const, 144 - ...basePreferences, 176 + $type: "site.standard.publication", 177 + name: overrides.name ?? normalizedPub?.name ?? "", 178 + description: resolveField(overrides.description, normalizedPub?.description, "description" in overrides), 179 + icon: resolveField(overrides.icon, normalizedPub?.icon, "icon" in overrides), 180 + theme: resolveField(overrides.theme, normalizedPub?.theme, "theme" in overrides), 181 + basicTheme: resolveField(overrides.basicTheme, normalizedPub?.basicTheme, "basicTheme" in overrides), 182 + url: basePath ? `https://${basePath}` : normalizedPub?.url || "", 183 + preferences: preferences ? { 184 + showInDiscover: preferences.showInDiscover, 185 + showComments: preferences.showComments, 186 + showMentions: preferences.showMentions, 187 + showPrevNext: preferences.showPrevNext, 188 + } : undefined, 145 189 }; 146 190 } 147 191 148 192 /** 149 - * Helper to build the base record fields (shared between all update functions) 193 + * Builds a record for the appropriate publication type. 150 194 */ 151 - function buildBaseRecord( 195 + function buildRecord( 152 196 normalizedPub: NormalizedPublication | null, 153 197 existingBasePath: string | undefined, 154 198 publicationType: PublicationType, 155 - overrides: { 156 - name?: string; 157 - description?: string; 158 - icon?: any; 159 - theme?: any; 160 - basicTheme?: NormalizedPublication["basicTheme"]; 161 - preferences?: NormalizedPublication["preferences"]; 162 - basePath?: string; 163 - }, 199 + overrides: RecordOverrides, 164 200 ): PubLeafletPublication.Record | SiteStandardPublication.Record { 165 - const name = overrides.name ?? normalizedPub?.name ?? ""; 166 - const description = overrides.description !== undefined 167 - ? overrides.description 168 - : normalizedPub?.description; 169 - const icon = overrides.icon !== undefined ? overrides.icon : normalizedPub?.icon; 170 - const theme = overrides.theme !== undefined ? overrides.theme : normalizedPub?.theme; 171 - const basicTheme = overrides.basicTheme !== undefined ? overrides.basicTheme : normalizedPub?.basicTheme; 172 - const preferencesData = overrides.preferences ?? normalizedPub?.preferences; 173 - const basePath = overrides.basePath ?? existingBasePath; 174 - 175 - if (publicationType === "site.standard.publication") { 176 - return { 177 - $type: publicationType, 178 - name, 179 - description, 180 - icon, 181 - theme, 182 - basicTheme, 183 - preferences: buildPreferences(preferencesData, publicationType), 184 - url: basePath ? `https://${basePath}` : normalizedPub?.url || "", 185 - } as SiteStandardPublication.Record; 201 + if (publicationType === "pub.leaflet.publication") { 202 + return buildLeafletRecord(normalizedPub, existingBasePath, overrides); 186 203 } 187 - 188 - return { 189 - $type: publicationType, 190 - name, 191 - description, 192 - icon, 193 - theme, 194 - preferences: buildPreferences(preferencesData, publicationType), 195 - base_path: basePath, 196 - } as PubLeafletPublication.Record; 204 + return buildStandardRecord(normalizedPub, existingBasePath, overrides); 197 205 } 198 206 199 207 export async function updatePublication({ ··· 223 231 } 224 232 } 225 233 226 - return buildBaseRecord(normalizedPub, existingBasePath, publicationType, { 234 + return buildRecord(normalizedPub, existingBasePath, publicationType, { 227 235 name, 228 236 description, 229 237 icon: iconBlob, ··· 240 248 base_path: string; 241 249 }): Promise<UpdatePublicationResult> { 242 250 return withPublicationUpdate(uri, async ({ normalizedPub, existingBasePath, publicationType }) => { 243 - return buildBaseRecord(normalizedPub, existingBasePath, publicationType, { 251 + return buildRecord(normalizedPub, existingBasePath, publicationType, { 244 252 basePath: base_path, 245 253 }); 246 254 }); ··· 306 314 }, 307 315 }; 308 316 309 - return buildBaseRecord(normalizedPub, existingBasePath, publicationType, { 317 + return buildRecord(normalizedPub, existingBasePath, publicationType, { 310 318 theme: themeData, 311 319 }); 312 320 });