AuthorPage.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const BasePage = require('./BasePage');
  2. class AuthorPage extends BasePage {
  3. constructor(config) {
  4. super(config);
  5. this.id = 'author';
  6. this.title = 'Авторы';
  7. }
  8. sortBooks(bookList) {
  9. //схлопывание серий
  10. const books = [];
  11. const seriesSet = new Set();
  12. for (const book of bookList) {
  13. if (book.series) {
  14. if (!seriesSet.has(book.series)) {
  15. books.push({
  16. type: 'series',
  17. book
  18. });
  19. seriesSet.add(book.series);
  20. }
  21. } else {
  22. books.push({
  23. type: 'book',
  24. book
  25. });
  26. }
  27. }
  28. //сортировка
  29. books.sort((a, b) => {
  30. if (a.type == 'series') {
  31. return (b.type == 'series' ? a.book.series.localeCompare(b.book.series) : -1);
  32. } else {
  33. return (b.type == 'book' ? a.book.title.localeCompare(b.book.title) : 1);
  34. }
  35. });
  36. return books;
  37. }
  38. sortSeriesBooks(seriesBooks) {
  39. seriesBooks.sort((a, b) => {
  40. const dserno = (a.serno || Number.MAX_VALUE) - (b.serno || Number.MAX_VALUE);
  41. const dtitle = a.title.localeCompare(b.title);
  42. const dext = a.ext.localeCompare(b.ext);
  43. return (dserno ? dserno : (dtitle ? dtitle : dext));
  44. });
  45. return seriesBooks;
  46. }
  47. async body(req) {
  48. const result = {};
  49. const query = {
  50. author: req.query.author || '',
  51. series: req.query.series || '',
  52. genre: req.query.genre || '',
  53. del: 0,
  54. all: req.query.all || '',
  55. depth: 0,
  56. };
  57. query.depth = query.author.length + 1;
  58. if (query.author == '___others') {
  59. query.author = '';
  60. query.depth = 1;
  61. query.others = true;
  62. }
  63. const entry = [];
  64. if (query.series) {
  65. //книги по серии
  66. const bookList = await this.webWorker.getSeriesBookList(query.series);
  67. if (bookList.books) {
  68. let books = JSON.parse(bookList.books);
  69. const booksAll = this.filterBooks(books, {del: 0});
  70. const filtered = (query.all ? booksAll : this.filterBooks(books, query));
  71. const sorted = this.sortSeriesBooks(filtered);
  72. if (booksAll.length > filtered.length) {
  73. entry.push(
  74. this.makeEntry({
  75. id: 'all_series_books',
  76. title: '[Все книги серии]',
  77. link: this.navLink({
  78. href: `/${this.id}?author=${encodeURIComponent(query.author)}` +
  79. `&series=${encodeURIComponent(query.series)}&all=1`}),
  80. })
  81. );
  82. }
  83. for (const book of sorted) {
  84. const title = `${book.serno ? `${book.serno}. `: ''}${book.title || 'Без названия'} (${book.ext})`;
  85. const e = {
  86. id: book._uid,
  87. title,
  88. link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
  89. };
  90. if (query.all) {
  91. e.content = {
  92. '*ATTRS': {type: 'text'},
  93. '*TEXT': this.bookAuthor(book.author),
  94. }
  95. }
  96. entry.push(
  97. this.makeEntry(e)
  98. );
  99. }
  100. }
  101. } else if (query.author && query.author[0] == '=') {
  102. //книги по автору
  103. const bookList = await this.webWorker.getAuthorBookList(0, query.author.substring(1));
  104. if (bookList.books) {
  105. let books = JSON.parse(bookList.books);
  106. books = this.sortBooks(this.filterBooks(books, query));
  107. for (const b of books) {
  108. if (b.type == 'series') {
  109. entry.push(
  110. this.makeEntry({
  111. id: b.book._uid,
  112. title: `Серия: ${b.book.series}`,
  113. link: this.navLink({
  114. href: `/${this.id}?author=${encodeURIComponent(query.author)}` +
  115. `&series=${encodeURIComponent(b.book.series)}&genre=${encodeURIComponent(query.genre)}`}),
  116. })
  117. );
  118. } else {
  119. const title = `${b.book.title || 'Без названия'} (${b.book.ext})`;
  120. entry.push(
  121. this.makeEntry({
  122. id: b.book._uid,
  123. title,
  124. link: this.acqLink({href: `/book?uid=${encodeURIComponent(b.book._uid)}`}),
  125. })
  126. );
  127. }
  128. }
  129. }
  130. } else {
  131. if (query.depth == 1 && !query.genre && !query.others) {
  132. entry.push(
  133. this.makeEntry({
  134. id: 'select_genre',
  135. title: '[Выбрать жанр]',
  136. link: this.navLink({href: `/genre?from=${this.id}`}),
  137. })
  138. );
  139. }
  140. //навигация по каталогу
  141. const queryRes = await this.opdsQuery('author', query, '[Остальные авторы]');
  142. for (const rec of queryRes) {
  143. entry.push(
  144. this.makeEntry({
  145. id: rec.id,
  146. title: this.bookAuthor(rec.title),
  147. link: this.navLink({href: `/${this.id}?author=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
  148. })
  149. );
  150. }
  151. }
  152. result.entry = entry;
  153. return this.makeBody(result, req);
  154. }
  155. }
  156. module.exports = AuthorPage;