( response: Partial<PatchyConfigResponse> | null | undefined, )
| 207 | // Layers the API config over the defaults one field at a time, so a missing, |
| 208 | // malformed or unknown value can only ever fall back, never break Patchy. |
| 209 | export const toPatchyConfig = ( |
| 210 | response: Partial<PatchyConfigResponse> | null | undefined, |
| 211 | ): PatchyConfig => { |
| 212 | const fallback = defaultPatchyConfig; |
| 213 | if (!response || typeof response !== 'object') { |
| 214 | return fallback; |
| 215 | } |
| 216 | |
| 217 | const faceImage = { ...fallback.faceImage }; |
| 218 | response.faces?.forEach(({ face, image }) => { |
| 219 | if (isOneOf(patchyFaces, face) && isText(image)) { |
| 220 | faceImage[face] = image; |
| 221 | } |
| 222 | }); |
| 223 | |
| 224 | const reactions = { ...fallback.reactions }; |
| 225 | response.reactions?.forEach((reaction) => { |
| 226 | if (!isOneOf(patchyTriggers, reaction?.trigger)) { |
| 227 | return; |
| 228 | } |
| 229 | const base = fallback.reactions[reaction.trigger]; |
| 230 | reactions[reaction.trigger] = { |
| 231 | enabled: |
| 232 | typeof reaction.enabled === 'boolean' ? reaction.enabled : base.enabled, |
| 233 | emote: isOneOf(patchyEmotes, reaction.emote) |
| 234 | ? reaction.emote |
| 235 | : base.emote, |
| 236 | face: isOneOf(patchyFaces, reaction.face) ? reaction.face : base.face, |
| 237 | }; |
| 238 | }); |
| 239 | |
| 240 | const moods = { ...fallback.moods }; |
| 241 | response.moods?.forEach((mood) => { |
| 242 | if (!isOneOf(patchyMoods, mood?.mood)) { |
| 243 | return; |
| 244 | } |
| 245 | const base = fallback.moods[mood.mood]; |
| 246 | moods[mood.mood] = { |
| 247 | face: isOneOf(patchyFaces, mood.face) ? mood.face : base.face, |
| 248 | copy: isText(mood.copy) ? mood.copy : base.copy, |
| 249 | label: isText(mood.label) ? mood.label : base.label, |
| 250 | }; |
| 251 | }); |
| 252 | |
| 253 | const fridayTags = response.fridayDeploy?.tags?.filter(isText); |
| 254 | |
| 255 | return { |
| 256 | awayMs: isCount(response.awayHours) |
| 257 | ? response.awayHours * HOUR |
| 258 | : fallback.awayMs, |
| 259 | lateNight: toWindow(response.lateNight, fallback.lateNight), |
| 260 | fridayDeploy: { |
| 261 | ...toWindow(response.fridayDeploy, fallback.fridayDeploy), |
| 262 | tags: fridayTags?.length ? fridayTags : fallback.fridayDeploy.tags, |
| 263 | }, |
| 264 | faceImage, |
| 265 | reactions, |
| 266 | moods, |
no test coverage detected