« get me outta code hell

generateArtistInfoPageTracksChunkedList.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateArtistInfoPageTracksChunkedList.js
blob: bce6cedfb6f8bdf7f35c3a81bf860cb7c263d72e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import {sortAlbumsTracksChronologically, sortEntryThingPairs} from '#sort';
import {accumulateSum, chunkByProperties, empty, stitchArrays} from '#sugar';

export default {
  contentDependencies: [
    'generateArtistInfoPageChunk',
    'generateArtistInfoPageChunkedList',
    'generateArtistInfoPageChunkItem',
    'generateArtistInfoPageOtherArtistLinks',
    'linkAlbum',
    'linkTrack',
  ],

  extraDependencies: ['html', 'language'],

  query(artist) {
    const processTrackEntry = ({track, contribs}) => ({
      thing: track,
      entry: {
        track: track,
        album: track.album,
        date: track.date,
        contribs: contribs,
      },
    });

    const processTrackEntries = ({tracks, contribs}) =>
      stitchArrays({
        track: tracks,
        contribs: contribs,
      }).map(processTrackEntry);

    const {tracksAsArtist, tracksAsContributor} = artist;

    const tracksAsArtistAndContributor =
      tracksAsArtist
        .filter(track => tracksAsContributor.includes(track));

    const tracksAsArtistOnly =
      tracksAsArtist
        .filter(track => !tracksAsContributor.includes(track));

    const tracksAsContributorOnly =
      tracksAsContributor
        .filter(track => !tracksAsArtist.includes(track));

    const tracksAsArtistAndContributorContribs =
      tracksAsArtistAndContributor
        .map(track => [
          ...
            track.artistContribs
              .map(contrib => ({...contrib, kind: 'artist'})),
          ...
            track.contributorContribs
              .map(contrib => ({...contrib, kind: 'contributor'})),
        ]);

    const tracksAsArtistOnlyContribs =
      tracksAsArtistOnly
        .map(track => track.artistContribs
          .map(contrib => ({...contrib, kind: 'artist'})));

    const tracksAsContributorOnlyContribs =
      tracksAsContributorOnly
        .map(track => track.contributorContribs
          .map(contrib => ({...contrib, kind: 'contributor'})));

    const tracksAsArtistAndContributorEntries =
      processTrackEntries({
        tracks: tracksAsArtistAndContributor,
        contribs: tracksAsArtistAndContributorContribs,
      });

    const tracksAsArtistOnlyEntries =
      processTrackEntries({
        tracks: tracksAsArtistOnly,
        contribs: tracksAsArtistOnlyContribs,
      });

    const tracksAsContributorOnlyEntries =
      processTrackEntries({
        tracks: tracksAsContributorOnly,
        contribs: tracksAsContributorOnlyContribs,
      });

    const entries = [
      ...tracksAsArtistAndContributorEntries,
      ...tracksAsArtistOnlyEntries,
      ...tracksAsContributorOnlyEntries,
    ];

    sortEntryThingPairs(entries, sortAlbumsTracksChronologically);

    const chunks =
      chunkByProperties(
        entries.map(({entry}) => entry),
        ['album', 'date']);

    return {chunks};
  },

  relations(relation, query, artist) {
    return {
      chunkedList:
        relation('generateArtistInfoPageChunkedList'),

      chunks:
        query.chunks.map(() => relation('generateArtistInfoPageChunk')),

      albumLinks:
        query.chunks.map(({album}) => relation('linkAlbum', album)),

      items:
        query.chunks.map(({chunk}) =>
          chunk.map(() => relation('generateArtistInfoPageChunkItem'))),

      trackLinks:
        query.chunks.map(({chunk}) =>
          chunk.map(({track}) => relation('linkTrack', track))),

      trackOtherArtistLinks:
        query.chunks.map(({chunk}) =>
          chunk.map(({contribs}) => relation('generateArtistInfoPageOtherArtistLinks', contribs, artist))),
    };
  },

  data(query, artist) {
    return {
      chunkDates:
        query.chunks.map(({date}) => date),

      chunkDurations:
        query.chunks.map(({chunk}) =>
          accumulateSum(
            chunk
              .filter(({track}) => track.duration && track.originalReleaseTrack === null)
              .map(({track}) => track.duration))),

      chunkDurationsApproximate:
        query.chunks.map(({chunk}) =>
          chunk
            .filter(({track}) => track.duration && track.originalReleaseTrack === null)
            .length > 1),

      trackDurations:
        query.chunks.map(({chunk}) =>
          chunk.map(({track}) => track.duration)),

      trackContributions:
        query.chunks.map(({chunk}) =>
          chunk
            .map(({contribs}) =>
              contribs.filter(contrib => contrib.artist === artist))
            .map(ownContribs => ({
              creditedAsArtist:
                ownContribs
                  .some(({kind}) => kind === 'artist'),

              creditedAsContributor:
                ownContribs
                  .some(({kind}) => kind === 'contributor'),

              annotatedContribs:
                ownContribs
                  .filter(({annotation}) => annotation),
            }))
            .map(({annotatedContribs, ...rest}) => ({
              ...rest,

              annotatedArtistContribs:
                annotatedContribs
                  .filter(({kind}) => kind === 'artist'),

              annotatedContributorContribs:
                annotatedContribs
                  .filter(({kind}) => kind === 'contributor'),
            }))
            .map(({
              creditedAsArtist,
              creditedAsContributor,
              annotatedArtistContribs,
              annotatedContributorContribs,
            }) => {
              // Don't display annotations associated with crediting in the
              // Contributors field if the artist is also credited as an Artist
              // *and* the Artist-field contribution is non-annotated. This is
              // so that we don't misrepresent the artist - the contributor
              // annotation tends to be for "secondary" and performance roles.
              // For example, this avoids crediting Marcy Nabors on Renewed
              // Return seemingly only for "bass clarinet" when they're also
              // the one who composed and arranged Renewed Return!
              if (
                creditedAsArtist &&
                creditedAsContributor &&
                empty(annotatedArtistContribs)
              ) {
                return [];
              }

              return [
                ...annotatedArtistContribs,
                ...annotatedContributorContribs,
              ];
            })
            .map(contribs =>
              contribs.map(({annotation}) => annotation))
            .map(contributions =>
              (empty(contributions)
                ? null
                : contributions))),

      trackRereleases:
        query.chunks.map(({chunk}) =>
          chunk.map(({track}) => track.originalReleaseTrack !== null)),
    };
  },

  generate(data, relations, {html, language}) {
    return relations.chunkedList.slots({
      chunks:
        stitchArrays({
          chunk: relations.chunks,
          albumLink: relations.albumLinks,
          date: data.chunkDates,
          duration: data.chunkDurations,
          durationApproximate: data.chunkDurationsApproximate,

          items: relations.items,
          trackLinks: relations.trackLinks,
          trackOtherArtistLinks: relations.trackOtherArtistLinks,
          trackDurations: data.trackDurations,
          trackContributions: data.trackContributions,
          trackRereleases: data.trackRereleases,
        }).map(({
            chunk,
            albumLink,
            date,
            duration,
            durationApproximate,

            items,
            trackLinks,
            trackOtherArtistLinks,
            trackDurations,
            trackContributions,
            trackRereleases,
          }) =>
            chunk.slots({
              mode: 'album',
              albumLink,
              date,
              duration,
              durationApproximate,

              items:
                stitchArrays({
                  item: items,
                  trackLink: trackLinks,
                  otherArtistLinks: trackOtherArtistLinks,
                  duration: trackDurations,
                  contribution: trackContributions,
                  rerelease: trackRereleases,
                }).map(({
                    item,
                    trackLink,
                    otherArtistLinks,
                    duration,
                    contribution,
                    rerelease,
                  }) =>
                    item.slots({
                      otherArtistLinks,
                      rerelease,

                      annotation:
                        (contribution
                          ? language.formatUnitList(contribution)
                          : html.blank()),

                      content:
                        (duration
                          ? language.$('artistPage.creditList.entry.track.withDuration', {
                              track: trackLink,
                              duration: language.formatDuration(duration),
                            })
                          : language.$('artistPage.creditList.entry.track', {
                              track: trackLink,
                            })),
                    })),
            })),
    });
  },
};