mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
updates to the profile
This commit is contained in:
@@ -7,27 +7,26 @@ Author: Peter Kleissner
|
||||
package webapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"bytes"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/PeernetOfficial/core/blockchain"
|
||||
"github.com/PeernetOfficial/core/blockchain"
|
||||
)
|
||||
|
||||
// apiProfileData contains profile metadata stored on the blockchain. Any data is treated as untrusted and unverified by default.
|
||||
type apiProfileData struct {
|
||||
Fields []apiBlockRecordProfile `json:"fields"` // All fields
|
||||
Status int `json:"status"` // Status of the operation, only used when this structure is returned from the API. See blockchain.StatusX.
|
||||
Fields []apiBlockRecordProfile `json:"fields"` // All fields
|
||||
Status int `json:"status"` // Status of the operation, only used when this structure is returned from the API. See blockchain.StatusX.
|
||||
}
|
||||
|
||||
// apiBlockRecordProfile provides information about the end user. Note that all profile data is arbitrary and shall be considered untrusted and unverified.
|
||||
// To establish trust, the user must load Certificates into the blockchain that validate certain data.
|
||||
type apiBlockRecordProfile struct {
|
||||
Type uint16 `json:"type"` // See ProfileX constants.
|
||||
// Depending on the exact type, one of the below fields is used for proper encoding:
|
||||
Text string `json:"text"` // Text value. UTF-8 encoding.
|
||||
Blob []byte `json:"blob"` // Binary data
|
||||
Type uint16 `json:"type"` // See ProfileX constants.
|
||||
// Depending on the exact type, one of the below fields is used for proper encoding:
|
||||
Text string `json:"text"` // Text value. UTF-8 encoding.
|
||||
Blob []byte `json:"blob"` // Binary data
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -38,72 +37,55 @@ Response: 200 with JSON structure apiProfileData
|
||||
*/
|
||||
func (api *WebapiInstance) apiProfileList(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
NodeID, valid := DecodeBlake3Hash(r.URL.Query().Get("node"))
|
||||
NodeID, valid := DecodeBlake3Hash(r.URL.Query().Get("node"))
|
||||
|
||||
var fields []blockchain.BlockRecordProfile
|
||||
var status int
|
||||
var fields []blockchain.BlockRecordProfile
|
||||
var status int
|
||||
|
||||
result := apiProfileData{Status: status}
|
||||
result := apiProfileData{Status: status}
|
||||
|
||||
fmt.Println(valid)
|
||||
fmt.Println(NodeID)
|
||||
fmt.Println(api.Backend.SelfNodeID())
|
||||
if valid && !bytes.Equal(NodeID, api.Backend.SelfNodeID()) {
|
||||
//_, node, _ := api.Backend.FindNode(NodeID, 100)
|
||||
|
||||
if valid && !bytes.Equal(NodeID, api.Backend.SelfNodeID()) {
|
||||
//_, node, _ := api.Backend.FindNode(NodeID, 100)
|
||||
peers := api.Backend.PeerlistGet()
|
||||
|
||||
fmt.Println("we are in the if condition")
|
||||
for i, _ := range peers {
|
||||
if bytes.Equal(peers[i].NodeID, NodeID) {
|
||||
// First iteration of the entire blockchain to search for the profile
|
||||
// image and Username of the user
|
||||
|
||||
peers := api.Backend.PeerlistGet()
|
||||
for blockN1 := peers[i].BlockchainHeight - 1; blockN1 > 0; blockN1-- {
|
||||
blockDecoded, _, found, _ := peers[i].Backend.ReadBlock(peers[i].PublicKey, peers[i].BlockchainVersion, blockN1)
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
|
||||
for i, _ := range peers {
|
||||
fmt.Println("in the loop")
|
||||
if bytes.Equal(peers[i].NodeID, NodeID) {
|
||||
// First iteration of the entire blockchain to search for the profile
|
||||
// image and Username of the user
|
||||
fmt.Println("Peer found")
|
||||
profile, _ := blockchain.DecodeBlockRecordProfile(blockDecoded.Block.RecordsRaw)
|
||||
// Adding profile image and Username to the output
|
||||
for raw, _ := range profile {
|
||||
|
||||
fmt.Println(peers[i])
|
||||
if profile[raw].Type == blockchain.ProfileName {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: profile[raw].Type, Data: profile[raw].Data[:]}))
|
||||
}
|
||||
if profile[raw].Type == blockchain.ProfilePicture {
|
||||
result.Status = 0
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: profile[raw].Type, Data: profile[raw].Data[:]}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//fields, status = peers[i].Backend.UserBlockchain.ProfileList()
|
||||
//
|
||||
//for j, _ := range fields {
|
||||
// result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: fields[j].Type, Data: fields[j].Data}))
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
for blockN1 := peers[i].BlockchainHeight - 1; blockN1 > 0; blockN1-- {
|
||||
blockDecoded, _, found, _ := peers[i].Backend.ReadBlock(peers[i].PublicKey, peers[i].BlockchainVersion, blockN1)
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
fields, status = api.Backend.UserBlockchain.ProfileList()
|
||||
result.Status = status
|
||||
for n := range fields {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(fields[n]))
|
||||
}
|
||||
}
|
||||
|
||||
profile, _ := blockchain.DecodeBlockRecordProfile(blockDecoded.Block.RecordsRaw)
|
||||
// Adding profile image and Username to the output
|
||||
for raw, _ := range profile {
|
||||
|
||||
if profile[raw].Type == blockchain.ProfileName {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: profile[raw].Type, Data: profile[raw].Data[:]}))
|
||||
}
|
||||
if profile[raw].Type == blockchain.ProfilePicture {
|
||||
result.Status = 0
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: profile[raw].Type, Data: profile[raw].Data[:]}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
fmt.Println("why are you here")
|
||||
fields, status = api.Backend.UserBlockchain.ProfileList()
|
||||
result.Status = status
|
||||
for n := range fields {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(fields[n]))
|
||||
}
|
||||
}
|
||||
|
||||
EncodeJSON(api.Backend, w, r, result)
|
||||
EncodeJSON(api.Backend, w, r, result)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -113,32 +95,32 @@ Request: GET /profile/read?field=[index]&node=[nodeid]
|
||||
Response: 200 with JSON structure apiProfileData
|
||||
*/
|
||||
func (api *WebapiInstance) apiProfileRead(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
fieldN, err1 := strconv.Atoi(r.URL.Query().Get("field"))
|
||||
NodeID, valid := DecodeBlake3Hash(r.URL.Query().Get("node"))
|
||||
r.ParseForm()
|
||||
fieldN, err1 := strconv.Atoi(r.URL.Query().Get("field"))
|
||||
NodeID, valid := DecodeBlake3Hash(r.URL.Query().Get("node"))
|
||||
|
||||
if err1 != nil || fieldN < 0 {
|
||||
http.Error(w, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err1 != nil || fieldN < 0 {
|
||||
http.Error(w, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var result apiProfileData
|
||||
var data []byte
|
||||
var result apiProfileData
|
||||
var data []byte
|
||||
|
||||
if !valid {
|
||||
_, node, _ := api.Backend.FindNode(NodeID, 100)
|
||||
if data, result.Status = node.Backend.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: uint16(fieldN), Data: data}))
|
||||
}
|
||||
} else {
|
||||
if api.Backend.NodelistLookup(NodeID) != nil {
|
||||
if data, result.Status = api.Backend.NodelistLookup(NodeID).Backend.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: uint16(fieldN), Data: data}))
|
||||
}
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
_, node, _ := api.Backend.FindNode(NodeID, 100)
|
||||
if data, result.Status = node.Backend.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: uint16(fieldN), Data: data}))
|
||||
}
|
||||
} else {
|
||||
if api.Backend.NodelistLookup(NodeID) != nil {
|
||||
if data, result.Status = api.Backend.NodelistLookup(NodeID).Backend.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK {
|
||||
result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: uint16(fieldN), Data: data}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EncodeJSON(api.Backend, w, r, result)
|
||||
EncodeJSON(api.Backend, w, r, result)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -148,24 +130,24 @@ Request: POST /profile/write with JSON structure apiProfileData
|
||||
Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
*/
|
||||
func (api *WebapiInstance) apiProfileWrite(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiProfileData
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
}
|
||||
var input apiProfileData
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, field := range input.Fields {
|
||||
api.Backend.LogError("apiProfileWrite(", "Upload type: %v\n", field.Type)
|
||||
}
|
||||
for _, field := range input.Fields {
|
||||
api.Backend.LogError("apiProfileWrite(", "Upload type: %v\n", field.Type)
|
||||
}
|
||||
|
||||
var fields []blockchain.BlockRecordProfile
|
||||
var fields []blockchain.BlockRecordProfile
|
||||
|
||||
for n := range input.Fields {
|
||||
fields = append(fields, blockRecordProfileFromAPI(input.Fields[n]))
|
||||
}
|
||||
for n := range input.Fields {
|
||||
fields = append(fields, blockRecordProfileFromAPI(input.Fields[n]))
|
||||
}
|
||||
|
||||
newHeight, newVersion, status := api.Backend.UserBlockchain.ProfileWrite(fields)
|
||||
newHeight, newVersion, status := api.Backend.UserBlockchain.ProfileWrite(fields)
|
||||
|
||||
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -175,54 +157,54 @@ Request: POST /profile/delete with JSON structure apiProfileData
|
||||
Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
*/
|
||||
func (api *WebapiInstance) apiProfileDelete(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiProfileData
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
}
|
||||
var input apiProfileData
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var fields []uint16
|
||||
var fields []uint16
|
||||
|
||||
for n := range input.Fields {
|
||||
fields = append(fields, input.Fields[n].Type)
|
||||
}
|
||||
for n := range input.Fields {
|
||||
fields = append(fields, input.Fields[n].Type)
|
||||
}
|
||||
|
||||
newHeight, newVersion, status := api.Backend.UserBlockchain.ProfileDelete(fields)
|
||||
newHeight, newVersion, status := api.Backend.UserBlockchain.ProfileDelete(fields)
|
||||
|
||||
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
EncodeJSON(api.Backend, w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight, Version: newVersion})
|
||||
}
|
||||
|
||||
// --- conversion from core to API data ---
|
||||
|
||||
func blockRecordProfileToAPI(input blockchain.BlockRecordProfile) (output apiBlockRecordProfile) {
|
||||
output.Type = input.Type
|
||||
output.Type = input.Type
|
||||
|
||||
switch input.Type {
|
||||
case blockchain.ProfileName, blockchain.ProfileEmail, blockchain.ProfileWebsite, blockchain.ProfileTwitter, blockchain.ProfileYouTube, blockchain.ProfileAddress:
|
||||
output.Text = input.Text()
|
||||
switch input.Type {
|
||||
case blockchain.ProfileName, blockchain.ProfileEmail, blockchain.ProfileWebsite, blockchain.ProfileTwitter, blockchain.ProfileYouTube, blockchain.ProfileAddress:
|
||||
output.Text = input.Text()
|
||||
|
||||
case blockchain.ProfilePicture:
|
||||
output.Blob = input.Data
|
||||
case blockchain.ProfilePicture:
|
||||
output.Blob = input.Data
|
||||
|
||||
default:
|
||||
output.Blob = input.Data
|
||||
}
|
||||
default:
|
||||
output.Blob = input.Data
|
||||
}
|
||||
|
||||
return output
|
||||
return output
|
||||
}
|
||||
|
||||
func blockRecordProfileFromAPI(input apiBlockRecordProfile) (output blockchain.BlockRecordProfile) {
|
||||
output.Type = input.Type
|
||||
output.Type = input.Type
|
||||
|
||||
switch input.Type {
|
||||
case blockchain.ProfileName, blockchain.ProfileEmail, blockchain.ProfileWebsite, blockchain.ProfileTwitter, blockchain.ProfileYouTube, blockchain.ProfileAddress:
|
||||
output.Data = []byte(input.Text)
|
||||
switch input.Type {
|
||||
case blockchain.ProfileName, blockchain.ProfileEmail, blockchain.ProfileWebsite, blockchain.ProfileTwitter, blockchain.ProfileYouTube, blockchain.ProfileAddress:
|
||||
output.Data = []byte(input.Text)
|
||||
|
||||
case blockchain.ProfilePicture:
|
||||
output.Data = input.Blob
|
||||
case blockchain.ProfilePicture:
|
||||
output.Data = input.Blob
|
||||
|
||||
default:
|
||||
output.Data = input.Blob
|
||||
}
|
||||
default:
|
||||
output.Data = input.Blob
|
||||
}
|
||||
|
||||
return output
|
||||
return output
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user