├── .gitattributes
├── .gitignore
├── .goreleaser.yml
├── LICENSE
├── Makefile
├── README.md
├── VERSION
├── cmd
├── debug
│ └── main.go
└── term
│ └── main.go
├── demos
└── basic.gif
├── go.mod
├── go.sum
└── pkg
├── prompt
└── prompt.go
├── s3
└── s3.go
├── service
├── auth.go
├── client.go
├── config.go
├── db.go
├── debug.go
├── events.go
├── events_test.go
├── legacy.go
├── match.go
├── service.go
├── service_test.go
├── tree.go
├── undo.go
├── undo_test.go
└── web.go
└── term
└── client.go
/.gitattributes:
--------------------------------------------------------------------------------
1 | **/*.gif filter=lfs diff=lfs merge=lfs -text
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Binaries for programs and plugins
2 | *.exe
3 | *.exe~
4 | *.dll
5 | *.so
6 | *.dylib
7 |
8 | # Test binary, built with `go test -c`
9 | *.test
10 |
11 | # Output of the go coverage tool, specifically when used with LiteIDE
12 | *.out
13 |
14 | # Dependency directories (remove the comment below to include it)
15 | # vendor/
16 |
17 | # Ctags
18 | tags
19 |
20 | # Bin directory
21 | bin/
22 | dist/
23 |
24 | # debug output
25 | debug/
26 |
--------------------------------------------------------------------------------
/.goreleaser.yml:
--------------------------------------------------------------------------------
1 | # This is an example .goreleaser.yml file with some sane defaults.
2 | # Make sure to check the documentation at http://goreleaser.com
3 | before:
4 | hooks:
5 | # You may remove this if you don't use go modules.
6 | - go mod tidy
7 | # you may remove this if you don't need go generate
8 | #- go generate ./...
9 | builds:
10 | - binary: "fzn"
11 | main: ./cmd/term
12 | env:
13 | - CGO_ENABLED=0
14 | goos:
15 | - linux
16 | - windows
17 | - darwin
18 | ldflags:
19 | # The options below are identical to the default, other than main.date which is explicitly set to commit unixtime
20 | - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitTimestamp}} -X main.builtBy=goreleaser
21 | archives:
22 | - replacements:
23 | darwin: Darwin
24 | linux: Linux
25 | windows: Windows
26 | 386: i386
27 | amd64: x86_64
28 | checksum:
29 | name_template: 'checksums.txt'
30 | snapshot:
31 | name_template: "{{ .Tag }}-next"
32 | changelog:
33 | sort: asc
34 | filters:
35 | exclude:
36 | - '^docs:'
37 | - '^test:'
38 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU AFFERO GENERAL PUBLIC LICENSE
2 | Version 3, 19 November 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU Affero General Public License is a free, copyleft license for
11 | software and other kinds of works, specifically designed to ensure
12 | cooperation with the community in the case of network server software.
13 |
14 | The licenses for most software and other practical works are designed
15 | to take away your freedom to share and change the works. By contrast,
16 | our General Public Licenses are intended to guarantee your freedom to
17 | share and change all versions of a program--to make sure it remains free
18 | software for all its users.
19 |
20 | When we speak of free software, we are referring to freedom, not
21 | price. Our General Public Licenses are designed to make sure that you
22 | have the freedom to distribute copies of free software (and charge for
23 | them if you wish), that you receive source code or can get it if you
24 | want it, that you can change the software or use pieces of it in new
25 | free programs, and that you know you can do these things.
26 |
27 | Developers that use our General Public Licenses protect your rights
28 | with two steps: (1) assert copyright on the software, and (2) offer
29 | you this License which gives you legal permission to copy, distribute
30 | and/or modify the software.
31 |
32 | A secondary benefit of defending all users' freedom is that
33 | improvements made in alternate versions of the program, if they
34 | receive widespread use, become available for other developers to
35 | incorporate. Many developers of free software are heartened and
36 | encouraged by the resulting cooperation. However, in the case of
37 | software used on network servers, this result may fail to come about.
38 | The GNU General Public License permits making a modified version and
39 | letting the public access it on a server without ever releasing its
40 | source code to the public.
41 |
42 | The GNU Affero General Public License is designed specifically to
43 | ensure that, in such cases, the modified source code becomes available
44 | to the community. It requires the operator of a network server to
45 | provide the source code of the modified version running there to the
46 | users of that server. Therefore, public use of a modified version, on
47 | a publicly accessible server, gives the public access to the source
48 | code of the modified version.
49 |
50 | An older license, called the Affero General Public License and
51 | published by Affero, was designed to accomplish similar goals. This is
52 | a different license, not a version of the Affero GPL, but Affero has
53 | released a new version of the Affero GPL which permits relicensing under
54 | this license.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | TERMS AND CONDITIONS
60 |
61 | 0. Definitions.
62 |
63 | "This License" refers to version 3 of the GNU Affero General Public License.
64 |
65 | "Copyright" also means copyright-like laws that apply to other kinds of
66 | works, such as semiconductor masks.
67 |
68 | "The Program" refers to any copyrightable work licensed under this
69 | License. Each licensee is addressed as "you". "Licensees" and
70 | "recipients" may be individuals or organizations.
71 |
72 | To "modify" a work means to copy from or adapt all or part of the work
73 | in a fashion requiring copyright permission, other than the making of an
74 | exact copy. The resulting work is called a "modified version" of the
75 | earlier work or a work "based on" the earlier work.
76 |
77 | A "covered work" means either the unmodified Program or a work based
78 | on the Program.
79 |
80 | To "propagate" a work means to do anything with it that, without
81 | permission, would make you directly or secondarily liable for
82 | infringement under applicable copyright law, except executing it on a
83 | computer or modifying a private copy. Propagation includes copying,
84 | distribution (with or without modification), making available to the
85 | public, and in some countries other activities as well.
86 |
87 | To "convey" a work means any kind of propagation that enables other
88 | parties to make or receive copies. Mere interaction with a user through
89 | a computer network, with no transfer of a copy, is not conveying.
90 |
91 | An interactive user interface displays "Appropriate Legal Notices"
92 | to the extent that it includes a convenient and prominently visible
93 | feature that (1) displays an appropriate copyright notice, and (2)
94 | tells the user that there is no warranty for the work (except to the
95 | extent that warranties are provided), that licensees may convey the
96 | work under this License, and how to view a copy of this License. If
97 | the interface presents a list of user commands or options, such as a
98 | menu, a prominent item in the list meets this criterion.
99 |
100 | 1. Source Code.
101 |
102 | The "source code" for a work means the preferred form of the work
103 | for making modifications to it. "Object code" means any non-source
104 | form of a work.
105 |
106 | A "Standard Interface" means an interface that either is an official
107 | standard defined by a recognized standards body, or, in the case of
108 | interfaces specified for a particular programming language, one that
109 | is widely used among developers working in that language.
110 |
111 | The "System Libraries" of an executable work include anything, other
112 | than the work as a whole, that (a) is included in the normal form of
113 | packaging a Major Component, but which is not part of that Major
114 | Component, and (b) serves only to enable use of the work with that
115 | Major Component, or to implement a Standard Interface for which an
116 | implementation is available to the public in source code form. A
117 | "Major Component", in this context, means a major essential component
118 | (kernel, window system, and so on) of the specific operating system
119 | (if any) on which the executable work runs, or a compiler used to
120 | produce the work, or an object code interpreter used to run it.
121 |
122 | The "Corresponding Source" for a work in object code form means all
123 | the source code needed to generate, install, and (for an executable
124 | work) run the object code and to modify the work, including scripts to
125 | control those activities. However, it does not include the work's
126 | System Libraries, or general-purpose tools or generally available free
127 | programs which are used unmodified in performing those activities but
128 | which are not part of the work. For example, Corresponding Source
129 | includes interface definition files associated with source files for
130 | the work, and the source code for shared libraries and dynamically
131 | linked subprograms that the work is specifically designed to require,
132 | such as by intimate data communication or control flow between those
133 | subprograms and other parts of the work.
134 |
135 | The Corresponding Source need not include anything that users
136 | can regenerate automatically from other parts of the Corresponding
137 | Source.
138 |
139 | The Corresponding Source for a work in source code form is that
140 | same work.
141 |
142 | 2. Basic Permissions.
143 |
144 | All rights granted under this License are granted for the term of
145 | copyright on the Program, and are irrevocable provided the stated
146 | conditions are met. This License explicitly affirms your unlimited
147 | permission to run the unmodified Program. The output from running a
148 | covered work is covered by this License only if the output, given its
149 | content, constitutes a covered work. This License acknowledges your
150 | rights of fair use or other equivalent, as provided by copyright law.
151 |
152 | You may make, run and propagate covered works that you do not
153 | convey, without conditions so long as your license otherwise remains
154 | in force. You may convey covered works to others for the sole purpose
155 | of having them make modifications exclusively for you, or provide you
156 | with facilities for running those works, provided that you comply with
157 | the terms of this License in conveying all material for which you do
158 | not control copyright. Those thus making or running the covered works
159 | for you must do so exclusively on your behalf, under your direction
160 | and control, on terms that prohibit them from making any copies of
161 | your copyrighted material outside their relationship with you.
162 |
163 | Conveying under any other circumstances is permitted solely under
164 | the conditions stated below. Sublicensing is not allowed; section 10
165 | makes it unnecessary.
166 |
167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168 |
169 | No covered work shall be deemed part of an effective technological
170 | measure under any applicable law fulfilling obligations under article
171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172 | similar laws prohibiting or restricting circumvention of such
173 | measures.
174 |
175 | When you convey a covered work, you waive any legal power to forbid
176 | circumvention of technological measures to the extent such circumvention
177 | is effected by exercising rights under this License with respect to
178 | the covered work, and you disclaim any intention to limit operation or
179 | modification of the work as a means of enforcing, against the work's
180 | users, your or third parties' legal rights to forbid circumvention of
181 | technological measures.
182 |
183 | 4. Conveying Verbatim Copies.
184 |
185 | You may convey verbatim copies of the Program's source code as you
186 | receive it, in any medium, provided that you conspicuously and
187 | appropriately publish on each copy an appropriate copyright notice;
188 | keep intact all notices stating that this License and any
189 | non-permissive terms added in accord with section 7 apply to the code;
190 | keep intact all notices of the absence of any warranty; and give all
191 | recipients a copy of this License along with the Program.
192 |
193 | You may charge any price or no price for each copy that you convey,
194 | and you may offer support or warranty protection for a fee.
195 |
196 | 5. Conveying Modified Source Versions.
197 |
198 | You may convey a work based on the Program, or the modifications to
199 | produce it from the Program, in the form of source code under the
200 | terms of section 4, provided that you also meet all of these conditions:
201 |
202 | a) The work must carry prominent notices stating that you modified
203 | it, and giving a relevant date.
204 |
205 | b) The work must carry prominent notices stating that it is
206 | released under this License and any conditions added under section
207 | 7. This requirement modifies the requirement in section 4 to
208 | "keep intact all notices".
209 |
210 | c) You must license the entire work, as a whole, under this
211 | License to anyone who comes into possession of a copy. This
212 | License will therefore apply, along with any applicable section 7
213 | additional terms, to the whole of the work, and all its parts,
214 | regardless of how they are packaged. This License gives no
215 | permission to license the work in any other way, but it does not
216 | invalidate such permission if you have separately received it.
217 |
218 | d) If the work has interactive user interfaces, each must display
219 | Appropriate Legal Notices; however, if the Program has interactive
220 | interfaces that do not display Appropriate Legal Notices, your
221 | work need not make them do so.
222 |
223 | A compilation of a covered work with other separate and independent
224 | works, which are not by their nature extensions of the covered work,
225 | and which are not combined with it such as to form a larger program,
226 | in or on a volume of a storage or distribution medium, is called an
227 | "aggregate" if the compilation and its resulting copyright are not
228 | used to limit the access or legal rights of the compilation's users
229 | beyond what the individual works permit. Inclusion of a covered work
230 | in an aggregate does not cause this License to apply to the other
231 | parts of the aggregate.
232 |
233 | 6. Conveying Non-Source Forms.
234 |
235 | You may convey a covered work in object code form under the terms
236 | of sections 4 and 5, provided that you also convey the
237 | machine-readable Corresponding Source under the terms of this License,
238 | in one of these ways:
239 |
240 | a) Convey the object code in, or embodied in, a physical product
241 | (including a physical distribution medium), accompanied by the
242 | Corresponding Source fixed on a durable physical medium
243 | customarily used for software interchange.
244 |
245 | b) Convey the object code in, or embodied in, a physical product
246 | (including a physical distribution medium), accompanied by a
247 | written offer, valid for at least three years and valid for as
248 | long as you offer spare parts or customer support for that product
249 | model, to give anyone who possesses the object code either (1) a
250 | copy of the Corresponding Source for all the software in the
251 | product that is covered by this License, on a durable physical
252 | medium customarily used for software interchange, for a price no
253 | more than your reasonable cost of physically performing this
254 | conveying of source, or (2) access to copy the
255 | Corresponding Source from a network server at no charge.
256 |
257 | c) Convey individual copies of the object code with a copy of the
258 | written offer to provide the Corresponding Source. This
259 | alternative is allowed only occasionally and noncommercially, and
260 | only if you received the object code with such an offer, in accord
261 | with subsection 6b.
262 |
263 | d) Convey the object code by offering access from a designated
264 | place (gratis or for a charge), and offer equivalent access to the
265 | Corresponding Source in the same way through the same place at no
266 | further charge. You need not require recipients to copy the
267 | Corresponding Source along with the object code. If the place to
268 | copy the object code is a network server, the Corresponding Source
269 | may be on a different server (operated by you or a third party)
270 | that supports equivalent copying facilities, provided you maintain
271 | clear directions next to the object code saying where to find the
272 | Corresponding Source. Regardless of what server hosts the
273 | Corresponding Source, you remain obligated to ensure that it is
274 | available for as long as needed to satisfy these requirements.
275 |
276 | e) Convey the object code using peer-to-peer transmission, provided
277 | you inform other peers where the object code and Corresponding
278 | Source of the work are being offered to the general public at no
279 | charge under subsection 6d.
280 |
281 | A separable portion of the object code, whose source code is excluded
282 | from the Corresponding Source as a System Library, need not be
283 | included in conveying the object code work.
284 |
285 | A "User Product" is either (1) a "consumer product", which means any
286 | tangible personal property which is normally used for personal, family,
287 | or household purposes, or (2) anything designed or sold for incorporation
288 | into a dwelling. In determining whether a product is a consumer product,
289 | doubtful cases shall be resolved in favor of coverage. For a particular
290 | product received by a particular user, "normally used" refers to a
291 | typical or common use of that class of product, regardless of the status
292 | of the particular user or of the way in which the particular user
293 | actually uses, or expects or is expected to use, the product. A product
294 | is a consumer product regardless of whether the product has substantial
295 | commercial, industrial or non-consumer uses, unless such uses represent
296 | the only significant mode of use of the product.
297 |
298 | "Installation Information" for a User Product means any methods,
299 | procedures, authorization keys, or other information required to install
300 | and execute modified versions of a covered work in that User Product from
301 | a modified version of its Corresponding Source. The information must
302 | suffice to ensure that the continued functioning of the modified object
303 | code is in no case prevented or interfered with solely because
304 | modification has been made.
305 |
306 | If you convey an object code work under this section in, or with, or
307 | specifically for use in, a User Product, and the conveying occurs as
308 | part of a transaction in which the right of possession and use of the
309 | User Product is transferred to the recipient in perpetuity or for a
310 | fixed term (regardless of how the transaction is characterized), the
311 | Corresponding Source conveyed under this section must be accompanied
312 | by the Installation Information. But this requirement does not apply
313 | if neither you nor any third party retains the ability to install
314 | modified object code on the User Product (for example, the work has
315 | been installed in ROM).
316 |
317 | The requirement to provide Installation Information does not include a
318 | requirement to continue to provide support service, warranty, or updates
319 | for a work that has been modified or installed by the recipient, or for
320 | the User Product in which it has been modified or installed. Access to a
321 | network may be denied when the modification itself materially and
322 | adversely affects the operation of the network or violates the rules and
323 | protocols for communication across the network.
324 |
325 | Corresponding Source conveyed, and Installation Information provided,
326 | in accord with this section must be in a format that is publicly
327 | documented (and with an implementation available to the public in
328 | source code form), and must require no special password or key for
329 | unpacking, reading or copying.
330 |
331 | 7. Additional Terms.
332 |
333 | "Additional permissions" are terms that supplement the terms of this
334 | License by making exceptions from one or more of its conditions.
335 | Additional permissions that are applicable to the entire Program shall
336 | be treated as though they were included in this License, to the extent
337 | that they are valid under applicable law. If additional permissions
338 | apply only to part of the Program, that part may be used separately
339 | under those permissions, but the entire Program remains governed by
340 | this License without regard to the additional permissions.
341 |
342 | When you convey a copy of a covered work, you may at your option
343 | remove any additional permissions from that copy, or from any part of
344 | it. (Additional permissions may be written to require their own
345 | removal in certain cases when you modify the work.) You may place
346 | additional permissions on material, added by you to a covered work,
347 | for which you have or can give appropriate copyright permission.
348 |
349 | Notwithstanding any other provision of this License, for material you
350 | add to a covered work, you may (if authorized by the copyright holders of
351 | that material) supplement the terms of this License with terms:
352 |
353 | a) Disclaiming warranty or limiting liability differently from the
354 | terms of sections 15 and 16 of this License; or
355 |
356 | b) Requiring preservation of specified reasonable legal notices or
357 | author attributions in that material or in the Appropriate Legal
358 | Notices displayed by works containing it; or
359 |
360 | c) Prohibiting misrepresentation of the origin of that material, or
361 | requiring that modified versions of such material be marked in
362 | reasonable ways as different from the original version; or
363 |
364 | d) Limiting the use for publicity purposes of names of licensors or
365 | authors of the material; or
366 |
367 | e) Declining to grant rights under trademark law for use of some
368 | trade names, trademarks, or service marks; or
369 |
370 | f) Requiring indemnification of licensors and authors of that
371 | material by anyone who conveys the material (or modified versions of
372 | it) with contractual assumptions of liability to the recipient, for
373 | any liability that these contractual assumptions directly impose on
374 | those licensors and authors.
375 |
376 | All other non-permissive additional terms are considered "further
377 | restrictions" within the meaning of section 10. If the Program as you
378 | received it, or any part of it, contains a notice stating that it is
379 | governed by this License along with a term that is a further
380 | restriction, you may remove that term. If a license document contains
381 | a further restriction but permits relicensing or conveying under this
382 | License, you may add to a covered work material governed by the terms
383 | of that license document, provided that the further restriction does
384 | not survive such relicensing or conveying.
385 |
386 | If you add terms to a covered work in accord with this section, you
387 | must place, in the relevant source files, a statement of the
388 | additional terms that apply to those files, or a notice indicating
389 | where to find the applicable terms.
390 |
391 | Additional terms, permissive or non-permissive, may be stated in the
392 | form of a separately written license, or stated as exceptions;
393 | the above requirements apply either way.
394 |
395 | 8. Termination.
396 |
397 | You may not propagate or modify a covered work except as expressly
398 | provided under this License. Any attempt otherwise to propagate or
399 | modify it is void, and will automatically terminate your rights under
400 | this License (including any patent licenses granted under the third
401 | paragraph of section 11).
402 |
403 | However, if you cease all violation of this License, then your
404 | license from a particular copyright holder is reinstated (a)
405 | provisionally, unless and until the copyright holder explicitly and
406 | finally terminates your license, and (b) permanently, if the copyright
407 | holder fails to notify you of the violation by some reasonable means
408 | prior to 60 days after the cessation.
409 |
410 | Moreover, your license from a particular copyright holder is
411 | reinstated permanently if the copyright holder notifies you of the
412 | violation by some reasonable means, this is the first time you have
413 | received notice of violation of this License (for any work) from that
414 | copyright holder, and you cure the violation prior to 30 days after
415 | your receipt of the notice.
416 |
417 | Termination of your rights under this section does not terminate the
418 | licenses of parties who have received copies or rights from you under
419 | this License. If your rights have been terminated and not permanently
420 | reinstated, you do not qualify to receive new licenses for the same
421 | material under section 10.
422 |
423 | 9. Acceptance Not Required for Having Copies.
424 |
425 | You are not required to accept this License in order to receive or
426 | run a copy of the Program. Ancillary propagation of a covered work
427 | occurring solely as a consequence of using peer-to-peer transmission
428 | to receive a copy likewise does not require acceptance. However,
429 | nothing other than this License grants you permission to propagate or
430 | modify any covered work. These actions infringe copyright if you do
431 | not accept this License. Therefore, by modifying or propagating a
432 | covered work, you indicate your acceptance of this License to do so.
433 |
434 | 10. Automatic Licensing of Downstream Recipients.
435 |
436 | Each time you convey a covered work, the recipient automatically
437 | receives a license from the original licensors, to run, modify and
438 | propagate that work, subject to this License. You are not responsible
439 | for enforcing compliance by third parties with this License.
440 |
441 | An "entity transaction" is a transaction transferring control of an
442 | organization, or substantially all assets of one, or subdividing an
443 | organization, or merging organizations. If propagation of a covered
444 | work results from an entity transaction, each party to that
445 | transaction who receives a copy of the work also receives whatever
446 | licenses to the work the party's predecessor in interest had or could
447 | give under the previous paragraph, plus a right to possession of the
448 | Corresponding Source of the work from the predecessor in interest, if
449 | the predecessor has it or can get it with reasonable efforts.
450 |
451 | You may not impose any further restrictions on the exercise of the
452 | rights granted or affirmed under this License. For example, you may
453 | not impose a license fee, royalty, or other charge for exercise of
454 | rights granted under this License, and you may not initiate litigation
455 | (including a cross-claim or counterclaim in a lawsuit) alleging that
456 | any patent claim is infringed by making, using, selling, offering for
457 | sale, or importing the Program or any portion of it.
458 |
459 | 11. Patents.
460 |
461 | A "contributor" is a copyright holder who authorizes use under this
462 | License of the Program or a work on which the Program is based. The
463 | work thus licensed is called the contributor's "contributor version".
464 |
465 | A contributor's "essential patent claims" are all patent claims
466 | owned or controlled by the contributor, whether already acquired or
467 | hereafter acquired, that would be infringed by some manner, permitted
468 | by this License, of making, using, or selling its contributor version,
469 | but do not include claims that would be infringed only as a
470 | consequence of further modification of the contributor version. For
471 | purposes of this definition, "control" includes the right to grant
472 | patent sublicenses in a manner consistent with the requirements of
473 | this License.
474 |
475 | Each contributor grants you a non-exclusive, worldwide, royalty-free
476 | patent license under the contributor's essential patent claims, to
477 | make, use, sell, offer for sale, import and otherwise run, modify and
478 | propagate the contents of its contributor version.
479 |
480 | In the following three paragraphs, a "patent license" is any express
481 | agreement or commitment, however denominated, not to enforce a patent
482 | (such as an express permission to practice a patent or covenant not to
483 | sue for patent infringement). To "grant" such a patent license to a
484 | party means to make such an agreement or commitment not to enforce a
485 | patent against the party.
486 |
487 | If you convey a covered work, knowingly relying on a patent license,
488 | and the Corresponding Source of the work is not available for anyone
489 | to copy, free of charge and under the terms of this License, through a
490 | publicly available network server or other readily accessible means,
491 | then you must either (1) cause the Corresponding Source to be so
492 | available, or (2) arrange to deprive yourself of the benefit of the
493 | patent license for this particular work, or (3) arrange, in a manner
494 | consistent with the requirements of this License, to extend the patent
495 | license to downstream recipients. "Knowingly relying" means you have
496 | actual knowledge that, but for the patent license, your conveying the
497 | covered work in a country, or your recipient's use of the covered work
498 | in a country, would infringe one or more identifiable patents in that
499 | country that you have reason to believe are valid.
500 |
501 | If, pursuant to or in connection with a single transaction or
502 | arrangement, you convey, or propagate by procuring conveyance of, a
503 | covered work, and grant a patent license to some of the parties
504 | receiving the covered work authorizing them to use, propagate, modify
505 | or convey a specific copy of the covered work, then the patent license
506 | you grant is automatically extended to all recipients of the covered
507 | work and works based on it.
508 |
509 | A patent license is "discriminatory" if it does not include within
510 | the scope of its coverage, prohibits the exercise of, or is
511 | conditioned on the non-exercise of one or more of the rights that are
512 | specifically granted under this License. You may not convey a covered
513 | work if you are a party to an arrangement with a third party that is
514 | in the business of distributing software, under which you make payment
515 | to the third party based on the extent of your activity of conveying
516 | the work, and under which the third party grants, to any of the
517 | parties who would receive the covered work from you, a discriminatory
518 | patent license (a) in connection with copies of the covered work
519 | conveyed by you (or copies made from those copies), or (b) primarily
520 | for and in connection with specific products or compilations that
521 | contain the covered work, unless you entered into that arrangement,
522 | or that patent license was granted, prior to 28 March 2007.
523 |
524 | Nothing in this License shall be construed as excluding or limiting
525 | any implied license or other defenses to infringement that may
526 | otherwise be available to you under applicable patent law.
527 |
528 | 12. No Surrender of Others' Freedom.
529 |
530 | If conditions are imposed on you (whether by court order, agreement or
531 | otherwise) that contradict the conditions of this License, they do not
532 | excuse you from the conditions of this License. If you cannot convey a
533 | covered work so as to satisfy simultaneously your obligations under this
534 | License and any other pertinent obligations, then as a consequence you may
535 | not convey it at all. For example, if you agree to terms that obligate you
536 | to collect a royalty for further conveying from those to whom you convey
537 | the Program, the only way you could satisfy both those terms and this
538 | License would be to refrain entirely from conveying the Program.
539 |
540 | 13. Remote Network Interaction; Use with the GNU General Public License.
541 |
542 | Notwithstanding any other provision of this License, if you modify the
543 | Program, your modified version must prominently offer all users
544 | interacting with it remotely through a computer network (if your version
545 | supports such interaction) an opportunity to receive the Corresponding
546 | Source of your version by providing access to the Corresponding Source
547 | from a network server at no charge, through some standard or customary
548 | means of facilitating copying of software. This Corresponding Source
549 | shall include the Corresponding Source for any work covered by version 3
550 | of the GNU General Public License that is incorporated pursuant to the
551 | following paragraph.
552 |
553 | Notwithstanding any other provision of this License, you have
554 | permission to link or combine any covered work with a work licensed
555 | under version 3 of the GNU General Public License into a single
556 | combined work, and to convey the resulting work. The terms of this
557 | License will continue to apply to the part which is the covered work,
558 | but the work with which it is combined will remain governed by version
559 | 3 of the GNU General Public License.
560 |
561 | 14. Revised Versions of this License.
562 |
563 | The Free Software Foundation may publish revised and/or new versions of
564 | the GNU Affero General Public License from time to time. Such new versions
565 | will be similar in spirit to the present version, but may differ in detail to
566 | address new problems or concerns.
567 |
568 | Each version is given a distinguishing version number. If the
569 | Program specifies that a certain numbered version of the GNU Affero General
570 | Public License "or any later version" applies to it, you have the
571 | option of following the terms and conditions either of that numbered
572 | version or of any later version published by the Free Software
573 | Foundation. If the Program does not specify a version number of the
574 | GNU Affero General Public License, you may choose any version ever published
575 | by the Free Software Foundation.
576 |
577 | If the Program specifies that a proxy can decide which future
578 | versions of the GNU Affero General Public License can be used, that proxy's
579 | public statement of acceptance of a version permanently authorizes you
580 | to choose that version for the Program.
581 |
582 | Later license versions may give you additional or different
583 | permissions. However, no additional obligations are imposed on any
584 | author or copyright holder as a result of your choosing to follow a
585 | later version.
586 |
587 | 15. Disclaimer of Warranty.
588 |
589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597 |
598 | 16. Limitation of Liability.
599 |
600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608 | SUCH DAMAGES.
609 |
610 | 17. Interpretation of Sections 15 and 16.
611 |
612 | If the disclaimer of warranty and limitation of liability provided
613 | above cannot be given local legal effect according to their terms,
614 | reviewing courts shall apply local law that most closely approximates
615 | an absolute waiver of all civil liability in connection with the
616 | Program, unless a warranty or assumption of liability accompanies a
617 | copy of the Program in return for a fee.
618 |
619 | END OF TERMS AND CONDITIONS
620 |
621 | How to Apply These Terms to Your New Programs
622 |
623 | If you develop a new program, and you want it to be of the greatest
624 | possible use to the public, the best way to achieve this is to make it
625 | free software which everyone can redistribute and change under these terms.
626 |
627 | To do so, attach the following notices to the program. It is safest
628 | to attach them to the start of each source file to most effectively
629 | state the exclusion of warranty; and each file should have at least
630 | the "copyright" line and a pointer to where the full notice is found.
631 |
632 |
633 | Copyright (C)
634 |
635 | This program is free software: you can redistribute it and/or modify
636 | it under the terms of the GNU Affero General Public License as published
637 | by the Free Software Foundation, either version 3 of the License, or
638 | (at your option) any later version.
639 |
640 | This program is distributed in the hope that it will be useful,
641 | but WITHOUT ANY WARRANTY; without even the implied warranty of
642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643 | GNU Affero General Public License for more details.
644 |
645 | You should have received a copy of the GNU Affero General Public License
646 | along with this program. If not, see .
647 |
648 | Also add information on how to contact you by electronic and paper mail.
649 |
650 | If your software can interact with users remotely through a computer
651 | network, you should also make sure that it provides a way for users to
652 | get its source. For example, if your program is a web application, its
653 | interface could display a "Source" link that leads users to an archive
654 | of the code. There are many ways you could offer source, and different
655 | solutions will be better for different programs; see section 13 for the
656 | specific requirements.
657 |
658 | You should also get your employer (if you work as a programmer) or school,
659 | if any, to sign a "copyright disclaimer" for the program, if necessary.
660 | For more information on this, and how to apply and follow the GNU AGPL, see
661 | .
662 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .PHONY: test
2 |
3 | FILE = VERSION
4 | VERSION := $(shell git describe --abbrev=0 --tags 2>/dev/null)
5 | DATE := $(shell git log -1 --format=%ct $(VERSION) 2>/dev/null)
6 |
7 | # Cover non-git case (compiling from release source package)
8 | # The date will be set to the time of compilation, the tag will be consistent
9 | ifeq ($(VERSION),)
10 | VERSION := $(shell cat $(FILE))
11 | DATE := $(shell date +%s)
12 | endif
13 |
14 | build:
15 | @go build \
16 | -buildmode=pie \
17 | -ldflags="-X 'main.version=$(VERSION)' -X 'main.date=$(DATE)'" \
18 | -o $(shell go env GOPATH)/bin/fzn ./cmd/term
19 | @echo "Build complete"
20 |
21 | # The following are sequential commands, but I'm separating to reduce the chance of mistakes...
22 | new-tag:
23 | test $(tag)
24 | echo "$(tag)" > $(FILE)
25 | git add $(FILE)
26 | git commit -m "Release: $(tag)"
27 | git tag $(tag)
28 | release:
29 | git push
30 | git push --tags
31 | goreleaser release --rm-dist
32 |
33 | test:
34 | go test ./... -count=1
35 |
36 | debug:
37 | dlv debug ./cmd/term/main.go
38 |
39 | test-debug:
40 | dlv test pkg/service/*
41 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Fuzzynote (fzn)
2 | 
3 | 
4 | 
5 | ---
6 |
7 | - [Installation](#installation)
8 | - [Quickstart](#quickstart)
9 | - [Controls](#controls)
10 | - [Configuration](#configuration)
11 | - [Import/export](#importexport)
12 | - [Future Plans](#future-plans)
13 | - [Issues/Considerations](#issuesconsiderations)
14 | - [Tests](#tests)
15 |
16 | [Follow me on Twitter](https://twitter.com/fzn_sam) for the latest `fzn` updates and announcements, or just to watch me talk to myself.
17 |
18 | ---
19 |
20 | # Terminal-based, hyper-fast, CRDT-backed, collaborative note-taking tool
21 |
22 | ## Simple, powerful, extremely fast search
23 |
24 | `fzn` is local-first; remote-second. It acts on local in-memory data, meaning no expensive I/O and native speeds.
25 |
26 | Instant search (over large datasets) via any number of full or fuzzy match groups.
27 |
28 | Zoom in using common prefixes.
29 |
30 |
31 | 
32 |
33 | #### Things the user does in this gif :point_up::
34 |
35 | 1. Opens `fzn`
36 | 2. Fuzzy searches for `shopping`, highlights matching lines with `Ctrl-s`, and zooms on common prefix with `Enter` (`=` denotes a full string match).
37 | 3. Adds new line (with auto-prepended `shopping ` prefix)
38 | 4. Presses `Escape` to go to search line
39 | 5. Fuzzy searches for `impo`, highlights and zooms on `important project`
40 | 6. Focuses on line ``write `hello` script``, and opens a note buffer (in vim) with `Ctrl-o`
41 | 7. Adds the script, then saves and closes the vim buffer, thus attaching the note to the line
42 | 8. Fuzzy matches on `fzn`, focuses on a line with a URL, and presses `Ctrl-_` to match the URL and open in default browser
43 |
44 | ## Real time collaboration
45 |
46 | Backed by a [CRDT](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type)-based, append-only, mergeable text database.
47 |
48 | Collaboration is done on a per-line/item basis; by adding `@{email}` to the item. Shared lists are displayed by adding the email as a search group.
49 |
50 | Offline changes will sync later with guaranteed/consistent output.
51 |
52 | In short, you can collaborate on multiple "documents" from the same view at the same time.
53 |
54 |
55 | # S3 ([quickstart](#setup-an-s3-remote))
56 |
57 | Configure an S3 bucket yourself to sync between machines/users (accessed via access key/secret)
58 |
59 | # Installation
60 |
61 | ## Local compilation
62 |
63 | Compile locally (requires Go):
64 |
65 | ```shell
66 | git clone git@github.com:Sambigeara/fuzzynote.git
67 | cd fuzzynote
68 | make build
69 | ```
70 |
71 | ## Direct download
72 |
73 | From the [releases page](https://github.com/Sambigeara/fuzzynote/releases/latest).
74 |
75 | ## ArchLinux
76 |
77 | [Link to AUR package](https://aur.archlinux.org/packages/fuzzynote/).
78 |
79 | ArchLinux users can build and install `fzn` with:
80 |
81 | ```shell
82 | yay -S fuzzynote
83 | ```
84 |
85 | # Quickstart
86 |
87 | - [Basic usage](#basic-usage)
88 | - [Web sign-up, terminal login](#web-sign-up-terminal-login)
89 | - [Add a friend](#add-a-friend)
90 | - [Share a line with a friend](#share-a-line-with-a-friend)
91 | - [Setup an S3 remote](#setup-an-s3-remote)
92 |
93 | ## Basic usage
94 |
95 | 1. [Install `fzn`](#installation)
96 | 2. Start
97 | ```shell
98 | ./fzn
99 | ```
100 |
101 | ## Web sign-up, terminal login
102 |
103 | 1. [Install `fzn`](#installation)
104 | 2. Sign up [here](https://fzn.auth.fuzzynote.co.uk/signup?client_id=5a7brt2fuvlfnl8aql1af3758m&redirect_uri=https%3A%2F%2Ffuzzynote.app&response_type=token&scope=profile+email+openid)
105 | 3. Login and follow prompts
106 | ```shell
107 | ./fzn login
108 | ```
109 | 4. Start
110 | ```shell
111 | ./fzn
112 | ```
113 |
114 | ## Add a friend
115 |
116 | 1. Sign up/log in
117 | 2. Add a line starting with `fzn_cfg:friend `, e.g.
118 | ```txt
119 | fzn_cfg:friend {friends_email}
120 | ```
121 | 3. Ensure you are are connected to the internet
122 | 4. Get your friend to do the same (emails swopped, of course)
123 |
124 | ## Share a line with a friend
125 |
126 | 1. Sign up/log in
127 | 2. Add a friend, and have them add you, as per the above
128 | 3. In the line you want to share, write the friends email, prefixed with a `@`, e.g.
129 | ```txt
130 | fzn_cfg:friend joe@bloggs.com
131 | Some random line I want to share @joe@bloggs.com
132 | ```
133 |
134 | ## Setup an S3 remote
135 |
136 | 1. Configure an S3 bucket with access via access key/secret - [link to AWS docs](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html).
137 |
138 | 2. Create a file called `config.yml` in the `fzn` root directory. By default, this is at `$HOME/.fzn/` on `*nix` systems, or `%USERPROFILE%\.fzn` on Windows. If you've already run `fzn`, the root directory will have a `primary.db` and one or more `wal_*.db` files, for reference.
139 |
140 | 3. Add the following to the file, using key/secret from above:
141 | ```yml
142 | s3:
143 | - key: {AWS_ACCESS_KEY}
144 | secret: {AWS_SECRET}
145 | bucket: bucket_name
146 | prefix: some_prefix
147 | ```
148 |
149 | 4. Start the app, if you haven't already
150 | ```shell
151 | ./fzn
152 | ```
153 |
154 | ## Other remote platforms?
155 |
156 | At present `fzn` only supports S3 as a remote target. However, it is easily extensible, so if there is demand for additional platforms, then please make a request via a [new issue](https://github.com/Sambigeara/fuzzynote/issues/new)!
157 |
158 | # Controls
159 |
160 | ## Navigation
161 |
162 | - General navigation: `Arrow keys`
163 | - Go to start of line: `Ctrl-a`
164 | - Go to end of line: `Ctrl-e`
165 | - Go to search line: `ESCAPE`
166 | - Exit: `Double ESCAPE`
167 |
168 | ## Search (top line)
169 |
170 | Any number of tab-separated search groups are applied to the lists independently. Use full, fuzzy, or inverse string matching.
171 |
172 | - Fuzzy string match, start the search group with `~`
173 | - Inverse string match (full strings), start the search group with `!`
174 | - Separate search groups: `TAB`
175 |
176 | ```shell
177 | ~foo # matches "fobo"
178 | foo # will not match "fobo"
179 | !foo # will ignore any lines with "foo" in it
180 | ```
181 |
182 | ## List items (lines)
183 |
184 | - Add new line (prepending search line text to new line): `Enter`
185 | - Delete line: `Ctrl-d`
186 | - Undo/Redo last operation: `Ctrl-u/Ctrl-r`
187 | - Moves current item up or down: `PageUp/PageDown`
188 | - Open note on the currently selected list item in selected terminal editor (default is Vim). Save in editor saves to list item: `Ctrl-o`
189 | - Copy current item into buffer: `Ctrl-c`
190 | - Paste current item from buffer: `Ctrl-p`
191 |
192 | ## Group operations
193 |
194 | - Select item under cursor: `Ctrl-s`
195 | - Set common prefix string to search line: `Enter`
196 | - Clear selected items: `Escape`
197 |
198 | ## Archive
199 |
200 | - Globally display/hide archived items: `Ctrl-v (top line)`
201 | - Archive/un-archive list item: `Ctrl-v`
202 |
203 | ## Handy functions
204 |
205 | - Open first URL in list item: `Ctrl-_`
206 | - Copy first URL from list item into the system clipboard: `Ctrl-c`
207 | - Export current matched lines to text file (will output to `current_dir/export_*.txt`): `Ctrl-^`
208 |
209 | ## Token operators
210 |
211 | The following character combinations will parse to different useful outputs:
212 |
213 | - `{d}`: A date in the form `Mon, Jan 2, 2006`
214 |
215 | # Configuration
216 |
217 | `fzn --help` will print out the configurable options.
218 |
219 | ```
220 | > fzn --help
221 | Usage: fzn [options] [arguments]
222 |
223 | OPTIONS
224 | --root/$FZN_ROOT
225 | --colour/$FZN_COLOUR (default: light)
226 | --editor/$FZN_EDITOR (default: vim)
227 | --help/-h
228 | display this help message
229 | --version/-v
230 | display version information
231 | ```
232 |
233 | - `editor`: specifies the terminal editor which is used when opening notes on list items. `vim`, `emacs` and `nano` all appear to work. Others may too.
234 | - `sync-frequency-ms`/`gather-frequency-ms`: these can be ignored for now
235 | - `root`: **(mostly for testing and can be ignored for general use)** specifies the directory that `fzn` will treat as it's root. By default, this is at `$HOME/.fzn/` on `*nix` systems, or `%USERPROFILE%\.fzn` on Windows.
236 |
237 | # Import/Export
238 |
239 | `fzn` supports importing from and exporting to line separated plain text files.
240 |
241 | ## Import
242 |
243 | Import will generate a new set of list items based on individual lines in a plain text file. You need to specify the visibility each time as per the examples below:
244 |
245 | ```shell
246 | ./fzn import path/to/file --show # Items will be shown by default
247 | ./fzn import --hide path/to/file # Items will be hidden by default
248 | ```
249 |
250 | ## Export
251 |
252 | Export allows you to generate a plain text file (in the directory from which `fzn` was invoked) based on the current match-set in the app. In short: search for something, press `Ctrl-^`, and `fzn` will spit out a file named something along the lines of `export_*.txt`.
253 |
254 | # Future plans
255 |
256 | - E2E encryption
257 |
258 | # Issues/Considerations
259 |
260 | The terminal client is fully functioning, however given the early stages of the project, and the (at points) rapid development, there are likely to be some bugs hanging around. Things to look out for:
261 |
262 | - Sometimes the sync algorithm gets confused. Usually, all that is needed is just to add or delete a line or character (adding additional events to the WAL will trigger a flush and get things moving). If that doesn't work, turning it off and on again usually does the trick.
263 | - Notice something wrong? Please do [open an issue](https://github.com/Sambigeara/fuzzynote/issues/new)!
264 |
265 | # Tests
266 |
267 | Almost entirely broken. Fuzzynote has undergone some fairly substantial changes over the past few months - the test suite will be updated to suit in due course (please don't judge me).
268 |
--------------------------------------------------------------------------------
/VERSION:
--------------------------------------------------------------------------------
1 | v0.25.5
2 |
--------------------------------------------------------------------------------
/cmd/debug/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | func main() {
4 | // Copy walfile into `debug/` dir and run e.g.
5 | // go run cmd/debug/main.go "1323068878:1642754346644000000"
6 |
7 | //if len(os.Args) != 2 {
8 | //log.Print("provide list item key as a single argument")
9 | //os.Exit(0)
10 | //}
11 |
12 | ////key := os.Args[1]
13 |
14 | //root := "debug/"
15 | //os.Mkdir(root, os.ModePerm)
16 |
17 | //webTokens := service.NewFileWebTokenStore(root)
18 | //localWalFile := service.NewLocalFileWalFile(root)
19 | //r := service.NewDBListRepo(localWalFile, webTokens)
20 |
21 | ////r.DebugWriteEventsToFile(root, key)
22 | //c := make(chan []service.EventLog)
23 | //go func() {
24 | //for {
25 | //wal := <-c
26 | //if err := r.Replay(wal); err != nil {
27 | //return
28 | //}
29 | //}
30 | //}()
31 | //r.TestPullLocal(c)
32 | //matches, _, _ := r.Match([][]rune{}, true, "", 0, 0)
33 | //_ = matches
34 | //runtime.Breakpoint()
35 | }
36 |
--------------------------------------------------------------------------------
/cmd/term/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "log"
7 | "os"
8 | "path"
9 | "strconv"
10 | "time"
11 |
12 | "github.com/ardanlabs/conf"
13 |
14 | "github.com/sambigeara/fuzzynote/pkg/prompt"
15 | "github.com/sambigeara/fuzzynote/pkg/s3"
16 | "github.com/sambigeara/fuzzynote/pkg/service"
17 | "github.com/sambigeara/fuzzynote/pkg/term"
18 | )
19 |
20 | const (
21 | namespace = "FZN"
22 | loginArg = "login"
23 | deleteArg = "delete"
24 | importArg = "import"
25 | )
26 |
27 | var (
28 | version = "development"
29 | date = "0" // the linker passes a unixtime string which needs to be converted
30 | )
31 |
32 | func main() {
33 | var cfg struct {
34 | Version conf.Version
35 | Root string
36 | Colour string `conf:"default:light"`
37 | Editor string `conf:"default:vim"`
38 | Args conf.Args
39 | }
40 |
41 | // Pre-instantiate default root direct (can't pass value dynamically to default above)
42 | home, err := os.UserHomeDir()
43 | if err != nil {
44 | log.Fatal(err)
45 | }
46 | cfg.Root = path.Join(home, ".fzn/")
47 |
48 | // Override if set via CLI/envvar
49 | if err := conf.Parse(os.Args[1:], namespace, &cfg); err != nil {
50 | // Handle `--help` on first attempt of parsing inputs
51 | if err == conf.ErrHelpWanted {
52 | usage, err := conf.Usage(namespace, &cfg)
53 | if err != nil {
54 | log.Fatalf("generating config usage: %s", err)
55 | }
56 | fmt.Println(usage)
57 | os.Exit(0)
58 | } else if err == conf.ErrVersionWanted {
59 | // Convert to ISO-8601 date, fail silently if unable
60 | dateString := "1970-01-01"
61 | i, err := strconv.ParseInt(date, 10, 64)
62 | if err == nil {
63 | unixTime := time.Unix(i, 0)
64 | dateString = unixTime.Format("2006-01-02")
65 | }
66 | fmt.Printf("fuzzynote %s (%s)\n", version, dateString)
67 | os.Exit(0)
68 | }
69 | log.Fatalf("main : Parsing Root Config : %v", err)
70 | }
71 |
72 | // Make sure the root directory exists
73 | // This also occurs in NewDBListRepo, but is required in the Login/WebToken flows below, so ensure
74 | // existence here.
75 | os.Mkdir(cfg.Root, os.ModePerm)
76 |
77 | // Create and register local app WalFile (based in root directory)
78 | localWalFile := service.NewLocalFileWalFile(cfg.Root)
79 |
80 | // Check for Login or Remotes management flow (run and exit - bypassing the main program)
81 | if len(cfg.Args) > 0 {
82 | switch cfg.Args.Num(0) {
83 | case loginArg:
84 | prompt.Login(cfg.Root)
85 | case deleteArg:
86 | localWalFile.Purge()
87 | case importArg:
88 | // Gather and assert existence of the remaining args.
89 | // Bit of an odd way of handling it, but we need to assert existence of `--show` or `--hide` explicitly, and then accept any
90 | // arbitrary input for the file path (within reason)
91 | filePath := ""
92 | visibilityArg := ""
93 | for i := 1; i <= 2; i++ {
94 | switch a := cfg.Args.Num(i); a {
95 | case "--show":
96 | visibilityArg = "s"
97 | case "--hide":
98 | visibilityArg = "h"
99 | default:
100 | filePath = a
101 | }
102 | }
103 | if filePath == "" || visibilityArg == "" {
104 | fmt.Println("please specify imported item visibility via one of: `--show` or `--hide`.\ne.g: `./fzn import --show path/to/file`")
105 | os.Exit(0)
106 | }
107 |
108 | hideItems := false
109 | if visibilityArg == "h" {
110 | hideItems = true
111 | }
112 |
113 | curWd, err := os.Getwd()
114 | if err != nil {
115 | fmt.Println("failed to retrieve local directory")
116 | os.Exit(1)
117 | }
118 |
119 | f, err := os.Open(path.Join(curWd, filePath))
120 | if err != nil {
121 | fmt.Println("failed to open plain text file:", filePath)
122 | os.Exit(0)
123 | }
124 | defer f.Close()
125 |
126 | if err := service.BuildWalFromPlainText(context.Background(), localWalFile, f, hideItems); err != nil {
127 | fmt.Println("failed to generate wal file from plain text file")
128 | os.Exit(1)
129 | }
130 | os.Exit(0)
131 | default:
132 | fmt.Println("unrecognised arg:", cfg.Args.Num(0))
133 | os.Exit(0)
134 | }
135 | }
136 |
137 | // Generate FileWebTokenStore
138 | webTokens := service.NewFileWebTokenStore(cfg.Root)
139 |
140 | // Instantiate listRepo
141 | listRepo := service.NewDBListRepo(
142 | localWalFile,
143 | webTokens,
144 | )
145 |
146 | s3Remotes := s3.GetS3Config(cfg.Root)
147 | for _, r := range s3Remotes {
148 | // centralise this logic across different remote types when relevant
149 | // TODO gracefully deal with missing config
150 | s3FileWal := s3.NewS3WalFile(r, cfg.Root)
151 | listRepo.AddWalFile(s3FileWal, true)
152 | }
153 |
154 | // Create term client
155 | client := term.NewTerm(listRepo, cfg.Colour, cfg.Editor)
156 |
157 | fmt.Println(listRepo.Start(client))
158 | }
159 |
--------------------------------------------------------------------------------
/demos/basic.gif:
--------------------------------------------------------------------------------
1 | version https://git-lfs.github.com/spec/v1
2 | oid sha256:6c139308145987be4ddd0c3e088bcfa6ceb555cba9456bbc1920b87e68667b4b
3 | size 2761352
4 |
--------------------------------------------------------------------------------
/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/sambigeara/fuzzynote
2 |
3 | go 1.18
4 |
5 | require (
6 | github.com/ardanlabs/conf v1.5.0
7 | github.com/atotto/clipboard v0.1.4
8 | github.com/aws/aws-sdk-go v1.40.33
9 | github.com/gdamore/tcell/v2 v2.4.0
10 | github.com/manifoldco/promptui v0.8.0
11 | github.com/mattn/go-runewidth v0.0.13
12 | gopkg.in/yaml.v2 v2.4.0
13 | mvdan.cc/xurls/v2 v2.3.0
14 | nhooyr.io/websocket v1.8.7
15 | )
16 |
17 | require (
18 | github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
19 | github.com/gdamore/encoding v1.0.0 // indirect
20 | github.com/gin-gonic/gin v1.9.1 // indirect
21 | github.com/golang/protobuf v1.5.0 // indirect
22 | github.com/google/go-cmp v0.5.6 // indirect
23 | github.com/jmespath/go-jmespath v0.4.0 // indirect
24 | github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect
25 | github.com/klauspost/compress v1.10.3 // indirect
26 | github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
27 | github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a // indirect
28 | github.com/mattn/go-colorable v0.0.9 // indirect
29 | github.com/mattn/go-isatty v0.0.19 // indirect
30 | github.com/rivo/uniseg v0.2.0 // indirect
31 | golang.org/x/sys v0.8.0 // indirect
32 | golang.org/x/term v0.8.0 // indirect
33 | golang.org/x/text v0.9.0 // indirect
34 | )
35 |
--------------------------------------------------------------------------------
/go.sum:
--------------------------------------------------------------------------------
1 | github.com/ardanlabs/conf v1.5.0 h1:5TwP6Wu9Xi07eLFEpiCUF3oQXh9UzHMDVnD3u/I5d5c=
2 | github.com/ardanlabs/conf v1.5.0/go.mod h1:ILsMo9dMqYzCxDjDXTiwMI0IgxOJd0MOiucbQY2wlJw=
3 | github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
4 | github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
5 | github.com/aws/aws-sdk-go v1.40.33 h1:I9CCcb+jCC73//P+5mqeHzIMwTzJ6MDEZm8b/XoSg/w=
6 | github.com/aws/aws-sdk-go v1.40.33/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q=
7 | github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
8 | github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
9 | github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
10 | github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
11 | github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
12 | github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
13 | github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
14 | github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
15 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
16 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
17 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
18 | github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
19 | github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
20 | github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
21 | github.com/gdamore/tcell/v2 v2.4.0 h1:W6dxJEmaxYvhICFoTY3WrLLEXsQ11SaFnKGVEXW57KM=
22 | github.com/gdamore/tcell/v2 v2.4.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
23 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
24 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
25 | github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
26 | github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
27 | github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
28 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
29 | github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
30 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
31 | github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
32 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
33 | github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
34 | github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
35 | github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
36 | github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
37 | github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
38 | github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
39 | github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
40 | github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
41 | github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
42 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
43 | github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk=
44 | github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
45 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
46 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
47 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
48 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
49 | github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
50 | github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
51 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
52 | github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
53 | github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
54 | github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
55 | github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
56 | github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
57 | github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
58 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
59 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
60 | github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
61 | github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
62 | github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=
63 | github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
64 | github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
65 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
66 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
67 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
68 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
69 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
70 | github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
71 | github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
72 | github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
73 | github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
74 | github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw=
75 | github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
76 | github.com/manifoldco/promptui v0.8.0 h1:R95mMF+McvXZQ7j1g8ucVZE1gLP3Sv6j9vlF9kyRqQo=
77 | github.com/manifoldco/promptui v0.8.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ=
78 | github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
79 | github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
80 | github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
81 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
82 | github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
83 | github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
84 | github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
85 | github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
86 | github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
87 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
88 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
89 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
90 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
91 | github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
92 | github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
93 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
94 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
95 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
96 | github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
97 | github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
98 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
99 | github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
100 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
101 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
102 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
103 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
104 | github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
105 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
106 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
107 | github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
108 | golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
109 | golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
110 | golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
111 | golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
112 | golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
113 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
114 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
115 | golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
116 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
117 | golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
118 | golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
119 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
120 | golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
121 | golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
122 | golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
123 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
124 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
125 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
126 | golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
127 | golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
128 | golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
129 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
130 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
131 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
132 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
133 | google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
134 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
135 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
136 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
137 | gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
138 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
139 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
140 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
141 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
142 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
143 | mvdan.cc/xurls/v2 v2.3.0 h1:59Olnbt67UKpxF1EwVBopJvkSUBmgtb468E4GVWIZ1I=
144 | mvdan.cc/xurls/v2 v2.3.0/go.mod h1:AjuTy7gEiUArFMjgBBDU4SMxlfUYsRokpJQgNWOt3e4=
145 | nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
146 | nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
147 |
--------------------------------------------------------------------------------
/pkg/prompt/prompt.go:
--------------------------------------------------------------------------------
1 | package prompt
2 |
3 | import (
4 | "errors"
5 | "fmt"
6 | "os"
7 | "regexp"
8 |
9 | "github.com/manifoldco/promptui"
10 |
11 | "github.com/sambigeara/fuzzynote/pkg/service"
12 | )
13 |
14 | var emailRegex = regexp.MustCompile("[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*")
15 |
16 | // isEmailValid checks if the email provided passes the required structure and length.
17 | func isEmailValid(e string) error {
18 | if len(e) < 3 && len(e) > 254 || !emailRegex.MatchString(e) {
19 | return errors.New("Invalid email address")
20 | }
21 | return nil
22 | }
23 |
24 | // Login starts an interacting CLI flow to accept user credentials, and uses them to try and authenticate.
25 | // If successful, the access and refresh tokens will be stored in memory, and persisted locally (dependent on the
26 | // client).
27 | func Login(root string) {
28 | defer os.Exit(0)
29 |
30 | prompt := promptui.Prompt{
31 | Label: "Enter email",
32 | Validate: isEmailValid,
33 | }
34 | email, err := prompt.Run()
35 | if err != nil {
36 | fmt.Printf("Prompt failed %v\n", err)
37 | os.Exit(1)
38 | }
39 |
40 | prompt = promptui.Prompt{
41 | Label: "Enter password",
42 | Validate: func(input string) error {
43 | if len(input) < 6 {
44 | return errors.New("Password must have more than 6 characters")
45 | }
46 | return nil
47 | },
48 | Mask: '*',
49 | }
50 | password, err := prompt.Run()
51 | if err != nil {
52 | fmt.Printf("Prompt failed %v\n", err)
53 | os.Exit(1)
54 | }
55 |
56 | // Attempt to authenticate
57 | body := map[string]string{
58 | "user": email,
59 | "password": password,
60 | }
61 |
62 | wt := service.NewFileWebTokenStore(root)
63 | err = service.Authenticate(wt, body)
64 | if err != nil {
65 | fmt.Print("Login unsuccessful :(\n")
66 | os.Exit(0)
67 | }
68 |
69 | fmt.Print("Login successful!")
70 | }
71 |
--------------------------------------------------------------------------------
/pkg/s3/s3.go:
--------------------------------------------------------------------------------
1 | package s3
2 |
3 | import (
4 | "bytes"
5 | "context"
6 | "fmt"
7 | "gopkg.in/yaml.v2"
8 | "io"
9 | "log"
10 | "os"
11 | "path"
12 | "strings"
13 |
14 | "github.com/aws/aws-sdk-go/aws"
15 | "github.com/aws/aws-sdk-go/aws/awserr"
16 | "github.com/aws/aws-sdk-go/aws/credentials"
17 | "github.com/aws/aws-sdk-go/aws/session"
18 | "github.com/aws/aws-sdk-go/service/s3"
19 | "github.com/aws/aws-sdk-go/service/s3/s3manager"
20 | )
21 |
22 | const (
23 | configFileName = "config.yml"
24 | walFilePattern = "wal_%v.db" // TODO dedup, as is in service package
25 | )
26 |
27 | type S3Remote struct {
28 | Key string
29 | Secret string
30 | Bucket string
31 | Prefix string
32 | }
33 |
34 | type Remotes struct {
35 | S3 []S3Remote
36 | }
37 |
38 | func GetS3Config(root string) []S3Remote {
39 | cfgFile := path.Join(root, configFileName)
40 | f, err := os.Open(cfgFile)
41 |
42 | r := Remotes{}
43 | if err == nil {
44 | decoder := yaml.NewDecoder(f)
45 | err = decoder.Decode(&r)
46 | if err != nil {
47 | //log.Fatalf("main : Parsing File Config : %v", err)
48 | // TODO handle with appropriate error message
49 | return r.S3
50 | }
51 | defer f.Close()
52 | }
53 | return r.S3
54 | }
55 |
56 | type s3WalFile struct {
57 | svc *s3.S3
58 | downloader *s3manager.Downloader
59 | uploader *s3manager.Uploader
60 | localRootDir string
61 | key string
62 | secret string
63 | bucket string
64 | prefix string
65 | }
66 |
67 | func NewS3WalFile(cfg S3Remote, root string) *s3WalFile {
68 | sess, err := session.NewSession(&aws.Config{
69 | Region: aws.String("eu-west-1"),
70 | Credentials: credentials.NewStaticCredentials(cfg.Key, cfg.Secret, ""),
71 | })
72 | if err != nil {
73 | log.Fatal(err)
74 | }
75 |
76 | return &s3WalFile{
77 | svc: s3.New(sess),
78 | downloader: s3manager.NewDownloader(sess),
79 | uploader: s3manager.NewUploader(sess),
80 | localRootDir: root,
81 | key: cfg.Key,
82 | secret: cfg.Secret,
83 | bucket: cfg.Bucket,
84 | prefix: cfg.Prefix,
85 | }
86 | }
87 |
88 | func (wf *s3WalFile) GetUUID() string {
89 | return wf.bucket + ":" + wf.GetRoot()
90 | }
91 |
92 | func (wf *s3WalFile) GetRoot() string {
93 | return wf.prefix
94 | }
95 |
96 | func (wf *s3WalFile) GetMatchingWals(ctx context.Context, matchPattern string) ([]string, error) {
97 | fileNames := []string{}
98 | // TODO matchPattern isn't actually doing anything atm
99 | resp, err := wf.svc.ListObjectsV2(&s3.ListObjectsV2Input{
100 | Bucket: aws.String(wf.bucket),
101 | Prefix: aws.String(wf.GetRoot()),
102 | })
103 | if err != nil {
104 | //exitErrorf("Unable to list items in bucket %q, %v", wf.bucket, err)
105 | return fileNames, err
106 | }
107 |
108 | for _, item := range resp.Contents {
109 | fileNames = append(fileNames, strings.Split(strings.Split(*item.Key, "_")[1], ".")[0])
110 | }
111 | return fileNames, nil
112 | }
113 |
114 | func (wf *s3WalFile) GetWalBytes(ctx context.Context, w io.Writer, fileName string) error {
115 | // TODO implement streaming
116 |
117 | // Read into bytes rather than file
118 | b := aws.NewWriteAtBuffer([]byte{})
119 |
120 | // Default concurrency = 5
121 | _, err := wf.downloader.Download(b,
122 | &s3.GetObjectInput{
123 | Bucket: aws.String(wf.bucket),
124 | Key: aws.String(fmt.Sprintf(path.Join(wf.GetRoot(), walFilePattern), fileName)),
125 | })
126 | if err != nil {
127 | // If the file has been removed, skip, as it means another process has already merged
128 | // and deleted this one
129 |
130 | if aerr, ok := err.(awserr.Error); ok {
131 | switch aerr.Code() {
132 | //case "NoSuchKey": // s3.ErrCodeNoSuchKey does not work, aws is missing this error code so we hardwire a string
133 | // return []EventLog{}, nil
134 | case s3.ErrCodeNoSuchKey:
135 | return nil
136 | default:
137 | //exitErrorf("Unable to download item %q, %v", fileName, err)
138 | // For now, continue silently rather than exiting
139 | return err
140 | }
141 | }
142 | }
143 |
144 | w.Write(b.Bytes())
145 | return nil
146 | }
147 |
148 | func (wf *s3WalFile) RemoveWals(ctx context.Context, fileNames []string) error {
149 | // Delete the item
150 | objects := []*s3.ObjectIdentifier{}
151 | for _, f := range fileNames {
152 | objects = append(objects, &s3.ObjectIdentifier{
153 | Key: aws.String(fmt.Sprintf(path.Join(wf.GetRoot(), walFilePattern), f)),
154 | })
155 | }
156 | //del := []s3.Delete{}
157 | _, err := wf.svc.DeleteObjects(&s3.DeleteObjectsInput{
158 | Bucket: aws.String(wf.bucket),
159 | Delete: &s3.Delete{
160 | Objects: objects,
161 | },
162 | })
163 | if err != nil {
164 | if aerr, ok := err.(awserr.Error); ok {
165 | switch aerr.Code() {
166 | //case "NotFound": // s3.ErrCodeNoSuchKey does not work, aws is missing this error code so we hardwire a string
167 | // return nil
168 | case s3.ErrCodeNoSuchKey:
169 | return nil
170 | default:
171 | //exitErrorf("Unable to delete objects %q from bucket %q, %v", fileNames, wf.bucket, err)
172 | // For now, continue silently rather than exiting
173 | return err
174 | }
175 | }
176 |
177 | }
178 |
179 | //err = wf.svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
180 | // Bucket: aws.String(wf.bucket),
181 | // Key: aws.String(fileName),
182 | //})
183 | //if err != nil {
184 | // exitErrorf("Error occurred while waiting for object %q to be deleted, %v", fileName, err)
185 | //}
186 |
187 | // TODO why was I also removing local??
188 | //return os.Remove(fileName)
189 | return nil
190 | }
191 |
192 | func (wf *s3WalFile) Flush(ctx context.Context, b *bytes.Buffer, randomUUID string) error {
193 | fileName := fmt.Sprintf(path.Join(wf.GetRoot(), walFilePattern), randomUUID)
194 | // IMPORTANT we need to take a copy here, because passing the bytes.Buffer as the Body io.Reader in
195 | // UploadInput means that the buffer is emptied on read.
196 | // In `gather`, we pass a single Buffer pointer to numerous `push` calls for efficiency - if this
197 | // s3 method is called before other walFile Flush calls, the buffer will be empty
198 | // TODO think about a less risky implementation of this...
199 | bCopy := *b
200 | _, err := wf.uploader.Upload(&s3manager.UploadInput{
201 | Bucket: aws.String(wf.bucket),
202 | Key: aws.String(fileName),
203 | //Body: b,
204 | Body: &bCopy,
205 | })
206 | if err != nil {
207 | //exitErrorf("Unable to upload %q to %q, %v", fileName, wf.bucket, err)
208 | // For now, continue silently rather than exiting
209 | return err
210 | }
211 | return nil
212 | }
213 |
214 | func exitErrorf(msg string, args ...interface{}) {
215 | fmt.Fprintf(os.Stderr, msg+"\n", args...)
216 | os.Exit(1)
217 | }
218 |
--------------------------------------------------------------------------------
/pkg/service/auth.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "bytes"
5 | "encoding/json"
6 | "io/ioutil"
7 | "log"
8 | "net/http"
9 | "net/url"
10 | "os"
11 | "path"
12 |
13 | "gopkg.in/yaml.v2"
14 | )
15 |
16 | const (
17 | webTokensFileName = ".tokens.yml"
18 | )
19 |
20 | type WebTokenStore interface {
21 | SetEmail(string)
22 | SetRefreshToken(string)
23 | SetIDToken(string)
24 | Email() string
25 | RefreshToken() string
26 | IDToken() string
27 | Flush()
28 | }
29 |
30 | type FileWebTokenStore struct {
31 | root string
32 | User string `yaml:"user"`
33 | Refresh string `yaml:"refreshToken"`
34 | ID string `yaml:"idToken"`
35 | }
36 |
37 | func NewFileWebTokenStore(root string) *FileWebTokenStore {
38 | // Attempt to read from file
39 | tokenFile := path.Join(root, webTokensFileName)
40 | f, err := os.Open(tokenFile)
41 | defer f.Close()
42 |
43 | wt := &FileWebTokenStore{root: root}
44 | if err == nil {
45 | decoder := yaml.NewDecoder(f)
46 | err = decoder.Decode(wt)
47 | if err != nil {
48 | log.Fatalf("main : Parsing Token File : %v", err)
49 | // TODO handle with appropriate error message
50 | return wt
51 | }
52 | }
53 | return wt
54 | }
55 |
56 | type authFailureError struct{}
57 |
58 | func (e authFailureError) Error() string {
59 | return "Authentication unsuccessful, please try logging in"
60 | }
61 |
62 | // TODO reconsider this interface...
63 | func (wt *FileWebTokenStore) SetEmail(s string) { wt.User = s }
64 | func (wt *FileWebTokenStore) SetRefreshToken(s string) { wt.Refresh = s }
65 | func (wt *FileWebTokenStore) SetIDToken(s string) { wt.ID = s }
66 | func (wt *FileWebTokenStore) Email() string { return wt.User }
67 | func (wt *FileWebTokenStore) RefreshToken() string { return wt.Refresh }
68 | func (wt *FileWebTokenStore) IDToken() string { return wt.ID }
69 | func (wt *FileWebTokenStore) Flush() {
70 | b, err := yaml.Marshal(&wt)
71 | if err != nil {
72 | log.Fatal(err)
73 | }
74 |
75 | tokenFile := path.Join(wt.root, webTokensFileName)
76 | f, err := os.Create(tokenFile)
77 | if err != nil {
78 | log.Fatal(err)
79 | }
80 | defer f.Close()
81 | f.Write(b)
82 | }
83 |
84 | type authenticationResultType struct {
85 | IdToken *string `type:"string" sensitive:"true"`
86 | RefreshToken *string `type:"string" sensitive:"true"`
87 | }
88 |
89 | func Authenticate(wt WebTokenStore, args map[string]string) error {
90 | body, err := json.Marshal(args)
91 | if err != nil {
92 | return err
93 | }
94 |
95 | u, _ := url.Parse(apiURL)
96 | u.Path = path.Join(u.Path, "auth")
97 | resp, err := http.Post(u.String(), "application/json", bytes.NewBuffer(body))
98 | if err != nil {
99 | return err
100 | }
101 | defer resp.Body.Close()
102 |
103 | if resp.StatusCode != http.StatusOK {
104 | return authFailureError{}
105 | }
106 |
107 | bodyBytes, err := ioutil.ReadAll(resp.Body)
108 | if err != nil {
109 | return err
110 | }
111 |
112 | var authResult authenticationResultType
113 | if err := json.Unmarshal(bodyBytes, &authResult); err != nil {
114 | return err
115 | }
116 |
117 | if email, ok := args["user"]; ok {
118 | wt.SetEmail(email)
119 | }
120 | if authResult.RefreshToken != nil {
121 | wt.SetRefreshToken(*authResult.RefreshToken)
122 | }
123 | if authResult.IdToken != nil {
124 | wt.SetIDToken(*authResult.IdToken)
125 | }
126 | wt.Flush()
127 | return nil
128 | }
129 |
130 | // CallWithReAuth accepts a pre-built request, attempts to call it, and if it fails authorisation due to an
131 | // expired IDToken, will reauth, and then retry the original function.
132 | func (w *Web) CallWithReAuth(req *http.Request) (*http.Response, error) {
133 | idToken := w.tokens.IDToken()
134 | var resp *http.Response
135 | var err error
136 | if idToken != "" {
137 | req.Header.Add(walSyncAuthorizationHeader, idToken)
138 | resp, err = w.client.Do(req)
139 | if err != nil && (resp == nil || resp.StatusCode != http.StatusUnauthorized) {
140 | return nil, err
141 | }
142 | }
143 | if idToken == "" || resp.StatusCode == http.StatusUnauthorized {
144 | defer w.tokens.Flush()
145 | w.tokens.SetIDToken("")
146 | body := map[string]string{
147 | "refreshToken": w.tokens.RefreshToken(),
148 | }
149 | err = Authenticate(w.tokens, body)
150 | if err != nil {
151 | if _, ok := err.(authFailureError); ok {
152 | w.tokens.SetRefreshToken("")
153 | w.tokens.Flush()
154 | }
155 | return nil, err
156 | }
157 | req.Header.Set(walSyncAuthorizationHeader, w.tokens.IDToken())
158 | resp, err = w.client.Do(req)
159 | }
160 | return resp, err
161 | }
162 |
--------------------------------------------------------------------------------
/pkg/service/client.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "log"
5 | "os/exec"
6 | "runtime"
7 | "strings"
8 | "time"
9 |
10 | "mvdan.cc/xurls/v2"
11 | )
12 |
13 | const (
14 | dateFormat = "Mon, Jan 02, 2006"
15 | emptySearchLinePrompt = "Search here..."
16 | searchGroupPrompt = "TAB: Create new search group"
17 | newLinePrompt = "Enter: Create new line"
18 | )
19 |
20 | // ClientBase ...
21 | type ClientBase struct {
22 | db *DBListRepo
23 | Search [][]rune
24 | matches []ListItem
25 | CurItem *ListItem // The currently selected item
26 | Editor string
27 | W, H int
28 | ReservedTopLines, ReservedBottomLines int
29 | CurX, CurY int // Cur "screen" index, not related to matched item lists
30 | VertOffset int // The index of the first displayed item in the match set
31 | HorizOffset int // The index of the first displayed char in the curItem
32 | ShowHidden bool
33 | SelectedItems map[string]ListItem
34 | copiedItem *ListItem
35 | HiddenMatchPrefix string // The common string that we want to truncate from each line
36 | useClientSearch bool
37 | //previousKey InteractionEventType // Keep track of the previous keypress
38 | //footerMessage string // Because we refresh on an ongoing basis, this needs to be emitted each time we paint
39 | }
40 |
41 | // NewClientBase ...
42 | func NewClientBase(db *DBListRepo, maxWidth int, maxHeight int, useClientSearch bool) *ClientBase {
43 | return &ClientBase{
44 | db: db,
45 | W: maxWidth,
46 | H: maxHeight,
47 | SelectedItems: make(map[string]ListItem),
48 | ReservedTopLines: 1,
49 | ReservedBottomLines: 2,
50 | useClientSearch: useClientSearch,
51 | }
52 | }
53 |
54 | // InteractionEventType ...
55 | type InteractionEventType uint32
56 |
57 | // InteractionEvent ...
58 | type InteractionEvent struct {
59 | T InteractionEventType
60 | Key string
61 | R []rune
62 | }
63 |
64 | const (
65 | KeyNull InteractionEventType = iota
66 | KeyEscape
67 | KeyEnter
68 | KeyDeleteItem
69 | KeyOpenNote
70 | KeyGotoStart
71 | KeyGotoEnd
72 | KeyVisibility
73 | KeyUndo
74 | KeyRedo
75 | KeyCopy
76 | KeyPaste
77 | KeyOpenURL
78 | KeyExport
79 | KeySelect
80 | KeyAddSearchGroup
81 | KeyBackspace
82 | KeyDelete
83 | KeyMoveItemUp
84 | KeyMoveItemDown
85 | KeyCursorDown
86 | KeyCursorUp
87 | KeyCursorRight
88 | KeyCursorLeft
89 | KeyRune
90 |
91 | SetText
92 | )
93 |
94 | // TODO duplicated in getHiddenLinePrefix function, figure out how to unify
95 | func getLenHiddenMatchPrefix(line string, hiddenMatchPrefix string) int {
96 | l := 0
97 | if strings.HasPrefix(strings.ToLower(line), hiddenMatchPrefix) {
98 | l += len([]byte(hiddenMatchPrefix))
99 | }
100 | return l
101 | }
102 |
103 | func getMapIntersection(a map[string]struct{}, b map[string]struct{}) map[string]struct{} {
104 | intersect := make(map[string]struct{})
105 | for i := range a {
106 | if _, exists := b[i]; exists {
107 | intersect[i] = struct{}{}
108 | }
109 | }
110 | return intersect
111 | }
112 |
113 | func GetCommonSearchPrefixAndFriends(selectedItems map[string]ListItem) [][]rune {
114 | searchGroups := [][]rune{}
115 | if len(selectedItems) == 0 {
116 | return searchGroups
117 | }
118 |
119 | var lines []string
120 |
121 | // We need to do a set intersection on the friends, as we only want to display
122 | // commonly shared emails
123 | // Therefore, we set the friends map to that of the first line, and then iterate
124 | // over the subsequent lines, taking the intersect of the resultant map as we go
125 | friends := make(map[string]struct{})
126 | for _, item := range selectedItems {
127 | for _, f := range item.Friends() {
128 | friends[f] = struct{}{}
129 | }
130 | break // We only need one item from the iterable map to instantiate the set
131 | }
132 | for _, item := range selectedItems {
133 | lines = append(lines, strings.TrimSpace(item.Line()))
134 | lineFriends := make(map[string]struct{})
135 | for _, f := range item.Friends() {
136 | lineFriends[f] = struct{}{}
137 | }
138 | friends = getMapIntersection(friends, lineFriends)
139 | }
140 |
141 | var prefix string
142 | // Only take longest common prefix on line if there's more than one line
143 | if len(lines) > 1 {
144 | prefix = strings.TrimSpace(longestCommonPrefix(lines))
145 | }
146 |
147 | if len(prefix) > 0 {
148 | searchGroups = append(searchGroups, []rune(prefix))
149 | }
150 |
151 | for f := range friends {
152 | searchGroups = append(searchGroups, []rune("@"+f))
153 | }
154 |
155 | return searchGroups
156 | }
157 |
158 | func longestCommonPrefix(strs []string) string {
159 | if len(strs) == 0 {
160 | return ""
161 | }
162 | // force lower case
163 | prefix := strings.ToLower(strs[0])
164 | for i := 1; i < len(strs); i++ {
165 | l := strings.ToLower(strs[i])
166 | for !strings.HasPrefix(l, prefix) {
167 | prefix = prefix[0 : len(prefix)-1]
168 | if len(prefix) == 0 {
169 | return ""
170 | }
171 | }
172 | }
173 | return prefix
174 | }
175 |
176 | func Min(a, b int) int {
177 | if a < b {
178 | return a
179 | }
180 | return b
181 | }
182 |
183 | func max(a, b int) int {
184 | if a > b {
185 | return a
186 | }
187 | return b
188 | }
189 |
190 | func (t *ClientBase) getLenSearchBox() int {
191 | // Add up max potential position based on number of runes in groups, and separators between
192 | lenSearchBox := max(0, len(t.Search)-1) // Account for spaces between search groups
193 | for _, g := range t.Search {
194 | lenSearchBox += len(g)
195 | }
196 | return lenSearchBox
197 | }
198 |
199 | func MatchFirstURL(line string, ensureScheme bool) string {
200 | // Attempt to match any urls in the line.
201 | // Try "Strict" match first. This only matches if scheme is included.
202 | rxStrict := xurls.Strict()
203 | var match string
204 | if match = rxStrict.FindString(line); match == "" {
205 | // Otherwise, if we succeed on a "Relaxed" match, we can infer
206 | // that the scheme wasn't present. The darwin/macOS `open` command
207 | // expects full URLs with scheme, so do a brute force prepend of
208 | // `http://` to see if that works.
209 | rxRelaxed := xurls.Relaxed()
210 | if match = rxRelaxed.FindString(line); match != "" {
211 | if ensureScheme {
212 | match = "http://" + match
213 | }
214 | }
215 | }
216 | return match
217 | }
218 |
219 | // open opens the specified URL in the default browser of the user.
220 | // https://stackoverflow.com/a/39324149
221 | func openURL(url string) error {
222 | var cmd string
223 | var args []string
224 |
225 | switch runtime.GOOS {
226 | case "windows":
227 | cmd = "cmd"
228 | args = []string{"/c", "start"}
229 | case "darwin":
230 | cmd = "open"
231 | default: // "linux", "freebsd", "openbsd", "netbsd"
232 | cmd = "xdg-open"
233 | }
234 | args = append(args, url)
235 | return exec.Command(cmd, args...).Start()
236 | }
237 |
238 | // GetSearchGroupIdxAndOffset returns the group index and offset within that group, respectively.
239 | // This might have unpredictable results if called on non-search lines (e.g. when CurY != 0)
240 | func (t *ClientBase) GetSearchGroupIdxAndOffset() (int, int) {
241 | // Get search group to operate on, and the char within that
242 | grpIdx, start := 0, 0
243 | // This is a public API, so it's worth covering false cases when this is called on zero/empty search
244 | // groups
245 | if len(t.Search) == 0 {
246 | return 0, 0
247 | }
248 | end := len(t.Search[grpIdx])
249 | for end < t.CurX {
250 | grpIdx++
251 | start = end + 1 // `1` accounts for the visual separator between groups
252 | end = start
253 | if grpIdx < len(t.Search) {
254 | end += len(t.Search[grpIdx])
255 | }
256 | }
257 | charOffset := t.CurX - start
258 | return grpIdx, charOffset
259 | }
260 |
261 | func insertCharInPlace(line []rune, offset int, newChars []rune) []rune {
262 | // TODO this is an ugly catch to prevent an edge condition that sometimes occurs.
263 | // Better to just ignore the op rather than crashing the program...
264 | // (until I find the solution I mean)
265 | if len(line) < offset {
266 | return line
267 | }
268 | lenNewChars := len(newChars)
269 | line = append(line, newChars...)
270 | copy(line[offset+lenNewChars:], line[offset:])
271 | copy(line[offset:], newChars)
272 | return line
273 | }
274 |
275 | func ParseOperatorGroups(sub string) string {
276 | // Match the op against any known operator (e.g. date) and parse if applicable.
277 | // TODO for now, just match `d` or `D` for date, we'll expand in the future.
278 | now := time.Now()
279 | dateString := now.Format(dateFormat)
280 | sub = strings.ReplaceAll(sub, "{d}", dateString)
281 | return sub
282 | }
283 |
284 | func getHiddenLinePrefix(keys [][]rune) string {
285 | // Only apply the trunaction on "closed" search groups (e.g. when the user has tabbed to
286 | // the next one).
287 | if len(keys) == 0 || (len(keys) == 1 && len(keys[0]) == 0) {
288 | return ""
289 | }
290 |
291 | // Only operate on the first key
292 | key := keys[0]
293 | _, nChars := GetMatchPattern(key) // TODO can be private function now in same service
294 | trimmedKey := string(key[nChars:])
295 |
296 | shortenedPrefix := strings.TrimSpace(strings.ToLower(trimmedKey)) + " "
297 |
298 | return shortenedPrefix
299 | }
300 |
301 | // TrimPrefix ...
302 | func (t *ClientBase) TrimPrefix(line string) string {
303 | // Truncate the full search string from any lines matching the entire thing,
304 | // ignoring search operators
305 | // Op needs to be case-insensitive, but must not mutate underlying line
306 |
307 | if t.HiddenMatchPrefix == "" {
308 | return line
309 | }
310 |
311 | // We explicitly avoid using `strings.TrimPrefix(...)` here because the lines are case-sensitive
312 | if strings.HasPrefix(strings.ToLower(line), t.HiddenMatchPrefix) {
313 | line = string([]rune(line)[len([]byte(t.HiddenMatchPrefix)):])
314 | // If we strip the match prefix, and there is a space remaining, trim that too
315 | return strings.TrimPrefix(line, " ")
316 | }
317 | return line
318 | }
319 |
320 | func (t *ClientBase) GetUnsearchedFriends(friends []string) []string {
321 | removedSearchFriends := []string{}
322 | for _, f := range friends {
323 | f = "@" + f
324 | isInSearch := false
325 | for _, s := range t.Search {
326 | if pattern, nChars := GetMatchPattern(s); pattern == FullMatchPattern {
327 | s = s[nChars:]
328 | } else if pattern == InverseMatchPattern {
329 | break
330 | }
331 | if f == string(s) {
332 | isInSearch = true
333 | break
334 | }
335 | }
336 | if !isInSearch {
337 | removedSearchFriends = append(removedSearchFriends, f)
338 | }
339 | }
340 | return removedSearchFriends
341 | }
342 |
343 | // TODO rename "t"
344 | func (t *ClientBase) HandleInteraction(ev InteractionEvent, search [][]rune, showHidden bool, bypassRefresh bool, limit int) ([]ListItem, bool, error) {
345 | // the terminal client passes t.Search and t.ShowHidden as the search argument, so nothing changes, however other clients (who don't
346 | // rely on backend search state) can override them on each iteration of HandleInteraction
347 | t.Search = search
348 | t.ShowHidden = showHidden
349 |
350 | posDiff := []int{0, 0} // x and y mutations to apply after db data mutations
351 |
352 | // itemKey represents the unique identifying key for the ListItem. We set it explicitly only
353 | // when creating new ListItems via the Add interface, so we can tell the backend what our
354 | // current item is when asking for Matches (and adjusting for cursor offsets due to live collab)
355 | // If itemKey is empty by the time we reach the Match call, we offset any movement against our
356 | // existing match set (e.g. for Move*) and then default to matchedItem.key
357 | itemKey := ""
358 |
359 | // relativeY accounts for any hidden lines at the top, which is required for match indexing
360 | relativeY := t.CurY + t.VertOffset
361 | onSearch := !t.useClientSearch && relativeY == t.ReservedTopLines-1
362 |
363 | curItem := t.db.matchListItems[ev.Key]
364 |
365 | // offsetX represents the position in the underying curItem.Line
366 | // Only apply the prefix offset if the line starts with the prefix, other lines will
367 | // match but not have the prefix truncated
368 | offsetX := t.HorizOffset + t.CurX
369 | lenHiddenMatchPrefix := 0
370 | if curItem != nil {
371 | lenHiddenMatchPrefix = getLenHiddenMatchPrefix(curItem.Line(), t.HiddenMatchPrefix)
372 | offsetX += lenHiddenMatchPrefix
373 | }
374 | var err error
375 | switch ev.T {
376 | case KeyEscape:
377 | if len(t.SelectedItems) > 0 {
378 | t.SelectedItems = make(map[string]ListItem)
379 | } else {
380 | t.VertOffset = 0
381 | relativeY = 0
382 | }
383 | case KeyEnter:
384 | if len(t.SelectedItems) > 0 {
385 | // Add common search prefix to search groups
386 | if newSearch := GetCommonSearchPrefixAndFriends(t.SelectedItems); len(newSearch) > 0 {
387 | t.Search = newSearch
388 | }
389 | t.SelectedItems = make(map[string]ListItem)
390 | t.CurY = 0
391 | if len(t.Search) > 0 {
392 | posDiff[0] += len(t.Search[0])
393 | }
394 | } else {
395 | // Add a new item below current cursor position
396 | // This will insert the contents of the current search string (omitting search args like `=`)
397 | var err error
398 | if onSearch {
399 | if len(t.Search) > 0 {
400 | posDiff[0] -= len([]byte(strings.TrimSpace(string(t.Search[0])))) + 1
401 | }
402 | }
403 | newString := GetNewLinePrefix(t.Search)
404 | // If the user is logged in, we automatically append the client email to each line (this is used
405 | // for collaboration purposes)
406 | // TODO do the email-in check better
407 | if t.db.email != "" {
408 | ownerPattern := " @" + t.db.email
409 | newString = newString + ownerPattern
410 | }
411 | itemKey, err = t.db.Add(newString, nil, curItem)
412 | if err != nil {
413 | log.Fatal(err)
414 | }
415 | posDiff[1]++
416 | }
417 | case KeyDeleteItem:
418 | if onSearch {
419 | if len(t.Search) > 0 {
420 | // Only remove the current search group
421 | grpIdx, _ := t.GetSearchGroupIdxAndOffset()
422 | if len(t.Search) == 1 {
423 | t.Search = [][]rune{[]rune{}}
424 | } else if grpIdx == 0 {
425 | t.Search = t.Search[1:]
426 | } else {
427 | t.Search = append(t.Search[:grpIdx], t.Search[grpIdx+1:]...)
428 | }
429 | }
430 | } else {
431 | // Copy into buffer in case we're moving it elsewhere
432 | t.copiedItem = curItem
433 | if relativeY-1 != len(t.matches)-1 {
434 | // TODO make `==` and reorder
435 | // Default behaviour on delete is to return and set position to the child item.
436 | // We don't want to do that here, so ignore the itemKey return from Delete, and
437 | // increment the Y position.
438 | // Because we increment, the key passed to Match below will exist and therefore
439 | // will resolve properly.
440 | _, err = t.db.Delete(curItem)
441 | if err != nil {
442 | log.Fatal(err)
443 | }
444 | posDiff[1]++
445 | } else {
446 | // APART from when calling CtrlD on the bottom item, in which case use the default
447 | // behaviour and target the child for new positioning
448 | itemKey, err = t.db.Delete(curItem)
449 | if err != nil {
450 | log.Fatal(err)
451 | }
452 | }
453 | }
454 | t.HorizOffset = 0
455 | //case KeyOpenNote:
456 | // if relativeY != 0 {
457 | // if err := t.S.Suspend(); err == nil {
458 | // err = t.openEditorSession()
459 | // if err != nil {
460 | // log.Fatal(err)
461 | // }
462 | // if err := t.S.Resume(); err != nil {
463 | // panic("failed to resume: " + err.Error())
464 | // }
465 | // }
466 | // }
467 | case KeyGotoStart:
468 | // Go to beginning of line
469 | // TODO decouple cursor mutation from key handling
470 | t.CurX = 0
471 | t.HorizOffset = 0
472 | case KeyGotoEnd:
473 | // Go to end of line
474 | if onSearch {
475 | t.CurX = t.getLenSearchBox()
476 | } else {
477 | // TODO
478 | t.CurX = len([]rune(curItem.Line()))
479 | }
480 | t.HorizOffset = t.CurX - t.W
481 | case KeyVisibility:
482 | // Toggle hidden item visibility
483 | if onSearch {
484 | t.ShowHidden = !t.ShowHidden
485 | } else {
486 | // Default returned itemKey behaviour on ToggleVisibility is one of the following:
487 | // - on "show", it will return itself
488 | // - on "hide", it will look for matchParent first, matchChild second
489 | // This is expected behaviour on the default "hide hidden" view, but when we're showing
490 | // and operating on all items (including hidden), we don't need to change the itemKey
491 | newItemKey, err := t.db.ToggleVisibility(curItem)
492 | if err != nil {
493 | log.Fatal(err)
494 | }
495 | if !t.ShowHidden {
496 | itemKey = newItemKey
497 | }
498 | }
499 | case KeyUndo:
500 | itemKey, err = t.db.Undo()
501 | if err != nil {
502 | log.Fatal(err)
503 | }
504 | case KeyRedo:
505 | itemKey, err = t.db.Redo()
506 | if err != nil {
507 | log.Fatal(err)
508 | }
509 | case KeyCopy:
510 | // Copy functionality
511 | if t.useClientSearch || relativeY != t.ReservedTopLines-1 {
512 | t.copiedItem = curItem
513 | }
514 | case KeyOpenURL:
515 | if relativeY != t.ReservedTopLines-1 {
516 | if url := MatchFirstURL(curItem.Line(), true); url != "" {
517 | openURL(url)
518 | }
519 | }
520 | case KeyExport:
521 | t.db.ExportToPlainText(t.Search, t.ShowHidden)
522 | case KeyPaste:
523 | // Paste functionality
524 | if t.copiedItem != nil {
525 | itemKey, err = t.db.Add(t.copiedItem.rawLine, nil, curItem)
526 | if err != nil {
527 | log.Fatal(err)
528 | }
529 | posDiff[1]++
530 | }
531 | case KeySelect:
532 | if t.useClientSearch || relativeY != t.ReservedTopLines-1 {
533 | // If exists, clear, otherwise set
534 | if curItem != nil {
535 | if _, ok := t.SelectedItems[curItem.key]; ok {
536 | delete(t.SelectedItems, curItem.key)
537 | } else {
538 | t.SelectedItems[curItem.key] = *curItem
539 | }
540 | }
541 | }
542 | case KeyAddSearchGroup:
543 | if onSearch {
544 | // If no search groups exist, rely on separate new char insertion elsewhere
545 | if len(t.Search) > 0 {
546 | // The location of the cursor will determine where the search group is added
547 | // If `Tabbing` in the middle of the search group, we need to split the group into two
548 | // The character immediately after the current position will represent the first
549 | // character in the new (right most) search group
550 | grpIdx, charOffset := t.GetSearchGroupIdxAndOffset()
551 | currentGroup := t.Search[grpIdx]
552 | newLeft, newRight := currentGroup[:charOffset], currentGroup[charOffset:]
553 | t.Search = append(t.Search, []rune{})
554 | copy(t.Search[grpIdx+1:], t.Search[grpIdx:])
555 | t.Search[grpIdx] = newLeft
556 | t.Search[grpIdx+1] = newRight
557 | }
558 | posDiff[0]++
559 | }
560 | case KeyBackspace:
561 | if onSearch {
562 | if len(t.Search) > 0 {
563 | grpIdx, charOffset := t.GetSearchGroupIdxAndOffset()
564 | newGroup := []rune(t.Search[grpIdx])
565 |
566 | // If charOffset == 0 we are acting on the previous separator
567 | if charOffset == 0 {
568 | // If we are operating on a middle (or initial) separator, we need to merge
569 | // previous and next search groups before cleaning up the search group
570 | if grpIdx > 0 {
571 | newGroup = append(t.Search[grpIdx-1], t.Search[grpIdx]...)
572 | t.Search[grpIdx-1] = newGroup
573 | t.Search = append(t.Search[:grpIdx], t.Search[grpIdx+1:]...)
574 | }
575 | } else {
576 | newGroup = append(newGroup[:charOffset-1], newGroup[charOffset:]...)
577 | t.Search[grpIdx] = newGroup
578 | }
579 | posDiff[0]--
580 | }
581 | } else {
582 | // If cursor in 0 position and current line is empty, delete current line and go
583 | // to end of previous line (if present)
584 | newLine := []rune(curItem.rawLine)
585 | if t.HorizOffset+t.CurX > 0 && len(curItem.Line()) > 0 {
586 | newLine = append(newLine[:offsetX-1], newLine[offsetX:]...)
587 | err = t.db.Update(string(newLine), curItem)
588 | if err != nil {
589 | log.Fatal(err)
590 | }
591 | posDiff[0]--
592 | } else if (offsetX-lenHiddenMatchPrefix) == 0 && (len(curItem.Line())-lenHiddenMatchPrefix) == 0 {
593 | itemKey, err = t.db.Delete(curItem)
594 | if err != nil {
595 | log.Fatal(err)
596 | }
597 | // Move up a cursor position
598 | posDiff[1]--
599 | if relativeY > t.ReservedTopLines {
600 | // TODO setting to the max width isn't completely robust as other
601 | // decrements will affect, but it's good enough for now as the cursor
602 | // repositioning logic will take care of over-increments
603 | posDiff[0] += t.W
604 | }
605 | }
606 | }
607 | case KeyDelete:
608 | // TODO this is very similar to the Backspace logic above, refactor to avoid duplication
609 | if onSearch {
610 | if len(t.Search) > 0 {
611 | grpIdx, charOffset := t.GetSearchGroupIdxAndOffset()
612 | newGroup := []rune(t.Search[grpIdx])
613 |
614 | // If charOffset == len(t.Search[grpIdx]) we need to merge with the next group (if present)
615 | if charOffset == len(t.Search[grpIdx]) {
616 | if grpIdx < len(t.Search)-1 {
617 | newGroup = append(t.Search[grpIdx], t.Search[grpIdx+1]...)
618 | t.Search[grpIdx] = newGroup
619 | t.Search = append(t.Search[:grpIdx+1], t.Search[grpIdx+2:]...)
620 | }
621 | } else {
622 | newGroup = append(newGroup[:charOffset], newGroup[charOffset+1:]...)
623 | t.Search[grpIdx] = newGroup
624 | }
625 | }
626 | } else {
627 | // If cursor in 0 position and current line is empty, delete current line and go
628 | // to end of previous line (if present)
629 | newLine := []rune(curItem.rawLine)
630 | if len(curItem.Line()) > 0 && t.HorizOffset+t.CurX+lenHiddenMatchPrefix < len(curItem.Line()) {
631 | newLine = append(newLine[:offsetX], newLine[offsetX+1:]...)
632 | err = t.db.Update(string(newLine), curItem)
633 | if err != nil {
634 | log.Fatal(err)
635 | }
636 | } else if (offsetX-lenHiddenMatchPrefix) == 0 && (len(curItem.Line())-lenHiddenMatchPrefix) == 0 {
637 | itemKey, err = t.db.Delete(curItem)
638 | if err != nil {
639 | log.Fatal(err)
640 | }
641 | // Move up a cursor position
642 | posDiff[1]--
643 | if relativeY > t.ReservedTopLines {
644 | // TODO setting to the max width isn't completely robust as other
645 | // decrements will affect, but it's good enough for now as the cursor
646 | // repositioning logic will take care of over-increments
647 | posDiff[0] += t.W
648 | }
649 | }
650 | }
651 | case KeyMoveItemUp:
652 | if t.useClientSearch || relativeY > t.ReservedTopLines-1 {
653 | // Move the current item up and follow with cursor
654 | if err = t.db.MoveUp(curItem); err != nil {
655 | log.Fatal(err)
656 | }
657 | // Set itemKey to current to ensure the cursor follows it
658 | itemKey = curItem.key
659 | }
660 | case KeyMoveItemDown:
661 | if t.useClientSearch || relativeY > t.ReservedTopLines-1 {
662 | // Move the current item down and follow with cursor
663 | if err = t.db.MoveDown(curItem); err != nil {
664 | log.Fatal(err)
665 | }
666 | // Set itemKey to current to ensure the cursor follows it
667 | itemKey = curItem.key
668 | }
669 | case KeyCursorDown:
670 | posDiff[1]++
671 | case KeyCursorUp:
672 | posDiff[1]--
673 | case KeyCursorRight:
674 | posDiff[0]++
675 | case KeyCursorLeft:
676 | posDiff[0]--
677 | case KeyRune:
678 | if onSearch {
679 | if len(t.Search) > 0 {
680 | grpIdx, charOffset := t.GetSearchGroupIdxAndOffset()
681 | newGroup := make([]rune, len(t.Search[grpIdx]))
682 | copy(newGroup, t.Search[grpIdx])
683 |
684 | // We want to insert a char into the current search group then update in place
685 | newGroup = insertCharInPlace(newGroup, charOffset, ev.R)
686 | oldLen := len(string(newGroup))
687 | parsedGroup := ParseOperatorGroups(string(newGroup))
688 | t.Search[grpIdx] = []rune(parsedGroup)
689 | posDiff[0] += len(parsedGroup) - oldLen
690 | } else {
691 | var newTerm []rune
692 | newTerm = append(newTerm, ev.R...)
693 | t.Search = append(t.Search, newTerm)
694 | }
695 | } else {
696 | newLine := []rune(curItem.rawLine)
697 | // Insert characters at position
698 | if len(newLine) == 0 {
699 | newLine = append(newLine, ev.R...)
700 | } else {
701 | newLine = insertCharInPlace(newLine, offsetX, ev.R)
702 | }
703 | oldLen := len(newLine)
704 | parsedNewLine := ParseOperatorGroups(string(newLine))
705 | err = t.db.Update(parsedNewLine, curItem)
706 | if err != nil {
707 | log.Fatal(err)
708 | }
709 | posDiff[0] += len([]rune(parsedNewLine)) - oldLen
710 | }
711 | posDiff[0] += len(ev.R)
712 | //t.footerMessage = ""
713 | case SetText:
714 | // This is only called from list items in the web client
715 | oldLen := len([]rune(curItem.rawLine))
716 | // The rune slice received will only have the line, and not the appended
717 | // friends (the client has no knowledge of the collaborators being stored
718 | // within the rawLine). These are appended in the TrimPrefix call below.
719 | parsedNewLine := ParseOperatorGroups(string(ev.R)) + strings.TrimPrefix(curItem.rawLine, curItem.Line())
720 | err = t.db.Update(parsedNewLine, curItem)
721 | if err != nil {
722 | log.Fatal(err)
723 | }
724 | posDiff[0] += len([]rune(parsedNewLine)) - oldLen
725 | }
726 |
727 | if bypassRefresh {
728 | return []ListItem{}, true, nil
729 | }
730 |
731 | t.HiddenMatchPrefix = getHiddenLinePrefix(t.Search)
732 |
733 | matchIdx := relativeY - t.ReservedTopLines
734 | // Adjust with any explicit moves
735 | matchIdx = Min(matchIdx+posDiff[1], len(t.matches)-1)
736 | // Set itemKey to the client's current curItem
737 | if !t.useClientSearch {
738 | if itemKey == "" && matchIdx >= 0 && matchIdx < len(t.matches) {
739 | itemKey = t.matches[matchIdx].key
740 | }
741 | } else if curItem != nil {
742 | itemKey = curItem.key
743 | }
744 |
745 | // Handle any offsets that occurred due to other collaborators interacting with the same list
746 | // at the same time
747 | t.matches, matchIdx, err = t.db.Match(t.Search, t.ShowHidden, itemKey, 0, limit)
748 |
749 | windowSize := t.H - t.ReservedTopLines - t.ReservedBottomLines
750 |
751 | t.CurY = 0
752 | if matchIdx >= 0 {
753 | if matchIdx < t.VertOffset {
754 | t.VertOffset = matchIdx
755 | t.CurY = t.ReservedTopLines
756 | } else if matchIdx >= t.VertOffset+windowSize {
757 | t.VertOffset = matchIdx - windowSize
758 | t.CurY = t.ReservedTopLines + windowSize
759 | } else {
760 | t.CurY = matchIdx - t.VertOffset + t.ReservedTopLines
761 | }
762 | }
763 |
764 | isSearchLine := t.CurY <= t.ReservedTopLines-1 // `- 1` for 0 idx
765 |
766 | // Set curItem before establishing max X position based on the len of the curItem line (to avoid
767 | // nonexistent array indexes). If on search line, just set to nil
768 | // This is legacy to support the terminal client until the key based interface refactor is done
769 | if isSearchLine || len(t.matches) == 0 {
770 | t.CurItem = nil
771 | } else {
772 | t.CurItem = &t.matches[matchIdx]
773 | }
774 |
775 | // Then refresh the X position based on vertical position and curItem
776 |
777 | // If we've moved up or down, clear the horizontal offset
778 | if posDiff[1] > 0 || posDiff[1] < 0 {
779 | t.HorizOffset = 0
780 | }
781 |
782 | // Some logic above does forceful operations to ensure that the cursor is moved to MINIMUM beginning of line
783 | // Therefore ensure we do not go < 0
784 | t.HorizOffset = max(0, t.HorizOffset)
785 |
786 | newXIdx := t.CurX + posDiff[0]
787 | if isSearchLine {
788 | newXIdx = max(0, newXIdx) // Prevent index < 0
789 | t.CurX = Min(newXIdx, t.getLenSearchBox())
790 | } else {
791 | // Deal with horizontal offset if applicable
792 | if newXIdx < 0 {
793 | if t.HorizOffset > 0 {
794 | t.HorizOffset--
795 | }
796 | t.CurX = 0 // Prevent index < 0
797 | } else {
798 | if newXIdx > t.W-1 && t.HorizOffset+t.W-1 < len(t.CurItem.Line()) {
799 | t.HorizOffset++
800 | }
801 | // We need to recalc lenHiddenMatchPrefix here to cover the case when we arrow down from
802 | // the search line to the top line, when there's a hidden prefix (otherwise there is a delay
803 | // before the cursor sets to the end of the line and we're at risk of an index error).
804 | lenHiddenMatchPrefix = getLenHiddenMatchPrefix(t.CurItem.Line(), t.HiddenMatchPrefix)
805 | newXIdx = Min(newXIdx, len([]rune(t.CurItem.Line()))-t.HorizOffset-lenHiddenMatchPrefix) // Prevent going out of range of the line
806 | t.CurX = Min(newXIdx, t.W-1) // Prevent going out of range of the page
807 | }
808 | }
809 |
810 | return t.matches, true, nil
811 | }
812 |
--------------------------------------------------------------------------------
/pkg/service/config.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "encoding/json"
5 | "fmt"
6 | "io/ioutil"
7 | "net/http"
8 | "net/url"
9 | "strings"
10 | "time"
11 |
12 | "nhooyr.io/websocket"
13 | )
14 |
15 | type Web struct {
16 | client *http.Client
17 | wsConn *websocket.Conn
18 | tokens WebTokenStore
19 | isActive bool
20 | }
21 |
22 | func NewWeb(webTokens WebTokenStore) *Web {
23 | return &Web{
24 | client: &http.Client{
25 | Timeout: 3 * time.Second,
26 | },
27 | tokens: webTokens,
28 | }
29 | }
30 |
31 | type WebRemote struct {
32 | Emails []string
33 | DTLastChange int64
34 | }
35 |
36 | type postRemoteResponse struct {
37 | ActiveFriends, PendingFriends []string
38 | }
39 |
40 | // postRemote is responsible for both additions and deletions
41 | func (w *Web) postRemote(remote *WebRemote, u *url.URL) (postRemoteResponse, error) {
42 | remoteResp := postRemoteResponse{}
43 |
44 | body, err := json.Marshal(remote)
45 | if err != nil {
46 | return remoteResp, err
47 | }
48 |
49 | req, err := http.NewRequest("POST", u.String(), strings.NewReader(string(body)))
50 | if err != nil {
51 | return remoteResp, err
52 | }
53 |
54 | resp, err := w.CallWithReAuth(req)
55 | if err != nil {
56 | return remoteResp, err
57 | }
58 | defer resp.Body.Close()
59 |
60 | respBytes, err := ioutil.ReadAll(resp.Body)
61 | if err != nil {
62 | return remoteResp, err
63 | }
64 | if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
65 | return remoteResp, fmt.Errorf("Error creating new remote: %s", respBytes)
66 | }
67 |
68 | if err := json.Unmarshal(respBytes, &remoteResp); err != nil {
69 | return remoteResp, err
70 | }
71 |
72 | return remoteResp, nil
73 | }
74 |
--------------------------------------------------------------------------------
/pkg/service/db.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "context"
5 | )
6 |
7 | type RefreshKey struct {
8 | AllowOverride bool
9 | ChangedKeys map[string]struct{}
10 | }
11 |
12 | type FinishWithPurgeError struct{}
13 |
14 | func (e FinishWithPurgeError) Error() string {
15 | return ""
16 | }
17 |
18 | type namedWal struct {
19 | name string
20 | wal []EventLog
21 | }
22 |
23 | // Start begins push/pull for all WalFiles
24 | func (r *DBListRepo) Start(client Client) error {
25 | inputEvtsChan := make(chan interface{})
26 |
27 | ctx, cancel := context.WithCancel(context.Background())
28 |
29 | replayChan := make(chan namedWal)
30 | //reorderAndReplayChan := make(chan []EventLog)
31 |
32 | // We need atomicity between wal pull/replays and handling of keypress events, as we need
33 | // events to operate on a predictable state (rather than a keypress being applied to state
34 | // that differs from when the user intended due to async updates).
35 | // Therefore, we consume client events into a channel, and consume from it in the same loop
36 | // as the pull/replay loop.
37 | errChan := make(chan error)
38 | go func() {
39 | for {
40 | select {
41 | case n := <-replayChan:
42 | name, wal := n.name, n.wal
43 | if err := r.Replay(wal); err != nil {
44 | errChan <- err
45 | return
46 | }
47 | if name != "" {
48 | r.setProcessedWalChecksum(name)
49 | }
50 | changedKeys, allowOverride := getChangedListItemKeysFromWal(wal)
51 | go func() {
52 | inputEvtsChan <- RefreshKey{
53 | ChangedKeys: changedKeys,
54 | AllowOverride: allowOverride,
55 | }
56 | }()
57 | case ev := <-r.remoteCursorMoveChan:
58 | // Update active key position of collaborator if changes have occurred
59 | updated := r.SetCollabPosition(ev)
60 | if updated {
61 | go func() {
62 | inputEvtsChan <- RefreshKey{}
63 | }()
64 | }
65 | case ev := <-inputEvtsChan:
66 | if err := client.HandleEvent(ev); err != nil {
67 | cancel()
68 | <-r.finalFlushChan
69 | _, isPurge := err.(FinishWithPurgeError)
70 | if finishErr := r.finish(isPurge); finishErr != nil {
71 | errChan <- finishErr
72 | }
73 | errChan <- err
74 | return
75 | }
76 | }
77 | }
78 | }()
79 |
80 | // To avoid blocking key presses on the main processing loop, run heavy sync ops in a separate
81 | // loop, and only add to channel for processing if there's any changes that need syncing
82 | // This is run after the goroutine above is triggered to ensure a thread is consuming from replayChan
83 | err := r.startSync(ctx, replayChan, inputEvtsChan)
84 | if err != nil {
85 | return err
86 | }
87 |
88 | // This is the main loop of operation in the app.
89 | // We consume all term events into our own channel (handled above).
90 | // This is handled in a separate goroutine due to pontential contention in the loop above, whereby we consume
91 | // from inputEvtsChan and publish to the errChan (rather than a single select with two options in the main thread),
92 | // e.g.
93 | // select {
94 | // case inputEvtsChan <- client.AwaitEvent():
95 | // case err <- errchan:
96 | // ...
97 | // }
98 | go func() {
99 | for {
100 | ev := client.AwaitEvent()
101 | inputEvtsChan <- ev
102 | }
103 | }()
104 | return <-errChan
105 | }
106 |
107 | func (r *DBListRepo) registerWeb() error {
108 | if err := r.web.establishWebSocketConnection(); err != nil {
109 | return err
110 | }
111 |
112 | if r.email == "" {
113 | if pong, err := r.web.ping(); err == nil {
114 | r.setEmail(pong.User)
115 | r.web.tokens.SetEmail(pong.User)
116 | r.web.tokens.Flush()
117 | }
118 | }
119 |
120 | r.DeleteWalFile(string(r.email))
121 | r.AddWalFile(
122 | &WebWalFile{
123 | uuid: string(r.email),
124 | web: r.web,
125 | },
126 | true,
127 | )
128 |
129 | return nil
130 | }
131 |
132 | func (r *DBListRepo) AddWalFile(wf WalFile, hasFullAccess bool) {
133 | r.allWalFileMut.Lock()
134 | r.allWalFiles[wf.GetUUID()] = wf
135 | r.allWalFileMut.Unlock()
136 |
137 | if hasFullAccess {
138 | r.syncWalFileMut.Lock()
139 | r.syncWalFiles[wf.GetUUID()] = wf
140 | r.syncWalFileMut.Unlock()
141 | }
142 |
143 | if _, ok := wf.(*WebWalFile); ok {
144 | r.webWalFileMut.Lock()
145 | r.webWalFiles[wf.GetUUID()] = wf
146 | r.webWalFileMut.Unlock()
147 | }
148 | }
149 |
150 | func (r *DBListRepo) DeleteWalFile(name string) {
151 | r.allWalFileMut.Lock()
152 | r.syncWalFileMut.Lock()
153 | r.webWalFileMut.Lock()
154 | defer r.allWalFileMut.Unlock()
155 | defer r.syncWalFileMut.Unlock()
156 | defer r.webWalFileMut.Unlock()
157 | delete(r.allWalFiles, name)
158 | delete(r.syncWalFiles, name)
159 | delete(r.webWalFiles, name)
160 | }
161 |
--------------------------------------------------------------------------------
/pkg/service/debug.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "os"
7 | "path"
8 | )
9 |
10 | var eventNameMap = map[EventType]string{
11 | NullEvent: "NullEvent",
12 | AddEvent: "AddEvent",
13 | UpdateEvent: "UpdateEvent",
14 | MoveUpEvent: "MoveUpEvent",
15 | MoveDownEvent: "MoveDownEvent",
16 | ShowEvent: "ShowEvent",
17 | HideEvent: "HideEvent",
18 | DeleteEvent: "DeleteEvent",
19 | }
20 |
21 | // DebugWriteEventsToFile is used for debug purposes. It prints all events for the given uuid/lamportTimestamp
22 | // combination to file, for inspection
23 | func (r *DBListRepo) DebugWriteEventsToFile(filename string, key string) {
24 | c := make(chan namedWal)
25 | go func() {
26 | r.pull(context.Background(), []WalFile{r.LocalWalFile}, c)
27 | }()
28 | n := <-c
29 |
30 | f, err := os.OpenFile(path.Join(filename, "results_"+key), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
31 | if err != nil {
32 | panic(err)
33 | }
34 | defer f.Close()
35 |
36 | for _, e := range n.wal {
37 | if e.ListItemKey == key {
38 | f.WriteString(fmt.Sprintf("[%s] [%s] [%s] [%v]\n", eventNameMap[e.EventType], e.key(), e.Line, e.Friends.Emails))
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/pkg/service/legacy.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "compress/gzip"
5 | "encoding/binary"
6 | "encoding/gob"
7 | "errors"
8 | "io"
9 | "sort"
10 | "strconv"
11 | "strings"
12 | )
13 |
14 | type walItemSchema2 struct {
15 | UUID uuid
16 | TargetUUID uuid
17 | ListItemCreationTime int64
18 | TargetListItemCreationTime int64
19 | EventTime int64
20 | EventType EventType
21 | LineLength uint64
22 | NoteExists bool
23 | NoteLength uint64
24 | }
25 |
26 | type LineFriendsSchema4 struct {
27 | IsProcessed bool
28 | Offset int
29 | Emails map[string]struct{}
30 | }
31 |
32 | type EventLogSchema4 struct {
33 | UUID, TargetUUID uuid
34 | ListItemCreationTime int64
35 | TargetListItemCreationTime int64
36 | UnixNanoTime int64
37 | EventType EventType
38 | Line string
39 | Note []byte
40 | Friends LineFriendsSchema4
41 | key, targetKey string
42 | }
43 |
44 | type EventLogSchema5 struct {
45 | UUID, TargetUUID uuid
46 | ListItemCreationTime int64
47 | TargetListItemCreationTime int64
48 | UnixNanoTime int64
49 | EventType EventType
50 | Line string
51 | Note []byte
52 | Friends LineFriends
53 | key, targetKey string
54 | }
55 |
56 | type EventLogSchema6 struct {
57 | UUID uuid // origin UUID
58 | LamportTimestamp int64
59 | EventType EventType
60 | ListItemKey, TargetListItemKey string
61 | Line string
62 | Note []byte
63 | Friends LineFriends
64 | cachedKey string
65 | }
66 |
67 | func (e *EventLogSchema6) key() string {
68 | if e.cachedKey == "" {
69 | e.cachedKey = strconv.Itoa(int(e.UUID)) + ":" + strconv.Itoa(int(e.LamportTimestamp))
70 | }
71 | return e.cachedKey
72 | }
73 |
74 | type LegacyListItem struct {
75 | rawLine string
76 | Note []byte
77 |
78 | IsHidden bool
79 |
80 | child *LegacyListItem
81 | parent *LegacyListItem
82 | matchChild *LegacyListItem
83 | matchParent *LegacyListItem
84 |
85 | friends LineFriends
86 |
87 | localEmail string // set at creation time and used to exclude from Friends() method
88 | key string
89 |
90 | isDeleted bool
91 | }
92 |
93 | func getNextEventLogFromWalFile(r io.Reader, schemaVersionID uint16) (*EventLogSchema6, error) {
94 | el := EventLogSchema6{}
95 |
96 | switch schemaVersionID {
97 | case 3:
98 | wi := walItemSchema2{}
99 | err := binary.Read(r, binary.LittleEndian, &wi)
100 | if err != nil {
101 | return nil, err
102 | }
103 |
104 | el.UUID = wi.UUID
105 | el.EventType = wi.EventType
106 | el.ListItemKey = strconv.Itoa(int(wi.UUID)) + ":" + strconv.Itoa(int(wi.ListItemCreationTime))
107 | el.TargetListItemKey = strconv.Itoa(int(wi.TargetUUID)) + ":" + strconv.Itoa(int(wi.TargetListItemCreationTime))
108 | el.LamportTimestamp = wi.EventTime
109 |
110 | line := make([]byte, wi.LineLength)
111 | err = binary.Read(r, binary.LittleEndian, &line)
112 | if err != nil {
113 | return nil, err
114 | }
115 | el.Line = string(line)
116 |
117 | if wi.NoteExists {
118 | el.Note = []byte{}
119 | }
120 | if wi.NoteLength > 0 {
121 | note := make([]byte, wi.NoteLength)
122 | err = binary.Read(r, binary.LittleEndian, ¬e)
123 | if err != nil {
124 | return nil, err
125 | }
126 | el.Note = note
127 | }
128 | default:
129 | return nil, errors.New("unrecognised wal schema version")
130 | }
131 | return &el, nil
132 | }
133 |
134 | func getOldEventLogKeys(i interface{}) (string, string) {
135 | var key, targetKey string
136 | switch e := i.(type) {
137 | case EventLogSchema4:
138 | key = strconv.Itoa(int(e.UUID)) + ":" + strconv.Itoa(int(e.ListItemCreationTime))
139 | targetKey = strconv.Itoa(int(e.TargetUUID)) + ":" + strconv.Itoa(int(e.TargetListItemCreationTime))
140 | case EventLogSchema5:
141 | key = strconv.Itoa(int(e.UUID)) + ":" + strconv.Itoa(int(e.ListItemCreationTime))
142 | targetKey = strconv.Itoa(int(e.TargetUUID)) + ":" + strconv.Itoa(int(e.TargetListItemCreationTime))
143 | }
144 | return key, targetKey
145 | }
146 |
147 | func legacyMigrateToVersionSix(pr *io.PipeReader, walSchemaVersionID uint16, errChan chan error) ([]EventLogSchema6, error) {
148 | var el []EventLogSchema6
149 | switch walSchemaVersionID {
150 | case 1, 2, 3:
151 | for {
152 | select {
153 | case err := <-errChan:
154 | if err != nil {
155 | return el, err
156 | }
157 | default:
158 | e, err := getNextEventLogFromWalFile(pr, walSchemaVersionID)
159 | if err != nil {
160 | switch err {
161 | case io.EOF:
162 | return el, nil
163 | case io.ErrUnexpectedEOF:
164 | // Given the distributed concurrent nature of this app, we sometimes pick up partially
165 | // uploaded files which will fail, but may well be complete later on, therefore just
166 | // return for now and attempt again later
167 | // TODO implement a decent retry mech here
168 | return el, nil
169 | default:
170 | return el, err
171 | }
172 | }
173 | el = append(el, *e)
174 | }
175 | }
176 | case 4:
177 | var oel []EventLogSchema4
178 | dec := gob.NewDecoder(pr)
179 | if err := dec.Decode(&oel); err != nil {
180 | return el, err
181 | }
182 | if err := <-errChan; err != nil {
183 | return el, err
184 | }
185 | for _, oe := range oel {
186 | key, targetKey := getOldEventLogKeys(oe)
187 | e := EventLogSchema6{
188 | UUID: oe.UUID,
189 | ListItemKey: key,
190 | TargetListItemKey: targetKey,
191 | EventType: oe.EventType,
192 | Line: oe.Line,
193 | Note: oe.Note,
194 | Friends: LineFriends{
195 | IsProcessed: oe.Friends.IsProcessed,
196 | Offset: oe.Friends.Offset,
197 | emailsMap: oe.Friends.Emails,
198 | },
199 | }
200 | e.LamportTimestamp = oe.UnixNanoTime
201 | for f := range oe.Friends.Emails {
202 | e.Friends.Emails = append(e.Friends.Emails, f)
203 | }
204 | sort.Strings(e.Friends.Emails)
205 | el = append(el, e)
206 | }
207 | case 5:
208 | var oel []EventLogSchema5
209 | dec := gob.NewDecoder(pr)
210 | if err := dec.Decode(&oel); err != nil {
211 | return el, err
212 | }
213 | if err := <-errChan; err != nil {
214 | return el, err
215 | }
216 | for _, oe := range oel {
217 | key, targetKey := getOldEventLogKeys(oe)
218 | e := EventLogSchema6{
219 | UUID: oe.UUID,
220 | ListItemKey: key,
221 | TargetListItemKey: targetKey,
222 | EventType: oe.EventType,
223 | Line: oe.Line,
224 | Note: oe.Note,
225 | Friends: oe.Friends,
226 | }
227 | e.LamportTimestamp = oe.UnixNanoTime
228 | el = append(el, e)
229 | }
230 | case 6:
231 | dec := gob.NewDecoder(pr)
232 | if err := dec.Decode(&el); err != nil {
233 | return el, err
234 | }
235 | if err := <-errChan; err != nil {
236 | return el, err
237 | }
238 | }
239 |
240 | return el, nil
241 | }
242 |
243 | type legacyDBListRepo struct {
244 | Root *LegacyListItem
245 | listItemTracker map[string]*LegacyListItem
246 | processedEventLogCache map[string]struct{}
247 | listItemProcessedEventLogTypeCache map[EventType]map[string]EventLogSchema6
248 | }
249 |
250 | func (r *legacyDBListRepo) add(key string, line string, friends LineFriends, note []byte, childItem *LegacyListItem) (*LegacyListItem, error) {
251 | newItem := &LegacyListItem{
252 | key: key,
253 | child: childItem,
254 | rawLine: line,
255 | Note: note,
256 | friends: friends,
257 | }
258 |
259 | // If `child` is nil, it's the first item in the list so set as root and return
260 | if childItem == nil {
261 | oldRoot := r.Root
262 | r.Root = newItem
263 | if oldRoot != nil {
264 | newItem.parent = oldRoot
265 | oldRoot.child = newItem
266 | }
267 | return newItem, nil
268 | }
269 |
270 | if childItem.parent != nil {
271 | childItem.parent.child = newItem
272 | newItem.parent = childItem.parent
273 | }
274 | childItem.parent = newItem
275 |
276 | return newItem, nil
277 | }
278 |
279 | func (r *legacyDBListRepo) update(item *LegacyListItem, line string, friends LineFriends, note []byte) error {
280 | if len(line) > 0 {
281 | item.rawLine = line
282 | } else {
283 | item.Note = note
284 | }
285 |
286 | // Just in case an Update occurs on a Deleted item (distributed race conditions)
287 | item.isDeleted = false
288 |
289 | return nil
290 | }
291 |
292 | func (r *legacyDBListRepo) del(item *LegacyListItem) error {
293 | item.isDeleted = true
294 |
295 | if item.child != nil {
296 | item.child.parent = item.parent
297 | } else {
298 | // If the item has no child, it is at the top of the list and therefore we need to update the root
299 | r.Root = item.parent
300 | }
301 |
302 | if item.parent != nil {
303 | item.parent.child = item.child
304 | }
305 |
306 | return nil
307 | }
308 |
309 | func (r *legacyDBListRepo) move(item *LegacyListItem, childItem *LegacyListItem) (*LegacyListItem, error) {
310 | var err error
311 | err = r.del(item)
312 | isHidden := item.IsHidden
313 | item, err = r.add(item.key, item.rawLine, item.friends, item.Note, childItem)
314 | if isHidden {
315 | r.setVisibility(item, false)
316 | }
317 | return item, err
318 | }
319 |
320 | func (r *legacyDBListRepo) setVisibility(item *LegacyListItem, isVisible bool) error {
321 | item.IsHidden = !isVisible
322 | return nil
323 | }
324 |
325 | func legacyCheckEquality(event1 EventLogSchema6, event2 EventLogSchema6) int {
326 | if event1.LamportTimestamp < event2.LamportTimestamp ||
327 | event1.LamportTimestamp == event2.LamportTimestamp && event1.UUID < event2.UUID {
328 | return leftEventOlder
329 | } else if event2.LamportTimestamp < event1.LamportTimestamp ||
330 | event2.LamportTimestamp == event1.LamportTimestamp && event2.UUID < event1.UUID {
331 | return rightEventOlder
332 | }
333 | return eventsEqual
334 | }
335 |
336 | func (r *legacyDBListRepo) legacyProcessEventLog(e EventLogSchema6) (*LegacyListItem, error) {
337 | item := r.listItemTracker[e.ListItemKey]
338 | targetItem := r.listItemTracker[e.TargetListItemKey]
339 |
340 | // Skip any events that have already been processed
341 | if _, exists := r.processedEventLogCache[e.key()]; exists {
342 | return item, nil
343 | }
344 | r.processedEventLogCache[e.key()] = struct{}{}
345 |
346 | // Else, skip any event of equal EventType that is <= the most recently processed for a given ListItem
347 | // TODO storing a whole EventLog might be expensive, use a specific/reduced type in the nested map
348 | if eventTypeCache, exists := r.listItemProcessedEventLogTypeCache[e.EventType]; exists {
349 | if ce, exists := eventTypeCache[e.ListItemKey]; exists {
350 | switch legacyCheckEquality(ce, e) {
351 | // if the new event is older or equal, skip
352 | case rightEventOlder, eventsEqual:
353 | return item, nil
354 | }
355 | }
356 | } else {
357 | r.listItemProcessedEventLogTypeCache[e.EventType] = make(map[string]EventLogSchema6)
358 | }
359 | r.listItemProcessedEventLogTypeCache[e.EventType][e.ListItemKey] = e
360 |
361 | // We need to maintain records of deleted items in the cache, but if deleted, want to assign nil ptrs
362 | // in the various funcs below, so set to nil
363 | if item != nil && item.isDeleted {
364 | item = nil
365 | }
366 | if targetItem != nil && targetItem.isDeleted {
367 | targetItem = nil
368 | }
369 |
370 | // When we're calling this function on initial WAL merge and load, we may come across
371 | // orphaned items. There MIGHT be a valid case to keep events around if the EventType
372 | // is Update. Item will obviously never exist for Add. For all other eventTypes,
373 | // we should just skip the event and return
374 | // TODO remove this AddEvent nil item passthrough
375 | if item == nil && e.EventType != AddEvent && e.EventType != UpdateEvent {
376 | return item, nil
377 | }
378 |
379 | var err error
380 | switch e.EventType {
381 | case AddEvent:
382 | if item != nil {
383 | // 21/11/21: There was a bug caused when a collaborator `Undo` was carried out on a collaborator origin
384 | // `Delete`, when the `Delete` was not picked up by the local. This resulted in an `Add` being carried out
385 | // with a duplicate ListItem key, which led to some F'd up behaviour in the match set. This catch covers
386 | // this case by doing a dumb "if Add on existing item, change to Update". We need to run two updates, as
387 | // Note and Line updates are individual operations.
388 | // TODO remove this when `Compact`/wal post-processing is smart enough to iron out these broken logs.
389 | err = r.update(item, e.Line, e.Friends, e.Note)
390 | err = r.update(item, "", e.Friends, e.Note)
391 | } else {
392 | item, err = r.add(e.ListItemKey, e.Line, e.Friends, e.Note, targetItem)
393 | }
394 | case UpdateEvent:
395 | // We have to cover an edge case here which occurs when merging two remote WALs. If the following occurs:
396 | // - wal1 creates item A
397 | // - wal2 copies wal1
398 | // - wal2 deletes item A
399 | // - wal1 updates item A
400 | // - wal1 copies wal2
401 | // We will end up with an attempted Update on a nonexistent item.
402 | // In this case, we will Add an item back in with the updated content
403 | // NOTE A side effect of this will be that the re-added item will be at the top of the list as it
404 | // becomes tricky to deal with child IDs
405 | if item != nil {
406 | err = r.update(item, e.Line, e.Friends, e.Note)
407 | } else {
408 | item, err = r.add(e.ListItemKey, e.Line, e.Friends, e.Note, targetItem)
409 | }
410 | case MoveDownEvent:
411 | if targetItem == nil {
412 | return item, nil
413 | }
414 | fallthrough
415 | case MoveUpEvent:
416 | item, err = r.move(item, targetItem)
417 | case ShowEvent:
418 | err = r.setVisibility(item, true)
419 | case HideEvent:
420 | err = r.setVisibility(item, false)
421 | case DeleteEvent:
422 | err = r.del(item)
423 | }
424 |
425 | if item != nil {
426 | r.listItemTracker[e.ListItemKey] = item
427 | }
428 |
429 | return item, err
430 | }
431 |
432 | // Build wals from unzipped data for old (pre CRDT tree related schema)
433 | func (r *DBListRepo) legacyBuildFromRaw(pr *io.PipeReader, walSchemaVersionID uint16, errChan chan error) ([]EventLog, error) {
434 | // walSchemaVersionID == 6 represents the last of the legacy event logs which backed the old event CRDT.
435 | // These previous versions were brittle and probably quite buggy, and efforts to migrate on
436 | // a per-log basis proved fruitless and unpredictable.
437 | // In the interest of a clean start, we will migrate from any version <6 -> 6, then build a
438 | // fresh fzn document based on that state, and then use that to build a completely new
439 | // event log using the new CRDT model.
440 |
441 | legacyEl, err := legacyMigrateToVersionSix(pr, walSchemaVersionID, errChan)
442 | if err != nil {
443 | return []EventLog{}, err
444 | }
445 |
446 | legacyRepo := legacyDBListRepo{
447 | listItemTracker: make(map[string]*LegacyListItem),
448 | processedEventLogCache: make(map[string]struct{}),
449 | listItemProcessedEventLogTypeCache: make(map[EventType]map[string]EventLogSchema6),
450 | }
451 |
452 | for _, e := range legacyEl {
453 | legacyRepo.legacyProcessEventLog(e)
454 | }
455 |
456 | i := 0
457 |
458 | // traverse from the Root item, and for each, creates an `UpdateEvent` and a `PositionEvent`
459 | el := []EventLog{}
460 | item := legacyRepo.Root
461 | var prev *LegacyListItem
462 | for item != nil {
463 | s := strings.Split(item.key, ":")
464 | id, _ := strconv.ParseInt(s[0], 10, 64)
465 | lamport, _ := strconv.ParseInt(s[1], 10, 64)
466 |
467 | u := EventLog{
468 | UUID: uuid(id),
469 | LamportTimestamp: lamport,
470 | EventType: UpdateEvent,
471 | ListItemKey: item.key,
472 | Line: item.rawLine,
473 | Note: item.Note,
474 | IsHidden: item.IsHidden,
475 | }
476 | u = r.repositionActiveFriends(u)
477 | el = append(el, u)
478 |
479 | p := EventLog{
480 | UUID: uuid(id),
481 | LamportTimestamp: lamport,
482 | EventType: PositionEvent,
483 | ListItemKey: item.key,
484 | }
485 | if prev != nil {
486 | p.TargetListItemKey = prev.key
487 | }
488 | el = append(el, p)
489 |
490 | prev = item
491 | item = item.parent
492 | i++
493 | }
494 |
495 | return el, nil
496 | }
497 |
498 | func (r *DBListRepo) legacyBuildFromFile(walSchemaVersionID uint16, raw io.Reader) ([]EventLog, error) {
499 | pr, pw := io.Pipe()
500 | errChan := make(chan error, 1)
501 | go func() {
502 | defer pw.Close()
503 | switch walSchemaVersionID {
504 | case 1, 2:
505 | if _, err := io.Copy(pw, raw); err != nil {
506 | errChan <- err
507 | }
508 | default:
509 | // Versions >=3 of the wal schema is gzipped after the first 2 bytes. Therefore, unzip those bytes
510 | // prior to passing it to the loop below
511 | zr, err := gzip.NewReader(raw)
512 | if err != nil {
513 | errChan <- err
514 | }
515 | defer zr.Close()
516 | if _, err := io.Copy(pw, zr); err != nil {
517 | errChan <- err
518 | }
519 | }
520 | errChan <- nil
521 | }()
522 |
523 | return r.legacyBuildFromRaw(pr, walSchemaVersionID, errChan)
524 | }
525 |
--------------------------------------------------------------------------------
/pkg/service/match.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "strings"
5 | "unicode"
6 | )
7 |
8 | func isSubString(sub string, full string) bool {
9 | if strings.Contains(strings.ToLower(full), strings.ToLower(sub)) {
10 | return true
11 | }
12 | return false
13 | }
14 |
15 | // Iterate through the full string, when you match the "head" of the sub rune slice,
16 | // pop it and continue through. If you clear sub, return true. Searches in O(n)
17 | func isFuzzyMatch(sub []rune, full string) bool {
18 | for _, c := range full {
19 | if unicode.ToLower(c) == unicode.ToLower(sub[0]) {
20 | _, sub = sub[0], sub[1:]
21 | }
22 | if len(sub) == 0 {
23 | return true
24 | }
25 | }
26 | return false
27 | }
28 |
29 | const (
30 | openOp rune = '{'
31 | closeOp rune = '}'
32 | )
33 |
34 | type MatchPattern int
35 |
36 | const (
37 | FullMatchPattern MatchPattern = iota
38 | InverseMatchPattern
39 | FuzzyMatchPattern
40 | NoMatchPattern
41 | )
42 |
43 | // matchChars represents the number of characters at the start of the string
44 | // which are attributed to the match pattern.
45 | // This is used elsewhere to strip the characters where appropriate
46 |
47 | func GetNewLinePrefix(search [][]rune) string {
48 | var searchStrings []string
49 | for _, group := range search {
50 | pattern, nChars := GetMatchPattern(group)
51 | if pattern != InverseMatchPattern && len(group) > 0 {
52 | searchStrings = append(searchStrings, string(group[nChars:]))
53 | }
54 | }
55 | newString := ""
56 | if len(searchStrings) > 0 {
57 | newString = strings.Join(searchStrings, " ") + " "
58 | }
59 | return newString
60 | }
61 |
62 | // GetMatchPattern will return the MatchPattern of a given string, if any, plus the number
63 | // of chars that can be omitted to leave only the relevant text
64 | func GetMatchPattern(sub []rune) (MatchPattern, int) {
65 | if len(sub) == 0 {
66 | return NoMatchPattern, 0
67 | }
68 | switch sub[0] {
69 | case '~':
70 | return FuzzyMatchPattern, 1
71 | case '!':
72 | return InverseMatchPattern, 1
73 | }
74 | return FullMatchPattern, 0
75 | }
76 |
77 | // If a matching group starts with `=` do a substring match, otherwise do a fuzzy search
78 | func isMatch(sub []rune, full string, pattern MatchPattern) bool {
79 | if len(sub) == 0 {
80 | return true
81 | }
82 | switch pattern {
83 | case FullMatchPattern:
84 | return isSubString(string(sub), full)
85 | case InverseMatchPattern:
86 | return !isSubString(string(sub), full)
87 | case FuzzyMatchPattern:
88 | return isFuzzyMatch(sub, full)
89 | default:
90 | // Shouldn't reach here
91 | return false
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/pkg/service/service.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "errors"
5 | "sort"
6 | "strconv"
7 | "strings"
8 | "sync"
9 | "time"
10 | )
11 |
12 | type (
13 | uuid uint32
14 | fileSchemaID uint16
15 | )
16 |
17 | const (
18 | walFilePattern = "wal_%v.db"
19 | viewFilePattern = "view_%v"
20 | exportFilePattern = "export_%v.txt"
21 | )
22 |
23 | type bits uint32
24 |
25 | const (
26 | hidden bits = 1 << iota
27 | )
28 |
29 | func set(b, flag bits) bits { return b | flag }
30 | func clear(b, flag bits) bits { return b &^ flag }
31 | func toggle(b, flag bits) bits { return b ^ flag }
32 | func has(b, flag bits) bool { return b&flag != 0 }
33 |
34 | type Client interface {
35 | HandleEvent(interface{}) error
36 | AwaitEvent() interface{}
37 | }
38 |
39 | type DBListRepo struct {
40 | eventLogger *DbEventLogger
41 | matchListItems map[string]*ListItem
42 |
43 | currentLamportTimestamp int64
44 | listItemCache map[string]*ListItem
45 |
46 | crdt *crdtTree
47 |
48 | // Wal stuff
49 | uuid uuid
50 | eventsChan chan EventLog
51 | web *Web
52 |
53 | remoteCursorMoveChan chan cursorMoveEvent
54 | localCursorMoveChan chan cursorMoveEvent
55 | collabPositions map[string]cursorMoveEvent
56 | collabMapLock *sync.Mutex
57 | previousListItemKey string
58 |
59 | email string
60 | //cfgFriendRegex *regexp.Regexp
61 | friends map[string]map[string]int64
62 | friendsUpdateLock *sync.RWMutex
63 | friendsOrdered []string // operating sort of like a queue, with earliest friends at the head
64 | activeFriends, pendingFriends map[string]struct{} // returned from the cloud
65 | activeFriendsMapLock *sync.RWMutex
66 | friendsMostRecentChangeDT int64
67 | friendsLastPushDT int64
68 |
69 | // TODO better naming convention
70 | LocalWalFile LocalWalFile
71 | webWalFiles map[string]WalFile
72 | allWalFiles map[string]WalFile
73 | syncWalFiles map[string]WalFile
74 | webWalFileMut *sync.RWMutex
75 | allWalFileMut *sync.RWMutex
76 | syncWalFileMut *sync.RWMutex
77 |
78 | processedWalChecksums map[string]struct{}
79 | processedWalChecksumLock *sync.Mutex
80 |
81 | pushTriggerTimer *time.Timer
82 | hasUnflushedEvents bool
83 | finalFlushChan chan struct{}
84 |
85 | hasSyncedRemotes bool
86 |
87 | isTest bool
88 | }
89 |
90 | // NewDBListRepo returns a pointer to a new instance of DBListRepo
91 | func NewDBListRepo(localWalFile LocalWalFile, webTokenStore WebTokenStore) *DBListRepo {
92 | listRepo := &DBListRepo{
93 | // TODO rename this cos it's solely for UNDO/REDO
94 | eventLogger: NewDbEventLogger(),
95 |
96 | // Wal stuff
97 | uuid: generateUUID(),
98 | listItemCache: make(map[string]*ListItem),
99 |
100 | crdt: newTree(),
101 |
102 | LocalWalFile: localWalFile,
103 | eventsChan: make(chan EventLog),
104 |
105 | collabMapLock: &sync.Mutex{},
106 |
107 | webWalFiles: make(map[string]WalFile),
108 | allWalFiles: make(map[string]WalFile),
109 | syncWalFiles: make(map[string]WalFile),
110 | webWalFileMut: &sync.RWMutex{},
111 | allWalFileMut: &sync.RWMutex{},
112 | syncWalFileMut: &sync.RWMutex{},
113 |
114 | processedWalChecksums: make(map[string]struct{}),
115 | processedWalChecksumLock: &sync.Mutex{},
116 |
117 | friends: make(map[string]map[string]int64),
118 | friendsUpdateLock: &sync.RWMutex{},
119 | activeFriendsMapLock: &sync.RWMutex{},
120 |
121 | pushTriggerTimer: time.NewTimer(time.Second * 0),
122 | finalFlushChan: make(chan struct{}),
123 | }
124 |
125 | // The localWalFile gets attached to the Wal independently (there are certain operations
126 | // that require us to only target the local walfile rather than all). We still need to register
127 | // it as we call all walfiles in the next line.
128 | listRepo.AddWalFile(localWalFile, true)
129 |
130 | if webTokenStore.Email() != "" {
131 | listRepo.setEmail(webTokenStore.Email())
132 | //listRepo.cfgFriendRegex = regexp.MustCompile(fmt.Sprintf("^fzn_cfg:friend +(%s) +@%s$", EmailRegex, regexp.QuoteMeta(listRepo.email)))
133 | //listRepo.cfgFriendRegex = regexp.MustCompile(fmt.Sprintf("^fzn_cfg:friend (%s) @%s$", EmailRegex, regexp.QuoteMeta(listRepo.email)))
134 | }
135 |
136 | // Tokens are generated on `login`
137 | // Keeping the web assignment outside of registerWeb, as we use registerWeb to reinstantiate
138 | // the web walfiles and connections periodically during runtime, and this makes it easier... (for now)
139 | listRepo.web = NewWeb(webTokenStore)
140 |
141 | // Establish the chan used to track and display collaborator cursor positions
142 | listRepo.remoteCursorMoveChan = make(chan cursorMoveEvent) // incoming events
143 | listRepo.localCursorMoveChan = make(chan cursorMoveEvent) // outgoing events
144 | listRepo.collabPositions = make(map[string]cursorMoveEvent) // map[collaboratorEmail]currentKey
145 |
146 | return listRepo
147 | }
148 |
149 | func (r *DBListRepo) setEmail(email string) {
150 | r.email = strings.ToLower(email)
151 | r.friends[email] = make(map[string]int64)
152 | }
153 |
154 | // ForceTriggerFlush will zero the flush timer, and block til completion. E.g. this is a synchronous flush
155 | func (r *DBListRepo) ForceTriggerFlush() {
156 | go func() {
157 | r.pushTriggerTimer.Reset(time.Millisecond * 1)
158 | }()
159 | }
160 |
161 | // IsSynced is a boolean value defining whether or now there are currently events held in memory that are yet to be
162 | // flushed to local storage
163 | func (r *DBListRepo) IsSynced() bool {
164 | return !r.hasUnflushedEvents
165 | }
166 |
167 | // ListItem is a mergeable data structure which represents a single item in the main list. It maintains record of the
168 | // last update lamport times
169 | type ListItem struct {
170 | rawLine string
171 | Note []byte // TODO make private
172 | IsHidden bool
173 |
174 | child *ListItem
175 | parent *ListItem
176 | matchChild *ListItem
177 | matchParent *ListItem
178 |
179 | friends LineFriends
180 |
181 | localEmail string // set at creation time and used to exclude from Friends() method
182 | key string
183 | }
184 |
185 | // Line returns a post-processed rawLine, with any matched collaborators omitted
186 | func (i *ListItem) Line() string {
187 | if i.friends.IsProcessed {
188 | return i.rawLine[:i.friends.Offset]
189 | }
190 | return i.rawLine
191 | }
192 |
193 | func (i *ListItem) Friends() []string {
194 | // The emails are stored as a map of strings. We need to generate a sorted slice to return
195 | // to the client
196 | // TODO cache for optimisation?? need to cover updates
197 | sortedEmails := []string{}
198 | for _, e := range i.friends.Emails {
199 | if e != i.localEmail {
200 | sortedEmails = append(sortedEmails, e)
201 | }
202 | }
203 | sort.Strings(sortedEmails)
204 | return sortedEmails
205 | }
206 |
207 | // TODO make attribute public directly??
208 | func (i *ListItem) Key() string {
209 | return i.key
210 | }
211 |
212 | func (r *DBListRepo) addEventLog(el EventLog) (*ListItem, error) {
213 | var err error
214 | var item *ListItem
215 |
216 | // At this point we inspect the Line in the event log for `@friends`, and if they're present and currently
217 | // enabled (e.g. in the friends cache), cut them from any central location in the line and append to the end.
218 | // This is required for later public client APIs (e.g. we only return a slice of the rawLine to clients via
219 | // the Line() API).
220 | el = r.repositionActiveFriends(el)
221 |
222 | item, err = r.processEventLog(el)
223 | r.eventsChan <- el
224 | return item, err
225 | }
226 |
227 | func (r *DBListRepo) update(line string, item *ListItem) EventLog {
228 | e := r.newEventLogFromListItem(UpdateEvent, item)
229 | e.Line = line
230 | return e
231 | }
232 |
233 | func (r *DBListRepo) updateNote(note []byte, item *ListItem) EventLog {
234 | e := r.newEventLogFromListItem(UpdateEvent, item)
235 | e.Note = note
236 | return e
237 | }
238 |
239 | func (r *DBListRepo) del(item *ListItem) EventLog {
240 | e := r.newEventLog(DeleteEvent)
241 | e.ListItemKey = item.key
242 | return e
243 | }
244 |
245 | func (r *DBListRepo) move(item, targetItem *ListItem) EventLog {
246 | e := r.newEventLog(PositionEvent)
247 | e.ListItemKey = item.key
248 | if targetItem != nil {
249 | e.TargetListItemKey = targetItem.key
250 | }
251 | return e
252 | }
253 |
254 | // Add adds a new LineItem with string, note and a position to insert the item into the matched list
255 | // It returns a string representing the unique key of the newly created item
256 | func (r *DBListRepo) Add(line string, note []byte, childItem *ListItem) (string, error) {
257 | var events []EventLog
258 |
259 | e := r.newEventLog(UpdateEvent)
260 | e.ListItemKey = strconv.Itoa(int(e.UUID)) + ":" + strconv.Itoa(int(e.LamportTimestamp))
261 | e.Line = line
262 | e.Note = note
263 | newItem, _ := r.addEventLog(e)
264 | ue := r.del(newItem)
265 | events = append(events, e)
266 |
267 | posEvent := r.move(newItem, childItem)
268 | r.addEventLog(posEvent)
269 | events = append(events, posEvent)
270 |
271 | r.addUndoLogs([]EventLog{ue}, events)
272 |
273 | return newItem.key, nil
274 | }
275 |
276 | // Update will update the line or note of an existing ListItem
277 | func (r *DBListRepo) Update(line string, item *ListItem) error {
278 | e := r.update(line, item)
279 | ue := r.update(item.rawLine, item)
280 |
281 | r.addEventLog(e)
282 | r.addUndoLogs([]EventLog{ue}, []EventLog{e})
283 | return nil
284 | }
285 |
286 | // TODO rethink this interface
287 | func (r *DBListRepo) UpdateNote(note []byte, item *ListItem) error {
288 | e := r.updateNote(note, item)
289 | ue := r.updateNote(item.Note, item)
290 |
291 | r.addEventLog(e)
292 | r.addUndoLogs([]EventLog{ue}, []EventLog{e})
293 | return nil
294 | }
295 |
296 | // Delete will remove an existing ListItem
297 | func (r *DBListRepo) Delete(item *ListItem) (string, error) {
298 | e := r.del(item)
299 | ue := r.newEventLogFromListItem(UpdateEvent, item)
300 |
301 | r.addEventLog(e)
302 | events := []EventLog{e}
303 | undoEvents := []EventLog{ue}
304 |
305 | r.addUndoLogs(undoEvents, events)
306 |
307 | // We use matchChild to set the next "current key", otherwise, if we delete the final matched item, which happens
308 | // to have a child in the full (un-matched) set, it will default to that on the return (confusing because it will
309 | // not match the current specified search groups)
310 | if item.matchChild != nil {
311 | return item.matchChild.key, nil
312 | }
313 | return "", nil
314 | }
315 |
316 | // MoveUp will swop a ListItem with the ListItem directly above it, taking visibility and
317 | // current matches into account.
318 | func (r *DBListRepo) MoveUp(item *ListItem) error {
319 | // We need to target the child of the child (as when we apply move events, we specify the target that we want to be
320 | // the new child. Only relevant for non-startup case
321 | if item.matchChild != nil {
322 | var events, undoEvents []EventLog
323 | e := r.move(item, item.matchChild.child)
324 | ue := r.move(item, item.matchChild)
325 | r.addEventLog(e)
326 | events = append(events, e)
327 | undoEvents = append(undoEvents, ue)
328 |
329 | // client is responsible for repointing the parent -
330 | // we operate only on true pointers (accounting for hidden items)
331 | if item.parent != nil {
332 | parentEvent := r.move(item.parent, item.child)
333 | undoParentEvent := r.move(item.parent, item)
334 | r.addEventLog(parentEvent)
335 | events = append(events, parentEvent)
336 | undoEvents = append(undoEvents, undoParentEvent)
337 | }
338 | r.addUndoLogs(undoEvents, events)
339 | }
340 | return nil
341 | }
342 |
343 | // MoveDown will swop a ListItem with the ListItem directly below it, taking visibility and
344 | // current matches into account.
345 | func (r *DBListRepo) MoveDown(item *ListItem) error {
346 | if item.matchParent != nil {
347 | var events, undoEvents []EventLog
348 | e := r.move(item, item.matchParent)
349 | ue := r.move(item, item.matchChild)
350 | r.addEventLog(e)
351 | events = append(events, e)
352 | undoEvents = append(undoEvents, ue)
353 |
354 | // client is responsible for repointing the parent -
355 | // we operate only on true pointers (accounting for hidden items)
356 | parentEvent := r.move(item.parent, item.child)
357 | undoParentEvent := r.move(item.parent, item)
358 | r.addEventLog(parentEvent)
359 | events = append(events, parentEvent)
360 | undoEvents = append(undoEvents, undoParentEvent)
361 |
362 | r.addUndoLogs(undoEvents, events)
363 | }
364 | return nil
365 | }
366 |
367 | // ToggleVisibility will toggle an item to be visible or invisible
368 | func (r *DBListRepo) ToggleVisibility(item *ListItem) (string, error) {
369 | //var evType, oppEvType EventType
370 | var newIsHidden bool
371 | var focusedItemKey string
372 | if item.IsHidden {
373 | newIsHidden = false
374 | // Cursor should remain on newly visible key
375 | focusedItemKey = item.key
376 | } else {
377 | newIsHidden = true
378 | // Set focusedItemKey to parent if available, else child (e.g. bottom of list)
379 | if item.matchParent != nil {
380 | focusedItemKey = item.matchParent.key
381 | } else if item.matchChild != nil {
382 | focusedItemKey = item.matchChild.key
383 | }
384 | }
385 | e := r.newEventLogFromListItem(UpdateEvent, item)
386 | e.IsHidden = newIsHidden
387 | r.addEventLog(e)
388 |
389 | ue := r.newEventLogFromListItem(UpdateEvent, item)
390 | ue.IsHidden = !newIsHidden
391 | r.addUndoLogs([]EventLog{ue}, []EventLog{e})
392 |
393 | return focusedItemKey, nil
394 | }
395 |
396 | func (r *DBListRepo) replayEventsFromUndoLog(events []EventLog) string {
397 | // TODO centralise
398 | // If the oppEvent event type == AddEvent, the event ListItemKey will become inconsistent with the
399 | // UUID and LamportTimestamp of the new event. However, we need to maintain the old key in order to map
400 | // to the old ListItem in the caches
401 | var key string
402 | for i, e := range events {
403 | e.LamportTimestamp = r.currentLamportTimestamp
404 | item, _ := r.addEventLog(e)
405 | if i == 0 && item != nil {
406 | if c := item.matchChild; e.EventType == DeleteEvent && c != nil {
407 | key = c.key
408 | } else {
409 | key = item.key
410 | }
411 | }
412 | }
413 | return key
414 | }
415 |
416 | func (r *DBListRepo) Undo() (string, error) {
417 | if r.eventLogger.curIdx > 0 {
418 | ue := r.eventLogger.log[r.eventLogger.curIdx]
419 | key := r.replayEventsFromUndoLog(ue.oppEvents)
420 | r.eventLogger.curIdx--
421 | return key, nil
422 | }
423 | return "", nil
424 | }
425 |
426 | func (r *DBListRepo) Redo() (string, error) {
427 | // Redo needs to look forward +1 index when actioning events
428 | if r.eventLogger.curIdx < len(r.eventLogger.log)-1 {
429 | ue := r.eventLogger.log[r.eventLogger.curIdx+1]
430 | key := r.replayEventsFromUndoLog(ue.events)
431 | r.eventLogger.curIdx++
432 | return key, nil
433 | }
434 | return "", nil
435 | }
436 |
437 | // Match takes a set of search groups and applies each to all ListItems, returning those that
438 | // fulfil all rules. `showHidden` dictates whether or not hidden items are returned. `curKey` is used to identify
439 | // the currently selected item. `offset` and `limit` can be passed to paginate over the match-set, if `limit==0`, all matches
440 | // from `offset` will be returned (e.g. no limit will be applied).
441 | func (r *DBListRepo) Match(keys [][]rune, showHidden bool, curKey string, offset int, limit int) ([]ListItem, int, error) {
442 | res := []ListItem{}
443 | if offset < 0 {
444 | return res, 0, errors.New("offset must be >= 0")
445 | } else if limit < 0 {
446 | return res, 0, errors.New("limit must be >= 0")
447 | }
448 |
449 | var last, lastMatched *ListItem
450 |
451 | r.matchListItems = make(map[string]*ListItem)
452 |
453 | idx := 0
454 | listItemMatchIdx := make(map[string]int)
455 | node := r.crdt.traverse(nil)
456 | for node != nil {
457 | cur := r.listItemCache[node.key]
458 | // Nullify match pointers
459 | // TODO centralise this logic, it's too closely coupled with the moveItem logic (if match pointers
460 | // aren't cleaned up between ANY ops, it can lead to weird behaviour as things operate based on
461 | // the existence and setting of them)
462 | cur.matchChild, cur.matchParent = nil, nil
463 |
464 | if showHidden || !cur.IsHidden {
465 | matched := true
466 | for _, group := range keys {
467 | // Match the currently selected item.
468 | // Also, match any items with empty Lines (this accounts for lines added when search is active)
469 | if cur.key == curKey || len(cur.rawLine) == 0 {
470 | break
471 | }
472 | // TODO unfortunate reuse of vars - refactor to tidy
473 | pattern, nChars := GetMatchPattern(group)
474 |
475 | // Rather than use rawLine (which houses the client local email too, which we _dont_ want to
476 | // match on), we generate a new line for the match func
477 | var sb strings.Builder
478 | sb.WriteString(cur.Line())
479 | for _, f := range cur.Friends() {
480 | sb.WriteString(" @")
481 | sb.WriteString(f)
482 | }
483 | if !isMatch(group[nChars:], sb.String(), pattern) {
484 | matched = false
485 | break
486 | }
487 | }
488 | if matched {
489 | // Pagination: only add to results set if we've surpassed the min boundary of the page,
490 | // otherwise only increment `idx`.
491 | if idx >= offset {
492 | r.matchListItems[cur.key] = cur
493 |
494 | // ListItems stored in the `res` slice are copies, and therefore will not reflect the
495 | // matchChild/matchParent setting below. This doesn't reflect normal function as we only
496 | // return `res` to the client for displaying lines (any mutations to backend state are done
497 | // via index and act on the matchListItems slice which stores the original items by ptr)
498 | // TODO centralise this
499 | res = append(res, *cur)
500 |
501 | if lastMatched != nil {
502 | lastMatched.matchParent = cur
503 | }
504 | cur.matchChild = lastMatched
505 | lastMatched = cur
506 |
507 | // Set the new idx for the next iteration
508 | listItemMatchIdx[cur.key] = idx
509 | }
510 | idx++
511 | }
512 | }
513 | // Terminate if we reach the root, or for when pagination is active and we reach the max boundary
514 | if limit > 0 && idx == offset+limit {
515 | break
516 | }
517 |
518 | // set full pointers (e.g. for moves when some items are hidden)
519 | cur.child = last
520 | if last != nil {
521 | last.parent = cur
522 | }
523 | last = cur
524 | node = r.crdt.traverse(node)
525 | }
526 | newPos := -1
527 | if p, exists := listItemMatchIdx[curKey]; exists {
528 | newPos = p
529 | }
530 | return res, newPos, nil
531 | }
532 |
533 | func (r *DBListRepo) GetFriendFromConfig(item ListItem) (string, bool) {
534 | if fields, isConfig := r.checkIfConfigLine(item.rawLine); isConfig {
535 | return string(fields[1]), true
536 | }
537 | return "", false
538 | }
539 |
540 | type FriendState int
541 |
542 | const (
543 | FriendNull FriendState = iota
544 | FriendPending
545 | FriendActive
546 | )
547 |
548 | func (r *DBListRepo) GetFriendState(f string) FriendState {
549 | r.activeFriendsMapLock.RLock()
550 | defer r.activeFriendsMapLock.RUnlock()
551 | f = strings.ToLower(f)
552 | if _, exists := r.activeFriends[f]; exists {
553 | return FriendActive
554 | } else if _, exists := r.pendingFriends[f]; exists {
555 | return FriendPending
556 | }
557 | return FriendNull
558 | }
559 |
560 | type SyncState int
561 |
562 | const (
563 | SyncOffline SyncState = iota
564 | SyncSyncing
565 | SyncSynced
566 | )
567 |
568 | type SyncEvent struct{}
569 |
570 | func (r *DBListRepo) GetSyncState() SyncState {
571 | if !r.web.isActive {
572 | return SyncOffline
573 | }
574 |
575 | if !r.hasSyncedRemotes || r.hasUnflushedEvents {
576 | return SyncSyncing
577 | }
578 |
579 | return SyncSynced
580 | }
581 |
582 | func (r *DBListRepo) GetListItem(key string) (ListItem, bool) {
583 | itemPtr, exists := r.listItemCache[key]
584 | if exists {
585 | return *itemPtr, true
586 | }
587 | return ListItem{}, false
588 | }
589 |
590 | func (r *DBListRepo) EmitCursorMoveEvent(key string) {
591 | // We need to _only_ emit an event if the curKey has changed since the previous Match call.
592 | // This prevents an endless loop that arises when more than one client is active and communicating on the same wal.
593 | // If we emitted every time, the following would happen:
594 | // 1. receive cursor move websocket event
595 | // 2. process it, trigger a client refresh
596 | // 3. which calls this function, which then emits an event
597 | // 4. trigger stage 1 on remote...
598 | if r.web.isActive && r.web.wsConn != nil {
599 | if key != r.previousListItemKey {
600 | //log.Println("cursor move: ", key)
601 | go func() {
602 | r.localCursorMoveChan <- cursorMoveEvent{
603 | listItemKey: key,
604 | unixNanoTime: time.Now().UnixNano(),
605 | }
606 | }()
607 | r.previousListItemKey = key
608 | }
609 | }
610 | }
611 |
612 | func getChangedListItemKeysFromWal(wal []EventLog) (map[string]struct{}, bool) {
613 | var allowOverride bool
614 | allowOverride = true
615 | keys := make(map[string]struct{})
616 | for _, el := range wal {
617 | keys[el.ListItemKey] = struct{}{}
618 | switch el.EventType {
619 | // TODO event updates
620 | case MoveUpEvent, MoveDownEvent, AddEvent, DeleteEvent:
621 | allowOverride = false
622 | }
623 | }
624 | return keys, allowOverride
625 | }
626 |
627 | func (r *DBListRepo) GetListItemNote(key string) []byte {
628 | var note []byte
629 | if item, exists := r.matchListItems[key]; exists {
630 | note = item.Note
631 | }
632 | return note
633 | }
634 |
635 | func (r *DBListRepo) SaveListItemNote(key string, note []byte) {
636 | if item, exists := r.matchListItems[key]; exists {
637 | r.UpdateNote(note, item)
638 | }
639 | }
640 |
641 | // GetCollabPositions returns a map of listItemKeys against all collaborators currently on that listItem
642 | func (r *DBListRepo) GetCollabPositions() map[string][]string {
643 | r.collabMapLock.Lock()
644 | defer r.collabMapLock.Unlock()
645 |
646 | pos := make(map[string][]string)
647 | for email, ev := range r.collabPositions {
648 | key := ev.listItemKey
649 | _, exists := pos[key]
650 | if !exists {
651 | pos[key] = []string{}
652 | }
653 | pos[key] = append(pos[key], email)
654 | }
655 | return pos
656 | }
657 |
658 | func (r *DBListRepo) SetCollabPosition(ev cursorMoveEvent) bool {
659 | r.collabMapLock.Lock()
660 | defer r.collabMapLock.Unlock()
661 |
662 | // Only update if the event occurred more recently
663 | old, exists := r.collabPositions[ev.email]
664 | if !exists || old.unixNanoTime < ev.unixNanoTime {
665 | r.collabPositions[ev.email] = ev
666 | return true
667 | }
668 | return false
669 | }
670 |
671 | func (r *DBListRepo) ExportToPlainText(matchKeys [][]rune, showHidden bool) error {
672 | matchedItems, _, _ := r.Match(matchKeys, showHidden, "", 0, 0)
673 | return generatePlainTextFile(matchedItems)
674 | }
675 |
--------------------------------------------------------------------------------
/pkg/service/tree.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "strconv"
5 | "strings"
6 | )
7 |
8 | const (
9 | crdtRootKey = ""
10 | crdtOrphanKey = "_orphan"
11 | )
12 |
13 | type crdtTree struct {
14 | cache map[string]*node
15 | addEventSet, deleteEventSet, positionEventSet map[string]EventLog
16 | }
17 |
18 | type node struct {
19 | key string
20 | originUUID uuid
21 | parent, left, right *node
22 | children childDll
23 | latestLamportTimestamp int64
24 | }
25 |
26 | func (n *node) moreRecentThan(o *node) bool {
27 | if n.key == "_orphan" {
28 | return false
29 | } else if o.key == "_orphan" {
30 | return false
31 | }
32 |
33 | if n.latestLamportTimestamp > o.latestLamportTimestamp {
34 | return true
35 | } else if n.latestLamportTimestamp < o.latestLamportTimestamp {
36 | return false
37 | }
38 | return n.originUUID > o.originUUID
39 | }
40 |
41 | // Seek out the next node in this precedence:
42 | // - immediate child
43 | // - right ptr
44 | // - right ptr of next parent which is not root
45 | func (n *node) getNext() *node {
46 | if n.children.firstChild != nil {
47 | return n.children.firstChild
48 | }
49 |
50 | if n.right != nil {
51 | return n.right
52 | }
53 |
54 | cur := n.parent
55 | for cur != nil {
56 | if cur.right != nil {
57 | return cur.right
58 | }
59 | cur = cur.parent
60 | }
61 |
62 | return nil
63 | }
64 |
65 | // Consider using https://pkg.go.dev/container/list
66 | type childDll struct {
67 | firstChild *node
68 | }
69 |
70 | func (l *childDll) insertInPlace(n *node) {
71 | right := l.firstChild
72 |
73 | var left *node
74 | for right != nil {
75 | if n.moreRecentThan(right) {
76 | break
77 | }
78 | left = right
79 | right = right.right
80 | }
81 |
82 | // insert into place
83 | if left != nil {
84 | left.right = n
85 | } else {
86 | l.firstChild = n
87 | }
88 | // If we traversed to the end
89 | if right != nil {
90 | right.left = n
91 | }
92 | n.left = left
93 | n.right = right
94 | }
95 |
96 | func (l *childDll) removeChild(n *node) {
97 | if n.left != nil {
98 | n.left.right = n.right
99 | } else {
100 | // node is firstChild, so reset firstChild in dll
101 | l.firstChild = n.right
102 | }
103 | if r := n.right; r != nil {
104 | r.left = n.left
105 | }
106 | n.left = nil
107 | n.right = nil
108 | return
109 | }
110 |
111 | func newTree() *crdtTree {
112 | orphan := &node{
113 | key: crdtOrphanKey,
114 | }
115 | root := &node{
116 | key: crdtRootKey,
117 | children: childDll{orphan},
118 | }
119 | orphan.parent = root
120 | return &crdtTree{
121 | cache: map[string]*node{
122 | crdtOrphanKey: orphan,
123 | crdtRootKey: root,
124 | },
125 | addEventSet: make(map[string]EventLog),
126 | deleteEventSet: make(map[string]EventLog),
127 | positionEventSet: make(map[string]EventLog),
128 | }
129 | }
130 |
131 | func (crdt *crdtTree) itemIsLive(key string) bool {
132 | latestAddEvent, isAdded := crdt.addEventSet[key]
133 | latestDeleteEvent, isDeleted := crdt.deleteEventSet[key]
134 | if isAdded && (!isDeleted || (isAdded && latestDeleteEvent.before(latestAddEvent))) {
135 | return true
136 | }
137 | return false
138 | }
139 |
140 | func (crdt *crdtTree) traverse(n *node) *node {
141 | if n == nil {
142 | n = crdt.cache[crdtRootKey]
143 | }
144 | n = n.getNext()
145 | for n != nil {
146 | if n.key != crdtRootKey && n.key != crdtOrphanKey && n.parent.key != crdtOrphanKey && crdt.itemIsLive(n.key) {
147 | return n
148 | }
149 | n = n.getNext()
150 | }
151 | return nil
152 | }
153 |
154 | func (crdt *crdtTree) generateEvents() []EventLog {
155 | events := []EventLog{}
156 |
157 | // Add all PositionEvents
158 | for _, e := range crdt.positionEventSet {
159 | events = append(events, e)
160 | }
161 |
162 | // Add UpdateEvents for active items (deleted items don't need content state, as any update that overrides deleted state will
163 | // come with it's own
164 | for k, addEvent := range crdt.addEventSet {
165 | if crdt.itemIsLive(k) {
166 | events = append(events, addEvent)
167 | }
168 | }
169 |
170 | // Add DeleteEvents
171 | for _, e := range crdt.deleteEventSet {
172 | events = append(events, e)
173 | }
174 |
175 | return events
176 | }
177 |
178 | func (crdt *crdtTree) addToTargetChildArray(item, target *node) {
179 | item.parent = target
180 |
181 | target.children.insertInPlace(item)
182 | }
183 |
184 | func (crdt *crdtTree) add(e EventLog) {
185 | target, exists := crdt.cache[e.TargetListItemKey]
186 | if !exists {
187 | // TODO centralise
188 | // TODO more efficient storage/inferrence of target item uuid/lamport
189 | r := strings.Split(e.TargetListItemKey, ":")
190 | var ts int64
191 | var originUUID uuid
192 | if len(r) > 0 {
193 | i, _ := strconv.ParseInt(r[0], 10, 64)
194 | originUUID = uuid(i)
195 | if len(r) > 1 {
196 | ts, _ = strconv.ParseInt(r[1], 10, 64)
197 | }
198 | }
199 |
200 | target = &node{
201 | key: e.TargetListItemKey,
202 | originUUID: originUUID,
203 | latestLamportTimestamp: ts,
204 | }
205 | crdt.cache[e.TargetListItemKey] = target
206 | orphanParent := crdt.cache[crdtOrphanKey]
207 | crdt.addToTargetChildArray(target, orphanParent)
208 | }
209 |
210 | item, exists := crdt.cache[e.ListItemKey]
211 | if !exists {
212 | item = &node{
213 | key: e.ListItemKey,
214 | originUUID: e.UUID,
215 | latestLamportTimestamp: e.LamportTimestamp,
216 | }
217 | crdt.cache[e.ListItemKey] = item
218 | } else {
219 | // remove from parent.children if pre-existing
220 | item.parent.children.removeChild(item)
221 | }
222 | item.latestLamportTimestamp = e.LamportTimestamp
223 |
224 | crdt.addToTargetChildArray(item, target)
225 | }
226 |
--------------------------------------------------------------------------------
/pkg/service/undo.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | // oppositeEvent returns the `undoing` event for a given type, e.g. delete an added item
4 | var oppositeEvent = map[EventType]EventType{
5 | AddEvent: DeleteEvent,
6 | DeleteEvent: AddEvent,
7 | UpdateEvent: UpdateEvent,
8 | MoveUpEvent: MoveDownEvent,
9 | MoveDownEvent: MoveUpEvent,
10 | ShowEvent: HideEvent,
11 | HideEvent: ShowEvent,
12 | }
13 |
14 | type undoLog struct {
15 | oppEvents []EventLog // The events required to undo the corresponding original events
16 | events []EventLog // The original events that were triggered on the client interface call (Add/Update/etc)
17 | }
18 |
19 | // DbEventLogger is used for in-mem undo/redo mechanism
20 | type DbEventLogger struct {
21 | curIdx int // Last index is latest/most recent in history (appends on new events)
22 | log []undoLog
23 | }
24 |
25 | // NewDbEventLogger Returns a new instance of DbEventLogger
26 | func NewDbEventLogger() *DbEventLogger {
27 | return &DbEventLogger{
28 | log: []undoLog{
29 | {
30 | oppEvents: []EventLog{
31 | {
32 | EventType: NullEvent,
33 | },
34 | },
35 | events: []EventLog{
36 | {
37 | EventType: NullEvent,
38 | },
39 | },
40 | },
41 | },
42 | }
43 | }
44 |
45 | func (r *DBListRepo) addUndoLogs(oppEvents []EventLog, originalEvents []EventLog) error {
46 | ul := undoLog{
47 | oppEvents: oppEvents,
48 | events: originalEvents,
49 | }
50 | // Truncate the event log, so when we Undo and then do something new, the previous Redo events
51 | // are overwritten
52 | r.eventLogger.log = r.eventLogger.log[:r.eventLogger.curIdx+1]
53 |
54 | // Append to log
55 | r.eventLogger.log = append(r.eventLogger.log, ul)
56 |
57 | r.eventLogger.curIdx++
58 |
59 | return nil
60 | }
61 |
--------------------------------------------------------------------------------
/pkg/service/undo_test.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "os"
5 | "testing"
6 | )
7 |
8 | func TestUndoTransaction(t *testing.T) {
9 | os.Mkdir(rootDir, os.ModePerm)
10 | t.Run("Undo on empty db", func(t *testing.T) {
11 | repo, clearUp := setupRepo()
12 | defer clearUp()
13 |
14 | repo.Undo()
15 |
16 | if len(repo.eventLogger.log) != 1 {
17 | t.Errorf("Event log should instantiate with a null event log at idx zero")
18 | }
19 |
20 | if matches, _, _ := repo.Match([][]rune{}, true, "", 0, 0); len(matches) != 0 {
21 | t.Errorf("Undo should have done nothing")
22 | }
23 | })
24 | t.Run("Undo single item Add", func(t *testing.T) {
25 | repo, clearUp := setupRepo()
26 | defer clearUp()
27 |
28 | line := "New item"
29 | repo.Add(line, nil, nil)
30 |
31 | if len(repo.eventLogger.log) != 2 {
32 | t.Errorf("Event log should have one null and one real event in it")
33 | }
34 |
35 | matches, _, _ := repo.Match([][]rune{}, true, "", 0, 0)
36 |
37 | item := repo.eventLogger.log[1]
38 | if item.events[0].EventType != UpdateEvent {
39 | t.Errorf("Event log event entry should be of type UpdateEvent")
40 | }
41 | if item.oppEvents[0].EventType != DeleteEvent {
42 | t.Errorf("Event log oppEvent entry should be of type Delete")
43 | }
44 |
45 | if item.events[0].ListItemKey != matches[0].Key() {
46 | t.Errorf("Event log list item should have the same key")
47 | }
48 | if item.oppEvents[0].ListItemKey != matches[0].Key() {
49 | t.Errorf("Event log list item should have the same key")
50 | }
51 |
52 | if (repo.eventLogger.curIdx) != 1 {
53 | t.Errorf("The event logger index should increment to 1")
54 | }
55 |
56 | repo.Undo()
57 |
58 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
59 |
60 | if len(matches) != 0 {
61 | t.Errorf("Undo should have removed the only item")
62 | }
63 |
64 | if len(repo.eventLogger.log) != 2 {
65 | t.Errorf("Event logger should persist the log")
66 | }
67 | if (repo.eventLogger.curIdx) != 0 {
68 | t.Errorf("The event logger index should decrement back to 0")
69 | }
70 | })
71 | t.Run("Undo single item Add and Update", func(t *testing.T) {
72 | repo, clearUp := setupRepo()
73 | defer clearUp()
74 |
75 | line := "New item"
76 | repo.Add(line, nil, nil)
77 |
78 | updatedLine := "Updated item"
79 |
80 | matches, _, _ := repo.Match([][]rune{}, true, "", 0, 0)
81 |
82 | repo.Update(updatedLine, &matches[0])
83 |
84 | if l := len(repo.eventLogger.log); l != 3 {
85 | t.Errorf("Event log should have one null and two real events in it, but has %d", l)
86 | }
87 | if repo.eventLogger.curIdx != 2 {
88 | t.Errorf("Event logger should be at position two")
89 | }
90 |
91 | newestLogItem := repo.eventLogger.log[2]
92 | if newestLogItem.events[0].EventType != UpdateEvent {
93 | t.Errorf("Newest event log event entry should be of type UpdateEvent")
94 | }
95 | if newestLogItem.oppEvents[0].EventType != UpdateEvent {
96 | t.Errorf("Newest event log oppEvent entry should be of type UpdateEvent")
97 | }
98 |
99 | if newestLogItem.events[0].Line != updatedLine {
100 | t.Errorf("Newest event log event line should have the updated line")
101 | }
102 | if newestLogItem.oppEvents[0].Line != line {
103 | t.Errorf("Newest event log oppEvent line should have the original line")
104 | }
105 |
106 | oldestLogItem := repo.eventLogger.log[1]
107 | if oldestLogItem.events[0].EventType != UpdateEvent {
108 | t.Errorf("Oldest event log event entry should be of type UpdateEvent")
109 | }
110 | if oldestLogItem.oppEvents[0].EventType != DeleteEvent {
111 | t.Errorf("Oldest event log oppEvent entry should be of type DeleteEvent")
112 | }
113 |
114 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
115 | if matches[0].Line() != updatedLine {
116 | t.Errorf("List item should have the updated line")
117 | }
118 |
119 | repo.Undo()
120 |
121 | if len(repo.eventLogger.log) != 3 {
122 | t.Errorf("Event log should still have three events in it")
123 | }
124 | if repo.eventLogger.curIdx != 1 {
125 | t.Errorf("Event logger should have decremented to one")
126 | }
127 |
128 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
129 | if len(matches) != 1 {
130 | t.Errorf("Undo should have updated the item, not deleted it")
131 | }
132 | if matches[0].Line() != line {
133 | t.Errorf("List item should now have the original line")
134 | }
135 |
136 | repo.Undo()
137 |
138 | if len(repo.eventLogger.log) != 3 {
139 | t.Errorf("Event log should still have three events in it")
140 | }
141 | if repo.eventLogger.curIdx != 0 {
142 | t.Errorf("Event logger should have decremented to zero")
143 | }
144 |
145 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
146 | if len(matches) != 0 {
147 | t.Errorf("Second undo should have deleted the item")
148 | }
149 | })
150 | t.Run("Add twice, Delete twice, Undo twice, Redo once", func(t *testing.T) {
151 | repo, clearUp := setupRepo()
152 | defer clearUp()
153 |
154 | line := "New item"
155 | repo.Add(line, nil, nil)
156 |
157 | if len(repo.eventLogger.log) != 2 {
158 | t.Errorf("Event log should have one null and one real event in it")
159 | }
160 | if repo.eventLogger.curIdx != 1 {
161 | t.Errorf("Event logger should have incremented to one")
162 | }
163 |
164 | logItem := &repo.eventLogger.log[1]
165 | checkFirstLogItemFn := func() string {
166 | if logItem.events[0].EventType != UpdateEvent {
167 | return "Event log entry event should be of type UpdateEvent"
168 | }
169 | if logItem.oppEvents[0].EventType != DeleteEvent {
170 | return "Event log entry oppEvent should be of type DeleteEvent"
171 | }
172 |
173 | if logItem.events[0].Line != line {
174 | return "Event log list event should have the original line"
175 | }
176 | return ""
177 | }
178 | if errStr := checkFirstLogItemFn(); errStr != "" {
179 | t.Error(errStr)
180 | }
181 |
182 | matches, _, _ := repo.Match([][]rune{}, true, "", 0, 0)
183 | listItem := matches[0]
184 | if logItem.events[0].ListItemKey != listItem.Key() {
185 | t.Errorf("The listItem key should be consistent with the original")
186 | }
187 | if logItem.oppEvents[0].ListItemKey != listItem.Key() {
188 | t.Errorf("The listItem key should be consistent with the original")
189 | }
190 |
191 | line2 := "Another item"
192 | repo.Add(line2, nil, &listItem)
193 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
194 | idx := 1
195 | listItem1 := matches[0]
196 | listItem2 := matches[idx]
197 |
198 | // Ensure the first logItem remains unchanged
199 | if errStr := checkFirstLogItemFn(); errStr != "" {
200 | t.Error("Original log item should still be in the first position in the log")
201 | }
202 |
203 | if len(repo.eventLogger.log) != 3 {
204 | t.Errorf("Event log should have one null and two real events in it")
205 | }
206 | if repo.eventLogger.curIdx != 2 {
207 | t.Errorf("Event logger should have incremented to two")
208 | }
209 |
210 | logItem2 := repo.eventLogger.log[2]
211 | if logItem2.events[0].EventType != UpdateEvent {
212 | t.Errorf("Event log entry event should be of type UpdateEvent")
213 | }
214 | if logItem2.oppEvents[0].EventType != DeleteEvent {
215 | t.Errorf("Event log entry oppEvent should be of type DeleteEvent")
216 | }
217 | if logItem2.events[0].Line != line2 {
218 | t.Errorf("Event log event should have the new line")
219 | }
220 |
221 | if logItem2.events[0].ListItemKey != listItem2.Key() {
222 | t.Errorf("The listItem key should be consistent with the original")
223 | }
224 | if logItem2.oppEvents[0].ListItemKey != listItem2.Key() {
225 | t.Errorf("The listItem key should be consistent with the original")
226 | }
227 |
228 | // need to reference the item in matchListItems
229 | repo.Delete(repo.matchListItems[listItem2.Key()])
230 |
231 | if len(repo.eventLogger.log) != 4 {
232 | t.Errorf("Event log should have one null and three real events in it")
233 | }
234 | if repo.eventLogger.curIdx != 3 {
235 | t.Errorf("Event logger should have incremented to three")
236 | }
237 |
238 | logItem3 := repo.eventLogger.log[3]
239 | if logItem3.events[0].EventType != DeleteEvent {
240 | t.Errorf("Event log event entry should be of type DeleteEvent")
241 | }
242 | if logItem3.oppEvents[0].EventType != UpdateEvent {
243 | t.Errorf("Event log oppEvent entry should be of type UpdateEvent")
244 | }
245 | if logItem3.oppEvents[0].Line != line2 {
246 | t.Errorf("Event log oppEvent should have the new line")
247 | }
248 |
249 | if logItem3.events[0].ListItemKey != listItem2.Key() {
250 | t.Errorf("The listItem key should be consistent with the original")
251 | }
252 | if logItem3.oppEvents[0].ListItemKey != listItem2.Key() {
253 | t.Errorf("The listItem key should be consistent with the original")
254 | }
255 |
256 | repo.Delete(repo.matchListItems[listItem1.Key()])
257 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
258 |
259 | if len(repo.eventLogger.log) != 5 {
260 | t.Errorf("Event log should have one null and four real events in it")
261 | }
262 | if repo.eventLogger.curIdx != 4 {
263 | t.Errorf("Event logger should have incremented to four")
264 | }
265 |
266 | logItem4 := repo.eventLogger.log[4]
267 | if logItem4.events[0].EventType != DeleteEvent {
268 | t.Errorf("Event log entry event should be of type DeleteEvent")
269 | }
270 | if logItem4.oppEvents[0].EventType != UpdateEvent {
271 | t.Errorf("Event log entry event should be of type UpdateEvent")
272 | }
273 | if logItem4.oppEvents[0].Line != line {
274 | t.Errorf("Event log list item should have the original line")
275 | }
276 |
277 | if logItem4.events[0].ListItemKey != listItem.Key() {
278 | t.Errorf("The listItem key should be consistent with the original")
279 | }
280 | if logItem4.oppEvents[0].ListItemKey != listItem.Key() {
281 | t.Errorf("The listItem key should be consistent with the original")
282 | }
283 |
284 | repo.Undo()
285 |
286 | if len(repo.eventLogger.log) != 5 {
287 | t.Errorf("Event log should still have five events in it")
288 | }
289 | if repo.eventLogger.curIdx != 3 {
290 | t.Errorf("Event logger should have decremented to three")
291 | }
292 |
293 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
294 | if len(matches) != 1 {
295 | t.Errorf("Undo should have added the original item back in")
296 | }
297 | if matches[0].Line() != line {
298 | t.Errorf("List item should now have the original line")
299 | }
300 |
301 | repo.Undo()
302 |
303 | if len(repo.eventLogger.log) != 5 {
304 | t.Errorf("Event log should still have five events in it")
305 | }
306 | if repo.eventLogger.curIdx != 2 {
307 | t.Errorf("Event logger should have decremented to two")
308 | }
309 |
310 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
311 | if len(matches) != 2 {
312 | t.Errorf("Undo should have added the second original item back in")
313 | }
314 | if matches[1].Line() != line2 {
315 | t.Errorf("List item should now have the original line")
316 | }
317 |
318 | repo.Redo()
319 |
320 | if len(repo.eventLogger.log) != 5 {
321 | t.Errorf("Event log should still have five events in it")
322 | }
323 | if repo.eventLogger.curIdx != 3 {
324 | t.Errorf("Event logger should have incremented to three")
325 | }
326 |
327 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
328 | if len(matches) != 1 {
329 | t.Errorf("Undo should have removed the second original item again")
330 | }
331 | if matches[0].Line() != line {
332 | t.Errorf("List item should now have the original line")
333 | }
334 | })
335 | t.Run("Add empty item, update with character, Undo, Redo", func(t *testing.T) {
336 | repo, clearUp := setupRepo()
337 | defer clearUp()
338 |
339 | repo.Add("", nil, nil)
340 |
341 | logItem := repo.eventLogger.log[1]
342 | if logItem.events[0].EventType != UpdateEvent {
343 | t.Errorf("Event log event entry should be of type UpdateEvent")
344 | }
345 | if logItem.oppEvents[0].EventType != DeleteEvent {
346 | t.Errorf("Event log event entry should be of type DeleteEvent")
347 | }
348 |
349 | matches, _, _ := repo.Match([][]rune{}, true, "", 0, 0)
350 |
351 | newLine := "a"
352 | repo.Update(newLine, &matches[0])
353 |
354 | if len(repo.eventLogger.log) != 3 {
355 | t.Errorf("Event log should have one null and two real events in it")
356 | }
357 | if repo.eventLogger.curIdx != 2 {
358 | t.Errorf("Event logger should have incremented to two")
359 | }
360 |
361 | logItem2 := repo.eventLogger.log[2]
362 | if logItem2.events[0].EventType != UpdateEvent {
363 | t.Errorf("Event log entry event should be of type UpdateEvent")
364 | }
365 | if logItem2.oppEvents[0].EventType != UpdateEvent {
366 | t.Errorf("Event log entry oppEvent should be of type UpdateEvent")
367 | }
368 | if logItem2.events[0].Line != newLine {
369 | t.Errorf("Event log entry event Line should be the new line")
370 | }
371 | if logItem2.oppEvents[0].Line != "" {
372 | t.Errorf("Event log entry event Line should be empty")
373 | }
374 |
375 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
376 | if matches[0].Line() != newLine {
377 | t.Errorf("List item should now have the new line")
378 | }
379 |
380 | repo.Undo()
381 |
382 | if len(repo.eventLogger.log) != 3 {
383 | t.Errorf("Event log should still have three events in it")
384 | }
385 | if repo.eventLogger.curIdx != 1 {
386 | t.Errorf("Event logger should have decremented to one")
387 | }
388 |
389 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
390 | // TODO currently caused by bad `if len(line) > 0` check in `update()` handling
391 | if matches[0].Line() != "" {
392 | t.Errorf("Undo should have emptied the line")
393 | }
394 |
395 | repo.Redo()
396 |
397 | if len(repo.eventLogger.log) != 3 {
398 | t.Errorf("Event log should still have two events in it")
399 | }
400 | if repo.eventLogger.curIdx != 2 {
401 | t.Errorf("Event logger should have returned to the head at two")
402 | }
403 |
404 | // TODO problem is, looking ahead to next log item for `Redo` redoes the old PRE state
405 | // Idea: store old and new state in the log item lines, Undo sets to old, Redo sets to new
406 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
407 | if matches[0].Line() != newLine {
408 | t.Errorf("Redo should have added the line back in")
409 | }
410 | })
411 | t.Run("Add line, Delete line, Undo, delete character, Undo", func(t *testing.T) {
412 | repo, clearUp := setupRepo()
413 | defer clearUp()
414 |
415 | originalLine := "Original line"
416 | repo.Add(originalLine, nil, nil)
417 |
418 | if len(repo.eventLogger.log) != 2 {
419 | t.Errorf("Event log should have a nullEvent and addEvent in it")
420 | }
421 | if repo.eventLogger.curIdx != 1 {
422 | t.Errorf("Event logger should be set to one")
423 | }
424 |
425 | matches, _, _ := repo.Match([][]rune{}, true, "", 0, 0)
426 |
427 | repo.Delete(&matches[0])
428 |
429 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
430 | if len(matches) != 0 {
431 | t.Errorf("Item should have been deleted")
432 | }
433 |
434 | if len(repo.eventLogger.log) != 3 {
435 | t.Errorf("Event log should have the nullEvent, addEvent and one deleteEvent")
436 | }
437 | if repo.eventLogger.curIdx != 2 {
438 | t.Errorf("Event logger should have incremented to one")
439 | }
440 |
441 | repo.Undo()
442 |
443 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
444 | if len(matches) != 1 {
445 | t.Errorf("Item should have been added back in")
446 | }
447 |
448 | if len(repo.eventLogger.log) != 3 {
449 | t.Errorf("Event log should be unchanged")
450 | }
451 | if repo.eventLogger.curIdx != 1 {
452 | t.Errorf("Event logger should have decremented to one")
453 | }
454 |
455 | newLine := "Updated line"
456 | repo.Update(newLine, &matches[0])
457 |
458 | if len(repo.eventLogger.log) != 3 {
459 | t.Errorf("Event log should have the nullEvent, addEvent and overriding updateEvent")
460 | }
461 | logEvent := repo.eventLogger.log[2]
462 | if logEvent.events[0].EventType != UpdateEvent {
463 | t.Errorf("Event logger item event should be of type updateEvent")
464 | }
465 | if logEvent.oppEvents[0].EventType != UpdateEvent {
466 | t.Errorf("Event logger item oppEvent should be of type updateEvent")
467 | }
468 | if repo.eventLogger.curIdx != 2 {
469 | t.Errorf("Event logger should have incremented to 2")
470 | }
471 |
472 | repo.Undo()
473 |
474 | matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
475 | if len(matches) != 1 {
476 | t.Errorf("There should still be one match")
477 | }
478 |
479 | if len(repo.eventLogger.log) != 3 {
480 | t.Errorf("Event log should be unchanged")
481 | }
482 | if repo.eventLogger.curIdx != 1 {
483 | t.Errorf("Event logger should have decremented to 1")
484 | }
485 |
486 | if matches[0].Line() != originalLine {
487 | t.Errorf("The line should have reverted back to the original")
488 | }
489 | })
490 | //t.Run("Add three, Move middle down, Move bottom up, Undo twice, Redo twice", func(t *testing.T) {
491 | // repo, clearUp := setupRepo()
492 | // defer clearUp()
493 |
494 | // repo.Add("Third", nil, nil)
495 | // repo.Add("Second", nil, nil)
496 | // repo.Add("First", nil, nil)
497 |
498 | // matches, _, _ := repo.Match([][]rune{}, true, "", 0, 0)
499 | // repo.MoveDown(&matches[1])
500 |
501 | // matches, _, _ = repo.Match([][]rune{}, true, "", 0, 0)
502 | // repo.MoveUp(&matches[2])
503 |
504 | // if l := len(repo.eventLogger.log); l != 4 {
505 | // t.Errorf("Event log len should be %d but is %d", 4, l)
506 | // }
507 | // if l := repo.eventLogger.curIdx; l != 3 {
508 | // t.Errorf("Event logger should be set to %d but is %d", 3, l)
509 | // }
510 | //})
511 | }
512 |
--------------------------------------------------------------------------------
/pkg/service/web.go:
--------------------------------------------------------------------------------
1 | package service
2 |
3 | import (
4 | "bytes"
5 | "context"
6 | "encoding/base64"
7 | "encoding/json"
8 | "errors"
9 | "fmt"
10 | "io"
11 | "io/ioutil"
12 | "log"
13 | "net/http"
14 | "net/url"
15 | "path"
16 | "strings"
17 | "time"
18 |
19 | "nhooyr.io/websocket"
20 | )
21 |
22 | const (
23 | websocketURL = "wss://ws.fuzzynote.co.uk/v1"
24 | apiURL = "https://api.fuzzynote.co.uk/v1"
25 |
26 | walSyncAuthorizationHeader = "Authorization"
27 |
28 | webPingInterval = time.Second * 30 // lower ping intervals (5s has been tested) cause performance degradation in the wasm app
29 | webRefreshInterval = time.Second * (2 << 6) // 128 seconds ~= 2 minutes, because we use exponential backoffs
30 | )
31 |
32 | type WebWalFile struct {
33 | // TODO rename uuid to email
34 | uuid string
35 | web *Web
36 | mode string
37 | }
38 |
39 | func (w *Web) establishWebSocketConnection() error {
40 | // TODO close off previous connection gracefully if present??
41 |
42 | dialFunc := func(token string) (*websocket.Conn, *http.Response, error) {
43 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
44 | defer cancel()
45 |
46 | u, _ := url.Parse(websocketURL)
47 | q, _ := url.ParseQuery(u.RawQuery)
48 | q.Add("auth", token)
49 | u.RawQuery = q.Encode()
50 |
51 | return websocket.Dial(ctx, u.String(), &websocket.DialOptions{})
52 | }
53 |
54 | var resp *http.Response
55 | var err error
56 | idToken := w.tokens.IDToken()
57 | if idToken != "" {
58 | w.wsConn, resp, err = dialFunc(w.tokens.IDToken())
59 | }
60 | // TODO re-authentication explicitly handled here as wss handshake only occurs once (doesn't require
61 | // retries) - can probably dedup at least a little
62 | if idToken == "" || err != nil || resp == nil || resp.StatusCode != http.StatusSwitchingProtocols {
63 | defer w.tokens.Flush()
64 | w.tokens.SetIDToken("")
65 | w.wsConn = nil
66 | body := map[string]string{
67 | "refreshToken": w.tokens.RefreshToken(),
68 | }
69 | err = Authenticate(w.tokens, body)
70 | if err != nil {
71 | if _, ok := err.(authFailureError); ok {
72 | w.tokens.SetRefreshToken("")
73 | w.tokens.Flush()
74 | }
75 | return err
76 | }
77 | w.wsConn, resp, err = dialFunc(w.tokens.IDToken())
78 | // need to return within this nested block otherwise the outside err still holds
79 | // data from previous calls
80 | return err
81 | }
82 | return err
83 | }
84 |
85 | type pong struct {
86 | Response string
87 | User string
88 | ActiveFriends, PendingFriends []string
89 | }
90 |
91 | func (w *Web) ping() (pong, error) {
92 | p := pong{}
93 | u, _ := url.Parse(apiURL)
94 | u.Path = path.Join(u.Path, "ping")
95 | req, err := http.NewRequest("GET", u.String(), nil)
96 | if err != nil {
97 | return p, err
98 | }
99 |
100 | resp, err := w.CallWithReAuth(req)
101 | if err != nil {
102 | return p, err
103 | }
104 | defer resp.Body.Close()
105 |
106 | if resp.StatusCode != http.StatusOK {
107 | return p, errors.New("ping did not return 201")
108 | }
109 | respBytes, err := ioutil.ReadAll(resp.Body)
110 | if err != nil {
111 | return p, err
112 | }
113 | if err := json.Unmarshal(respBytes, &p); err != nil {
114 | return p, err
115 | }
116 | if p.Response != "pong" {
117 | return p, errors.New("ping did not return a pong")
118 | }
119 | return p, nil
120 | }
121 |
122 | type websocketMessage struct {
123 | Action string `json:"action"`
124 |
125 | // `wal` events
126 | UUID string `json:"uuid"`
127 | Wal string `json:"wal"`
128 |
129 | // `position` events (collaborator cursor positions)
130 | Email string `json:"email"`
131 | Key string `json:"key"`
132 | UnixNanoTime int64 `json:"dt"`
133 | }
134 |
135 | type cursorMoveEvent struct {
136 | email string
137 | listItemKey string
138 | unixNanoTime int64
139 | }
140 |
141 | func (w *Web) pushWebsocket(m websocketMessage) error {
142 | marshalData, err := json.Marshal(m)
143 | if err != nil {
144 | //log.Fatal("Json marshal: malformed WAL data on websocket push")
145 | // TODO proper handling
146 | return err
147 | }
148 |
149 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
150 | defer cancel()
151 |
152 | return w.wsConn.Write(ctx, websocket.MessageText, []byte(marshalData))
153 | //if err != nil {
154 | // // Re-establish websocket connection on error
155 | // // TODO currently attempting to re-establish connection on ANY error - do better at
156 | // // identifying websocket connect issues
157 | // w.establishWebSocketConnection()
158 |
159 | // // if re-establish successful (e.g. wsConn != nil) reattempt write
160 | // if w.wsConn != nil {
161 | // err = w.wsConn.Write(ctx, websocket.MessageText, []byte(marshalData))
162 | // if err != nil {
163 | // w.wsConn = nil
164 | // }
165 | // }
166 | //}
167 | }
168 |
169 | func (r *DBListRepo) consumeWebsocket(ctx context.Context) ([]EventLog, error) {
170 | var el []EventLog
171 | _, body, err := r.web.wsConn.Read(ctx)
172 | if err != nil {
173 | return el, err
174 | }
175 |
176 | var m websocketMessage
177 | err = json.Unmarshal(body, &m)
178 | if err != nil {
179 | return el, nil
180 | }
181 |
182 | switch m.Action {
183 | case "wal":
184 | strWal, err := base64.StdEncoding.DecodeString(m.Wal)
185 | if err != nil {
186 | // TODO proper handling
187 | return el, nil
188 | }
189 |
190 | buf := bytes.NewBuffer([]byte(strWal))
191 | if el, err = r.buildFromFile(buf); err != nil {
192 | return el, err
193 | }
194 | case "position":
195 | r.remoteCursorMoveChan <- cursorMoveEvent{
196 | email: m.Email,
197 | listItemKey: m.Key,
198 | unixNanoTime: m.UnixNanoTime,
199 | }
200 | }
201 |
202 | // TODO proper handling
203 | return el, nil
204 | }
205 |
206 | func (wf *WebWalFile) getPresignedURLForWal(ctx context.Context, owner string, checksum string, method string) (string, error) {
207 | u, _ := url.Parse(apiURL)
208 | q, _ := url.ParseQuery(u.RawQuery)
209 | q.Add("method", method)
210 | q.Add("owner", owner)
211 | q.Add("checksum", checksum)
212 | u.RawQuery = q.Encode()
213 |
214 | u.Path = path.Join(u.Path, "wal", "presigned")
215 |
216 | req, err := http.NewRequest("GET", u.String(), nil)
217 | if err != nil {
218 | return "", err
219 | }
220 |
221 | req = req.WithContext(ctx)
222 |
223 | resp, err := wf.web.CallWithReAuth(req)
224 | if err != nil {
225 | return "", err
226 | }
227 | defer resp.Body.Close()
228 | body, _ := ioutil.ReadAll(resp.Body)
229 | if resp.StatusCode != http.StatusOK {
230 | return "", fmt.Errorf("Unable to generate presigned `put` url: %s", body)
231 | }
232 |
233 | // The response body will contain the presigned URL we will use to actually retrieve the wal
234 | if err != nil {
235 | return "", fmt.Errorf("Error parsing presigned url from response: %s", err)
236 | }
237 |
238 | return string(body), nil
239 | }
240 |
241 | func (wf *WebWalFile) GetUUID() string {
242 | return wf.uuid
243 | }
244 |
245 | func (wf *WebWalFile) GetRoot() string { return "" }
246 |
247 | func (wf *WebWalFile) GetMatchingWals(ctx context.Context, pattern string) ([]string, error) {
248 | u, _ := url.Parse(apiURL)
249 | // we now pass emails as `wf.uuid`, so escape it here
250 | escapedEmail := url.PathEscape(wf.uuid)
251 | u.Path = path.Join(u.Path, "wal", "list", escapedEmail)
252 |
253 | req, err := http.NewRequest("GET", u.String(), nil)
254 | if err != nil {
255 | log.Fatalf("Error creating wal list request: %v", err)
256 | }
257 |
258 | req = req.WithContext(ctx)
259 |
260 | resp, err := wf.web.CallWithReAuth(req)
261 | if err != nil {
262 | return nil, err
263 | }
264 | defer resp.Body.Close()
265 | if resp.StatusCode != http.StatusOK {
266 | //log.Printf("Error retrieving wal list: %v", err)
267 | return nil, nil
268 | }
269 |
270 | byteUUIDs, err := ioutil.ReadAll(resp.Body)
271 | if err != nil {
272 | return nil, nil
273 | }
274 |
275 | var uuids []string
276 | err = json.Unmarshal(byteUUIDs, &uuids)
277 | if err != nil {
278 | return nil, err
279 | }
280 |
281 | return uuids, nil
282 | }
283 |
284 | func (wf *WebWalFile) GetWalBytes(ctx context.Context, w io.Writer, fileName string) error {
285 | presignedURL, err := wf.getPresignedURLForWal(ctx, wf.GetUUID(), fileName, "get")
286 | if err != nil {
287 | //log.Printf("Error retrieving wal %s: %s", fileName, err)
288 | return err
289 | }
290 |
291 | req, err := http.NewRequest("GET", presignedURL, nil)
292 | req = req.WithContext(ctx)
293 |
294 | resp, err := http.DefaultClient.Do(req)
295 | if err != nil {
296 | return err
297 | }
298 | defer resp.Body.Close()
299 |
300 | dec := base64.NewDecoder(base64.StdEncoding, resp.Body)
301 |
302 | io.Copy(w, dec)
303 |
304 | return nil
305 | }
306 |
307 | func (wf *WebWalFile) RemoveWals(ctx context.Context, fileNames []string) error {
308 | var uuids []string
309 | for _, f := range fileNames {
310 | uuids = append(uuids, f)
311 | }
312 |
313 | u, _ := url.Parse(apiURL)
314 | escapedEmail := url.PathEscape(wf.uuid)
315 | u.Path = path.Join(u.Path, "wal", "delete", escapedEmail)
316 |
317 | marshalNames, err := json.Marshal(fileNames)
318 | if err != nil {
319 | return err
320 | }
321 |
322 | req, err := http.NewRequest("POST", u.String(), strings.NewReader(string(marshalNames)))
323 | if err != nil {
324 | return err
325 | }
326 |
327 | req = req.WithContext(ctx)
328 |
329 | resp, err := wf.web.CallWithReAuth(req)
330 | if err != nil {
331 | return err
332 | }
333 | resp.Body.Close()
334 | return nil
335 | }
336 |
337 | func (wf *WebWalFile) Flush(ctx context.Context, b *bytes.Buffer, checksum string) error {
338 | // TODO refactor to pass only UUID, rather than full path (currently blocked by all WalFile != WebWalFile
339 | //tempUUID := strings.Split(strings.Split(fileName, "_")[1], ".")[0]
340 |
341 | presignedURL, err := wf.getPresignedURLForWal(ctx, wf.GetUUID(), checksum, "put")
342 | if err != nil || presignedURL == "" {
343 | return nil
344 | }
345 |
346 | b64Wal := base64.StdEncoding.EncodeToString(b.Bytes())
347 |
348 | req, err := http.NewRequest("PUT", presignedURL, strings.NewReader(b64Wal))
349 | if err != nil {
350 | return err
351 | }
352 |
353 | req = req.WithContext(ctx)
354 |
355 | resp, err := http.DefaultClient.Do(req)
356 | if err != nil {
357 | return err
358 | }
359 | resp.Body.Close()
360 | return nil
361 | }
362 |
--------------------------------------------------------------------------------
/pkg/term/client.go:
--------------------------------------------------------------------------------
1 | package term
2 |
3 | import (
4 | "errors"
5 | "fmt"
6 | "io/ioutil"
7 | "log"
8 | "math/rand"
9 | "os"
10 | "os/exec"
11 | "strings"
12 |
13 | "github.com/atotto/clipboard"
14 | "github.com/gdamore/tcell/v2"
15 | "github.com/gdamore/tcell/v2/encoding"
16 | "github.com/mattn/go-runewidth"
17 |
18 | "github.com/sambigeara/fuzzynote/pkg/service"
19 | )
20 |
21 | const (
22 | reservedEndChars = 1
23 | emptySearchLinePrompt = "Search here..."
24 | searchGroupPrompt = "TAB: Create new search group"
25 | newLinePrompt = "Enter: Create new line"
26 | )
27 |
28 | type Terminal struct {
29 | db *service.DBListRepo
30 | c *service.ClientBase
31 |
32 | S tcell.Screen
33 | style tcell.Style
34 | colour string
35 | Editor string
36 |
37 | previousKey tcell.Key // Keep track of the previous keypress
38 |
39 | //footerMessage string // Because we refresh on an ongoing basis, this needs to be emitted each time we paint
40 | }
41 |
42 | func NewTerm(db *service.DBListRepo, colour string, editor string) *Terminal {
43 | encoding.Register()
44 |
45 | defStyle := tcell.StyleDefault.
46 | Background(tcell.ColorWhite).
47 | Foreground(tcell.ColorBlack)
48 |
49 | if colour == "dark" {
50 | defStyle = defStyle.Reverse(true)
51 | }
52 |
53 | s := newInstantiatedScreen(defStyle)
54 |
55 | w, h := s.Size()
56 | t := Terminal{
57 | db: db,
58 | c: service.NewClientBase(db, w, h, false),
59 | S: s,
60 | style: defStyle,
61 | colour: colour,
62 | Editor: editor,
63 | }
64 | return &t
65 | }
66 |
67 | func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
68 | for _, c := range str {
69 | var comb []rune
70 | w := runewidth.RuneWidth(c)
71 | if w == 0 {
72 | comb = []rune{c}
73 | c = ' '
74 | w = 1
75 | }
76 | s.SetContent(x, y, c, comb, style)
77 | x += w
78 | }
79 | }
80 |
81 | func newInstantiatedScreen(style tcell.Style) tcell.Screen {
82 | s, e := tcell.NewScreen()
83 | if e != nil {
84 | fmt.Fprintf(os.Stderr, "%v\n", e)
85 | os.Exit(1)
86 | }
87 | if e := s.Init(); e != nil {
88 | fmt.Fprintf(os.Stderr, "%v\n", e)
89 | os.Exit(1)
90 | }
91 | s.SetStyle(style)
92 | //s.EnableMouse()
93 | s.Clear()
94 | return s
95 | }
96 |
97 | func (t *Terminal) buildSearchBox(s tcell.Screen) {
98 | // If no search items at all, display emptySearchLinePrompt and return
99 | if len(t.c.Search) == 0 || len(t.c.Search) == 1 && len(t.c.Search[0]) == 0 {
100 | emitStr(s, 0, 0, t.style.Dim(true), emptySearchLinePrompt)
101 | }
102 |
103 | searchStyle := tcell.StyleDefault.
104 | Foreground(tcell.ColorWhite).Background(tcell.ColorGrey)
105 |
106 | var pos, l int
107 | for _, key := range t.c.Search {
108 | emitStr(s, pos, 0, searchStyle, string(key))
109 | l = len(key)
110 | pos = pos + l + 1 // Add a separator between groups with `+ 1`
111 | }
112 |
113 | // Display `TAB` prompt after final search group if only one search group
114 | // +1 just to give some breathing room
115 | if len(t.c.Search) == 1 && len(t.c.Search[0]) > 0 && t.c.CurY == 0 {
116 | emitStr(s, pos+1, 0, t.style.Dim(true), searchGroupPrompt)
117 | }
118 |
119 | // Display whether all items or just non-hidden items are currently displayed
120 | if t.c.ShowHidden {
121 | indicator := "VIS"
122 | emitStr(s, t.c.W-len([]byte(indicator))+reservedEndChars, 0, searchStyle, indicator)
123 | } else {
124 | indicator := "HID"
125 | emitStr(s, t.c.W-len([]byte(indicator))+reservedEndChars, 0, searchStyle, indicator)
126 | }
127 | }
128 |
129 | func (t *Terminal) buildFooter(s tcell.Screen, text string) {
130 | footer := tcell.StyleDefault.
131 | Foreground(tcell.ColorBlack).Background(tcell.ColorYellow)
132 |
133 | // Pad out remaining line with spaces to ensure whole bar is filled
134 | lenStr := len([]rune(text))
135 | text += string(make([]rune, t.c.W-lenStr))
136 | // ReservedBottomLines is subtracted from t.c.H globally, and we want to print on the bottom line
137 | // so add it back in here
138 | emitStr(s, 0, t.c.H-1+t.c.ReservedBottomLines, footer, text)
139 | }
140 |
141 | func (t *Terminal) buildSingleStyleCollabDisplay(s tcell.Screen, style tcell.Style, collaborators []string, xOffset int, yOffset int) {
142 | friendStyles := map[tcell.Style][]string{
143 | style: []string{},
144 | }
145 | for _, c := range collaborators {
146 | friendStyles[style] = append(friendStyles[style], c)
147 | }
148 | t.buildCollabDisplay(t.S, friendStyles, xOffset, yOffset)
149 | }
150 |
151 | func (t *Terminal) buildCollabDisplay(s tcell.Screen, collaborators map[tcell.Style][]string, xOffset int, yOffset int) {
152 | x := xOffset
153 | for style, collabStrSlice := range collaborators {
154 | for _, collabStr := range collabStrSlice {
155 | emitStr(s, x, yOffset, style, collabStr)
156 | x += len(collabStr) + 1
157 | }
158 | }
159 | }
160 |
161 | func (t *Terminal) resizeScreen() {
162 | w, h := t.S.Size()
163 | t.c.W = w - reservedEndChars
164 | t.c.H = h - t.c.ReservedBottomLines
165 | }
166 |
167 | // A selection of colour combos to apply to collaborators
168 | var (
169 | collabStyleCombos []tcell.Style = []tcell.Style{
170 | tcell.StyleDefault.
171 | Background(tcell.Color25).
172 | Foreground(tcell.ColorWhite),
173 | tcell.StyleDefault.
174 | Background(tcell.Color90).
175 | Foreground(tcell.ColorWhite),
176 | tcell.StyleDefault.
177 | Background(tcell.Color124).
178 | Foreground(tcell.ColorWhite),
179 | tcell.StyleDefault.
180 | Background(tcell.Color126).
181 | Foreground(tcell.ColorWhite),
182 | tcell.StyleDefault.
183 | Background(tcell.Color136).
184 | Foreground(tcell.ColorWhite),
185 | tcell.StyleDefault.
186 | Background(tcell.Color166).
187 | Foreground(tcell.ColorWhite),
188 | }
189 | collabStyleIncStart = rand.Intn(len(collabStyleCombos))
190 | )
191 |
192 | func (t *Terminal) paint(matches []service.ListItem, saveWarning bool) error {
193 | t.S.Clear()
194 | t.resizeScreen()
195 |
196 | // Get collaborator map
197 | collabMap := t.db.GetCollabPositions()
198 |
199 | // Build top search box
200 | t.buildSearchBox(t.S)
201 |
202 | // Store comma separated strings of collaborator emails against the style
203 | collaborators := make(map[tcell.Style][]string)
204 |
205 | // Randomise the starting colour index for bants
206 | collabStyleInc := collabStyleIncStart
207 | offset := 0
208 | for i, r := range matches[t.c.VertOffset:service.Min(len(matches), t.c.VertOffset+t.c.H-t.c.ReservedTopLines)] {
209 | style := t.style
210 | offset = i + t.c.ReservedTopLines
211 |
212 | // Get current collaborators on item, if any
213 | lineCollabers := collabMap[r.Key()]
214 |
215 | // Mutually exclusive style triggers
216 | if _, ok := t.c.SelectedItems[matches[i].Key()]; ok {
217 | // Currently selected with Ctrl-S
218 | // By default, we reverse the colourscheme for "dark" settings, so undo the
219 | // reversal, to reverse again...
220 | if t.colour == "light" {
221 | style = style.Reverse(true)
222 | } else if t.colour == "dark" {
223 | style = style.Reverse(false)
224 | }
225 | } else if len(lineCollabers) > 0 {
226 | // If collaborators are on line
227 | style = collabStyleCombos[collabStyleInc%len(collabStyleCombos)]
228 | collaborators[style] = []string{strings.Join(lineCollabers, ",")}
229 | collabStyleInc++
230 | }
231 |
232 | if len(r.Note) > 0 {
233 | style = style.Underline(true).Bold(true)
234 | }
235 |
236 | if r.IsHidden {
237 | style = style.Dim(true)
238 | }
239 |
240 | line := t.c.TrimPrefix(r.Line())
241 |
242 | // Account for horizontal offset if on curItem
243 | if i == t.c.CurY-t.c.ReservedTopLines {
244 | if len(line) > 0 {
245 | line = line[t.c.HorizOffset:]
246 | }
247 | }
248 |
249 | // Emit line
250 | emitStr(t.S, 0, offset, style, line)
251 |
252 | // If the line is shared with anyone, paint the collaborators after the line
253 | if friends := r.Friends(); len(friends) > 0 {
254 | // TODO optimise
255 | // Don't bother displaying friends that are currently being searched for
256 | removedSearchFriends := t.c.GetUnsearchedFriends(friends)
257 | s := tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorYellow).Dim(true)
258 | t.buildSingleStyleCollabDisplay(t.S, s, removedSearchFriends, len([]rune(line))+1, offset)
259 | }
260 |
261 | if offset == t.c.H {
262 | break
263 | }
264 | }
265 |
266 | // If no matches, display help prompt on first line
267 | // TODO ordering
268 | if len(matches) == 0 {
269 | if len(t.c.Search) > 0 && len(t.c.Search[0]) > 0 {
270 | newLinePrefixPrompt := "Enter: Create new line with search prefix: \"" + service.GetNewLinePrefix(t.c.Search) + "\""
271 | emitStr(t.S, 0, t.c.ReservedTopLines, t.style.Dim(true), newLinePrefixPrompt)
272 | } else {
273 | emitStr(t.S, 0, t.c.ReservedTopLines, t.style.Dim(true), newLinePrompt)
274 | }
275 | }
276 |
277 | // Show active collaborators
278 | if len(collaborators) > 0 {
279 | t.buildCollabDisplay(t.S, collaborators, 0, t.c.H-2+t.c.ReservedBottomLines)
280 | }
281 |
282 | if t.c.CurItem != nil {
283 | if friends := t.c.CurItem.Friends(); len(friends) > 0 {
284 | friends := append([]string{"Shared with:"}, friends...) // Add a prompt as the initial string
285 | s := tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorYellow)
286 | t.buildSingleStyleCollabDisplay(t.S, s, friends, 0, t.c.H-1+t.c.ReservedBottomLines)
287 | }
288 | }
289 |
290 | t.S.ShowCursor(t.c.CurX, t.c.CurY)
291 | t.S.Show()
292 |
293 | return nil
294 | }
295 |
296 | func (t *Terminal) openEditorSession() error {
297 | // Write text to temp file
298 | tmpfile, err := ioutil.TempFile("", "fzn_buffer")
299 | if err != nil {
300 | log.Fatal(err)
301 | }
302 |
303 | defer os.Remove(tmpfile.Name())
304 |
305 | if _, err := tmpfile.Write(t.c.CurItem.Note); err != nil {
306 | log.Fatal(err)
307 | return err
308 | }
309 |
310 | //https://stackoverflow.com/questions/21513321/how-to-start-vim-from-go
311 | cmd := exec.Command(t.Editor, tmpfile.Name())
312 | cmd.Stdin = os.Stdin
313 | cmd.Stdout = os.Stdout
314 | err = cmd.Run()
315 | if err != nil {
316 | // For now, show a warning and return
317 | // TODO make more robust
318 | //t.footerMessage = "Unable to open Note using editor setting : \"" + t.Editor + "\""
319 | }
320 |
321 | // Read back from the temp file, and return to the write function
322 | newDat, err := ioutil.ReadFile(tmpfile.Name())
323 | if err != nil {
324 | log.Fatal(err)
325 | return nil
326 | }
327 |
328 | err = t.db.UpdateNote(newDat, t.c.CurItem)
329 | if err != nil {
330 | log.Fatal(err)
331 | }
332 |
333 | if err := tmpfile.Close(); err != nil {
334 | log.Fatal(err)
335 | }
336 |
337 | return nil
338 | }
339 |
340 | func (t *Terminal) AwaitEvent() interface{} {
341 | return t.S.PollEvent()
342 | }
343 |
344 | func (t *Terminal) HandleEvent(ev interface{}) error {
345 | interactionEvent := service.InteractionEvent{}
346 | switch ev := ev.(type) {
347 | case *tcell.EventKey:
348 | switch ev.Key() {
349 | case tcell.KeyEscape:
350 | interactionEvent.T = service.KeyEscape
351 | if t.previousKey == tcell.KeyEscape {
352 | t.S.Fini()
353 | return errors.New("closing gracefully")
354 | }
355 | case tcell.KeyEnter:
356 | interactionEvent.T = service.KeyEnter
357 | case tcell.KeyCtrlD:
358 | interactionEvent.T = service.KeyDeleteItem
359 | case tcell.KeyCtrlO:
360 | //interactionEvent.T = service.KeyOpenNote
361 | if t.c.CurY+t.c.VertOffset != 0 {
362 | if err := t.S.Suspend(); err == nil {
363 | err = t.openEditorSession()
364 | if err != nil {
365 | log.Fatal(err)
366 | }
367 | if err := t.S.Resume(); err != nil {
368 | panic("failed to resume: " + err.Error())
369 | }
370 | }
371 | }
372 | case tcell.KeyCtrlA:
373 | interactionEvent.T = service.KeyGotoStart
374 | case tcell.KeyCtrlE:
375 | interactionEvent.T = service.KeyGotoEnd
376 | case tcell.KeyCtrlV:
377 | interactionEvent.T = service.KeyVisibility
378 | case tcell.KeyCtrlU:
379 | interactionEvent.T = service.KeyUndo
380 | case tcell.KeyCtrlR:
381 | interactionEvent.T = service.KeyRedo
382 | case tcell.KeyCtrlC:
383 | if t.c.CurItem != nil {
384 | if url := service.MatchFirstURL(t.c.CurItem.Line(), true); url != "" {
385 | clipboard.WriteAll(url)
386 | }
387 | }
388 | interactionEvent.T = service.KeyCopy
389 | case tcell.KeyCtrlUnderscore:
390 | interactionEvent.T = service.KeyOpenURL
391 | case tcell.KeyCtrlCarat:
392 | interactionEvent.T = service.KeyExport
393 | case tcell.KeyCtrlP:
394 | interactionEvent.T = service.KeyPaste
395 | case tcell.KeyCtrlS:
396 | interactionEvent.T = service.KeySelect
397 | case tcell.KeyTab:
398 | interactionEvent.T = service.KeyAddSearchGroup
399 | case tcell.KeyBackspace:
400 | fallthrough
401 | case tcell.KeyBackspace2:
402 | interactionEvent.T = service.KeyBackspace
403 | case tcell.KeyDelete:
404 | interactionEvent.T = service.KeyDelete
405 | case tcell.KeyPgUp:
406 | interactionEvent.T = service.KeyMoveItemUp
407 | case tcell.KeyPgDn:
408 | interactionEvent.T = service.KeyMoveItemDown
409 | case tcell.KeyDown:
410 | interactionEvent.T = service.KeyCursorDown
411 | case tcell.KeyUp:
412 | interactionEvent.T = service.KeyCursorUp
413 | case tcell.KeyRight:
414 | interactionEvent.T = service.KeyCursorRight
415 | case tcell.KeyLeft:
416 | interactionEvent.T = service.KeyCursorLeft
417 | default:
418 | interactionEvent.T = service.KeyRune
419 | interactionEvent.R = []rune{ev.Rune()}
420 | //t.footerMessage = ""
421 | }
422 | t.previousKey = ev.Key()
423 | }
424 |
425 | if t.c.CurItem != nil {
426 | interactionEvent.Key = t.c.CurItem.Key()
427 | }
428 |
429 | matches, _, err := t.c.HandleInteraction(interactionEvent, t.c.Search, t.c.ShowHidden, false, 0)
430 | if err != nil {
431 | return err
432 | }
433 |
434 | var newKey string
435 | if t.c.CurItem != nil {
436 | newKey = t.c.CurItem.Key()
437 | }
438 | t.db.EmitCursorMoveEvent(newKey)
439 |
440 | t.paint(matches, false)
441 |
442 | return nil
443 | }
444 |
--------------------------------------------------------------------------------