-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcyber-acid.go
1179 lines (1089 loc) · 42.3 KB
/
cyber-acid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"log"
"net/http"
"sort"
"strconv"
"time"
"github.com/NYTimes/gziphandler"
"github.com/foolin/mixer"
"github.com/maxence-charriere/go-app/v10/pkg/app"
"github.com/mitchellh/mapstructure"
shell "github.com/stateless-minds/go-ipfs-api"
)
const dbNameIssue = "issue"
const dbNameCitizenReputation = "citizen_reputation"
const typeShortage = "shortage"
const (
topicCritical = "critical"
topicIssue = "issue"
)
const (
NotificationSuccess NotificationStatus = "positive"
NotificationInfo NotificationStatus = "info"
NotificationWarning NotificationStatus = "warning"
NotificationDanger NotificationStatus = "negative"
SuccessHeader = "Success"
ErrorHeader = "Error"
)
const (
asideTitleCreate = "Suggest Solution"
asideTitleList = "List Solutions"
)
// pubsub is a component that does a simple pubsub on ipfs. A component is a
// customizable, independent, and reusable UI element. It is created by
// embedding app.Compo into a struct.
type acid struct {
app.Compo
sh *shell.Shell
sub *shell.PubSubSubscription
citizenID string
issues []Issue
categoryIssues map[string][]Issue
ranks []CitizenReputation
delegates []Delegate
currentIssueInSlice int
Solutions []Solution
currentSolutionDescription string
notifications map[string]notification
notificationID int
AsideTitle string
}
type NotificationStatus string
type notification struct {
id int
status string
header string
message string
}
type Issue struct {
ID string `mapstructure:"_id" json:"_id" validate:"uuid_rfc4122"`
Type string `mapstructure:"type" json:"type" validate:"uuid_rfc4122"`
Category string `mapstructure:"category" json:"category" validate:"uuid_rfc4122"`
Description string `mapstructure:"description" json:"description" validate:"uuid_rfc4122"`
Delegates []Delegate `mapstructure:"delegates" json:"delegates" validate:"uuid_rfc4122"`
Solutions []Solution `mapstructure:"solutions" json:"solutions" validate:"uuid_rfc4122"`
Voters []string `mapstructure:"voters" json:"voters" validate:"uuid_rfc4122"`
}
type Solution struct {
ID string `mapstructure:"_id" json:"_id" validate:"uuid_rfc4122"`
Description string `mapstructure:"description" json:"description" validate:"uuid_rfc4122"`
Votes int `mapstructure:"votes" json:"votes" validate:"uuid_rfc4122"`
}
type Delegate struct {
CitizenID string `mapstructure:"citizenId" json:"citizenId" validate:"uuid_rfc4122"`
Votes int `mapstructure:"votes" json:"votes" validate:"uuid_rfc4122"`
Selected int `mapstructure:"selected" json:"selected" validate:"uuid_rfc4122"`
OwnVote bool `mapstructure:"voted" json:"voted" validate:"uuid_rfc4122"`
}
type CitizenReputation struct {
ID string `mapstructure:"_id" json:"_id" validate:"uuid_rfc4122"`
Type string `mapstructure:"type" json:"type" validate:"uuid_rfc4122"`
CitizenID string `mapstructure:"citizenId" json:"citizenId" validate:"uuid_rfc4122"`
ReputationIndex float64 `mapstructure:"reputationIndex" json:"reputationIndex" validate:"uuid_rfc4122"`
}
func (a *acid) OnMount(ctx app.Context) {
sh := shell.NewShell("localhost:5001")
a.sh = sh
myPeer, err := a.sh.ID()
if err != nil {
log.Fatal(err)
}
citizenID := myPeer.ID[len(myPeer.ID)-8:]
// replace password with your own
password := "mysecretpassword"
a.citizenID = mixer.EncodeString(password, citizenID)
a.subscribeToCriticalTopic(ctx)
a.subscribeToIssueTopic(ctx)
a.notifications = make(map[string]notification)
a.categoryIssues = make(map[string][]Issue)
ctx.Async(func() {
// err := a.sh.OrbitDocsDelete(dbNameIssue, "all")
// if err != nil {
// log.Fatal(err)
// }
// err := a.sh.OrbitDocsDelete(dbNameCitizenReputation, "4")
// if err != nil {
// log.Fatal(err)
// }
cr, err := a.sh.OrbitDocsQuery(dbNameCitizenReputation, "type", "reputation")
if err != nil {
log.Fatal(err)
}
var cc []interface{}
err = json.Unmarshal(cr, &cc)
if err != nil {
log.Fatal(err)
}
for _, zz := range cc {
r := CitizenReputation{}
err = mapstructure.Decode(zz, &r)
if err != nil {
log.Fatal(err)
}
ctx.Dispatch(func(ctx app.Context) {
a.ranks = append(a.ranks, r)
sort.SliceStable(a.ranks, func(i, j int) bool {
return a.ranks[i].ID < a.ranks[j].ID
})
})
}
v, err := a.sh.OrbitDocsQuery(dbNameIssue, "type", "shortage")
if err != nil {
log.Fatal(err)
}
var vv []interface{}
err = json.Unmarshal(v, &vv)
if err != nil {
log.Fatal(err)
}
for _, ii := range vv {
i := Issue{}
err = mapstructure.Decode(ii, &i)
if err != nil {
log.Fatal(err)
}
ctx.Dispatch(func(ctx app.Context) {
a.categoryIssues[i.Category] = append(a.categoryIssues[i.Category], i)
a.issues = append(a.issues, i)
sort.SliceStable(a.issues, func(i, j int) bool {
return a.issues[i].ID < a.issues[j].ID
})
})
}
})
}
func (a *acid) subscribeToCriticalTopic(ctx app.Context) {
ctx.Async(func() {
topic := topicCritical
subscription, err := a.sh.PubSubSubscribe(topic)
if err != nil {
log.Fatal(err)
}
a.sub = subscription
a.subscriptionCritical(ctx)
})
}
func (a *acid) subscribeToIssueTopic(ctx app.Context) {
ctx.Async(func() {
topic := topicIssue
subscription, err := a.sh.PubSubSubscribe(topic)
if err != nil {
log.Fatal(err)
}
a.sub = subscription
a.subscriptionIssue(ctx)
})
}
func (a *acid) subscriptionIssue(ctx app.Context) {
ctx.Async(func() {
defer a.sub.Cancel()
// wait on pubsub
res, err := a.sub.Next()
if err != nil {
log.Fatal(err)
}
// Decode the string data.
str := string(res.Data)
log.Println("Subscriber of topic issue received message: " + str)
ctx.Async(func() {
a.subscribeToIssueTopic(ctx)
})
s := Issue{}
err = json.Unmarshal([]byte(str), &s)
if err != nil {
log.Fatal(err)
}
id, err := strconv.Atoi(s.ID)
if err != nil {
log.Fatal(err)
}
ctx.Dispatch(func(ctx app.Context) {
a.issues[id-1] = s
})
})
}
func (a *acid) subscriptionCritical(ctx app.Context) {
ctx.Async(func() {
defer a.sub.Cancel()
// wait on pubsub
res, err := a.sub.Next()
if err != nil {
log.Fatal(err)
}
// Decode the string data.
str := string(res.Data)
log.Println("Subscriber of topic critical received message: " + str)
ctx.Async(func() {
a.subscribeToCriticalTopic(ctx)
})
s := Issue{}
err = json.Unmarshal([]byte(str), &s)
if err != nil {
log.Fatal(err)
}
var lastID int
unique := true
for n, i := range a.issues {
a.categoryIssues[i.Category] = append(a.categoryIssues[i.Category], i)
if s.Description == i.Description {
unique = false
}
if n == 0 {
lastID, err = strconv.Atoi(i.ID)
if err != nil {
log.Fatal(err)
}
} else {
currentID, err := strconv.Atoi(i.ID)
if err != nil {
log.Fatal(err)
}
previousID, err := strconv.Atoi(a.issues[n-1].ID)
if err != nil {
log.Fatal(err)
}
if currentID > previousID {
lastID = currentID
}
}
}
if unique {
newID := lastID + 1
issue := Issue{
ID: strconv.Itoa(newID),
Type: typeShortage,
Category: s.Category,
Description: s.Description,
Solutions: []Solution{},
}
i, err := json.Marshal(issue)
if err != nil {
log.Fatal(err)
}
err = a.sh.OrbitDocsPut(dbNameIssue, i)
if err != nil {
log.Fatal(err)
}
err = a.sh.PubSubPublish(topicIssue, string(i))
if err != nil {
log.Fatal(err)
}
ctx.Dispatch(func(ctx app.Context) {
a.issues = append(a.issues, issue)
})
}
})
}
// The Render method is where the component appearance is defined. Here, a
// "pubsub World!" is displayed as a heading.
func (a *acid) Render() app.UI {
return app.Div().Class("l-application").Role("presentation").Body(
app.Link().Rel("stylesheet").Href("https://assets.ubuntu.com/v1/vanilla-framework-version-3.8.0.min.css"),
app.Link().Rel("stylesheet").Href("https://use.fontawesome.com/releases/v6.2.0/css/all.css"),
app.Link().Rel("stylesheet").Href("/app.css"),
app.Header().Class("l-navigation is-collapsed").Body(
app.Div().Class("l-navigation__drawer").Body(
app.Div().Class("p-panel is-dark").Body(
app.Div().Class("p-panel__header is-sticky").Body(
app.A().Class("p-panel__logo").Href("#").Body(
app.H5().Class("p-heading--2").Text("Cyber Acid"),
),
),
app.Hr(),
app.P().Class("p-heading--6").Body(
app.Text("Liquid democracy politics simulator based on the automated data feed from the moneyless economy simulator "),
app.A().Href("https://github.com/stateless-minds/cyber-stasis").Text("Cyber Stasis"),
).Style("padding", "0 10%;"),
app.Hr(),
app.Div().Class("p-panel__content").Body(
app.Div().Class("p-side-navigation--icons is-dark").ID("drawer-icons").Body(
app.Nav().Aria("label", "Main"),
app.Ul().Class("p-side-navigation__list").Body(
app.Li().Class("p-side-navigation__item--title").Body(
app.A().Class("p-side-navigation__link").Href("#").Body(
app.I().Class("p-icon--help is-light p-side-navigation__icon"),
app.Span().Class("p-side-navigation__label").Text("How to play"),
).OnClick(a.openHowToDialog),
app.A().Class("p-side-navigation__link").Href("#").Body(
app.I().Class("p-icon--warning is-light p-side-navigation__icon"),
app.Span().Class("p-side-navigation__label").Text("Shortages"),
).Aria("current", "page"),
app.A().Class("p-side-navigation__link").Href("#").Body(
app.I().Class("p-icon--share is-light p-side-navigation__icon"),
app.Span().Class("p-side-navigation__label").Text("Delegate rankings"),
).OnClick(a.openRankingsDialog),
),
),
),
),
),
),
),
app.Main().Class("l-main").Body(
app.Div().Class("p-panel").Body(
app.If(len(a.notifications) > 0, func() app.UI {
return app.Range(a.notifications).Map(func(s string) app.UI {
return app.Div().Class("p-notification--" + a.notifications[s].status).Body(
app.Div().Class("p-notification__content").Body(
app.H5().Class("p-notification__title").Text(a.notifications[s].header),
app.P().Class("p-notification__message").Text(a.notifications[s].message),
),
)
})
}),
app.Div().Class("p-panel__header").Body(
app.H4().Class("p-panel__title").Text("Open Issues"),
),
app.Div().Class("p-panel__content").Body(
app.Div().Class("u-fixed-width").Body(
app.If(len(a.categoryIssues) > 0, func() app.UI {
return app.Range(a.categoryIssues).Map(func(s string) app.UI {
return app.Table().Aria("label", "Issues table").Class("p-main-table").Body(
app.THead().Body(
app.Tr().Body(
app.Th().Body(
app.Span().Class("status-icon is-running").Text("Category "+s),
),
app.Th().Text("Actions"),
),
),
app.If(len(a.categoryIssues[s]) > 0, func() app.UI {
return app.TBody().Body(
app.Range(a.categoryIssues[s]).Slice(func(i int) app.UI {
return app.Tr().DataSet("id", i).Body(
app.Td().DataSet("column", "issue").Body(
app.Div().Text(a.categoryIssues[s][i].Description),
),
app.Td().DataSet("column", "action").Body(
app.Div().Body(
app.Button().Class("u-no-margin--bottom").Text("List Solutions").Value(a.categoryIssues[s][i].ID).OnMouseOver(a.asidePreloadList).OnClick(a.asideOpenList),
app.Button().Class("u-no-margin--bottom").Text("Suggest Solution").Value(a.categoryIssues[s][i].ID).OnMouseOver(a.asidePreloadCreate).OnClick(a.asideOpenCreate),
),
),
)
}),
)
}),
)
})
}),
),
app.Div().Class("p-modal").ID("howto-modal").Style("display", "none").Body(
app.Section().Class("p-modal__dialog").Role("dialog").Aria("modal", true).Aria("labelledby", "modal-title").Aria("describedby", "modal-description").Body(
app.Header().Class("p-modal__header").Body(
app.H2().Class("p-modal__title").ID("modal-title").Text("How to play"),
app.Button().Class("p-modal__close").Aria("label", "Close active modal").Aria("controls", "modal").OnClick(a.closeHowToModal),
),
app.Div().Class("p-heading-icon--small").Body(
app.Aside().Class("p-accordion").Body(
app.Ul().Class("p-accordion__list").Body(
app.Li().Class("p-accordion__group").Body(
app.Div().Role("heading").Aria("level", "3").Class("p-accordion__heading").Body(
app.Button().Type("button").Class("p-accordion__tab").ID("tab1").Aria("controls", "tab1-section").Aria("expanded", true).Text("What is Cyber Acid").Value("tab1-section").OnClick(a.toggleAccordion),
),
app.Section().Class("p-accordion__panel").ID("tab1-section").Aria("hidden", false).Aria("labelledby", "tab1").Body(
app.P().Text("Cyber Acid is a political simulator based on the liquid democracy concept. It is designed as an integration module that works with Cyber Stasis - the moneyless economy simulator."),
),
),
app.Li().Class("p-accordion__group").Body(
app.Div().Role("heading").Aria("level", "3").Class("p-accordion__heading").Body(
app.Button().Type("button").Class("p-accordion__tab").ID("tab2").Aria("controls", "tab2-section").Aria("expanded", true).Text("What is liquid democracy").Value("tab2-section").OnClick(a.toggleAccordion),
),
app.Section().Class("p-accordion__panel").ID("tab2-section").Aria("hidden", true).Aria("labelledby", "tab1").Body(
app.P().Text("Liquid democracy meets the transparency and accountability of direct democracy with the easy of use of representative democracy. Vote directly for what you want and delegate one-time voting rights per topic."),
),
),
app.Li().Class("p-accordion__group").Body(
app.Div().Role("heading").Aria("level", "3").Class("p-accordion__heading").Body(
app.Button().Type("button").Class("p-accordion__tab").ID("tab3").Aria("controls", "tab3-section").Aria("expanded", true).Text("How it works").Value("tab3-section").OnClick(a.toggleAccordion),
),
app.Section().Class("p-accordion__panel").ID("tab3-section").Aria("hidden", true).Aria("labelledby", "tab3").Body(
app.P().Text("The simulator receives live data from Cyber Stasis about critical shortages of production and resources. The goal of all participants is to suggest solutions to those issues. For example - replacing a resource with another one, researching new technologies etc."),
),
),
app.Li().Class("p-accordion__group").Body(
app.Div().Role("heading").Aria("level", "3").Class("p-accordion__heading").Body(
app.Button().Type("button").Class("p-accordion__tab").ID("tab4").Aria("controls", "tab4-section").Aria("expanded", true).Text("Features").Value("tab4-section").OnClick(a.toggleAccordion),
),
app.Section().Class("p-accordion__panel").ID("tab4-section").Aria("hidden", true).Aria("labelledby", "tab3").Body(
app.Ul().Class("p-matrix").Body(
app.Li().Class("p-matrix__item").Body(
app.Div().Class("p-matrix__content").Body(
app.H3().Class("p-matrix__title").Text("Check shortages"),
app.Div().Class("p-matrix__desc").Body(
app.P().Text("Review pressing issues."),
),
),
),
app.Li().Class("p-matrix__item").Body(
app.Div().Class("p-matrix__content").Body(
app.H3().Class("p-matrix__title").Text("Suggest a solution"),
app.Div().Class("p-matrix__desc").Body(
app.P().Text("Contribute with your expertise."),
),
),
),
app.Li().Class("p-matrix__item").Body(
app.Div().Class("p-matrix__content").Body(
app.H3().Class("p-matrix__title").Text("Vote for solutions"),
app.Div().Class("p-matrix__desc").Body(
app.P().Text("Vote for the best solution."),
),
),
),
app.Li().Class("p-matrix__item").Body(
app.Div().Class("p-matrix__content").Body(
app.H3().Class("p-matrix__title").Text("Delegate your vote"),
app.Div().Class("p-matrix__desc").Body(
app.P().Text("Not competent? Delegate your vote."),
),
),
),
app.Li().Class("p-matrix__item").Body(
app.Div().Class("p-matrix__content").Body(
app.H3().Class("p-matrix__title").Text("Infinite delegation"),
app.Div().Class("p-matrix__desc").Body(
app.P().Text("Delegation can be chained for maximum participation."),
),
),
),
app.Li().Class("p-matrix__item").Body(
app.Div().Class("p-matrix__content").Body(
app.H3().Class("p-matrix__title").Text("Cross delegation"),
app.Div().Class("p-matrix__desc").Body(
app.P().Text("Cross delegation is also supported."),
),
),
),
),
),
),
app.Li().Class("p-accordion__group").Body(
app.Div().Role("heading").Aria("level", "3").Class("p-accordion__heading").Body(
app.Button().Type("button").Class("p-accordion__tab").ID("tab5").Aria("controls", "tab5-section").Aria("expanded", true).Text("Support us").Value("tab5-section").OnClick(a.toggleAccordion),
),
app.Section().Class("p-accordion__panel").ID("tab5-section").Aria("hidden", true).Aria("labelledby", "tab5").Body(
app.A().Href("https://opencollective.com/stateless-minds-collective").Text("https://opencollective.com/stateless-minds-collective"),
),
),
app.Li().Class("p-accordion__group").Body(
app.Div().Role("heading").Aria("level", "3").Class("p-accordion__heading").Body(
app.Button().Type("button").Class("p-accordion__tab").ID("tab6").Aria("controls", "tab6-section").Aria("expanded", true).Text("Terms of service").Value("tab6-section").OnClick(a.toggleAccordion),
),
app.Section().Class("p-accordion__panel").ID("tab6-section").Aria("hidden", true).Aria("labelledby", "tab6").Body(
app.Div().Class("p-card").Body(
app.H3().Text("Introduction"),
app.P().Class("p-card__content").Text("Cyber Acid is a liquid democracy political simulator in the form of a fictional game based on real-time data from Cyber Stasis. By using the application you are implicitly agreeing to share your peer id with the IPFS public network."),
),
app.Div().Class("p-card").Body(
app.H3().Text("Application Hosting"),
app.P().Class("p-card__content").Text("Cyber Acid is a decentralized application and is hosted on a public peer to peer network. By using the application you agree to host it on the public IPFS network free of charge for as long as your usage is."),
),
app.Div().Class("p-card").Body(
app.H3().Text("User-Generated Content"),
app.P().Class("p-card__content").Text("All published content is user-generated, fictional and creators are not responsible for it."),
),
),
),
app.Li().Class("p-accordion__group").Body(
app.Div().Role("heading").Aria("level", "3").Class("p-accordion__heading").Body(
app.Button().Type("button").Class("p-accordion__tab").ID("tab7").Aria("controls", "tab7-section").Aria("expanded", true).Text("Privacy policy").Value("tab7-section").OnClick(a.toggleAccordion),
),
app.Section().Class("p-accordion__panel").ID("tab7-section").Aria("hidden", true).Aria("labelledby", "tab7").Body(
app.Div().Class("p-card").Body(
app.H3().Text("Personal data"),
app.P().Class("p-card__content").Text("There is no personal information collected within Cyber Acid. We store a small portion of your peer ID encrypted as a non-unique identifier which is used for displaying the ranks interface."),
),
app.Div().Class("p-card").Body(
app.H3().Text("Coookies"),
app.P().Class("p-card__content").Text("Cyber Acid does not use cookies."),
),
app.Div().Class("p-card").Body(
app.H3().Text("Links to Cyber Stasis"),
app.P().Class("p-card__content").Text("Cyber Acid contains links to its sister project Cyber Stasis and depends on its data to function properly."),
),
app.Div().Class("p-card").Body(
app.H3().Text("Changes to this privacy policy"),
app.P().Class("p-card__content").Text("This Privacy Policy might be updated from time to time. Thus, it is advised to review this page periodically for any changes. You will be notified of any changes from this page. Changes are effective immediately after they are posted on this page."),
),
),
),
),
),
),
).Style("left", "10%").Style("width", "80%"),
),
app.Div().Class("p-modal").ID("rankings-modal").Style("display", "none").Body(
app.Section().Class("p-modal__dialog").Role("dialog").Aria("modal", true).Aria("labelledby", "modal-title").Aria("describedby", "modal-description").Body(
app.Header().Class("p-modal__header").Body(
app.H2().Class("p-modal__title").ID("modal-title").Text("Delegate rankings"),
app.Button().Class("p-modal__close").Aria("label", "Close active modal").Aria("controls", "modal").OnClick(a.closeRankingsModal),
),
app.Table().Aria("label", "Rankings table").Class("p-main-table").Body(
app.THead().Body(
app.Tr().Body(
app.Th().Body(
app.Span().Class("status-icon is-blocked").Text("Delegate ID"),
),
app.Th().Text("Trust"),
),
),
app.If(len(a.delegates) > 0, func() app.UI {
return app.TBody().Body(
app.Range(a.delegates).Slice(func(i int) app.UI {
return app.Tr().DataSet("id", i).Body(
app.Td().DataSet("column", "delegate").Body(
app.Div().Text(a.delegates[i].CitizenID),
),
app.Td().DataSet("column", "trust").Body(
app.Div().Text(a.delegates[i].Selected),
),
)
}),
)
}),
),
).Style("left", "10%").Style("width", "80%"),
),
),
),
),
app.Aside().Class("l-aside is-collapsed").ID("aside-panel").Body(
app.Div().Class("p-panel").Body(
app.Div().Class("p-panel__header").Body(
app.H4().Class("p-panel__title").Text(a.AsideTitle),
app.Div().Class("p-panel__controls").Body(
app.Button().Class("p-button--base u-no-margin--bottom has-icon").Body(app.I().Class("p-icon--close")).OnClick(a.asideClose),
),
),
app.If(a.AsideTitle == asideTitleCreate, func() app.UI {
return app.Div().Class("p-panel__content").Body(
app.Div().Class("p-form p-form--stacked").Body(
app.Div().Class("p-form__group row").Body(
app.Textarea().ID("solution").Name("solution").Rows(3).OnKeyUp(a.onSolution),
),
),
app.Div().Class("row").Body(
app.Div().Class("col-12").Body(
app.Button().Class("p-button--positive u-float-right").Name("submit-solution").Text("Submit Solution").OnClick(a.submitSolution),
),
),
)
}).ElseIf(a.AsideTitle == asideTitleList, func() app.UI {
return app.Div().Class("p-panel__content").Body(
app.Ul().Class("p-list-tree").Aria("multiselectable", true).Role("tree").Body(
app.Li().Class("p-list-tree__item p-list-tree__item--group").Role("treeitem").Body(
app.Button().Class("p-list-tree__toggle").ID("sub-1-btn").Aria("controls", "sub-1").Aria("expanded", true).Text("Suggested Solutions"),
app.Ul().Class("p-list-tree").Role("group").ID("sub-1").Aria("hidden", false).Aria("labelledby", "sub-1-btn").Body(
app.Range(a.Solutions).Slice(func(i int) app.UI {
return app.Li().Class("p-list-tree__item").Role("treeitem").Body(
app.P().Text(a.Solutions[i].Description),
app.If(len(a.issues[a.currentIssueInSlice].Voters) > 0, func() app.UI {
return app.If(sliceContains(a.issues[a.currentIssueInSlice].Voters, a.citizenID), func() app.UI {
return app.Button().
Class("p-button is-small is-inline").
Text("Vote").
Value(a.Solutions[i].ID).
OnClick(a.vote).
Disabled(true).
Body(
app.I().Class("fa-solid fa-thumbs-up"), // Icon
app.Span().
Class("p-badge").
Aria("label", strconv.Itoa(a.Solutions[i].Votes)+" votes").
Text(strconv.Itoa(a.Solutions[i].Votes)), // Votes count
)
}).Else( func() app.UI {
return app.Div().Class("vote-delegate-container").Body(
// Vote Button with Icon
app.Button().
Class("p-button is-small is-inline").
Text("Vote").
Value(a.Solutions[i].ID).
OnClick(a.vote).
Body(
app.I().Class("fa-regular fa-thumbs-up"), // Icon for Vote
),
// Delegate Button
app.Button().
Class("p-button is-small is-inline").
ID("show-modal").
Text("Delegate...").
Aria("controls", "modal").
Value(a.Solutions[i].ID).
OnClick(a.openDelegateDialog),
// Modal for Delegation
app.Div().
Class("p-modal").
ID("delegate-modal").
Style("display", "none"). // Initially hidden
Body(
app.Section().
Class("p-modal__dialog").
Role("dialog").
Aria("modal", true).
Aria("labelledby", "modal-title").
Aria("describedby", "modal-description").
Body(
app.Header().
Class("p-modal__header").
Body(
app.H2().
Class("p-modal__title").
ID("modal-title").
Text("Delegate"),
app.Button().
Class("p-modal__close").
Aria("label", "Close active modal").
Aria("controls", "modal").
Value(a.Solutions[i].ID).
OnClick(a.closeDelegateModal),
),
app.P().
ID("modal-description").
Text("Select a citizen to represent your vote for this issue:"),
app.Div().
Class("p-heading-icon--small").
Body(
app.Range(a.ranks).Slice(func(i int) app.UI {
return app.If(a.ranks[i].CitizenID != a.citizenID, func() app.UI {
return app.Div().Class("p-heading-icon__header").Body(
app.Button().
Class("p-chip").
Aria("pressed", true).
Disabled(true).
Body(
app.Span().Class("p-chip__value").Text("Citizen"),
app.Span().Class("p-badge").Aria("label", "Citizen").Text(a.ranks[i].CitizenID),
),
app.Button().
Class("p-chip").
Aria("pressed", true).
Disabled(true).
Body(
app.Span().Class("p-chip__value").Text("Reputation"),
app.Span().Class("p-badge").Aria("label", "Reputation").Text(a.ranks[i].ReputationIndex),
),
app.Button().
Class("p-chip").
Body(
app.Span().Class("p-chip__value").Text("Select"),
).Value(a.ranks[i].CitizenID).OnClick(a.delegate),
)
})
}),
),
),
),
)
})
}).Else( func() app.UI {
return app.Div().Class("vote-delegate-container").Body(
// Vote Button with Icon
app.Button().
Class("p-button is-small is-inline").
Text("Vote").
Value(a.Solutions[i].ID).
OnClick(a.vote).
Body(
app.I().Class("fa-regular fa-thumbs-up"), // Icon for Vote
),
// Delegate Button
app.Button().
Class("p-button is-small is-inline").
ID("show-modal").
Text("Delegate...").
Aria("controls", "modal").
Value(a.Solutions[i].ID).
OnClick(a.openDelegateDialog),
// Modal for Delegation
app.Div().
Class("p-modal").
ID("delegate-modal").
Style("display", "none"). // Initially hidden
Body(
app.Section().
Class("p-modal__dialog").
Role("dialog").
Aria("modal", true).
Aria("labelledby", "modal-title").
Aria("describedby", "modal-description").
Body(
app.Header().
Class("p-modal__header").
Body(
app.H2().
Class("p-modal__title").
ID("modal-title").
Text("Delegate"),
app.Button().
Class("p-modal__close").
Aria("label", "Close active modal").
Aria("controls", "modal").
Value(a.Solutions[i].ID).
OnClick(a.closeDelegateModal),
),
app.P().
ID("modal-description").
Text("Select a citizen to represent your vote for this issue:"),
app.Div().
Class("p-heading-icon--small").
Body(
app.Range(a.ranks).Slice(func(i int) app.UI {
return app.If(a.ranks[i].CitizenID != a.citizenID, func() app.UI {
return app.Div().Class("p-heading-icon__header").Body(
app.Button().
Class("p-chip").
Aria("pressed", true).
Disabled(true).
Body(
app.Span().Class("p-chip__value").Text("Citizen"),
app.Span().Class("p-badge").Aria("label", "Citizen").Text(a.ranks[i].CitizenID),
),
app.Button().
Class("p-chip").
Aria("pressed", true).
Disabled(true).
Body(
app.Span().Class("p-chip__value").Text("Reputation"),
app.Span().Class("p-badge").Aria("label", "Reputation").Text(a.ranks[i].ReputationIndex),
),
app.Button().
Class("p-chip").
Body(
app.Span().Class("p-chip__value").Text("Select"),
).Value(a.ranks[i].CitizenID).OnClick(a.delegate),
)
})
}),
),
),
),
)
}),
)
}),
),
),
),
)
}),
),
),
)
}
func (a *acid) asidePreloadList(ctx app.Context, e app.Event) {
issueID := ctx.JSSrc().Get("value").String()
issueIDInt, err := strconv.Atoi(issueID)
if err != nil {
log.Fatal(err)
}
a.currentIssueInSlice = issueIDInt - 1
a.Solutions = a.issues[issueIDInt-1].Solutions
a.AsideTitle = asideTitleList
}
func (a *acid) asideOpenList(ctx app.Context, e app.Event) {
app.Window().Get("document").Call("querySelector", ".l-aside").Get("classList").Call("remove", "is-collapsed")
}
func (a *acid) asidePreloadCreate(ctx app.Context, e app.Event) {
a.AsideTitle = asideTitleCreate
}
func (a *acid) asideOpenCreate(ctx app.Context, e app.Event) {
app.Window().Get("document").Call("querySelector", ".l-aside").Get("classList").Call("remove", "is-collapsed")
app.Window().Get("document").Call("querySelector", ".p-button--positive").Call("setAttribute", "id", ctx.JSSrc().Get("value").String())
}
func (a *acid) asideClose(ctx app.Context, e app.Event) {
app.Window().Get("document").Call("querySelector", ".l-aside").Get("classList").Call("add", "is-collapsed")
}
func (a *acid) onSolution(ctx app.Context, e app.Event) {
a.currentSolutionDescription = ctx.JSSrc().Get("value").String()
}
func (a *acid) vote(ctx app.Context, e app.Event) {
ctx.JSSrc().Get("firstChild").Get("classList").Call("remove", "fa-regular")
val := ctx.JSSrc().Get("value").String()
solutionID, err := strconv.Atoi(val)
if err != nil {
log.Fatal(err)
}
currentIssue := a.issues[a.currentIssueInSlice]
var delegate bool
var delegatedVotes int
var ownVote bool
// delegated voting logic
for i, d := range currentIssue.Delegates {
if a.citizenID == d.CitizenID {
delegate = true
ownVote = currentIssue.Delegates[i].OwnVote
if !d.OwnVote {
currentIssue.Delegates[i].OwnVote = true
}
delegatedVotes = d.Votes
currentIssue.Delegates[i].Votes = 0
}
}
currentIssue.Voters = append(currentIssue.Voters, a.citizenID)
if delegate {
if !ownVote {
currentIssue.Solutions[solutionID-1].Votes += delegatedVotes + 1
} else {
currentIssue.Solutions[solutionID-1].Votes += delegatedVotes
}
} else {
currentIssue.Solutions[solutionID-1].Votes++
}
i, err := json.Marshal(currentIssue)
if err != nil {
log.Fatal(err)
}
ctx.Async(func() {
err = a.sh.OrbitDocsPut(dbNameIssue, i)
if err != nil {
ctx.Dispatch(func(ctx app.Context) {
a.createNotification(ctx, NotificationDanger, ErrorHeader, "Could not vote for solution. Try again later.")
log.Fatal(err)
})
}
err = a.sh.PubSubPublish(topicIssue, string(i))
if err != nil {
log.Fatal(err)
}
ctx.Dispatch(func(ctx app.Context) {
a.issues[a.currentIssueInSlice] = currentIssue
a.createNotification(ctx, NotificationSuccess, SuccessHeader, "Vote accepted.")
})
})
}
func (a *acid) openRankingsDialog(ctx app.Context, e app.Event) {
for _, i := range a.issues {
a.delegates = append(a.delegates, i.Delegates...)
}
sort.SliceStable(a.delegates, func(i, j int) bool {
return a.delegates[i].Selected > a.delegates[j].Selected
})
app.Window().GetElementByID("rankings-modal").Set("style", "display:flex")
}
func (a *acid) openHowToDialog(ctx app.Context, e app.Event) {
app.Window().GetElementByID("howto-modal").Set("style", "display:flex")
}
func (a *acid) openDelegateDialog(ctx app.Context, e app.Event) {
app.Window().GetElementByID("delegate-modal").Set("style", "display:flex")
}
func (a *acid) delegate(ctx app.Context, e app.Event) {
citizenID := ctx.JSSrc().Get("value").String()
issue := a.issues[a.currentIssueInSlice]
var delegateExists bool
var delegate Delegate
votesTransfer := 1
if len(issue.Delegates) > 0 {
for ii, dd := range issue.Delegates {
// recursive delegation logic
if dd.CitizenID == a.citizenID {
// transfer origin votes to recipient plus own vote
votesTransfer = dd.Votes + 1
// set origin delegator's votes to zero
issue.Delegates[ii].Votes = 0
// set origin delegator as voted
issue.Delegates[ii].OwnVote = true
}
}
for i, d := range issue.Delegates {
if d.CitizenID == citizenID {
issue.Delegates[i].Votes += votesTransfer
issue.Delegates[i].Selected++
delegateExists = true
}
}
}
if len(issue.Delegates) == 0 || !delegateExists {
delegate = Delegate{
CitizenID: citizenID,
Votes: votesTransfer,
Selected: 1,
}
issue.Delegates = append(issue.Delegates, delegate)
}
issue.Voters = append(issue.Voters, a.citizenID)
var voters []string
for _, v := range issue.Voters {
// if the delegate already voted previously remove from voters so he can vote again on new delegation
if citizenID != v {
voters = append(voters, v)
}
}
issue.Voters = voters
i, err := json.Marshal(issue)
if err != nil {
log.Fatal(err)
}
ctx.Async(func() {
err = a.sh.OrbitDocsPut(dbNameIssue, i)
if err != nil {
log.Fatal(err)
}
err = a.sh.PubSubPublish(topicIssue, string(i))
if err != nil {
log.Fatal(err)
}
ctx.Dispatch(func(ctx app.Context) {
a.issues[a.currentIssueInSlice] = issue