tangled
alpha
login
or
join now
margin.at
/
margin
87
fork
atom
Write on the margins of the internet. Powered by the AT Protocol.
margin.at
extension
web
atproto
comments
87
fork
atom
overview
issues
4
pulls
1
pipelines
fix: do not serve user feed from PDS
Henrique Dias
3 weeks ago
915b40f4
b791eaab
-154
1 changed file
expand all
collapse all
unified
split
backend
internal
api
handler.go
-154
backend/internal/api/handler.go
···
11
11
"sort"
12
12
"strconv"
13
13
"strings"
14
14
-
"sync"
15
14
"time"
16
15
17
16
"github.com/go-chi/chi/v5"
···
172
171
feedType := r.URL.Query().Get("type")
173
172
174
173
viewerDID := h.getViewerDID(r)
175
175
-
176
176
-
if viewerDID != "" && (feedType == "my-feed" || creator == viewerDID) {
177
177
-
h.serveUserFeedFromPDS(w, r, viewerDID, tag, r.URL.Query().Get("motivation"), limit, offset)
178
178
-
return
179
179
-
}
180
174
181
175
var annotations []db.Annotation
182
176
var highlights []db.Highlight
···
482
476
"items": feed,
483
477
"totalItems": len(feed),
484
478
})
485
485
-
}
486
486
-
487
487
-
func (h *Handler) serveUserFeedFromPDS(w http.ResponseWriter, r *http.Request, did, tag, motivation string, limit, offset int) {
488
488
-
var wg sync.WaitGroup
489
489
-
var rawAnnos, rawHighs, rawBooks []interface{}
490
490
-
var errAnnos, errHighs, errBooks error
491
491
-
492
492
-
fetchLimit := limit + offset
493
493
-
if fetchLimit < 50 {
494
494
-
fetchLimit = 50
495
495
-
}
496
496
-
497
497
-
if motivation == "" || motivation == "commenting" {
498
498
-
wg.Add(1)
499
499
-
go func() {
500
500
-
defer wg.Done()
501
501
-
rawAnnos, errAnnos = h.FetchLatestUserRecords(r, did, xrpc.CollectionAnnotation, fetchLimit)
502
502
-
}()
503
503
-
}
504
504
-
if motivation == "" || motivation == "highlighting" {
505
505
-
wg.Add(1)
506
506
-
go func() {
507
507
-
defer wg.Done()
508
508
-
rawHighs, errHighs = h.FetchLatestUserRecords(r, did, xrpc.CollectionHighlight, fetchLimit)
509
509
-
}()
510
510
-
}
511
511
-
if motivation == "" || motivation == "bookmarking" {
512
512
-
wg.Add(1)
513
513
-
go func() {
514
514
-
defer wg.Done()
515
515
-
rawBooks, errBooks = h.FetchLatestUserRecords(r, did, xrpc.CollectionBookmark, fetchLimit)
516
516
-
}()
517
517
-
}
518
518
-
wg.Wait()
519
519
-
520
520
-
if errAnnos != nil {
521
521
-
log.Printf("PDS Fetch Error (Annos): %v", errAnnos)
522
522
-
}
523
523
-
if errHighs != nil {
524
524
-
log.Printf("PDS Fetch Error (Highs): %v", errHighs)
525
525
-
}
526
526
-
if errBooks != nil {
527
527
-
log.Printf("PDS Fetch Error (Books): %v", errBooks)
528
528
-
}
529
529
-
530
530
-
var annotations []db.Annotation
531
531
-
var highlights []db.Highlight
532
532
-
var bookmarks []db.Bookmark
533
533
-
534
534
-
for _, r := range rawAnnos {
535
535
-
if a, ok := r.(*db.Annotation); ok {
536
536
-
if tag == "" || containsTag(a.TagsJSON, tag) {
537
537
-
annotations = append(annotations, *a)
538
538
-
}
539
539
-
}
540
540
-
}
541
541
-
for _, r := range rawHighs {
542
542
-
if h, ok := r.(*db.Highlight); ok {
543
543
-
if tag == "" || containsTag(h.TagsJSON, tag) {
544
544
-
highlights = append(highlights, *h)
545
545
-
}
546
546
-
}
547
547
-
}
548
548
-
for _, r := range rawBooks {
549
549
-
if b, ok := r.(*db.Bookmark); ok {
550
550
-
if tag == "" || containsTag(b.TagsJSON, tag) {
551
551
-
bookmarks = append(bookmarks, *b)
552
552
-
}
553
553
-
}
554
554
-
}
555
555
-
556
556
-
go func() {
557
557
-
for _, a := range annotations {
558
558
-
h.db.CreateAnnotation(&a)
559
559
-
}
560
560
-
for _, hi := range highlights {
561
561
-
h.db.CreateHighlight(&hi)
562
562
-
}
563
563
-
for _, b := range bookmarks {
564
564
-
h.db.CreateBookmark(&b)
565
565
-
}
566
566
-
}()
567
567
-
568
568
-
collectionItems := []db.CollectionItem{}
569
569
-
if tag == "" && motivation == "" {
570
570
-
items, err := h.db.GetCollectionItemsByAuthor(did)
571
571
-
if err != nil {
572
572
-
log.Printf("Error fetching collection items for user feed: %v", err)
573
573
-
} else {
574
574
-
collectionItems = items
575
575
-
}
576
576
-
}
577
577
-
578
578
-
if len(collectionItems) > 0 {
579
579
-
var sembleURIs []string
580
580
-
for _, item := range collectionItems {
581
581
-
if strings.Contains(item.AnnotationURI, "network.cosmik.card") {
582
582
-
sembleURIs = append(sembleURIs, item.AnnotationURI)
583
583
-
}
584
584
-
}
585
585
-
if len(sembleURIs) > 0 {
586
586
-
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
587
587
-
defer cancel()
588
588
-
ensureSembleCardsIndexed(ctx, h.db, sembleURIs)
589
589
-
}
590
590
-
}
591
591
-
592
592
-
authAnnos, _ := hydrateAnnotations(h.db, annotations, did)
593
593
-
authHighs, _ := hydrateHighlights(h.db, highlights, did)
594
594
-
authBooks, _ := hydrateBookmarks(h.db, bookmarks, did)
595
595
-
authCollectionItems, _ := hydrateCollectionItems(h.db, collectionItems, did)
596
596
-
597
597
-
var feed []interface{}
598
598
-
for _, a := range authAnnos {
599
599
-
feed = append(feed, a)
600
600
-
}
601
601
-
for _, h := range authHighs {
602
602
-
feed = append(feed, h)
603
603
-
}
604
604
-
for _, b := range authBooks {
605
605
-
feed = append(feed, b)
606
606
-
}
607
607
-
if motivation == "" {
608
608
-
for _, ci := range authCollectionItems {
609
609
-
feed = append(feed, ci)
610
610
-
}
611
611
-
}
612
612
-
613
613
-
sortFeed(feed)
614
614
-
615
615
-
if offset < len(feed) {
616
616
-
feed = feed[offset:]
617
617
-
} else {
618
618
-
feed = []interface{}{}
619
619
-
}
620
620
-
621
621
-
if len(feed) > limit {
622
622
-
feed = feed[:limit]
623
623
-
}
624
624
-
625
625
-
w.Header().Set("Content-Type", "application/json")
626
626
-
json.NewEncoder(w).Encode(map[string]interface{}{
627
627
-
"@context": "http://www.w3.org/ns/anno.jsonld",
628
628
-
"type": "Collection",
629
629
-
"items": feed,
630
630
-
"totalItems": len(feed),
631
631
-
})
632
632
-
633
479
}
634
480
635
481
func containsTag(tagsJSON *string, tag string) bool {