( response: Partial<PatchyConfigResponse> | null | undefined, )
| 171 | // Layers the API config over the defaults one field at a time, so a missing, |
| 172 | // malformed or unknown value can only ever fall back, never break Patchy. |
| 173 | export const toPatchyConfig = ( |
| 174 | response: Partial<PatchyConfigResponse> | null | undefined, |
| 175 | ): PatchyConfig => { |
| 176 | const fallback = defaultPatchyConfig; |
| 177 | if (!response || typeof response !== 'object') { |
| 178 | return fallback; |
| 179 | } |
| 180 | |
| 181 | const faceImage = { ...fallback.faceImage }; |
| 182 | response.faces?.forEach(({ face, image }) => { |
| 183 | if (isOneOf(patchyFaces, face) && isText(image)) { |
| 184 | faceImage[face] = image; |
| 185 | } |
| 186 | }); |
| 187 | |
| 188 | const reactions = { ...fallback.reactions }; |
| 189 | response.reactions?.forEach((reaction) => { |
| 190 | if (!isOneOf(patchyTriggers, reaction?.trigger)) { |
| 191 | return; |
| 192 | } |
| 193 | const base = fallback.reactions[reaction.trigger]; |
| 194 | reactions[reaction.trigger] = { |
| 195 | enabled: |
| 196 | typeof reaction.enabled === 'boolean' ? reaction.enabled : base.enabled, |
| 197 | emote: isOneOf(patchyEmotes, reaction.emote) |
| 198 | ? reaction.emote |
| 199 | : base.emote, |
| 200 | face: isOneOf(patchyFaces, reaction.face) ? reaction.face : base.face, |
| 201 | }; |
| 202 | }); |
| 203 | |
| 204 | const moods = { ...fallback.moods }; |
| 205 | response.moods?.forEach((mood) => { |
| 206 | if (!isOneOf(patchyMoods, mood?.mood)) { |
| 207 | return; |
| 208 | } |
| 209 | const base = fallback.moods[mood.mood]; |
| 210 | moods[mood.mood] = { |
| 211 | face: isOneOf(patchyFaces, mood.face) ? mood.face : base.face, |
| 212 | copy: isText(mood.copy) ? mood.copy : base.copy, |
| 213 | label: isText(mood.label) ? mood.label : base.label, |
| 214 | }; |
| 215 | }); |
| 216 | |
| 217 | const fridayTags = response.fridayDeploy?.tags?.filter(isText); |
| 218 | |
| 219 | return { |
| 220 | awayMs: isCount(response.awayHours) |
| 221 | ? response.awayHours * HOUR |
| 222 | : fallback.awayMs, |
| 223 | lateNight: toWindow(response.lateNight, fallback.lateNight), |
| 224 | fridayDeploy: { |
| 225 | ...toWindow(response.fridayDeploy, fallback.fridayDeploy), |
| 226 | tags: fridayTags?.length ? fridayTags : fallback.fridayDeploy.tags, |
| 227 | }, |
| 228 | faceImage, |
| 229 | reactions, |
| 230 | moods, |
no test coverage detected