github.com/decred/politeia@v1.4.0/politeiad/backendv2/tstorebe/plugins/usermd/cmds.go (about) 1 // Copyright (c) 2020-2021 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package usermd 6 7 import ( 8 "encoding/json" 9 10 "github.com/decred/politeia/politeiad/plugins/usermd" 11 ) 12 13 // cmdAuthor returns the user ID of a record's author. 14 func (p *usermdPlugin) cmdAuthor(token []byte) (string, error) { 15 // Get user metadata 16 r, err := p.tstore.RecordPartial(token, 0, nil, true) 17 if err != nil { 18 return "", err 19 } 20 um, err := userMetadataDecode(r.Metadata) 21 if err != nil { 22 return "", err 23 } 24 25 // Prepare reply 26 ar := usermd.AuthorReply{ 27 UserID: um.UserID, 28 } 29 reply, err := json.Marshal(ar) 30 if err != nil { 31 return "", err 32 } 33 34 return string(reply), nil 35 } 36 37 // cmdUserRecords retrieves the tokens of all records that were submitted by 38 // the provided user ID. The returned tokens are sorted from newest to oldest. 39 func (p *usermdPlugin) cmdUserRecords(payload string) (string, error) { 40 // Decode payload 41 var ur usermd.UserRecords 42 err := json.Unmarshal([]byte(payload), &ur) 43 if err != nil { 44 return "", err 45 } 46 47 // Get user records 48 uc, err := p.userCache(ur.UserID) 49 if err != nil { 50 return "", err 51 } 52 53 // The tokens in the user cache are ordered oldest to 54 // newest. We need to return them newest to oldest. 55 var ( 56 unvetted = make([]string, 0, len(uc.Unvetted)) 57 vetted = make([]string, 0, len(uc.Vetted)) 58 ) 59 for i := len(uc.Unvetted) - 1; i >= 0; i-- { 60 unvetted = append(unvetted, uc.Unvetted[i]) 61 } 62 for i := len(uc.Vetted) - 1; i >= 0; i-- { 63 vetted = append(vetted, uc.Vetted[i]) 64 } 65 66 // Prepare reply 67 urr := usermd.UserRecordsReply{ 68 Unvetted: unvetted, 69 Vetted: vetted, 70 } 71 reply, err := json.Marshal(urr) 72 if err != nil { 73 return "", err 74 } 75 76 return string(reply), nil 77 }