@@ -112,7 +112,18 @@ const keyboards = {
112112 [
113113 Markup . callbackButton ( 'Back' , backActionName )
114114 ]
115- ] ) . extra ( )
115+ ] ) . extra ( ) ,
116+ //ToDo: pagination
117+ paginationTable : ( backActionName , actionName , items ) => Markup . inlineKeyboard ( [
118+ ...items . map ( ( item , index ) => [ Markup . callbackButton ( item , `${ actionName } :${ index } ` ) ] ) ,
119+ [
120+ Markup . callbackButton ( 'prev' , '' ) ,
121+ Markup . callbackButton ( 'next' , '' )
122+ ] ,
123+ [
124+ Markup . callbackButton ( 'Back' , backActionName )
125+ ]
126+ ] ) . extra ( ) ,
116127} ;
117128
118129
@@ -121,7 +132,9 @@ class Bot {
121132 this . bot = new Telegraf ( API_TOKEN ) ;
122133 this . db = db ;
123134
124- this . bot . use ( memorySession ( ) ) ;
135+ this . bot . use ( memorySession ( {
136+ getSessionKey : ( ctx ) => `${ ctx . chat && ctx . chat . id } `
137+ } ) ) ;
125138
126139 this . bot . telegram . getMe ( ) . then ( ( botInfo ) => {
127140 this . bot . options . username = botInfo . username ;
@@ -139,13 +152,14 @@ class Bot {
139152 this . bot . action ( 'addRepo' , this . addRepo . bind ( this ) ) ;
140153
141154 this . bot . action ( 'getReleases' , this . getReleases . bind ( this ) ) ;
142- this . bot . action ( / g e t R e l e a s e s : e x p a n d : ( .+ ) / , this . getReleasesExpanded . bind ( this ) ) ;
155+ this . bot . action ( / ^ g e t R e l e a s e s : e x p a n d : ( .+ ) $ / , this . getReleasesExpanded . bind ( this ) ) ;
143156 this . bot . action ( 'getReleases:all' , this . getReleasesAll . bind ( this ) ) ;
144157 this . bot . action ( 'getReleases:one' , this . getReleasesOne . bind ( this ) ) ;
145- this . bot . action ( / g e t R e l e a s e s : o n e : ( .+ ) / , this . getReleasesOneSelected . bind ( this ) ) ;
158+ this . bot . action ( / ^ g e t R e l e a s e s : o n e : ( \d + ) $ / , this . getReleasesOneRepo . bind ( this ) ) ;
159+ this . bot . action ( / ^ g e t R e l e a s e s : o n e : ( \d + ?) : r e l e a s e : ( \d + ?) $ / , this . getReleasesOneRepoRelease . bind ( this ) ) ;
146160
147161 this . bot . action ( 'editRepos' , this . editRepos . bind ( this ) ) ;
148- this . bot . action ( / e d i t R e p o s : d e l e t e : ( .+ ) / , this . editReposDelete . bind ( this ) ) ;
162+ this . bot . action ( / ^ e d i t R e p o s : d e l e t e : ( .+ ) $ / , this . editReposDelete . bind ( this ) ) ;
149163
150164 this . bot . hears ( / .+ / , this . handleAnswer . bind ( this ) ) ;
151165
@@ -296,7 +310,7 @@ class Bot {
296310 )
297311 }
298312
299- async getReleasesOneSelected ( ctx ) {
313+ async getReleasesOneRepo ( ctx ) {
300314 ctx . answerCallbackQuery ( '' ) ;
301315
302316 try {
@@ -307,12 +321,36 @@ class Bot {
307321
308322 const repo = await this . db . getRepo ( owner , name ) ;
309323
310- ctx . answerCallbackQuery ( '' ) ;
324+ return ctx . editMessageText (
325+ 'Select release' ,
326+ keyboards . table (
327+ `getReleases:one` ,
328+ `getReleases:one:${ index } :release` ,
329+ repo . releases . slice ( - 10 ) . map ( ( { name, isPrerelease} ) => `${ name } ${ isPrerelease ? ' (pre-release)' : '' } ` )
330+ )
331+ )
332+ }
333+ } catch ( error ) {
334+ return ctx . editMessageText ( 'Data is broken' ) ;
335+ }
336+ }
337+
338+ async getReleasesOneRepoRelease ( ctx ) {
339+ ctx . answerCallbackQuery ( '' ) ;
340+
341+ try {
342+ const repoIndex = parseInt ( ctx . match [ 1 ] ) ;
343+ const releaseIndex = parseInt ( ctx . match [ 2 ] ) ;
344+
345+ if ( ctx . session . subscriptions && ctx . session . subscriptions [ repoIndex ] ) {
346+ const { owner, name} = ctx . session . subscriptions [ repoIndex ] ;
347+
348+ const repo = await this . db . getRepo ( owner , name ) ;
311349
312350 return this . sendReleases (
313- ctx ,
314- [ Object . assign ( repo , { releases : repo . releases . slice ( - 5 ) } ) ] ,
315- ctx . replyWithHTML
351+ null ,
352+ [ Object . assign ( repo , { releases : [ repo . releases [ releaseIndex ] ] } ) ] ,
353+ ctx . replyWithMarkdown
316354 ) ;
317355 }
318356 } catch ( error ) {
0 commit comments