32 lines
734 B
Plaintext
32 lines
734 B
Plaintext
load_nodes_from_json = function(thread_data, cursor_id)
|
|
-- massage JSON into a table from id to node
|
|
Nodes = {} -- id to object
|
|
Nodes[thread_data.id] = thread_data
|
|
for _,x in ipairs(thread_data.descendants) do
|
|
Nodes[x.id] = x
|
|
end
|
|
-- initialize Root
|
|
Root = thread_data
|
|
-- compute children
|
|
for _,x in pairs(Nodes) do
|
|
parent_id = x.in_reply_to_id
|
|
if x.id ~= Root.id then
|
|
assert(parent_id)
|
|
local parent = Nodes[parent_id]
|
|
if parent then -- watch out for private posts
|
|
if parent.children == nil then
|
|
parent.children = {}
|
|
end
|
|
table.insert(parent.children, x.id)
|
|
end
|
|
end
|
|
end
|
|
-- sort children by time
|
|
for _,x in pairs(Nodes) do
|
|
if x.children then
|
|
table.sort(x.children)
|
|
end
|
|
end
|
|
end
|
|
|