github.com/decred/politeia@v1.4.0/politeiawww/legacy/user/cockroachdb/convert.go (about) 1 package cockroachdb 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 8 "github.com/decred/politeia/politeiawww/legacy/user" 9 "github.com/google/uuid" 10 ) 11 12 func convertIdentityFromUser(id user.Identity) Identity { 13 return Identity{ 14 PublicKey: id.String(), 15 Activated: id.Activated, 16 Deactivated: id.Deactivated, 17 } 18 } 19 20 func convertIdentitiesFromUser(ids []user.Identity) []Identity { 21 s := make([]Identity, 0, len(ids)) 22 for _, v := range ids { 23 s = append(s, convertIdentityFromUser(v)) 24 } 25 return s 26 } 27 28 func convertUserFromUser(u user.User, blob []byte) User { 29 return User{ 30 ID: u.ID, 31 Username: u.Username, 32 Identities: convertIdentitiesFromUser(u.Identities), 33 Blob: blob, 34 } 35 } 36 37 func (c *cockroachdb) convertCMSUserFromDatabase(cu CMSUser) (*user.CMSUser, error) { 38 proposalsOwned := strings.Split(cu.ProposalsOwned, ",") 39 superUserIds := strings.Split(cu.SupervisorUserID, ",") 40 parsedUUIds := make([]uuid.UUID, 0, len(superUserIds)) 41 proposalsSlice := make([]string, 0, len(proposalsOwned)) 42 for _, proposal := range proposalsOwned { 43 if proposal == "" { 44 continue 45 } 46 proposalsSlice = append(proposalsSlice, strings.TrimSpace(proposal)) 47 } 48 for _, userIds := range superUserIds { 49 if userIds == "" { 50 continue 51 } 52 parsed, err := uuid.Parse(strings.TrimSpace(userIds)) 53 if err != nil { 54 return nil, err 55 } 56 parsedUUIds = append(parsedUUIds, parsed) 57 } 58 u := user.CMSUser{ 59 Domain: cu.Domain, 60 GitHubName: cu.GitHubName, 61 MatrixName: cu.MatrixName, 62 ContractorType: cu.ContractorType, 63 ContractorName: cu.ContractorName, 64 ContractorLocation: cu.ContractorLocation, 65 ContractorContact: cu.ContractorContact, 66 SupervisorUserIDs: parsedUUIds, 67 ProposalsOwned: proposalsSlice, 68 } 69 b, _, err := c.decrypt(cu.User.Blob) 70 if err != nil { 71 return nil, err 72 } 73 usr, err := user.DecodeUser(b) 74 if err != nil { 75 return nil, err 76 } 77 u.User = *usr 78 return &u, nil 79 } 80 81 func (c *cockroachdb) convertCMSUsersFromDatabase(cu []CMSUser) ([]user.CMSUser, error) { 82 users := make([]user.CMSUser, 0, len(cu)) 83 for _, v := range cu { 84 u, err := c.convertCMSUserFromDatabase(v) 85 if err != nil { 86 return nil, err 87 } 88 users = append(users, *u) 89 } 90 return users, nil 91 } 92 93 func convertCodestatsToDatabase(cs user.CodeStats) CMSCodeStats { 94 prs := "" 95 reviews := "" 96 commits := "" 97 for i, pr := range cs.PRs { 98 if i < len(cs.PRs)-1 { 99 prs += pr + "," 100 } else { 101 prs += pr 102 } 103 } 104 for i, review := range cs.Reviews { 105 if i < len(cs.Reviews)-1 { 106 reviews += review + "," 107 } else { 108 reviews += review 109 } 110 } 111 for i, commit := range cs.Commits { 112 if i < len(cs.Commits)-1 { 113 commits += commit + "," 114 } else { 115 commits += commit 116 } 117 } 118 id := fmt.Sprintf("%v-%v-%v-%v", cs.GitHubName, cs.Repository, 119 strconv.Itoa(cs.Month), strconv.Itoa(cs.Year)) 120 return CMSCodeStats{ 121 ID: id, 122 GitHubName: cs.GitHubName, 123 Repository: cs.Repository, 124 Month: cs.Month, 125 Year: cs.Year, 126 PRs: prs, 127 Reviews: reviews, 128 Commits: commits, 129 MergedAdditions: cs.MergedAdditions, 130 MergedDeletions: cs.MergedDeletions, 131 UpdatedAdditions: cs.UpdatedAdditions, 132 UpdatedDeletions: cs.UpdatedDeletions, 133 ReviewAdditions: cs.ReviewAdditions, 134 ReviewDeletions: cs.ReviewDeletions, 135 CommitAdditions: cs.CommitAdditions, 136 CommitDeletions: cs.CommitDeletions, 137 } 138 } 139 140 func convertCodestatsFromDatabase(cs CMSCodeStats) user.CodeStats { 141 prs := strings.Split(cs.PRs, ",") 142 reviews := strings.Split(cs.Reviews, ",") 143 commits := strings.Split(cs.Commits, ",") 144 return user.CodeStats{ 145 ID: cs.ID, 146 GitHubName: cs.GitHubName, 147 Repository: cs.Repository, 148 Month: cs.Month, 149 Year: cs.Year, 150 PRs: prs, 151 Reviews: reviews, 152 Commits: commits, 153 MergedAdditions: cs.MergedAdditions, 154 MergedDeletions: cs.MergedDeletions, 155 UpdatedAdditions: cs.UpdatedAdditions, 156 UpdatedDeletions: cs.UpdatedDeletions, 157 ReviewAdditions: cs.ReviewAdditions, 158 ReviewDeletions: cs.ReviewDeletions, 159 CommitAdditions: cs.CommitAdditions, 160 CommitDeletions: cs.CommitDeletions, 161 } 162 }