Skip to content

Commit

Permalink
v2.0.6b
Browse files Browse the repository at this point in the history
  • Loading branch information
ejnshtein committed Feb 2, 2019
1 parent 0186f79 commit b9abe3e
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 7 deletions.
24 changes: 22 additions & 2 deletions actions/chapter-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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}`
}
14 changes: 11 additions & 3 deletions actions/chapter-page.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -40,14 +48,14 @@ 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',
reply_markup: {
inline_keyboard: keyboard
}
})
setCurrentlyReading(chapter.manga_id, chapterId, ctx.state.user)
})

module.exports = app => {
Expand Down
1 change: 1 addition & 0 deletions actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ module.exports = app => {
require('./chapter-page')(app)
require('./chapter-list')(app)
require('./delete')(app)
require('./mark-as-read')(app)
}
39 changes: 39 additions & 0 deletions actions/mark-as-read.js
Original file line number Diff line number Diff line change
@@ -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'
}
38 changes: 38 additions & 0 deletions core/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
26 changes: 26 additions & 0 deletions lib/set-currenly-reading.js
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit b9abe3e

Please sign in to comment.