tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
simplify updatePublication
awarm.space
2 months ago
9cd907c7
9ab87603
+74
-66
1 changed file
expand all
collapse all
unified
split
app
lish
createPub
updatePublication.ts
+74
-66
app/lish/createPub/updatePublication.ts
···
1
1
"use server";
2
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
45
-
buildRecord: RecordBuilder,
44
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
90
-
const record = await buildRecord({
89
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
119
+
/** Fields that can be overridden when building a record */
120
120
+
interface RecordOverrides {
121
121
+
name?: string;
122
122
+
description?: string;
123
123
+
icon?: any;
124
124
+
theme?: any;
125
125
+
basicTheme?: NormalizedPublication["basicTheme"];
126
126
+
preferences?: NormalizedPublication["preferences"];
127
127
+
basePath?: string;
128
128
+
}
129
129
+
130
130
+
/** Merges override with existing value, respecting explicit undefined */
131
131
+
function resolveField<T>(override: T | undefined, existing: T | undefined, hasOverride: boolean): T | undefined {
132
132
+
return hasOverride ? override : existing;
133
133
+
}
134
134
+
120
135
/**
121
121
-
* Helper to build preferences object with correct $type based on publication type.
122
122
-
* site.standard.publication preferences is a simple object type, no $type needed.
123
123
-
* pub.leaflet.publication preferences requires $type.
136
136
+
* Builds a pub.leaflet.publication record.
137
137
+
* Uses base_path for the URL path component.
124
138
*/
125
125
-
function buildPreferences(
126
126
-
preferencesData: NormalizedPublication["preferences"] | undefined,
127
127
-
publicationType: PublicationType,
128
128
-
): SiteStandardPublication.Preferences | PubLeafletPublication.Preferences | undefined {
129
129
-
if (!preferencesData) return undefined;
139
139
+
function buildLeafletRecord(
140
140
+
normalizedPub: NormalizedPublication | null,
141
141
+
existingBasePath: string | undefined,
142
142
+
overrides: RecordOverrides,
143
143
+
): PubLeafletPublication.Record {
144
144
+
const preferences = overrides.preferences ?? normalizedPub?.preferences;
130
145
131
131
-
const basePreferences = {
132
132
-
showInDiscover: preferencesData.showInDiscover,
133
133
-
showComments: preferencesData.showComments,
134
134
-
showMentions: preferencesData.showMentions,
135
135
-
showPrevNext: preferencesData.showPrevNext,
146
146
+
return {
147
147
+
$type: "pub.leaflet.publication",
148
148
+
name: overrides.name ?? normalizedPub?.name ?? "",
149
149
+
description: resolveField(overrides.description, normalizedPub?.description, "description" in overrides),
150
150
+
icon: resolveField(overrides.icon, normalizedPub?.icon, "icon" in overrides),
151
151
+
theme: resolveField(overrides.theme, normalizedPub?.theme, "theme" in overrides),
152
152
+
base_path: overrides.basePath ?? existingBasePath,
153
153
+
preferences: preferences ? {
154
154
+
$type: "pub.leaflet.publication#preferences",
155
155
+
showInDiscover: preferences.showInDiscover,
156
156
+
showComments: preferences.showComments,
157
157
+
showMentions: preferences.showMentions,
158
158
+
showPrevNext: preferences.showPrevNext,
159
159
+
} : undefined,
136
160
};
161
161
+
}
137
162
138
138
-
if (publicationType === "site.standard.publication") {
139
139
-
return basePreferences;
140
140
-
}
163
163
+
/**
164
164
+
* Builds a site.standard.publication record.
165
165
+
* Uses url for the full URL. Also supports basicTheme.
166
166
+
*/
167
167
+
function buildStandardRecord(
168
168
+
normalizedPub: NormalizedPublication | null,
169
169
+
existingBasePath: string | undefined,
170
170
+
overrides: RecordOverrides,
171
171
+
): SiteStandardPublication.Record {
172
172
+
const preferences = overrides.preferences ?? normalizedPub?.preferences;
173
173
+
const basePath = overrides.basePath ?? existingBasePath;
141
174
142
175
return {
143
143
-
$type: "pub.leaflet.publication#preferences" as const,
144
144
-
...basePreferences,
176
176
+
$type: "site.standard.publication",
177
177
+
name: overrides.name ?? normalizedPub?.name ?? "",
178
178
+
description: resolveField(overrides.description, normalizedPub?.description, "description" in overrides),
179
179
+
icon: resolveField(overrides.icon, normalizedPub?.icon, "icon" in overrides),
180
180
+
theme: resolveField(overrides.theme, normalizedPub?.theme, "theme" in overrides),
181
181
+
basicTheme: resolveField(overrides.basicTheme, normalizedPub?.basicTheme, "basicTheme" in overrides),
182
182
+
url: basePath ? `https://${basePath}` : normalizedPub?.url || "",
183
183
+
preferences: preferences ? {
184
184
+
showInDiscover: preferences.showInDiscover,
185
185
+
showComments: preferences.showComments,
186
186
+
showMentions: preferences.showMentions,
187
187
+
showPrevNext: preferences.showPrevNext,
188
188
+
} : undefined,
145
189
};
146
190
}
147
191
148
192
/**
149
149
-
* Helper to build the base record fields (shared between all update functions)
193
193
+
* Builds a record for the appropriate publication type.
150
194
*/
151
151
-
function buildBaseRecord(
195
195
+
function buildRecord(
152
196
normalizedPub: NormalizedPublication | null,
153
197
existingBasePath: string | undefined,
154
198
publicationType: PublicationType,
155
155
-
overrides: {
156
156
-
name?: string;
157
157
-
description?: string;
158
158
-
icon?: any;
159
159
-
theme?: any;
160
160
-
basicTheme?: NormalizedPublication["basicTheme"];
161
161
-
preferences?: NormalizedPublication["preferences"];
162
162
-
basePath?: string;
163
163
-
},
199
199
+
overrides: RecordOverrides,
164
200
): PubLeafletPublication.Record | SiteStandardPublication.Record {
165
165
-
const name = overrides.name ?? normalizedPub?.name ?? "";
166
166
-
const description = overrides.description !== undefined
167
167
-
? overrides.description
168
168
-
: normalizedPub?.description;
169
169
-
const icon = overrides.icon !== undefined ? overrides.icon : normalizedPub?.icon;
170
170
-
const theme = overrides.theme !== undefined ? overrides.theme : normalizedPub?.theme;
171
171
-
const basicTheme = overrides.basicTheme !== undefined ? overrides.basicTheme : normalizedPub?.basicTheme;
172
172
-
const preferencesData = overrides.preferences ?? normalizedPub?.preferences;
173
173
-
const basePath = overrides.basePath ?? existingBasePath;
174
174
-
175
175
-
if (publicationType === "site.standard.publication") {
176
176
-
return {
177
177
-
$type: publicationType,
178
178
-
name,
179
179
-
description,
180
180
-
icon,
181
181
-
theme,
182
182
-
basicTheme,
183
183
-
preferences: buildPreferences(preferencesData, publicationType),
184
184
-
url: basePath ? `https://${basePath}` : normalizedPub?.url || "",
185
185
-
} as SiteStandardPublication.Record;
201
201
+
if (publicationType === "pub.leaflet.publication") {
202
202
+
return buildLeafletRecord(normalizedPub, existingBasePath, overrides);
186
203
}
187
187
-
188
188
-
return {
189
189
-
$type: publicationType,
190
190
-
name,
191
191
-
description,
192
192
-
icon,
193
193
-
theme,
194
194
-
preferences: buildPreferences(preferencesData, publicationType),
195
195
-
base_path: basePath,
196
196
-
} as PubLeafletPublication.Record;
204
204
+
return buildStandardRecord(normalizedPub, existingBasePath, overrides);
197
205
}
198
206
199
207
export async function updatePublication({
···
223
231
}
224
232
}
225
233
226
226
-
return buildBaseRecord(normalizedPub, existingBasePath, publicationType, {
234
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
243
-
return buildBaseRecord(normalizedPub, existingBasePath, publicationType, {
251
251
+
return buildRecord(normalizedPub, existingBasePath, publicationType, {
244
252
basePath: base_path,
245
253
});
246
254
});
···
306
314
},
307
315
};
308
316
309
309
-
return buildBaseRecord(normalizedPub, existingBasePath, publicationType, {
317
317
+
return buildRecord(normalizedPub, existingBasePath, publicationType, {
310
318
theme: themeData,
311
319
});
312
320
});