@@ -26,6 +26,8 @@ const CMD_START = 'CMD_START',
2626 CMD_ADD_LINK = 'CMD_ADD_LINK' ,
2727 CMD_CHOOSE_COLLECTION = 'CMD_CHOOSE_COLLECTION' ,
2828 CMD_UPLOAD_PHOTO = 'CMD_UPLOAD_PHOTO' ,
29+ CMD_SET_DEFAULT = 'CMD_SET_DEFAULT' ,
30+ CMD_CLEAR_DEFAULT = 'CMD_CLEAR_DEFAULT' ,
2931 CMD_HELP = 'CMD_HELP'
3032
3133const commandMatchers = {
@@ -58,6 +60,14 @@ const commandMatchers = {
5860 const args = m . text . match ( / ^ ( \d + ) $ / )
5961 return args ? args . slice ( 1 ) : null
6062 } ,
63+ [ CMD_SET_DEFAULT ] : function ( m ) {
64+ const args = m . text . match ( / ^ \/ s e t d e f a u l t \s ( \d + ) $ / )
65+ return args ? args . slice ( 1 ) : null
66+ } ,
67+ [ CMD_CLEAR_DEFAULT ] : function ( m ) {
68+ const args = m . text . match ( / ^ \/ c l e a r d e f a u l t $ / )
69+ return args ? args . slice ( 1 ) : null
70+ } ,
6171 [ CMD_UPLOAD_PHOTO ] : function ( m ) {
6272 return m . photo ? [ ] : null
6373 } ,
@@ -130,20 +140,24 @@ const commandProcessors = {
130140 date : Date . now ( )
131141 }
132142
133- fetchCollections ( user )
134- . then ( ( { data } ) => {
135- // this is not robust to the collections changing while the user does their choice
136- let text = '👇 Choose a collection to save the link to by typing its number:\n\n'
137- text += data
138- . map ( ( c , i ) => {
139- return `**${ i + 1 } .** ${ c . name } `
140- } )
141- . join ( '\n' )
142- return tgutils . doRequest ( 'sendMessage' , { chat_id : rawMessage . chat . id , text : text , parse_mode : 'Markdown' } )
143- } )
144- . catch ( ( ) => {
145- return tgutils . doRequest ( 'sendMessage' , { chat_id : rawMessage . chat . id , text : '❌ Failed to add link, sorry.' } )
146- } )
143+ if ( user . telegramDefaultCollId ) {
144+ commandProcessors [ CMD_CHOOSE_COLLECTION ] ( [ user . telegramDefaultCollId ] , rawMessage )
145+ } else {
146+ fetchCollections ( user )
147+ . then ( ( { data } ) => {
148+ // this is not robust to the collections changing while the user does their choice
149+ let text = '👇 Choose a collection to save the link to by typing its number:\n\n'
150+ text += data
151+ . map ( ( c , i ) => {
152+ return `**${ i + 1 } .** ${ c . name } `
153+ } )
154+ . join ( '\n' )
155+ return tgutils . doRequest ( 'sendMessage' , { chat_id : rawMessage . chat . id , text : text , parse_mode : 'Markdown' } )
156+ } )
157+ . catch ( ( ) => {
158+ return tgutils . doRequest ( 'sendMessage' , { chat_id : rawMessage . chat . id , text : '❌ Failed to add link, sorry.' } )
159+ } )
160+ }
147161 } )
148162 . catch ( unauthenticatedHandler ( rawMessage ) )
149163 } ,
@@ -174,6 +188,26 @@ const commandProcessors = {
174188 } )
175189 . catch ( unauthenticatedHandler ( rawMessage ) )
176190 } ,
191+ [ CMD_SET_DEFAULT ] : function ( args , rawMessage ) {
192+ return tgutils . resolveUser ( rawMessage . from )
193+ . then ( ( user ) => {
194+ User . updateOne ( { _id : user . _id } , { telegramDefaultCollId : parseInt ( args [ 0 ] ) } )
195+ . then ( ( ) => {
196+ return tgutils . doRequest ( 'sendMessage' , { chat_id : rawMessage . chat . id , text : '✅ Set default collection.' } )
197+ } )
198+ } )
199+ . catch ( unauthenticatedHandler ( rawMessage ) )
200+ } ,
201+ [ CMD_CLEAR_DEFAULT ] : function ( args , rawMessage ) {
202+ return tgutils . resolveUser ( rawMessage . from )
203+ . then ( ( user ) => {
204+ User . updateOne ( { _id : user . _id } , { $unset : { telegramDefaultCollId : '' } } )
205+ . then ( ( ) => {
206+ return tgutils . doRequest ( 'sendMessage' , { chat_id : rawMessage . chat . id , text : '✅ Unset default collection.' } )
207+ } )
208+ } )
209+ . catch ( unauthenticatedHandler ( rawMessage ) )
210+ } ,
177211 [ CMD_UPLOAD_PHOTO ] : function ( args , rawMessage ) {
178212 return tgutils . resolveUser ( rawMessage . from )
179213 . then ( ( user ) => {
0 commit comments