@@ -7,13 +7,33 @@ import { Resource } from './reactions/resource';
77import { ImportToolResultsWebview } from './webviews/importToolResultsWebview' ;
88import { commentController } from './controllers/comments' ;
99import { reactionHandler } from './handlers/reaction' ;
10- import { loadCommentsFromFile , saveCommentsToFile } from './persistence' ;
11- import { saveNoteComment , setNoteStatus } from './helpers' ;
12-
13- const noteList : vscode . CommentThread [ ] = [ ] ;
10+ import {
11+ getSetting ,
12+ saveNoteComment ,
13+ setNoteStatus ,
14+ syncNoteMapWithRemote ,
15+ } from './helpers' ;
16+ import { RemoteDb } from './persistence/remote-db' ;
17+ import { loadCommentsFromFile , saveCommentsToFile } from './persistence/local' ;
18+
19+ const noteMap = new Map < string , vscode . CommentThread > ( ) ;
20+ let remoteDb : RemoteDb | undefined ;
1421
1522export function activate ( context : vscode . ExtensionContext ) {
1623 Resource . initialize ( context ) ;
24+ if ( getSetting ( 'collab.enabled' ) ) {
25+ remoteDb = new RemoteDb (
26+ getSetting ( 'collab.host' ) ,
27+ getSetting ( 'collab.port' ) ,
28+ getSetting ( 'collab.username' ) ,
29+ getSetting ( 'collab.password' ) ,
30+ getSetting ( 'collab.database' ) ,
31+ getSetting ( 'collab.projectName' ) ,
32+ noteMap ,
33+ ) ;
34+ } else {
35+ remoteDb = undefined ;
36+ }
1737
1838 // A `CommentController` is able to provide comments for documents.
1939 context . subscriptions . push ( commentController ) ;
@@ -37,7 +57,7 @@ export function activate(context: vscode.ExtensionContext) {
3757 vscode . commands . registerCommand (
3858 'security-notes.createNote' ,
3959 ( reply : vscode . CommentReply ) => {
40- saveNoteComment ( reply . thread , reply . text , true , noteList ) ;
60+ saveNoteComment ( reply . thread , reply . text , true , noteMap , '' , remoteDb ) ;
4161 } ,
4262 ) ,
4363 ) ;
@@ -47,7 +67,7 @@ export function activate(context: vscode.ExtensionContext) {
4767 vscode . commands . registerCommand (
4868 'security-notes.replyNoteComment' ,
4969 ( reply : vscode . CommentReply ) => {
50- saveNoteComment ( reply . thread , reply . text , false , noteList ) ;
70+ saveNoteComment ( reply . thread , reply . text , false , noteMap , '' , remoteDb ) ;
5171 } ,
5272 ) ,
5373 ) ;
@@ -119,6 +139,9 @@ export function activate(context: vscode.ExtensionContext) {
119139 cmt . mode = vscode . CommentMode . Preview ;
120140 }
121141
142+ if ( remoteDb && comment . parent ) {
143+ remoteDb . pushNoteComment ( comment . parent , false ) ;
144+ }
122145 return cmt ;
123146 } ) ;
124147 } ,
@@ -150,7 +173,13 @@ export function activate(context: vscode.ExtensionContext) {
150173 vscode . commands . registerCommand (
151174 'security-notes.setNoteStatusVulnerable' ,
152175 ( commentReply : vscode . CommentReply ) =>
153- setNoteStatus ( commentReply , NoteStatus . Vulnerable ) ,
176+ setNoteStatus (
177+ commentReply . thread ,
178+ NoteStatus . Vulnerable ,
179+ noteMap ,
180+ '' ,
181+ remoteDb ,
182+ ) ,
154183 ) ,
155184 ) ;
156185
@@ -159,7 +188,13 @@ export function activate(context: vscode.ExtensionContext) {
159188 vscode . commands . registerCommand (
160189 'security-notes.setNoteStatusNotVulnerable' ,
161190 ( commentReply : vscode . CommentReply ) =>
162- setNoteStatus ( commentReply , NoteStatus . NotVulnerable ) ,
191+ setNoteStatus (
192+ commentReply . thread ,
193+ NoteStatus . NotVulnerable ,
194+ noteMap ,
195+ '' ,
196+ remoteDb ,
197+ ) ,
163198 ) ,
164199 ) ;
165200
@@ -168,14 +203,15 @@ export function activate(context: vscode.ExtensionContext) {
168203 vscode . commands . registerCommand (
169204 'security-notes.setNoteStatusToDo' ,
170205 ( commentReply : vscode . CommentReply ) =>
171- setNoteStatus ( commentReply , NoteStatus . TODO ) ,
206+ setNoteStatus ( commentReply . thread , NoteStatus . TODO , noteMap , '' , remoteDb ) ,
172207 ) ,
173208 ) ;
174209
175210 // webview for importing tool results
176211 const importToolResultsWebview = new ImportToolResultsWebview (
177212 context . extensionUri ,
178- noteList ,
213+ noteMap ,
214+ remoteDb ,
179215 ) ;
180216 context . subscriptions . push (
181217 vscode . window . registerWebviewViewProvider (
@@ -185,10 +221,22 @@ export function activate(context: vscode.ExtensionContext) {
185221 ) ;
186222
187223 // load persisted comments from file
188- noteList . push ( ...loadCommentsFromFile ( ) ) ;
224+ const persistedThreads = loadCommentsFromFile ( ) ;
225+ persistedThreads . forEach ( ( thread ) => {
226+ noteMap . set ( thread . contextValue ? thread . contextValue : '' , thread ) ;
227+ } ) ;
228+
229+ // initial retrieval of notes from database
230+ setTimeout ( ( ) => {
231+ if ( remoteDb ) {
232+ remoteDb . retrieveAll ( ) . then ( ( remoteThreads ) => {
233+ syncNoteMapWithRemote ( noteMap , remoteThreads , remoteDb ) ;
234+ } ) ;
235+ }
236+ } , 1500 ) ;
189237}
190238
191239export function deactivate ( context : vscode . ExtensionContext ) {
192240 // persist comments in file
193- saveCommentsToFile ( noteList ) ;
241+ saveCommentsToFile ( noteMap ) ;
194242}
0 commit comments