Store last tweet ID in database so all tweets are not forwarded on reconnect. Send proper timestamp of tweets

This commit is contained in:
HanzZ 2012-11-27 22:18:15 +01:00
parent a76d99e4b6
commit 9388d59915
3 changed files with 50 additions and 12 deletions

View file

@ -322,7 +322,7 @@ void TwitterPlugin::pollForTweets()
std::set<std::string>::iterator it = onlineUsers.begin();
while(it != onlineUsers.end()) {
std::string user = *it;
tp->runAsThread(new TimelineRequest(userdb[user].sessions, user, "", userdb[user].mostRecentTweetID,
tp->runAsThread(new TimelineRequest(userdb[user].sessions, user, "", getMostRecentTweetIDUnsafe(user),
boost::bind(&TwitterPlugin::displayTweets, this, _1, _2, _3, _4)));
it++;
}
@ -517,14 +517,39 @@ void TwitterPlugin::updateLastTweetID(const std::string user, const std::string
{
boost::mutex::scoped_lock lock(userlock);
userdb[user].mostRecentTweetID = ID;
UserInfo info;
if(storagebackend->getUser(user, info) == false) {
LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
return;
}
storagebackend->updateUserSetting((long)info.id, "twitter_last_tweet", ID);
}
std::string TwitterPlugin::getMostRecentTweetIDUnsafe(const std::string user)
{
std::string ID = "";
if(onlineUsers.count(user)) {
ID = userdb[user].mostRecentTweetID;
if (ID.empty()) {
int type;
UserInfo info;
if(storagebackend->getUser(user, info) == false) {
LOG4CXX_ERROR(logger, "Didn't find entry for " << user << " in the database!")
}
else {
storagebackend->getUserSetting(info.id, "twitter_last_tweet", type, ID);
}
}
}
return ID;
}
std::string TwitterPlugin::getMostRecentTweetID(const std::string user)
{
boost::mutex::scoped_lock lock(userlock);
std::string ID = "-1";
if(onlineUsers.count(user)) ID = userdb[user].mostRecentTweetID;
return ID;
{
boost::mutex::scoped_lock lock(userlock);
return getMostRecentTweetIDUnsafe(user);
}
void TwitterPlugin::updateLastDMID(const std::string user, const std::string ID)
@ -622,16 +647,17 @@ void TwitterPlugin::displayTweets(std::string &user, std::string &userRequested,
std::map<std::string, int> lastTweet;
std::map<std::string, int>::iterator it;
for(int i=0 ; i<tweets.size() ; i++) {
for(int i = tweets.size() - 1 ; i >= 0 ; i--) {
if(userdb[user].twitterMode != CHATROOM) {
timeline += " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + " (MsgId: " + tweets[i].getID() + ")\n";
std::string m = " - " + tweets[i].getUserData().getScreenName() + ": " + tweets[i].getTweet() + " (MsgId: " + tweets[i].getID() + ")\n";
handleMessage(user, adminLegacyName, m, "", "", tweets[i].getCreationTime());
std::string scrname = tweets[i].getUserData().getScreenName();
if(lastTweet.count(scrname) == 0 || cmp(tweets[lastTweet[scrname]].getID(), tweets[i].getID()) <= 0) lastTweet[scrname] = i;
} else {
handleMessage(user, userdb[user].twitterMode == CHATROOM ? adminChatRoom : adminLegacyName,
tweets[i].getTweet() + " (MsgId: " + tweets[i].getID() + ")", tweets[i].getUserData().getScreenName());
tweets[i].getTweet() + " (MsgId: " + tweets[i].getID() + ")", tweets[i].getUserData().getScreenName(), "", tweets[i].getCreationTime());
}
}

View file

@ -134,6 +134,8 @@ class TwitterPlugin : public NetworkPlugin {
/***********************************************************************************/
private:
std::string getMostRecentTweetIDUnsafe(const std::string user);
enum status {NEW, WAITING_FOR_PIN, CONNECTED, DISCONNECTED};
enum mode {SINGLECONTACT, MULTIPLECONTACT, CHATROOM};

View file

@ -11,6 +11,15 @@ static std::string tolowercase(std::string inp)
return out;
}
static std::string toIsoTime(std::string in) {
time_t now = time(0);
struct tm *mtime = gmtime(&now);
strptime(in.c_str(), "%a %b %d %H:%M:%S %z %Y", mtime);
char buf[80];
strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", mtime);
return buf;
}
EmbeddedStatus getEmbeddedStatus(const Swift::ParserElement::ref &element, const std::string xmlns)
{
EmbeddedStatus status;
@ -19,7 +28,8 @@ EmbeddedStatus getEmbeddedStatus(const Swift::ParserElement::ref &element, const
return status;
}
status.setCreationTime( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) );
status.setCreationTime( toIsoTime(std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ) );
status.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
status.setTweet( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) );
status.setTruncated( std::string( element->getChild(TwitterReponseTypes::truncated, xmlns)->getText() )=="true" );
@ -60,7 +70,7 @@ Status getStatus(const Swift::ParserElement::ref &element, const std::string xml
return status;
}
status.setCreationTime( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) );
status.setCreationTime( toIsoTime ( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ) );
status.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
status.setTweet( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) );
status.setTruncated( std::string( element->getChild(TwitterReponseTypes::truncated, xmlns)->getText() )=="true" );
@ -82,7 +92,7 @@ DirectMessage getDirectMessage(const Swift::ParserElement::ref &element, const s
return DM;
}
DM.setCreationTime( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) );
DM.setCreationTime( toIsoTime ( std::string( element->getChild(TwitterReponseTypes::created_at, xmlns)->getText() ) ) );
DM.setID( std::string( element->getChild(TwitterReponseTypes::id, xmlns)->getText() ) );
DM.setMessage( std::string( element->getChild(TwitterReponseTypes::text, xmlns)->getText() ) );
DM.setSenderID( std::string( element->getChild(TwitterReponseTypes::sender_id, xmlns)->getText() ) );