From 8b0d468455ace38cf4d6682412467015d36c3b12 Mon Sep 17 00:00:00 2001 From: Akilan Date: Thu, 1 Jun 2023 18:16:51 +0100 Subject: [PATCH] added possibility to get profile using NodeID --- webapi/Profile.go | 31 +++++++++++++++++++----- webapi/Shared Recent.go | 53 +++++++++++++++++++---------------------- webapi/readme.md | 4 ++-- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/webapi/Profile.go b/webapi/Profile.go index 8598380..0477aeb 100644 --- a/webapi/Profile.go +++ b/webapi/Profile.go @@ -31,11 +31,21 @@ type apiBlockRecordProfile struct { /* apiProfileList lists all users profile fields. -Request: GET /profile/list +Request: GET /profile/list?node[nodeid] Response: 200 with JSON structure apiProfileData */ func (api *WebapiInstance) apiProfileList(w http.ResponseWriter, r *http.Request) { - fields, status := api.Backend.UserBlockchain.ProfileList() + + NodeID, valid := DecodeBlake3Hash(r.Form.Get("node")) + + var fields []blockchain.BlockRecordProfile + var status int + + if valid { + fields, status = api.Backend.NodelistLookup(NodeID).Backend.UserBlockchain.ProfileList() + } else { + fields, status = api.Backend.UserBlockchain.ProfileList() + } result := apiProfileData{Status: status} for n := range fields { @@ -48,12 +58,13 @@ func (api *WebapiInstance) apiProfileList(w http.ResponseWriter, r *http.Request /* apiProfileRead reads a specific users profile field. See core.ProfileX for recognized fields. -Request: GET /profile/read?field=[index] +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.Form.Get("field")) + NodeID, valid := DecodeBlake3Hash(r.Form.Get("node")) if err1 != nil || fieldN < 0 { http.Error(w, "", http.StatusBadRequest) @@ -61,10 +72,18 @@ func (api *WebapiInstance) apiProfileRead(w http.ResponseWriter, r *http.Request } var result apiProfileData - var data []byte - if data, result.Status = api.Backend.UserBlockchain.ProfileReadField(uint16(fieldN)); result.Status == blockchain.StatusOK { - result.Fields = append(result.Fields, blockRecordProfileToAPI(blockchain.BlockRecordProfile{Type: uint16(fieldN), Data: data})) + + if !valid { + if data, result.Status = api.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) diff --git a/webapi/Shared Recent.go b/webapi/Shared Recent.go index 1e72ce6..f7a29cc 100644 --- a/webapi/Shared Recent.go +++ b/webapi/Shared Recent.go @@ -44,31 +44,31 @@ func (api *WebapiInstance) queryRecentShared(backend *core.Backend, fileType int var filesFromPeer uint64 - var ProfileImage []byte - var Name string + //var ProfileImage []byte + //var Name string - ProfileNameFound := false - ProfilePictureFound := false - - // First iteration of the entire blockchain to search for the profile - // image and Username of the user - for blockN1 := peer.BlockchainHeight - 1; blockN1 > 0; blockN1-- { - blockDecoded, _, found, _ := backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN1) - if !found { - continue - } - // Adding profile image and Username to the output - for raw := range blockDecoded.Block.RecordsRaw { - if blockDecoded.Block.RecordsRaw[raw].Type == blockchain.ProfilePicture && !ProfilePictureFound { - ProfileImage = blockDecoded.Block.RecordsRaw[raw].Data - ProfilePictureFound = true - } - if blockDecoded.Block.RecordsRaw[raw].Type == blockchain.ProfileName && !ProfileNameFound { - Name = string(blockDecoded.Block.RecordsRaw[raw].Data[:]) - ProfileNameFound = true - } - } - } + //ProfileNameFound := false + //ProfilePictureFound := false + // + //// First iteration of the entire blockchain to search for the profile + //// image and Username of the user + //for blockN1 := peer.BlockchainHeight - 1; blockN1 > 0; blockN1-- { + // blockDecoded, _, found, _ := backend.ReadBlock(peer.PublicKey, peer.BlockchainVersion, blockN1) + // if !found { + // continue + // } + // // Adding profile image and Username to the output + // for raw := range blockDecoded.Block.RecordsRaw { + // if blockDecoded.Block.RecordsRaw[raw].Type == blockchain.ProfilePicture && !ProfilePictureFound { + // ProfileImage = blockDecoded.Block.RecordsRaw[raw].Data + // ProfilePictureFound = true + // } + // if blockDecoded.Block.RecordsRaw[raw].Type == blockchain.ProfileName && !ProfileNameFound { + // Name = string(blockDecoded.Block.RecordsRaw[raw].Data[:]) + // ProfileNameFound = true + // } + // } + //} // decode blocks from top down blockLoop: @@ -88,11 +88,6 @@ func (api *WebapiInstance) queryRecentShared(backend *core.Backend, fileType int file.Tags = append(file.Tags, blockchain.TagFromText(blockchain.TagSharedByGeoIP, sharedByGeoIP)) } - // Add profile image - file.ProfileImage = ProfileImage - // Add profile name - file.Username = Name - // found a new file! append. if filesFromPeer < limitPeer { filesFromPeer++ diff --git a/webapi/readme.md b/webapi/readme.md index d591cba..62af342 100644 --- a/webapi/readme.md +++ b/webapi/readme.md @@ -496,7 +496,7 @@ Below is the list of well known profile information. Clients may define addition This lists all profile fields. ``` -Request: GET /profile/list +Request: GET /profile/list&node=[node id] Response: 200 with JSON structure apiProfileData ``` @@ -538,7 +538,7 @@ Example response: This reads a specific profile field. See ProfileX for recognized fields. ``` -Request: GET /profile/read?field=[index] +Request: GET /profile/read?field=[index]&node=[node id] Response: 200 with JSON structure apiProfileData ```