forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
at packages 2.5 kB view raw
1package repo 2 3import ( 4 "context" 5 "crypto/rand" 6 "fmt" 7 "log" 8 "math/big" 9 10 "github.com/go-git/go-git/v5/plumbing/object" 11 "tangled.sh/tangled.sh/core/appview/db" 12) 13 14func uniqueEmails(commits []*object.Commit) []string { 15 emails := make(map[string]struct{}) 16 for _, commit := range commits { 17 if commit.Author.Email != "" { 18 emails[commit.Author.Email] = struct{}{} 19 } 20 if commit.Committer.Email != "" { 21 emails[commit.Committer.Email] = struct{}{} 22 } 23 } 24 var uniqueEmails []string 25 for email := range emails { 26 uniqueEmails = append(uniqueEmails, email) 27 } 28 return uniqueEmails 29} 30 31func balanceIndexItems(commitCount, branchCount, tagCount, fileCount int) (commitsTrunc int, branchesTrunc int, tagsTrunc int) { 32 if commitCount == 0 && tagCount == 0 && branchCount == 0 { 33 return 34 } 35 36 // typically 1 item on right side = 2 files in height 37 availableSpace := fileCount / 2 38 39 // clamp tagcount 40 if tagCount > 0 { 41 tagsTrunc = 1 42 availableSpace -= 1 // an extra subtracted for headers etc. 43 } 44 45 // clamp branchcount 46 if branchCount > 0 { 47 branchesTrunc = min(max(branchCount, 1), 2) 48 availableSpace -= branchesTrunc // an extra subtracted for headers etc. 49 } 50 51 // show 52 if commitCount > 0 { 53 commitsTrunc = max(availableSpace, 3) 54 } 55 56 return 57} 58 59func EmailToDidOrHandle(r *Repo, emails []string) map[string]string { 60 emailToDid, err := db.GetEmailToDid(r.db, emails, true) // only get verified emails for mapping 61 if err != nil { 62 log.Printf("error fetching dids for emails: %v", err) 63 return nil 64 } 65 66 var dids []string 67 for _, v := range emailToDid { 68 dids = append(dids, v) 69 } 70 resolvedIdents := r.idResolver.ResolveIdents(context.Background(), dids) 71 72 didHandleMap := make(map[string]string) 73 for _, identity := range resolvedIdents { 74 if !identity.Handle.IsInvalidHandle() { 75 didHandleMap[identity.DID.String()] = fmt.Sprintf("@%s", identity.Handle.String()) 76 } else { 77 didHandleMap[identity.DID.String()] = identity.DID.String() 78 } 79 } 80 81 // Create map of email to didOrHandle for commit display 82 emailToDidOrHandle := make(map[string]string) 83 for email, did := range emailToDid { 84 if didOrHandle, ok := didHandleMap[did]; ok { 85 emailToDidOrHandle[email] = didOrHandle 86 } 87 } 88 89 return emailToDidOrHandle 90} 91 92func randomString(n int) string { 93 const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 94 result := make([]byte, n) 95 96 for i := 0; i < n; i++ { 97 n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letters)))) 98 result[i] = letters[n.Int64()] 99 } 100 101 return string(result) 102}