RZB6HZ2NI5PIUIWQL63CSKXZGGUG4L6XCSCGR3TI723OO57NDT3AC ?assertEqual(LoadedPost, LoadedByHash),
?assertEqual(Decoded, LoadedByHash),db:close(Db).all_posts_test() ->Examples = [ examples:post_text(), examples:post_delete(), examples:post_info(), examples:post_topic(), examples:post_join(), examples:post_leave()],Db = open_db(?current_function_name()),Test = fun(Ex) ->[{binary, Bin}, _] = Ex,Decoded = posts:decode(Bin),[Header, _] = Decoded,{ok, Id} = db:savePost(Db, Decoded),?assert(is_integer(Id)),{ok, LoadedPost} = db:loadPost(Db, Id),?assertEqual(Decoded, LoadedPost),Hash = proplists:get_value(hash, Header),{ok, LoadedByHash} = db:loadPost(Db, Hash),?assertEqual(Decoded, LoadedByHash)end,lists:foreach(Test, Examples),
ExpectedCols = ["id", "hash", "type", "timestamp", "public_key", "signature", "channel", "text", "topic", "deletedHashes", "infos"],[{columns, ExpectedCols}, {rows, [Row]}] = sqlite3:read(Db, posts, {id, IntId}),{IntId, {blob, Hash}, Type, Timestamp, {blob, PubKey}, {blob, Sig}, Channel, TextBlob, Topic, Deleteds, Infos} = Row,[{columns, ["source", "parent"]}, {rows, LinkRows}] = sqlite3:read(Db, links, {source, {blob, Hash}}),Links = [Parent || {{blob, _Source}, {blob, Parent}} <-LinkRows],[io:format("[DEBUG] Links:~p~n", [I]) || I <- Links],Header = [ {public_key, PubKey}, {signature, Sig}, {links, Links}, {type, Type}, {timestamp, Timestamp}, {hash, Hash}],Body = case Type of0 ->{blob, Text} = TextBlob,%% TODO: utf8 channels[{channel,binary_to_list(Channel)}, {text, Text}];_ -> io:format("TODO type: ~p~n", [Type])end,{reply, {ok, [Header, Body]}, State};
Res = sqlite3:read(Db, posts, {id, IntId}),Post = unpackPost(Db, Res),{reply, {ok, Post}, State};
unpackPost(Db, ReadResult) ->ExpectedCols = ["id", "hash", "type", "timestamp", "public_key", "signature", "channel", "text", "topic", "deletedHashes", "infos"],[{columns, ExpectedCols}, {rows, [Row]}] = ReadResult,{_RowId, {blob, Hash}, Type, Timestamp, {blob, PubKey}, {blob, Sig},ChannelBlob, TextBlob, TopicBlob, Deleteds, InfosBlob} = Row,[{columns, ["source", "parent"]}, {rows, LinkRows}] = sqlite3:read(Db, links, {source, {blob, Hash}}),Links = [Parent || {{blob, _Source}, {blob, Parent}} <-LinkRows],[io:format("[DEBUG] Links:~p~n", [I]) || I <- Links],Header = [ {public_key, PubKey}, {signature, Sig}, {links, Links}, {type, Type}, {timestamp, Timestamp}, {hash, Hash}],%% TODO: utf8 channelsBody = case Type of0 ->{blob, Text} = TextBlob,{blob, Channel} = ChannelBlob,[{channel, Channel}, {text, Text}];1 ->{blob, DeletedConcat} = Deleteds,[{hashes, split_concated_hashes(DeletedConcat)}];2 ->{blob, InfosJson} = InfosBlob,[{infos, jsone:decode(InfosJson)}];3 ->{blob, Topic} = TopicBlob,{blob, Channel} = ChannelBlob,[{channel, Channel}, {topic, Topic}];4 ->{blob, Channel} = ChannelBlob,[{channel, Channel}];5 ->{blob, Channel} = ChannelBlob,[{channel, Channel}]end,[Header, Body].
split_concated_hashes(Hashes) when is_binary(Hashes) ->split_concated_hashes(Hashes, []).split_concated_hashes(Bin, Acc) when byte_size(Bin) > 0 -><<H:32/binary, Rest/binary>> = Bin,split_concated_hashes(Rest, Acc ++ [H]);split_concated_hashes(<<>>, Acc) ->Acc.