From 3e64454b7d85c5f2834358880e18b520017c373a Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Sat, 26 Oct 2013 13:42:50 -0200 Subject: [PATCH] Save lastk field to post so torrent-less navigation through posts is possible. => DONE --- TODO | 2 +- src/twister.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/TODO b/TODO index e730e042..2a912581 100644 --- a/TODO +++ b/TODO @@ -58,7 +58,7 @@ Key pair might have changed and currently we receive a lot of errors from other - save_file() must truncate file. -- Save lastk field to post so torrent-less navigation through posts is possible. +- Save lastk field to post so torrent-less navigation through posts is possible. => DONE - Implement dht-to-torrent gateway, the "swarm" resource (so poster may not need to be member of his own torrent) diff --git a/src/twister.cpp b/src/twister.cpp index 9ca6f429..c4b6b598 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -1024,6 +1024,34 @@ Value dhtget(const Array& params, bool fHelp) return ret; } +int findLastPublicPostLocalUser( std::string strUsername ) +{ + int lastk = -1; + + LOCK(cs_twister); + if( strUsername.size() && m_userTorrent.count(strUsername) && + m_userTorrent[strUsername].is_valid() ){ + + std::vector pieces; + int max_id = std::numeric_limits::max(); + int since_id = -1; + m_userTorrent[strUsername].get_pieces(pieces, 1, max_id, since_id, USERPOST_FLAG_RT); + + if( pieces.size() ) { + string const& piece = pieces.front(); + lazy_entry v; + int pos; + error_code ec; + if (lazy_bdecode(piece.data(), piece.data()+piece.size(), v, ec, &pos) == 0) { + lazy_entry const* post = v.dict_find_dict("userpost"); + lastk = post->dict_find_int_value("k",-1); + } + } + } + return lastk; +} + + Value newpostmsg(const Array& params, bool fHelp) { if (fHelp || (params.size() != 3 && params.size() != 5)) @@ -1047,6 +1075,11 @@ Value newpostmsg(const Array& params, bool fHelp) } entry v; + // [MF] Warning: findLastPublicPostLocalUser requires that we follow ourselves + int lastk = findLastPublicPostLocalUser(strUsername); + if( lastk >= 0 ) + v["userpost"]["lastk"] = lastk; + if( !createSignedUserpost(v, strUsername, k, strMsg, NULL, NULL, NULL, strReplyN, replyK) ) @@ -1166,6 +1199,11 @@ Value newrtmsg(const Array& params, bool fHelp) entry const *sig_rt= vrt.find_key("sig_userpost"); entry v; + // [MF] Warning: findLastPublicPostLocalUser requires that we follow ourselves + int lastk = findLastPublicPostLocalUser(strUsername); + if( lastk >= 0 ) + v["userpost"]["lastk"] = lastk; + if( !createSignedUserpost(v, strUsername, k, "", rt, sig_rt, NULL, std::string(""), 0) )