|
| 1 | +const { configurationForIdentity, set } = require("./identities.js"); |
| 2 | + |
1 | 3 | const fs = require("fs"); |
2 | 4 | const fsp = require("fs").promises; |
3 | 5 | const Path = require("path"); |
@@ -91,6 +93,18 @@ module.exports = function (ssbConfig) { |
91 | 93 | "full-text-search.sqlite", |
92 | 94 | ); |
93 | 95 |
|
| 96 | + // load and start search engine |
| 97 | + |
| 98 | + electron.ipcRenderer.on("search", (ev, terms) => { |
| 99 | + electron.ipcRenderer.send("search-unavailable"); |
| 100 | + }); |
| 101 | + |
| 102 | + electron.ipcRenderer.on("is-search-available", (ev, terms) => { |
| 103 | + electron.ipcRenderer.send("search-unavailable"); |
| 104 | + }); |
| 105 | + |
| 106 | + electron.ipcRenderer.send("search-unavailable"); |
| 107 | + |
94 | 108 | const loadOrCreateSqlite = async () => { |
95 | 109 | console.log("load or create SQLITE"); |
96 | 110 | let indexData; |
@@ -118,124 +132,6 @@ module.exports = function (ssbConfig) { |
118 | 132 | return { database, lastIndexedTimestamp }; |
119 | 133 | }; |
120 | 134 |
|
121 | | - // const loadOrCreateMiniSearch = async () => { |
122 | | - // let controlData; |
123 | | - // let indexData; |
124 | | - // let miniSearch = new MiniSearch({ |
125 | | - // idField: "key", |
126 | | - // fields: ["content"], |
127 | | - // storeFields: ["key", "content", "raw", "timestamp"], |
128 | | - // }); |
129 | | - |
130 | | - // if ( |
131 | | - // fs.existsSync(minisearchControlPath) && fs.existsSync(minisearchIndexPath) |
132 | | - // ) { |
133 | | - // // load previous saved work. |
134 | | - // try { |
135 | | - // console.log(`loading search index data from file...`); |
136 | | - // console.log( |
137 | | - // `\t- control: ${minisearchControlPath}\n\t- index: ${minisearchIndexPath}`, |
138 | | - // ); |
139 | | - // controlData = decode(await fsp.readFile(minisearchControlPath)); |
140 | | - // console.time("loading file"); |
141 | | - // indexData = decode(await fsp.readFile(minisearchIndexPath)); |
142 | | - // console.timeEnd("loading file"); |
143 | | - // console.time("loadJS"); |
144 | | - // miniSearch = await MiniSearch.loadJSAsync(indexData, { |
145 | | - // idField: "key", |
146 | | - // fields: ["content"], |
147 | | - // storeFields: ["key", "content", "raw", "timestamp"], |
148 | | - // }); |
149 | | - // console.timeEnd("loadJS"); |
150 | | - // console.log(`loaded search index data.`); |
151 | | - // console.log( |
152 | | - // `Last indexed timestamp: ${controlData.lastIndexedTimestamp}`, |
153 | | - // ); |
154 | | - // lastIndexedTimestamp = controlData.lastIndexedTimestamp; |
155 | | - // } catch (e) { |
156 | | - // console.log("error loading search data"); |
157 | | - // console.log(e); |
158 | | - // } |
159 | | - // } |
160 | | - // return { miniSearch, controlData, indexData }; |
161 | | - // }; |
162 | | - |
163 | | - // const loadMessagesIntoSearchIndex = ( |
164 | | - // { miniSearch, controlData, indexData }, |
165 | | - // ) => { |
166 | | - // console.log("Starting message indexing pull stream..."); |
167 | | - // pull( |
168 | | - // context.sbot.messagesByType({ |
169 | | - // type: "post", |
170 | | - // live: true, |
171 | | - // gt: lastIndexedTimestamp, |
172 | | - // }), |
173 | | - // pull.map((m) => { |
174 | | - // // console.log("mapping", m); |
175 | | - // if (m.sync) return m; |
176 | | - // return { |
177 | | - // key: m.key, |
178 | | - // timestamp: m.timestamp, |
179 | | - // content: m.value?.content?.text, |
180 | | - // raw: JSON.stringify(m), |
181 | | - // }; |
182 | | - // }), |
183 | | - // pull.drain((m) => { |
184 | | - // if (m.sync) { |
185 | | - // // finished indexing |
186 | | - // console.log(`finished indexing for now ${lastIndexedTimestamp}`); |
187 | | - |
188 | | - // controlData = { |
189 | | - // lastIndexedTimestamp, |
190 | | - // }; |
191 | | - |
192 | | - // indexData = miniSearch.toJSON(); |
193 | | - // const encodedIndex = encode(indexData); |
194 | | - // const encodedControl = encode(controlData); |
195 | | - |
196 | | - // fs.writeFileSync(minisearchIndexPath, encodedIndex); |
197 | | - // fs.writeFileSync(minisearchControlPath, encodedControl); |
198 | | - |
199 | | - // enableSearch(miniSearch); |
200 | | - // return; |
201 | | - // } else { |
202 | | - // miniSearch.add(m); |
203 | | - // console.log(miniSearch.documentCount); |
204 | | - // lastIndexedTimestamp = m.timestamp; |
205 | | - // } |
206 | | - // }), |
207 | | - // ); |
208 | | - // }; |
209 | | - |
210 | | - // load and start search engine |
211 | | - |
212 | | - electron.ipcRenderer.on("search", (ev, terms) => { |
213 | | - electron.ipcRenderer.send("search-unavailable"); |
214 | | - }); |
215 | | - |
216 | | - electron.ipcRenderer.on("is-search-available", (ev, terms) => { |
217 | | - electron.ipcRenderer.send("search-unavailable"); |
218 | | - }); |
219 | | - |
220 | | - electron.ipcRenderer.send("search-unavailable"); |
221 | | - |
222 | | - // const enableSearch = (miniSearch) => { |
223 | | - // // handle searches |
224 | | - // electron.ipcRenderer.removeAllListeners("search"); |
225 | | - // electron.ipcRenderer.removeAllListeners("is-search-available"); |
226 | | - // electron.ipcRenderer.on("is-search-available", (ev, terms) => { |
227 | | - // electron.ipcRenderer.send("search-available"); |
228 | | - // }); |
229 | | - |
230 | | - // electron.ipcRenderer.send("search-available"); |
231 | | - |
232 | | - // electron.ipcRenderer.on("search", (ev, terms) => { |
233 | | - // const result = miniSearch.search(terms, { combineWith: "AND" }); |
234 | | - // console.log(`result count: ${result.length}`); |
235 | | - // electron.ipcRenderer.send("search-results", result); |
236 | | - // }); |
237 | | - // }; |
238 | | - |
239 | 135 | const enableSqliteSearch = (database) => { |
240 | 136 | // handle searches |
241 | 137 | electron.ipcRenderer.removeAllListeners("search"); |
@@ -266,8 +162,23 @@ module.exports = function (ssbConfig) { |
266 | 162 | }; |
267 | 163 |
|
268 | 164 | const loadMessagesIntoSQLite = ( |
269 | | - { database, controlData }, |
| 165 | + { database, controlData, delayPullStream = false }, |
270 | 166 | ) => { |
| 167 | + // if it is an imported account doing first-time sync |
| 168 | + // attempting a pull stream at the same time is very |
| 169 | + // costly for the UI. |
| 170 | + // |
| 171 | + // Let's delay that for 15 minutes. |
| 172 | + if (delayPullStream) { |
| 173 | + console.log("SQLITE: imported account, delay pulling"); |
| 174 | + setTimeout(() => { |
| 175 | + console.log("SQLITE: delayed pulling about to start..."); |
| 176 | + set(ssbConfig.keys.id, "imported", false); |
| 177 | + loadMessagesIntoSQLite({ database, controlData }); |
| 178 | + }, 15 * 60000); |
| 179 | + return; |
| 180 | + } |
| 181 | + |
271 | 182 | console.log("SQLITE: Starting message indexing pull stream..."); |
272 | 183 | const insert = database.prepare( |
273 | 184 | "INSERT OR REPLACE INTO messages (key, content, raw, timestamp) VALUES (?, ?, ?, ?)", |
@@ -324,7 +235,12 @@ module.exports = function (ssbConfig) { |
324 | 235 |
|
325 | 236 | loadOrCreateSqlite().then(({ database, controlData }) => { |
326 | 237 | console.log("SQLITE: callback"); |
327 | | - loadMessagesIntoSQLite({ database, controlData }); |
| 238 | + let delayPullStream = false; |
| 239 | + let configuration = configurationForIdentity(ssbConfig.keys.id); |
| 240 | + if (configuration?.imported) { |
| 241 | + delayPullStream = true; |
| 242 | + } |
| 243 | + loadMessagesIntoSQLite({ database, controlData, delayPullStream }); |
328 | 244 | }).catch((e) => { |
329 | 245 | console.log("SQLITE: ERROR"); |
330 | 246 | console.error(JSON.stringify(e)); |
|
0 commit comments