Write on the margins of the internet. Powered by the AT Protocol. margin.at
extension web atproto comments
at ui-refactor 60 lines 1.5 kB view raw
1package api 2 3import ( 4 "encoding/json" 5 "time" 6 7 "margin.at/internal/db" 8) 9 10func (s *AnnotationService) checkDuplicateAnnotation(did, url, text string) (*db.Annotation, error) { 11 recentAnnos, err := s.db.GetAnnotationsByAuthor(did, 5, 0) 12 if err != nil { 13 return nil, err 14 } 15 for _, a := range recentAnnos { 16 if a.TargetSource == url && 17 ((a.BodyValue == nil && text == "") || (a.BodyValue != nil && *a.BodyValue == text)) && 18 time.Since(a.CreatedAt) < 10*time.Second { 19 return &a, nil 20 } 21 } 22 return nil, nil 23} 24 25func (s *AnnotationService) checkDuplicateHighlight(did, url string, selector json.RawMessage) (*db.Highlight, error) { 26 recentHighs, err := s.db.GetHighlightsByAuthor(did, 5, 0) 27 if err != nil { 28 return nil, err 29 } 30 for _, h := range recentHighs { 31 matchSelector := false 32 if h.SelectorJSON == nil && selector == nil { 33 matchSelector = true 34 } else if h.SelectorJSON != nil && selector != nil { 35 selectorBytes, _ := json.Marshal(selector) 36 if *h.SelectorJSON == string(selectorBytes) { 37 matchSelector = true 38 } 39 } 40 41 if h.TargetSource == url && matchSelector && time.Since(h.CreatedAt) < 10*time.Second { 42 return &h, nil 43 } 44 } 45 return nil, nil 46} 47 48func (s *AnnotationService) checkDuplicateBookmark(did, url string) (*db.Bookmark, error) { 49 urlHash := db.HashURL(url) 50 bookmarks, err := s.db.GetBookmarksByTargetHash(urlHash, 50, 0) 51 if err != nil { 52 return nil, err 53 } 54 for _, b := range bookmarks { 55 if b.AuthorDID == did && b.Source == url { 56 return &b, nil 57 } 58 } 59 return nil, nil 60}