1
1
import { Notice , type App } from "obsidian" ;
2
2
import axios from "axios" ;
3
- import type { RaindropArticle , RaindropCollection , RaindropHighlight , RaindropUser } from "./types" ;
3
+ import type { RaindropBookmark , RaindropCollection , RaindropHighlight , RaindropUser } from "./types" ;
4
4
import TokenManager from "./tokenManager" ;
5
5
6
6
const BASEURL = "https://api.raindrop.io/rest/v1"
@@ -42,17 +42,21 @@ export class RaindropAPI {
42
42
43
43
async getCollections ( ) : Promise < RaindropCollection [ ] > {
44
44
let res = await this . get ( `${ BASEURL } /collections` , { } ) ;
45
- const collectionMap : { [ id : number ] : string } = { }
45
+ const collectionMap : { [ id : number ] : string } = { } ;
46
46
47
- let collections : RaindropCollection [ ] = res . items . map ( ( collection : any ) => {
47
+ let collections : RaindropCollection [ ] = [
48
+ { id : - 1 , title : 'Unsorted' } ,
49
+ { id : - 99 , title : 'Trash' } ,
50
+ ] ;
51
+ res . items . forEach ( ( collection : any ) => {
48
52
const id = collection [ '_id' ] ;
49
53
const title = collection [ 'title' ] ;
50
54
collectionMap [ id ] = title ;
51
- return {
55
+ collections . push ( {
52
56
title : title ,
53
57
id : id ,
54
- } ;
55
- } )
58
+ } ) ;
59
+ } ) ;
56
60
57
61
res = await this . get ( `${ BASEURL } /collections/childrens` , { } ) ;
58
62
res . items . forEach ( ( collection : any ) => {
@@ -72,14 +76,14 @@ export class RaindropAPI {
72
76
return collections ;
73
77
}
74
78
75
- async getRaindropsAfter ( collectionId : number , lastSync ?: Date ) : Promise < RaindropArticle [ ] > {
79
+ async getRaindropsAfter ( collectionId : number , lastSync ?: Date ) : Promise < RaindropBookmark [ ] > {
76
80
const notice = new Notice ( "Fetch Raindrops highlights" , 0 ) ;
77
81
let res = await this . get ( `${ BASEURL } /raindrops/${ collectionId } ` , {
78
82
"page" : 0 ,
79
83
"sort" : "-lastUpdate"
80
84
} ) ;
81
85
let raindropsCnt = res . count ;
82
- let articles = this . parseArticles ( res . items ) ;
86
+ let bookmarks = this . parseRaindrops ( res . items ) ;
83
87
let remainPages = Math . ceil ( raindropsCnt / 25 ) - 1 ;
84
88
let totalPages = Math . ceil ( raindropsCnt / 25 ) - 1 ;
85
89
let page = 1 ;
@@ -89,37 +93,37 @@ export class RaindropAPI {
89
93
"page" : page ,
90
94
"sort" : "-lastUpdate"
91
95
} ) ;
92
- articles = articles . concat ( this . parseArticles ( res . items ) ) ;
96
+ bookmarks = bookmarks . concat ( this . parseRaindrops ( res . items ) ) ;
93
97
}
94
98
95
- if ( articles . length > 0 ) {
99
+ if ( bookmarks . length > 0 ) {
96
100
if ( lastSync === undefined ) { // sync all
97
101
while ( remainPages -- ) {
98
102
notice . setMessage ( `Sync Raindrop pages: ${ totalPages - remainPages } /${ totalPages } ` )
99
103
await addNewPages ( page ++ ) ;
100
104
}
101
105
} else { // sync article after lastSync
102
- while ( articles [ articles . length - 1 ] . lastUpdate >= lastSync && remainPages -- ) {
106
+ while ( bookmarks [ bookmarks . length - 1 ] . lastUpdate >= lastSync && remainPages -- ) {
103
107
notice . setMessage ( `Sync Raindrop pages: ${ totalPages - remainPages } /${ totalPages } ` )
104
108
await addNewPages ( page ++ ) ;
105
109
}
106
- articles = articles . filter ( article => {
107
- return article . lastUpdate >= lastSync ;
110
+ bookmarks = bookmarks . filter ( bookmark => {
111
+ return bookmark . lastUpdate >= lastSync ;
108
112
} ) ;
109
113
}
110
114
}
111
115
112
116
// get real highlights (raindrop returns only 3 highlights in /raindrops/${collectionId} endpoint)
113
- for ( let [ idx , article ] of articles . entries ( ) ) {
114
- notice . setMessage ( `Sync Raindrop articles : ${ idx + 1 } /${ articles . length } ` )
115
- if ( article . highlights . length == 3 ) {
116
- let res = await this . get ( `${ BASEURL } /raindrop/${ article . id } ` , { } ) ;
117
- article [ 'highlights' ] = this . parseHighlights ( res . item . highlights ) ;
117
+ for ( let [ idx , bookmark ] of bookmarks . entries ( ) ) {
118
+ notice . setMessage ( `Sync Raindrop bookmarks : ${ idx + 1 } /${ bookmarks . length } ` )
119
+ if ( bookmark . highlights . length == 3 ) {
120
+ let res = await this . get ( `${ BASEURL } /raindrop/${ bookmark . id } ` , { } ) ;
121
+ bookmark [ 'highlights' ] = this . parseHighlights ( res . item . highlights ) ;
118
122
}
119
123
}
120
124
121
125
notice . hide ( ) ;
122
- return articles ;
126
+ return bookmarks ;
123
127
}
124
128
125
129
async getUser ( ) : Promise < RaindropUser > {
@@ -151,20 +155,20 @@ export class RaindropAPI {
151
155
} ;
152
156
}
153
157
154
- async getArticle ( id : number ) : Promise < RaindropArticle > {
158
+ async getRaindrop ( id : number ) : Promise < RaindropBookmark > {
155
159
const res = await this . get ( `${ BASEURL } /raindrop/${ id } ` , { } ) ;
156
- const article = this . parseArticle ( res . item ) ;
157
- return article ;
160
+ const bookmark = this . parseRaindrop ( res . item ) ;
161
+ return bookmark ;
158
162
}
159
163
160
- private parseArticles ( articles : any ) : RaindropArticle [ ] {
161
- return articles . map ( ( raindrop : any ) => {
162
- return this . parseArticle ( raindrop ) ;
164
+ private parseRaindrops ( bookmarks : any ) : RaindropBookmark [ ] {
165
+ return bookmarks . map ( ( raindrop : any ) => {
166
+ return this . parseRaindrop ( raindrop ) ;
163
167
} ) ;
164
168
}
165
169
166
- private parseArticle ( raindrop : any ) : RaindropArticle {
167
- const article : RaindropArticle = {
170
+ private parseRaindrop ( raindrop : any ) : RaindropBookmark {
171
+ const bookmark : RaindropBookmark = {
168
172
id : raindrop [ '_id' ] ,
169
173
collectionId : raindrop [ 'collectionId' ] ,
170
174
title : raindrop [ 'title' ] ,
@@ -178,7 +182,7 @@ export class RaindropAPI {
178
182
type : raindrop [ 'type' ] ,
179
183
important : raindrop [ 'important' ] ,
180
184
} ;
181
- return article ;
185
+ return bookmark ;
182
186
}
183
187
184
188
private parseHighlights ( highlights : any ) : RaindropHighlight [ ] {
0 commit comments