mirror of
https://github.com/p-stream/p-stream.git
synced 2026-03-11 17:55:33 +00:00
fix groups (collections) not syncing correctly
This commit is contained in:
parent
3b4b891535
commit
1c05f99b3e
2 changed files with 38 additions and 8 deletions
|
|
@ -29,6 +29,8 @@ export interface BookmarkUpdateItem {
|
||||||
poster?: string;
|
poster?: string;
|
||||||
type?: "show" | "movie";
|
type?: "show" | "movie";
|
||||||
group?: string[];
|
group?: string[];
|
||||||
|
/** Groups before modification - used to sync removals to Trakt lists */
|
||||||
|
previousGroup?: string[];
|
||||||
favoriteEpisodes?: string[];
|
favoriteEpisodes?: string[];
|
||||||
action: "delete" | "add";
|
action: "delete" | "add";
|
||||||
}
|
}
|
||||||
|
|
@ -115,6 +117,7 @@ export const useBookmarkStore = create(
|
||||||
},
|
},
|
||||||
addBookmarkWithGroups(meta, groups) {
|
addBookmarkWithGroups(meta, groups) {
|
||||||
set((s) => {
|
set((s) => {
|
||||||
|
const existingBookmark = s.bookmarks[meta.tmdbId];
|
||||||
updateId += 1;
|
updateId += 1;
|
||||||
const item: BookmarkUpdateItem = {
|
const item: BookmarkUpdateItem = {
|
||||||
id: updateId.toString(),
|
id: updateId.toString(),
|
||||||
|
|
@ -125,6 +128,7 @@ export const useBookmarkStore = create(
|
||||||
year: meta.releaseYear,
|
year: meta.releaseYear,
|
||||||
poster: meta.poster,
|
poster: meta.poster,
|
||||||
group: groups,
|
group: groups,
|
||||||
|
previousGroup: existingBookmark?.group,
|
||||||
};
|
};
|
||||||
s.updateQueue.push(item);
|
s.updateQueue.push(item);
|
||||||
s.traktUpdateQueue.push(item);
|
s.traktUpdateQueue.push(item);
|
||||||
|
|
@ -245,13 +249,13 @@ export const useBookmarkStore = create(
|
||||||
set((s) => {
|
set((s) => {
|
||||||
const { modifiedBookmarks, result: modificationResult } =
|
const { modifiedBookmarks, result: modificationResult } =
|
||||||
modifyBookmarks(s.bookmarks, bookmarkIds, options);
|
modifyBookmarks(s.bookmarks, bookmarkIds, options);
|
||||||
s.bookmarks = modifiedBookmarks;
|
|
||||||
result = modificationResult;
|
result = modificationResult;
|
||||||
|
|
||||||
// Add to update queue for modified bookmarks
|
// Add to update queue for modified bookmarks (capture previousGroup before overwriting)
|
||||||
if (result.hasChanges) {
|
if (result.hasChanges) {
|
||||||
result.modifiedIds.forEach((bookmarkId) => {
|
result.modifiedIds.forEach((bookmarkId) => {
|
||||||
const bookmark = s.bookmarks[bookmarkId];
|
const originalBookmark = s.bookmarks[bookmarkId];
|
||||||
|
const bookmark = modifiedBookmarks[bookmarkId];
|
||||||
if (bookmark) {
|
if (bookmark) {
|
||||||
updateId += 1;
|
updateId += 1;
|
||||||
const item: BookmarkUpdateItem = {
|
const item: BookmarkUpdateItem = {
|
||||||
|
|
@ -263,6 +267,7 @@ export const useBookmarkStore = create(
|
||||||
poster: bookmark.poster,
|
poster: bookmark.poster,
|
||||||
type: bookmark.type,
|
type: bookmark.type,
|
||||||
group: bookmark.group,
|
group: bookmark.group,
|
||||||
|
previousGroup: originalBookmark?.group,
|
||||||
favoriteEpisodes: bookmark.favoriteEpisodes,
|
favoriteEpisodes: bookmark.favoriteEpisodes,
|
||||||
};
|
};
|
||||||
s.updateQueue.push(item);
|
s.updateQueue.push(item);
|
||||||
|
|
@ -270,6 +275,8 @@ export const useBookmarkStore = create(
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.bookmarks = modifiedBookmarks;
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -285,13 +292,13 @@ export const useBookmarkStore = create(
|
||||||
set((s) => {
|
set((s) => {
|
||||||
const { modifiedBookmarks, result: modificationResult } =
|
const { modifiedBookmarks, result: modificationResult } =
|
||||||
modifyBookmarksByGroup(s.bookmarks, options);
|
modifyBookmarksByGroup(s.bookmarks, options);
|
||||||
s.bookmarks = modifiedBookmarks;
|
|
||||||
result = modificationResult;
|
result = modificationResult;
|
||||||
|
|
||||||
// Add to update queue for modified bookmarks
|
// Add to update queue for modified bookmarks (capture previousGroup before overwriting)
|
||||||
if (result.hasChanges) {
|
if (result.hasChanges) {
|
||||||
result.modifiedIds.forEach((bookmarkId) => {
|
result.modifiedIds.forEach((bookmarkId) => {
|
||||||
const bookmark = s.bookmarks[bookmarkId];
|
const originalBookmark = s.bookmarks[bookmarkId];
|
||||||
|
const bookmark = modifiedBookmarks[bookmarkId];
|
||||||
if (bookmark) {
|
if (bookmark) {
|
||||||
updateId += 1;
|
updateId += 1;
|
||||||
const item: BookmarkUpdateItem = {
|
const item: BookmarkUpdateItem = {
|
||||||
|
|
@ -303,6 +310,7 @@ export const useBookmarkStore = create(
|
||||||
poster: bookmark.poster,
|
poster: bookmark.poster,
|
||||||
type: bookmark.type,
|
type: bookmark.type,
|
||||||
group: bookmark.group,
|
group: bookmark.group,
|
||||||
|
previousGroup: originalBookmark?.group,
|
||||||
favoriteEpisodes: bookmark.favoriteEpisodes,
|
favoriteEpisodes: bookmark.favoriteEpisodes,
|
||||||
};
|
};
|
||||||
s.updateQueue.push(item);
|
s.updateQueue.push(item);
|
||||||
|
|
@ -310,6 +318,8 @@ export const useBookmarkStore = create(
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.bookmarks = modifiedBookmarks;
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -70,8 +70,28 @@ export function TraktBookmarkSyncer() {
|
||||||
|
|
||||||
if (item.action === "add") {
|
if (item.action === "add") {
|
||||||
await traktService.addToWatchlist(contentData);
|
await traktService.addToWatchlist(contentData);
|
||||||
if (hasLists && item.group?.length) {
|
if (hasLists) {
|
||||||
for (const groupName of item.group) {
|
const newGroups = item.group ?? [];
|
||||||
|
const prevGroups = item.previousGroup ?? [];
|
||||||
|
|
||||||
|
// Remove from Trakt lists that the bookmark no longer belongs to
|
||||||
|
const groupsToRemove = prevGroups.filter(
|
||||||
|
(g) => !newGroups.includes(g),
|
||||||
|
);
|
||||||
|
for (const groupName of groupsToRemove) {
|
||||||
|
const list = await findListByName(slug!, groupName);
|
||||||
|
if (list) {
|
||||||
|
await traktService.removeFromList(slug!, listId(list), [
|
||||||
|
contentData,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add to Trakt lists that are new
|
||||||
|
const groupsToAdd = newGroups.filter(
|
||||||
|
(g) => !prevGroups.includes(g),
|
||||||
|
);
|
||||||
|
for (const groupName of groupsToAdd) {
|
||||||
const list = await ensureListExists(slug!, groupName);
|
const list = await ensureListExists(slug!, groupName);
|
||||||
if (list) {
|
if (list) {
|
||||||
await traktService.addToList(slug!, listId(list), [
|
await traktService.addToList(slug!, listId(list), [
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue