From b9abe3ee6bf330e0389e650bb9b8fcae0e2dd976 Mon Sep 17 00:00:00 2001 From: Okabe Rintaro Date: Sat, 2 Feb 2019 22:01:52 +0200 Subject: [PATCH] v2.0.6b --- actions/chapter-list.js | 24 +++++++++++++++++++++-- actions/chapter-page.js | 14 ++++++++++--- actions/index.js | 1 + actions/mark-as-read.js | 39 +++++++++++++++++++++++++++++++++++++ core/database/index.js | 38 ++++++++++++++++++++++++++++++++++++ lib/index.js | 3 ++- lib/set-currenly-reading.js | 26 +++++++++++++++++++++++++ package.json | 2 +- 8 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 actions/mark-as-read.js create mode 100644 lib/set-currenly-reading.js diff --git a/actions/chapter-list.js b/actions/chapter-list.js index 342f725..d80a401 100644 --- a/actions/chapter-list.js +++ b/actions/chapter-list.js @@ -10,6 +10,10 @@ composer.action(/chapterlist=(\S+):id=(\S+):offset=(\S+?):(\S+)/i, async ctx => const offset = Number.parseInt(ctx.match[3]) const history = ctx.match[4] + const { user } = ctx.state + + const alreadyRead = user.already_read && user.already_read.map(el => el.chapter_id) + const { chapter, manga } = await getManga(mangaId) const chapters = chapter @@ -24,8 +28,8 @@ composer.action(/chapterlist=(\S+):id=(\S+):offset=(\S+?):(\S+)/i, async ctx => for (let chapterId = 0; chapterId < slicedChapters.length; chapterId++) { const chapter = slicedChapters[chapterId] const button = { - text: `${cachedChapters.some(el => el.id === chapter.id) ? '🗲 ' : ''}${chapter.volume ? `Vol. ${chapter.volume} ` : ''}Ch. ${chapter.chapter}`, - callback_data: `chapter=${chapter.id}:prev=${slicedChapters[chapterId - 1] ? slicedChapters[chapterId - 1].id : 'null'}:next=${slicedChapters[chapterId + 1] ? slicedChapters[chapterId + 1].id : 'null'}:offset=${offset}:${history}` + text: `${alreadyRead ? alreadyRead.includes(chapter.id) ? '👁 ' : '' : ''}${cachedChapters.some(el => el.id === chapter.id) ? '🗲 ' : ''}${chapter.chapter ? `${chapter.volume ? `Vol. ${chapter.volume} ` : ''}Ch. ${chapter.chapter}` : chapter.title}${chapters.some(el => el.chapter === chapter.chapter && el.id !== chapter.id) ? getGroupName(chapter) : ''}`, + callback_data: `chapter=${chapter.id}:read=${alreadyRead ? alreadyRead.includes(chapter.id) ? 'true' : 'false' : 'false'}:next=${slicedChapters[chapterId + 1] ? slicedChapters[chapterId + 1].id : 'null'}:offset=${offset}:${history}` } if (keyboard[keyboard.length - 1].length < 2) { keyboard[keyboard.length - 1].push(button) @@ -80,3 +84,19 @@ composer.action(/chapterlist=(\S+):id=(\S+):offset=(\S+?):(\S+)/i, async ctx => module.exports = app => { app.use(composer.middleware()) } + +function getGroupName (chapter) { + return ` by ${chapter.group_name + ? chapter.group_name + : chapter.group_name_2 + ? chapter.group_name_2 + : chapter.group_name_3 + ? chapter.group_name_3 + : chapter.group_id + ? chapter.group_id + : chapter.group_id_2 + ? chapter.group_id_2 + : chapter.group_id_3 + ? chapter.group_id_3 + : chapter.id}` +} diff --git a/actions/chapter-page.js b/actions/chapter-page.js index f819338..ca60d88 100644 --- a/actions/chapter-page.js +++ b/actions/chapter-page.js @@ -1,11 +1,15 @@ const Composer = require('telegraf/composer') const composer = new Composer() const { getChapter, getManga } = require('mangadex-api').default -const { templates } = require('../lib') +const { templates, setCurrentlyReading } = require('../lib') const getFiles = require('../lib/get-files') -composer.action(/chapter=(\S+):prev=(\S+):next=(\S+):offset=(\S+?):(\S+)/i, async ctx => { +composer.action([ + /chapter=(\S+):prev=(\S+):next=(\S+):offset=(\S+?):(\S+)/i, + /chapter=(\S+):read=(\S+):next=(\S+):offset=(\S+?):(\S+)/i +], async ctx => { const chapterId = ctx.match[1] + const markedRead = ctx.match[2] === 'true' const offset = ctx.match[4] const history = ctx.match[5] let chapter = await getChapter(chapterId) @@ -14,6 +18,10 @@ composer.action(/chapter=(\S+):prev=(\S+):next=(\S+):offset=(\S+?):(\S+)/i, asyn if (!chapter) { return } const keyboard = [ [ + { + text: markedRead ? 'Mark unread' : 'Mark read', + callback_data: `read:${chapterId}` + }, { text: 'Desktop Instant View', url: chapter.telegraph @@ -40,7 +48,6 @@ composer.action(/chapter=(\S+):prev=(\S+):next=(\S+):offset=(\S+?):(\S+)/i, asyn } ] : undefined ].filter(Boolean) - // console.log(ctx.callbackQuery.message) const messageText = templates.manga.chapter(chapter, ctx.callbackQuery.message) ctx.editMessageText(messageText, { parse_mode: 'HTML', @@ -48,6 +55,7 @@ composer.action(/chapter=(\S+):prev=(\S+):next=(\S+):offset=(\S+?):(\S+)/i, asyn inline_keyboard: keyboard } }) + setCurrentlyReading(chapter.manga_id, chapterId, ctx.state.user) }) module.exports = app => { diff --git a/actions/index.js b/actions/index.js index 0d4bd42..ee45059 100644 --- a/actions/index.js +++ b/actions/index.js @@ -5,4 +5,5 @@ module.exports = app => { require('./chapter-page')(app) require('./chapter-list')(app) require('./delete')(app) + require('./mark-as-read')(app) } diff --git a/actions/mark-as-read.js b/actions/mark-as-read.js new file mode 100644 index 0000000..40a99e7 --- /dev/null +++ b/actions/mark-as-read.js @@ -0,0 +1,39 @@ +const Composer = require('telegraf/composer') +const composer = new Composer() + +composer.action(/read:([0-9]+)/i, async ctx => { + const result = await markRead(ctx.state.user, ctx.match[1]) + ctx.answerCbQuery(result) +}) + +module.exports = app => { + app.use(composer.middleware()) +} + +async function markRead (user, chapterId) { + chapterId = typeof chapterId !== 'number' ? Number.parseInt(chapterId) : chapterId + if (user.already_read) { + if (user.already_read.some(el => el.chapter_id === chapterId)) { + const chapter = user.already_read.find(el => el.chapter_id === chapterId) + try { + await chapter.remove() + } catch (e) { + return 'Something went wrong...' + } + user.markModified('already_read') + await user.save() + return 'Chapter unmarked as read' + } else { + user.already_read.push({ + chapter_id: chapterId + }) + } + } else { + user.already_read.create({ + chapter_id: chapterId + }) + } + user.markModified('already_read') + await user.save() + return 'Chapter marked as read' +} diff --git a/core/database/index.js b/core/database/index.js index 6197488..eac7e56 100644 --- a/core/database/index.js +++ b/core/database/index.js @@ -49,6 +49,44 @@ const collections = [ ], default: [], required: false + }, + currently_reading: { + type: [ + new Schema({ + manga_id: { + type: Number, + unique: true, + required: true + }, + chapter_id: { + type: Number, + required: true + } + }, { + timestamps: { + createdAt: 'created_at', + updatedAt: 'updated_at' + } + }) + ], + required: true, + default: [] + }, + already_read: { + type: [ + new Schema({ + chapter_id: { + type: Number + } + }, { + timestamps: { + createdAt: 'created_at', + updatedAt: 'updated_at' + } + }) + ], + required: true, + default: [] } }, { timestamps: { diff --git a/lib/index.js b/lib/index.js index 73c5f16..7e723a9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,5 +9,6 @@ module.exports = { getStatus: require('./get-status'), groupBy: require('./group-by'), buffer: require('./buffer'), - storeHistory: require('./store-history') + storeHistory: require('./store-history'), + setCurrentlyReading: require('./set-currenly-reading') } diff --git a/lib/set-currenly-reading.js b/lib/set-currenly-reading.js new file mode 100644 index 0000000..945f1aa --- /dev/null +++ b/lib/set-currenly-reading.js @@ -0,0 +1,26 @@ +module.exports = (mangaId, chapterId, user) => { + chapterId = typeof chapterId !== 'number' ? Number.parseInt(chapterId) : chapterId + mangaId = typeof mangaId !== 'number' ? Number.parseInt(mangaId) : mangaId + if (user.currently_reading) { + if (user.currently_reading.some(el => el.manga_id === mangaId)) { + const chapter = user.currently_reading.find(el => el.manga_id === mangaId) + if (chapter.chapter_id !== chapterId) { + chapter.chapter_id = chapterId + } else { + chapter.updated_at = Date.now() + } + } else { + user.currently_reading.push({ + manga_id: mangaId, + chapter_id: chapterId + }) + } + } else { + user.currently_reading = [{ + manga_id: mangaId, + chapter_id: chapterId + }] + } + user.markModified('currently_reading') + return user.save() +} diff --git a/package.json b/package.json index faeaa8d..41b2d55 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mangadex_bot", - "version": "2.0.5", + "version": "2.0.6b", "description": "telegram-bot for reading manga on mangadex right in telegram", "main": "app.js", "dependencies": {