Compare commits

...

3 Commits

4 changed files with 75 additions and 12 deletions

View File

@ -37,7 +37,7 @@ export class ShelvesController extends ViewController {
}
getTargetShelf () {
const target = this.targetShelf + (this.targetDomain !== null ? `/${this.targetDomain}` : '');
const target = this.targetShelf + '/' + (this.targetDomain !== null ? `${this.targetDomain}` : '');
return fetch('/api/shelf/get/' + target).then(response => response.json()).then(shelf => {
this.state.loadedShelves[this.targetShelf] = shelf;
});

View File

@ -67,7 +67,12 @@ export const shelfView = (shelvesController, emit) => {
<div class="flex two">
<div class="two-third three-fourth-700">
<h2>${shelf.name}</h2>
<span>${__('shelves.owned_by')} ${shelf.user === null ? __('shelves.you') : `<a href="/profile?user=${shelf.user.handle}" title=${shelf.user.handle}>${shelf.user.name}</a>`}</span>
<span>
${__('shelves.owned_by')}
${shelf.user === null
? __('shelves.you')
: `<a href="/profile?user=${shelf.user.handle}" title=${shelf.user.handle}>${shelf.user.name}</a>`}
</span>
</div>
<div class="third sixth-700">
<button class="pseudo" onclick=${() => {
@ -86,7 +91,7 @@ export const shelfView = (shelvesController, emit) => {
<img src=${shelfItem.coverURL} alt="cover ${shelfItem.coverEdition}" />
</div>
<div class="full half-700">
<h3>${shelfItem.name}</h3>
<h3>${shelfItem.title}</h3>
<span>${shelfItem.author}</span>
</div>
<div class="full third-700">
@ -94,7 +99,7 @@ export const shelfView = (shelvesController, emit) => {
${shelfItem.review !== null
? modal(`itemModal${index}`, shelvesController, html`<article>${shelfItem.review}</article>`, {
buttonText: 'My Review',
headerText: `${__('review.review_of')} ${shelfItem.name}`,
headerText: `${__('review.review_of')} ${shelfItem.title}`,
})
: null}
</div>

View File

@ -1,3 +1,5 @@
import fetch from 'node-fetch';
class ShelfController {
constructor (shelfModel, shelfItemModel) {
this.model = shelfModel;
@ -22,6 +24,12 @@ class ShelfController {
return true;
}
async static CheckExternalDomainForShelf (domain, shelfId) {
const response = await fetch(`https://${domain}/api/shelf/get/${shelfId}/`).then(response => response.json());
// validate response somehow
return response;
}
async createDefaultShelves (user) {
try {
const defaultShelvesCreated = await this.model.bulkCreate([
@ -187,18 +195,58 @@ class ShelfController {
return userOwnsShelf || shelf.isPublic;
}
async scrubShelfData (shelf, user) {
const userOwnsShelf = user.id === shelf.userId;
const shelfUser = userOwnsShelf ? null : shelf.getUser();
let userData = {};
async scrubShelfData (shelf, currentUser) {
const userOwnsShelf = currentUser.id === shelf.userId;
const shelfUser = userOwnsShelf ? null : await shelf.getUser({ attributes: ['username, displayName'] });
let user = {};
if (shelfUser !== null) {
userData.name = shelfUser.displayName;
userData.handle = `@${shelfUser.username}`;
user.name = shelfUser.displayName;
user.handle = shelfUser.username;
} else {
user = null;
}
const rawShelfItems = await shelf.getShelfItems({
attributes: ['bookId', 'createdAt', 'updatedAt'],
order: [['order', 'ASC']],
})
const bookReferenceMap = await Promise.all(shelfItems.map(shelfItem => {
return shelfItem.getBookReference({
attributes: ['name', 'description', 'sources', 'covers'],
});
}));
const bookReferenceStatuses = await Promise.all(bookReferenceMap.map(bookReference => {
return bookReference.getStatuses({
where: {
userId: shelf.userId,
},
order: [['updatedAt', 'DESC']],
});
}));
const bookReferences = bookReferenceMap.map(bookReference => {
bookReference.updates = bookReferenceStatuses.findAll(status => status.type === 1);
bookReference.rating = bookReferenceStatuses.find(status => status.type === 3);
bookReference.review = bookReferenceStatuses.find(status => status.type === 4);
return bookReference;
});
const shelfItems = rawShelfItems.map(shelfItem => {
const bookReference = bookReferences.find(bookData => bookData.id === shelfItem.bookId);
shelfItem.title = bookReference.name;
shelfItem.author = bookReference.description;
shelfItem.sources = bookReference.sources;
shelfItem.covers = bookReference.covers;
shelfItem.updates = bookReference.updates;
shelfItem.rating = bookReference.rating.data;
shelfItem.review = bookReference.review.data;
return shelfItem;
})
const shelfData = {
name: shelf.name,
user: userOwnsShelf ? null : shelf.getUser(),
}
user,
shelfItems,
};
return shelfData;
}
}

View File

@ -34,6 +34,16 @@ async function routes(fastify, options) {
message: 'api.shelf.get.missing_id',
});
}
if (isNaN(parseInt(request.params.shelfId))) {
return reply.code(400).send({
error: true,
message: 'api.shelf.get.invalid_id',
});
}
if (request.params.domain.trim() !== '') {
return ShelfController.CheckExternalDomainForShelf(request.params.domain.trim(), request.params.shelfId);
}
const shelfController = new ShelfController(fastify.models.Shelf, fastify.models.ShelfItem);