gnoverse / dsocial

Experimental: Social apps, tools, researchs and dApps on Gno.land
8 stars 4 forks source link

chore: Add GetJsonTopPostsByID #62

Closed jefft0 closed 6 months ago

jefft0 commented 6 months ago

We already have GetThreadPosts which is good to use an index range to get the posts in-order of a thread (or reply thread). But the home feed includes followed posts inserted at any point in the feed. We need a way to get arbitrary top-level posts by Post ID. This PR adds GetJsonTopPostsByID.

Tested in the GnoSoclal demo app with the following code:

const result = await gno.qEval("gno.land/r/berty/social", "GetJsonTopPostsByID([]UserAndPostID{" + 
  `{"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", 1}, ` + 
  `{"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", 3}, ` + "})");
if (!(result.startsWith('(') && result.endsWith(' string)'))) throw new Error("Malformed GetJsonTopPostsByID response");
const quoted = result.substring(1, result.length - ' string)'.length);
const json = JSON.parse(quoted);
const info = JSON.parse(json);

Example value of json:

[ {"id": 1, "createdAt": "2024-04-23T15:46:36Z", "creator": "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "n_replies": 0, "n_replies_all": 0, "parent_id": 0, "body": "test_1 first"},
  {"id": 3, "createdAt": "2024-04-23T15:49:38Z", "creator": "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5", "n_replies": 0, "n_replies_all": 0, "parent_id": 0, "body": "test_1 third"}]