« get me outta code hell

artist.js « things « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things/artist.js
blob: 8ac370c2d6b2238cd9f04ace2a1b184fbd728fd1 (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
294
295
296
297
298
299
300
301
302
export const ARTIST_DATA_FILE = 'artists.yaml';

import {inspect} from 'node:util';

import CacheableObject from '#cacheable-object';
import {colors} from '#cli';
import {input} from '#composite';
import find from '#find';
import {sortAlphabetically} from '#sort';
import {stitchArrays, unique} from '#sugar';
import Thing from '#thing';
import {isName, validateArrayItems} from '#validators';
import {getKebabCase} from '#wiki-data';

import {exposeDependency} from '#composite/control-flow';
import {withReverseContributionList} from '#composite/wiki-data';

import {
  contentString,
  directory,
  fileExtension,
  flag,
  name,
  reverseContributionList,
  reverseReferenceList,
  singleReference,
  urls,
  wikiData,
} from '#composite/wiki-properties';

import {artistTotalDuration} from '#composite/things/artist';

export class Artist extends Thing {
  static [Thing.referenceType] = 'artist';
  static [Thing.wikiDataArray] = 'artistData';

  static [Thing.getPropertyDescriptors] = ({Album, Flash, Group, Track}) => ({
    // Update & expose

    name: name('Unnamed Artist'),
    directory: directory(),
    urls: urls(),

    contextNotes: contentString(),

    hasAvatar: flag(false),
    avatarFileExtension: fileExtension('jpg'),

    aliasNames: {
      flags: {update: true, expose: true},
      update: {validate: validateArrayItems(isName)},
      expose: {transform: (names) => names ?? []},
    },

    isAlias: flag(),

    aliasedArtist: singleReference({
      class: input.value(Artist),
      find: input.value(find.artist),
      data: 'artistData',
    }),

    // Update only

    albumData: wikiData({
      class: input.value(Album),
    }),

    artistData: wikiData({
      class: input.value(Artist),
    }),

    flashData: wikiData({
      class: input.value(Flash),
    }),

    groupData: wikiData({
      class: input.value(Group),
    }),

    trackData: wikiData({
      class: input.value(Track),
    }),

    // Expose only

    trackArtistContributions: reverseContributionList({
      data: 'trackData',
      list: input.value('artistContribs'),
    }),

    trackContributorContributions: reverseContributionList({
      data: 'trackData',
      list: input.value('contributorContribs'),
    }),

    trackCoverArtistContributions: reverseContributionList({
      data: 'trackData',
      list: input.value('coverArtistContribs'),
    }),

    tracksAsCommentator: reverseReferenceList({
      data: 'trackData',
      list: input.value('commentatorArtists'),
    }),

    albumArtistContributions: reverseContributionList({
      data: 'albumData',
      list: input.value('artistContribs'),
    }),

    albumCoverArtistContributions: reverseContributionList({
      data: 'albumData',
      list: input.value('coverArtistContribs'),
    }),

    albumWallpaperArtistContributions: reverseContributionList({
      data: 'albumData',
      list: input.value('wallpaperArtistContribs'),
    }),

    albumBannerArtistContributions: reverseContributionList({
      data: 'albumData',
      list: input.value('bannerArtistContribs'),
    }),

    albumsAsCommentator: reverseReferenceList({
      data: 'albumData',
      list: input.value('commentatorArtists'),
    }),

    flashContributorContributions: reverseContributionList({
      data: 'flashData',
      list: input.value('contributorContribs'),
    }),

    flashesAsCommentator: reverseReferenceList({
      data: 'flashData',
      list: input.value('commentatorArtists'),
    }),

    closelyLinkedGroups: reverseReferenceList({
      data: 'groupData',
      list: input.value('closelyLinkedArtists'),
    }),

    totalDuration: artistTotalDuration(),
  });

  static [Thing.getSerializeDescriptors] = ({
    serialize: S,
  }) => ({
    name: S.id,
    directory: S.id,
    urls: S.id,
    contextNotes: S.id,

    hasAvatar: S.id,
    avatarFileExtension: S.id,

    aliasNames: S.id,

    tracksAsCommentator: S.toRefs,
    albumsAsCommentator: S.toRefs,
  });

  static [Thing.findSpecs] = {
    artist: {
      referenceTypes: ['artist', 'artist-gallery'],
      bindTo: 'artistData',

      include: artist => !artist.isAlias,
    },

    artistAlias: {
      referenceTypes: ['artist', 'artist-gallery'],
      bindTo: 'artistData',

      include: artist => artist.isAlias,

      getMatchableDirectories(artist) {
        const originalArtist = artist.aliasedArtist;

        // Aliases never match by the same directory as the original.
        if (artist.directory === originalArtist.directory) {
          return [];
        }

        // Aliases never match by the same directory as some *previous* alias
        // in the original's alias list. This is honestly a bit awkward, but it
        // avoids artist aliases conflicting with each other when checking for
        // duplicate directories.
        for (const aliasName of originalArtist.aliasNames) {
          // These are trouble. We should be accessing aliases' directories
          // directly, but artists currently don't expose a reverse reference
          // list for aliases. (This is pending a cleanup of "reverse reference"
          // behavior in general.) It doesn't actually cause problems *here*
          // because alias directories are computed from their names 100% of the
          // time, but that *is* an assumption this code makes.
          if (aliasName === artist.name) continue;
          if (artist.directory === getKebabCase(aliasName)) {
            return [];
          }
        }

        // And, aliases never return just a blank string. This part is pretty
        // spooky because it doesn't handle two differently named aliases, on
        // different artists, who have names that are similar *apart* from a
        // character that's shortened. But that's also so fundamentally scary
        // that we can't support it properly with existing code, anyway - we
        // would need to be able to specifically set a directory *on an alias,*
        // which currently can't be done in YAML data files.
        if (artist.directory === '') {
          return [];
        }

        return [artist.directory];
      },
    },
  };

  static [Thing.yamlDocumentSpec] = {
    fields: {
      'Artist': {property: 'name'},
      'Directory': {property: 'directory'},
      'URLs': {property: 'urls'},
      'Context Notes': {property: 'contextNotes'},

      'Has Avatar': {property: 'hasAvatar'},
      'Avatar File Extension': {property: 'avatarFileExtension'},

      'Aliases': {property: 'aliasNames'},

      'Dead URLs': {ignore: true},

      'Review Points': {ignore: true},
    },
  };

  static [Thing.getYamlLoadingSpec] = ({
    documentModes: {allInOne},
    thingConstructors: {Artist},
  }) => ({
    title: `Process artists file`,
    file: ARTIST_DATA_FILE,

    documentMode: allInOne,
    documentThing: Artist,

    save(results) {
      const artists = results;

      const artistRefs =
        artists.map(artist => Thing.getReference(artist));

      const artistAliasNames =
        artists.map(artist => artist.aliasNames);

      const artistAliases =
        stitchArrays({
          originalArtistRef: artistRefs,
          aliasNames: artistAliasNames,
        }).flatMap(({originalArtistRef, aliasNames}) =>
            aliasNames.map(name => {
              const alias = new Artist();
              alias.name = name;
              alias.isAlias = true;
              alias.aliasedArtist = originalArtistRef;
              return alias;
            }));

      const artistData = [...artists, ...artistAliases];

      return {artistData};
    },

    sort({artistData}) {
      sortAlphabetically(artistData);
    },
  });

  [inspect.custom]() {
    const parts = [];

    parts.push(Thing.prototype[inspect.custom].apply(this));

    if (CacheableObject.getUpdateValue(this, 'isAlias')) {
      parts.unshift(`${colors.yellow('[alias]')} `);

      let aliasedArtist;
      try {
        aliasedArtist = this.aliasedArtist.name;
      } catch (_error) {
        aliasedArtist = CacheableObject.getUpdateValue(this, 'aliasedArtist');
      }

      parts.push(` ${colors.yellow(`[of ${aliasedArtist}]`)}`);
    }

    return parts.join('');
  }
}