-
Notifications
You must be signed in to change notification settings - Fork 1
/
flow.go
1309 lines (1077 loc) · 35.8 KB
/
flow.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 frontend
import (
"encoding/json"
"log"
"net/url"
"sort"
"strconv"
"strings"
"time"
"go.savla.dev/littr/configs"
"go.savla.dev/littr/pkg/models"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type FlowPage struct {
app.Compo
}
type flowContent struct {
app.Compo
eventListener func()
loaderShow bool
loaderShowImage bool
contentLoadFinished bool
loggedUser string
user models.User
key string
toastShow bool
toastText string
toastType string
buttonDisabled bool
postButtonsDisabled bool
modalReplyActive bool
replyPostContent string
newFigLink string
newFigFile string
newFigData []byte
deletePostModalShow bool
deleteModalButtonsDisabled bool
interactedPostKey string
singlePostID string
isPost bool
userFlowNick string
paginationEnd bool
pagination int
pageNo int
pageNoToFetch int
lastFire int64
processingFire bool
lastPageFetched bool
postKey string
posts map[string]models.Post
users map[string]models.User
sortedPosts []models.Post
refreshClicked bool
eventListenerMsg func()
keyDownEventListener func()
}
func (p *FlowPage) OnNav(ctx app.Context) {
ctx.Page().SetTitle("flow / littr")
}
func (p *FlowPage) Render() app.UI {
return app.Div().Body(
&header{},
&footer{},
&flowContent{},
)
}
func (c *flowContent) onClickLink(ctx app.Context, e app.Event) {
key := ctx.JSSrc().Get("id").String()
url := ctx.Page().URL()
scheme := url.Scheme
host := url.Host
// write the link to browsers's clipboard
navigator := app.Window().Get("navigator")
if !navigator.IsNull() {
clipboard := navigator.Get("clipboard")
if !clipboard.IsNull() && !clipboard.IsUndefined() {
clipboard.Call("writeText", scheme+"://"+host+"/flow/post/"+key)
}
}
ctx.Navigate("/flow/post/" + key)
}
func (c *flowContent) onClickDismiss(ctx app.Context, e app.Event) {
ctx.Dispatch(func(ctx app.Context) {
// hotfix which ensures reply modal is not closed if there is also a snackbar/toast active
if c.toastText == "" {
c.modalReplyActive = false
}
c.toastShow = false
c.toastText = ""
c.toastType = ""
c.buttonDisabled = false
c.postButtonsDisabled = false
c.deletePostModalShow = false
})
}
// https://github.com/maxence-charriere/go-app/issues/882
func (c *flowContent) handleFigUpload(ctx app.Context, e app.Event) {
var toastText string
file := e.Get("target").Get("files").Index(0)
//log.Println("name", file.Get("name").String())
//log.Println("size", file.Get("size").Int())
//log.Println("type", file.Get("type").String())
c.postButtonsDisabled = true
ctx.Async(func() {
if data, err := readFile(file); err != nil {
toastText = err.Error()
ctx.Dispatch(func(ctx app.Context) {
c.postButtonsDisabled = true
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
} else {
toastText = "image uploaded"
ctx.Dispatch(func(ctx app.Context) {
c.toastType = "success"
c.toastText = toastText
c.toastShow = (toastText != "")
c.postButtonsDisabled = true
c.newFigFile = file.Get("name").String()
c.newFigData = data
})
return
}
})
}
func (c *flowContent) onClickImage(ctx app.Context, e app.Event) {
key := ctx.JSSrc().Get("id").String()
src := ctx.JSSrc().Get("src").String()
split := strings.Split(src, ".")
ext := split[len(split)-1]
// image preview (thumbnail) to the actual image logic
if (ext != "gif" && strings.Contains(src, "thumb")) || (ext == "gif" && strings.Contains(src, "click")) {
ctx.JSSrc().Set("src", "/web/pix/"+key+"."+ext)
//ctx.JSSrc().Set("style", "max-height: 90vh; max-height: 100%; transition: max-height 0.1s; z-index: 1; max-width: 100%; background-position: center")
ctx.JSSrc().Set("style", "max-height: 90vh; transition: max-height 0.1s; z-index: 5; max-width: 100%; background-position")
} else if ext == "gif" && !strings.Contains(src, "thumb") {
ctx.JSSrc().Set("src", "/web/click-to-see.gif")
ctx.JSSrc().Set("style", "z-index: 1; max-height: 100%; max-width: 100%")
} else {
ctx.JSSrc().Set("src", "/web/pix/thumb_"+key+"."+ext)
ctx.JSSrc().Set("style", "z-index: 1; max-height: 100%; max-width: 100%")
}
}
func (c *flowContent) handleImage(ctx app.Context, a app.Action) {
ctx.JSSrc().Set("src", "")
}
func (c *flowContent) onClickUserFlow(ctx app.Context, e app.Event) {
key := ctx.JSSrc().Get("id").String()
//c.buttonDisabled = true
ctx.Navigate("/flow/user/" + key)
}
// onClickReply acts like a caller function evoked when user click on the reply icon at one's post
func (c *flowContent) onClickReply(ctx app.Context, e app.Event) {
ctx.Dispatch(func(ctx app.Context) {
c.interactedPostKey = ctx.JSSrc().Get("id").String()
c.modalReplyActive = true
c.postButtonsDisabled = false
c.buttonDisabled = true
})
}
// onClickPostReply acts like a caller function evoked when user clicks on "reply" button in the reply modal
func (c *flowContent) onClickPostReply(ctx app.Context, e app.Event) {
// prevent double-posting
if c.postButtonsDisabled {
return
}
ctx.Dispatch(func(ctx app.Context) {
c.modalReplyActive = true
c.postButtonsDisabled = true
c.buttonDisabled = true
})
ctx.NewAction("reply")
}
func (c *flowContent) handleReply(ctx app.Context, a app.Action) {
ctx.Async(func() {
toastText := ""
// TODO: allow figs in replies
// check if the contents is a valid URL, then change the type to "fig"
postType := "post"
// trim the spaces on the extremites
textarea := app.Window().GetElementByID("reply-textarea").Get("value").String()
replyPost := strings.TrimSpace(textarea)
// allow picture-only posting
if replyPost == "" && c.newFigFile == "" {
toastText = "no valid reply entered"
ctx.Dispatch(func(ctx app.Context) {
c.postButtonsDisabled = true
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
}
//newPostID := time.Now()
//stringID := strconv.FormatInt(newPostID.UnixNano(), 10)
path := "/api/v1/posts"
// TODO: the Post data model has to be changed
// migrate Post.ReplyID (int) to Post.ReplyID (string)
// ReplyID is to be string key to easily refer to other post
payload := models.Post{
//ID: stringID,
Nickname: c.user.Nickname,
Type: postType,
Content: replyPost,
ReplyToID: c.interactedPostKey,
Data: c.newFigData,
Figure: c.newFigFile,
//Timestamp: newPostID,
//ReplyTo: replyID, <--- is type int
}
postsRaw := struct {
Posts map[string]models.Post `posts`
}{}
// add new post/poll to backend struct
if resp, _ := litterAPI("POST", path, payload, c.user.Nickname, c.pageNo); resp != nil {
err := json.Unmarshal(*resp, &postsRaw)
if err != nil {
log.Println(err.Error())
toastText = "JSON parsing error: " + err.Error()
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
c.interactedPostKey = ""
c.newFigData = []byte{}
c.newFigFile = ""
})
return
}
} else {
log.Println("cannot fetch post flow list")
toastText = "API error: cannot fetch the post list"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
c.interactedPostKey = ""
c.newFigData = []byte{}
c.newFigFile = ""
})
return
}
payloadNotif := struct {
OriginalPost string `json:"original_post"`
}{
OriginalPost: c.interactedPostKey,
}
// create a notification
if _, ok := litterAPI("POST", "/api/v1/push/notification/"+c.interactedPostKey, payloadNotif, c.user.Nickname, c.pageNo); !ok {
toastText = "cannot POST new notification"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
c.interactedPostKey = ""
c.newFigData = []byte{}
c.newFigFile = ""
})
return
}
posts := c.posts
// we do not know the ID, as it is assigned in the BE logic,
// so we need to loop over the list of posts (1)...
for k, p := range postsRaw.Posts {
posts[k] = p
}
ctx.Dispatch(func(ctx app.Context) {
// add new post to post list on frontend side to render
//c.posts[stringID] = payload
c.posts = posts
c.modalReplyActive = false
c.postButtonsDisabled = false
c.buttonDisabled = false
c.interactedPostKey = ""
c.newFigData = []byte{}
c.newFigFile = ""
})
})
}
func (c *flowContent) onKeyDown(ctx app.Context, e app.Event) {
textarea := app.Window().GetElementByID("reply-textarea")
if textarea.IsNull() {
return
}
if e.Get("ctrlKey").Bool() && e.Get("key").String() == "Enter" && len(textarea.Get("value").String()) != 0 {
app.Window().GetElementByID("reply").Call("click")
}
}
func (c *flowContent) onScroll(ctx app.Context, e app.Event) {
ctx.NewAction("scroll")
}
func (c *flowContent) handleScroll(ctx app.Context, a app.Action) {
ctx.Async(func() {
elem := app.Window().GetElementByID("page-end-anchor")
boundary := elem.JSValue().Call("getBoundingClientRect")
bottom := boundary.Get("bottom").Int()
_, height := app.Window().Size()
// limit the fire rate to 1/5 Hz
now := time.Now().Unix()
if now-c.lastFire < 2 {
return
}
if bottom-height < 0 && !c.paginationEnd && !c.processingFire && !c.loaderShow {
ctx.Dispatch(func(ctx app.Context) {
c.loaderShow = true
c.processingFire = true
c.contentLoadFinished = false
})
var newPosts map[string]models.Post
var newUsers map[string]models.User
posts := c.posts
users := c.users
updated := false
lastPageFetched := c.lastPageFetched
// fetch more posts
//if (c.pageNoToFetch+1)*(c.pagination*2) >= len(posts) && !lastPageFetched {
if !lastPageFetched {
opts := pageOptions{
PageNo: c.pageNoToFetch,
Context: ctx,
CallerID: c.user.Nickname,
//SinglePost: parts.SinglePost,
SinglePost: c.singlePostID != "",
//SinglePostID: parts.SinglePostID,
SinglePostID: c.singlePostID,
//UserFlow: parts.UserFlow,
UserFlow: c.userFlowNick != "",
//UserFlowNick: parts.UserFlowNick,
UserFlowNick: c.userFlowNick,
}
newPosts, newUsers = c.fetchFlowPage(opts)
postControlCount := len(posts)
// patch single-post and user flow atypical scenarios
if posts == nil {
posts = make(map[string]models.Post)
}
if users == nil {
users = make(map[string]models.User)
}
// append/insert more posts/users
for key, post := range newPosts {
posts[key] = post
}
for key, user := range newUsers {
users[key] = user
}
updated = true
// no more posts, fetching another page does not make sense
if len(posts) == postControlCount {
updated = false
lastPageFetched = true
}
}
ctx.Dispatch(func(ctx app.Context) {
c.lastFire = now
c.pageNoToFetch++
c.pageNo++
if updated {
c.posts = posts
c.users = users
log.Println("updated")
}
c.processingFire = false
c.loaderShow = false
c.contentLoadFinished = true
c.lastPageFetched = lastPageFetched
log.Println("new content page request fired")
})
return
}
})
}
func (c *flowContent) onClickDeleteButton(ctx app.Context, e app.Event) {
key := ctx.JSSrc().Get("id").String()
ctx.Dispatch(func(ctx app.Context) {
c.interactedPostKey = key
c.deleteModalButtonsDisabled = false
c.deletePostModalShow = true
})
}
func (c *flowContent) onClickDelete(ctx app.Context, e app.Event) {
key := c.interactedPostKey
ctx.NewActionWithValue("delete", key)
ctx.Dispatch(func(ctx app.Context) {
c.deleteModalButtonsDisabled = true
})
}
func (c *flowContent) handleDelete(ctx app.Context, a app.Action) {
key, ok := a.Value.(string)
if !ok {
return
}
c.postKey = key
ctx.Async(func() {
var toastText string = ""
key := c.postKey
interactedPost := c.posts[key]
if interactedPost.Nickname != c.user.Nickname {
toastText = "you only can delete your own posts!"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
c.deletePostModalShow = false
c.deleteModalButtonsDisabled = false
})
}
if _, ok := litterAPI("DELETE", "/api/v1/posts/"+interactedPost.ID, interactedPost, c.user.Nickname, c.pageNo); !ok {
toastText = "backend error: cannot delete a post"
}
ctx.Dispatch(func(ctx app.Context) {
delete(c.posts, key)
c.toastText = toastText
c.toastShow = (toastText != "")
c.deletePostModalShow = false
c.deleteModalButtonsDisabled = false
})
})
}
func (c *flowContent) onClickStar(ctx app.Context, e app.Event) {
key := ctx.JSSrc().Get("id").String()
ctx.NewActionWithValue("star", key)
}
func (c *flowContent) handleStar(ctx app.Context, a app.Action) {
key, ok := a.Value.(string)
if !ok {
return
}
// runs on the main UI goroutine via a component ActionHandler
post := c.posts[key]
post.ReactionCount++
c.posts[key] = post
c.postKey = key
ctx.Async(func() {
//var author string
var toastText string = ""
//key := ctx.JSSrc().Get("id").String()
key := c.postKey
//author = c.user.Nickname
interactedPost := c.posts[key]
//interactedPost.ReactionCount++
postsRaw := struct {
Posts map[string]models.Post `json:"posts"`
}{}
// add new post to backend struct
if resp, ok := litterAPI("PATCH", "/api/v1/posts/"+interactedPost.ID+"/star", interactedPost, c.user.Nickname, c.pageNo); ok {
err := json.Unmarshal(*resp, &postsRaw)
if err != nil {
log.Println(err.Error())
toastText = "JSON parsing error: " + err.Error()
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
}
} else {
toastText = "backend error: cannot rate a post"
}
ctx.Dispatch(func(ctx app.Context) {
c.posts[key] = postsRaw.Posts[key]
c.toastText = toastText
c.toastShow = (toastText != "")
})
})
}
func (c *flowContent) OnMount(ctx app.Context) {
ctx.Handle("delete", c.handleDelete)
ctx.Handle("image", c.handleImage)
ctx.Handle("reply", c.handleReply)
ctx.Handle("scroll", c.handleScroll)
ctx.Handle("star", c.handleStar)
ctx.Handle("message", c.handleNewPost)
c.paginationEnd = false
c.pagination = 0
c.pageNo = 1
c.pageNoToFetch = 0
c.lastPageFetched = false
c.deletePostModalShow = false
c.deleteModalButtonsDisabled = false
c.eventListener = app.Window().AddEventListener("scroll", c.onScroll)
c.eventListenerMsg = app.Window().AddEventListener("message", c.onMessage)
c.keyDownEventListener = app.Window().AddEventListener("keydown", c.onKeyDown)
}
func (c *flowContent) onMessage(ctx app.Context, e app.Event) {
data := e.JSValue().Get("data").String()
log.Println("msg event: data:" + data)
if data == "heartbeat" || data == c.user.Nickname {
return
}
if _, flowed := c.user.FlowList[data]; !flowed {
return
}
ctx.Dispatch(func(ctx app.Context) {
c.toastText = "new post added above"
c.toastType = "info"
})
}
func (c *flowContent) handleNewPost(ctx app.Context, a app.Action) {
log.Println("msg event lmaooooooooooooo")
}
func (c *flowContent) OnDismount() {
// https://go-app.dev/reference#BrowserWindow
//c.eventListener()
}
func (c *flowContent) onClickRefresh(ctx app.Context, e app.Event) {
ctx.Dispatch(func(ctx app.Context) {
c.loaderShow = true
c.loaderShowImage = true
c.contentLoadFinished = false
c.refreshClicked = true
c.postButtonsDisabled = true
//c.pageNoToFetch = 0
c.toastText = ""
c.posts = nil
c.users = nil
})
ctx.Async(func() {
// nasty hotfix, TODO
c.pageNoToFetch = 0
//parts := c.parseFlowURI(ctx)
opts := pageOptions{
PageNo: c.pageNoToFetch,
Context: ctx,
CallerID: c.user.Nickname,
//SinglePost: parts.SinglePost,
SinglePost: c.singlePostID != "",
//SinglePostID: parts.SinglePostID,
SinglePostID: c.singlePostID,
//UserFlow: parts.UserFlow,
UserFlow: c.userFlowNick != "",
//UserFlowNick: parts.UserFlowNick,
UserFlowNick: c.userFlowNick,
}
posts, users := c.fetchFlowPage(opts)
ctx.Dispatch(func(ctx app.Context) {
c.posts = posts
c.users = users
c.user = users[c.key]
c.loaderShow = false
c.loaderShowImage = false
c.refreshClicked = false
c.postButtonsDisabled = false
c.contentLoadFinished = true
})
})
}
type pageOptions struct {
PageNo int `default:0`
Context app.Context
CallerID string
SinglePost bool `default:false`
UserFlow bool `default:false`
SinglePostID string `default:""`
UserFlowNick string `default:""`
}
func (c *flowContent) fetchFlowPage(opts pageOptions) (map[string]models.Post, map[string]models.User) {
var toastText string
resp := struct {
Posts map[string]models.Post `json:"posts"`
Users map[string]models.User `json:"users"`
Code int `json:"code"`
Key string `json:"key"`
}{}
ctx := opts.Context
pageNo := opts.PageNo
if opts.Context == nil {
toastText = "app context pointer cannot be nil"
log.Println(toastText)
return nil, nil
}
//pageNo := c.pageNoToFetch
if c.refreshClicked {
pageNo = 0
}
//pageNoString := strconv.FormatInt(int64(pageNo), 10)
url := "/api/v1/posts"
if opts.UserFlow || opts.SinglePost {
if opts.SinglePostID != "" {
url += "/" + opts.SinglePostID
}
if opts.UserFlowNick != "" {
//url += "/user/" + opts.UserFlowNick
url = "/api/v1/users/" + opts.UserFlowNick + "/posts"
}
if opts.SinglePostID == "" && opts.UserFlowNick == "" {
toastText = "single post/user flow parameters cannot be blank"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
c.refreshClicked = false
})
return nil, nil
}
}
if byteData, _ := litterAPI("GET", url, nil, c.user.Nickname, pageNo); byteData != nil {
err := json.Unmarshal(*byteData, &resp)
if err != nil {
log.Println(err.Error())
toastText = "JSON parsing error: " + err.Error()
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
c.refreshClicked = false
})
return nil, nil
}
} else {
log.Println("cannot fetch the flow page")
toastText = "API error: cannot fetch the flow page"
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
c.refreshClicked = false
})
return nil, nil
}
if resp.Code == 401 {
toastText = "please log-in again"
ctx.LocalStorage().Set("user", "")
ctx.LocalStorage().Set("authGranted", false)
}
ctx.Dispatch(func(ctx app.Context) {
c.refreshClicked = false
c.toastText = toastText
c.key = resp.Key
if resp.Key != "" {
c.user = c.users[resp.Key]
}
})
return resp.Posts, resp.Users
}
type URIParts struct {
SinglePost bool
SinglePostID string
UserFlow bool
UserFlowNick string
}
func (c *flowContent) parseFlowURI(ctx app.Context) URIParts {
parts := URIParts{
SinglePost: false,
SinglePostID: "",
UserFlow: false,
UserFlowNick: "",
}
url := strings.Split(ctx.Page().URL().Path, "/")
if len(url) > 3 && url[3] != "" {
switch url[2] {
case "post":
parts.SinglePost = true
parts.SinglePostID = url[3]
break
case "user":
parts.UserFlow = true
parts.UserFlowNick = url[3]
break
}
}
isPost := true
if _, err := strconv.Atoi(parts.SinglePostID); parts.SinglePostID != "" && err != nil {
// prolly not a post ID, but an user's nickname
isPost = false
}
ctx.Dispatch(func(ctx app.Context) {
c.isPost = isPost
c.userFlowNick = parts.UserFlowNick
c.singlePostID = parts.SinglePostID
})
return parts
}
func (c *flowContent) OnNav(ctx app.Context) {
ctx.Dispatch(func(ctx app.Context) {
c.loaderShow = true
c.loaderShowImage = true
c.contentLoadFinished = false
c.toastText = ""
c.posts = nil
c.users = nil
})
toastText := ""
isPost := true
ctx.Async(func() {
parts := c.parseFlowURI(ctx)
opts := pageOptions{
PageNo: 0,
Context: ctx,
CallerID: c.user.Nickname,
SinglePost: parts.SinglePost,
SinglePostID: parts.SinglePostID,
UserFlow: parts.UserFlow,
UserFlowNick: parts.UserFlowNick,
}
posts, users := c.fetchFlowPage(opts)
// try the singlePostID/userFlowNick var if present
if parts.SinglePostID != "" && parts.SinglePost {
if _, found := posts[parts.SinglePostID]; !found {
toastText = "post not found"
}
}
if parts.UserFlowNick != "" && parts.UserFlow {
if _, found := users[parts.UserFlowNick]; !found {
toastText = "user not found, or not in your flow"
}
isPost = false
}
// Storing HTTP response in component field:
ctx.Dispatch(func(ctx app.Context) {
c.pagination = 25
c.pageNo = 1
c.pageNoToFetch = 1
c.user = users[c.key]
c.users = users
c.posts = posts
c.singlePostID = parts.SinglePostID
c.userFlowNick = parts.UserFlowNick
c.isPost = isPost
if toastText != "" {
c.toastText = toastText
c.toastShow = (toastText != "")
}
c.loaderShow = false
c.loaderShowImage = false
c.contentLoadFinished = true
})
return
})
}
func (c *flowContent) sortPosts() []models.Post {
var sortedPosts []models.Post
posts := c.posts
if posts == nil {
posts = make(map[string]models.Post)
}
flowList := c.user.FlowList
if len(flowList) == 0 {
return sortedPosts
}
// fetch posts and put them in an array
for _, sortedPost := range posts {
// do not append a post that is not meant to be shown
if !c.user.FlowList[sortedPost.Nickname] && sortedPost.Nickname != "system" {
continue
}
sortedPosts = append(sortedPosts, sortedPost)
}
return sortedPosts
}
func (c *flowContent) Render() app.UI {
toastColor := ""
switch c.toastType {
case "success":
toastColor = "green10"
break
case "info":
toastColor = "blue10"
break
default:
toastColor = "red10"
}
counter := 0
sortedPosts := c.sortPosts()
// order posts by timestamp DESC
sort.SliceStable(sortedPosts, func(i, j int) bool {
return sortedPosts[i].Timestamp.After(sortedPosts[j].Timestamp)
})
// compose a summary of a long post to be replied to
replySummary := ""
if c.modalReplyActive && len(c.posts[c.interactedPostKey].Content) > configs.MaxPostLength {
replySummary = c.posts[c.interactedPostKey].Content[:configs.MaxPostLength/10] + "- [...]"
}
return app.Main().Class("responsive").Body(
// page heading
app.Div().Class("row").Body(
app.Div().Class("max padding").Body(
app.If(c.userFlowNick != "" && !c.isPost,
app.H5().Text(c.userFlowNick+"'s flow"),
).ElseIf(c.singlePostID != "" && c.isPost,
app.H5().Text("single post and replies"),
).Else(
app.H5().Text("flow"),
//app.P().Text("exclusive content incoming frfr"),
),
),
app.Div().Class("small-padding").Body(
app.Button().Class("border black white-text bold").Style("border-radius", "8px").OnClick(c.onClickRefresh).Disabled(c.postButtonsDisabled).Body(
app.If(c.refreshClicked,
app.Progress().Class("circle deep-orange-border small"),
),
app.Text("refresh"),
),
),
),
app.If(c.userFlowNick != "" && !c.isPost,
app.Div().Class("row top-padding").Body(
app.Img().Class("responsive max left").Src(c.users[c.userFlowNick].AvatarURL).Style("max-width", "80px").Style("border-radius", "50%"),
/*;app.P().Class("max").Body(
app.A().Class("bold deep-orange-text").Text(c.singlePostID).ID(c.singlePostID),
//app.B().Text(post.Nickname).Class("deep-orange-text"),
),*/
app.If(c.users[c.userFlowNick].About != "",
app.Article().Class("max").Style("word-break", "break-word").Style("hyphens", "auto").Text(c.users[c.userFlowNick].About),
),
),
),
app.Div().Class("space"),