-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters