Skip to content

Commit f91a26b

Browse files
authored
fix(servarr): replace spaces in arr user tags with - (#2231)
* fix: sanitize disallowed characters in arr tags Updates the tag creation to normalize diacritics, replace spaces with hyphens and stip any non-alphanumeric characters from display name fix #2229, fix #1897 * refactor: improve display name sanitization in tag creation * fix: include displayName in user selection for tag migration * fix(migrator): retrieve all user fields in tag migration This is a one time migration so performance is neglible. This should trigger the @afterload hooks which sets the `displayName`
1 parent 0c95b5e commit f91a26b

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

server/lib/settings/migrations/0007_migrate_arr_tags.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ const migrationArrTags = async (settings: any): Promise<AllSettings> => {
1313
}
1414

1515
const userRepository = getRepository(User);
16-
const users = await userRepository.find({
17-
select: ['id'],
18-
});
16+
const users = await userRepository.find();
1917

2018
let errorOccurred = false;
2119

@@ -30,15 +28,26 @@ const migrationArrTags = async (settings: any): Promise<AllSettings> => {
3028
});
3129
const radarrTags = await radarr.getTags();
3230
for (const user of users) {
33-
const userTag = radarrTags.find((v) =>
34-
v.label.startsWith(user.id + ' - ')
31+
const userTag = radarrTags.find(
32+
(v) =>
33+
v.label.startsWith(user.id + ' - ') ||
34+
v.label.startsWith(user.id + '-')
3535
);
3636
if (!userTag) {
3737
continue;
3838
}
3939
await radarr.renameTag({
4040
id: userTag.id,
41-
label: userTag.label.replace(`${user.id} - `, `${user.id}-`),
41+
label:
42+
user.id +
43+
'-' +
44+
user.displayName
45+
.normalize('NFD')
46+
.replace(/[\u0300-\u036f]/g, '')
47+
.replace(/\s+/g, '-')
48+
.replace(/[^a-z0-9-]/gi, '')
49+
.replace(/-+/g, '-')
50+
.replace(/^-|-$/g, ''),
4251
});
4352
}
4453
} catch (error) {
@@ -61,15 +70,26 @@ const migrationArrTags = async (settings: any): Promise<AllSettings> => {
6170
});
6271
const sonarrTags = await sonarr.getTags();
6372
for (const user of users) {
64-
const userTag = sonarrTags.find((v) =>
65-
v.label.startsWith(user.id + ' - ')
73+
const userTag = sonarrTags.find(
74+
(v) =>
75+
v.label.startsWith(user.id + ' - ') ||
76+
v.label.startsWith(user.id + '-')
6677
);
6778
if (!userTag) {
6879
continue;
6980
}
7081
await sonarr.renameTag({
7182
id: userTag.id,
72-
label: userTag.label.replace(`${user.id} - `, `${user.id}-`),
83+
label:
84+
user.id +
85+
'-' +
86+
user.displayName
87+
.normalize('NFD')
88+
.replace(/[\u0300-\u036f]/g, '')
89+
.replace(/\s+/g, '-')
90+
.replace(/[^a-z0-9-]/gi, '')
91+
.replace(/-+/g, '-')
92+
.replace(/^-|-$/g, ''),
7393
});
7494
}
7595
} catch (error) {

server/subscriber/MediaRequestSubscriber.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ import type {
2929
} from 'typeorm';
3030
import { EventSubscriber } from 'typeorm';
3131

32+
const sanitizeDisplayName = (displayName: string): string => {
33+
return displayName
34+
.normalize('NFD')
35+
.replace(/[\u0300-\u036f]/g, '')
36+
.replace(/\s+/g, '-')
37+
.replace(/[^a-z0-9-]/gi, '')
38+
.replace(/-+/g, '-')
39+
.replace(/^-|-$/g, '');
40+
};
41+
3242
@EventSubscriber()
3343
export class MediaRequestSubscriber
3444
implements EntitySubscriberInterface<MediaRequest>
@@ -310,11 +320,15 @@ export class MediaRequestSubscriber
310320
mediaId: entity.media.id,
311321
userId: entity.requestedBy.id,
312322
newTag:
313-
entity.requestedBy.id + '-' + entity.requestedBy.displayName,
323+
entity.requestedBy.id +
324+
'-' +
325+
sanitizeDisplayName(entity.requestedBy.displayName),
314326
});
315327
userTag = await radarr.createTag({
316328
label:
317-
entity.requestedBy.id + '-' + entity.requestedBy.displayName,
329+
entity.requestedBy.id +
330+
'-' +
331+
sanitizeDisplayName(entity.requestedBy.displayName),
318332
});
319333
}
320334
if (userTag.id) {
@@ -631,11 +645,15 @@ export class MediaRequestSubscriber
631645
mediaId: entity.media.id,
632646
userId: entity.requestedBy.id,
633647
newTag:
634-
entity.requestedBy.id + '-' + entity.requestedBy.displayName,
648+
entity.requestedBy.id +
649+
'-' +
650+
sanitizeDisplayName(entity.requestedBy.displayName),
635651
});
636652
userTag = await sonarr.createTag({
637653
label:
638-
entity.requestedBy.id + '-' + entity.requestedBy.displayName,
654+
entity.requestedBy.id +
655+
'-' +
656+
sanitizeDisplayName(entity.requestedBy.displayName),
639657
});
640658
}
641659
if (userTag.id) {

0 commit comments

Comments
 (0)