Friendiqa/source-android/js/news.js
2017-11-07 21:57:40 +01:00

426 lines
26 KiB
JavaScript

.pragma library
.import QtQuick.LocalStorage 2.0 as Sql
.import "qrc:/js/helper.js" as Helperjs
function requestFriends(login,database,rootwindow,callback){
// return array of friends
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
var result = tx.executeSql('UPDATE contacts SET isFriend=0 where username="'+login.username+'"')}); // clean old friends
Helperjs.friendicaRequest(login,"/api/statuses/friends", rootwindow,function (obj){
var friends=JSON.parse(obj);
for (var i=0;i<friends.length;i++){ friends[i].isFriend=1}
callback(friends)
});
}
function requestGroups(login,database,rootwindow,callback){
// retrieve, save and return groups. Other features currently not implemented
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
Helperjs.friendicaRequest(login,"/api/friendica/group_show",rootwindow, function (obj){
var groups=JSON.parse(obj);
db.transaction( function(tx) {
var result = tx.executeSql('DELETE from groups where username="'+login.username+'"'); // clean old groups
for (var i=0;i<groups.length;i++){
var memberarray=[]; for (var user in groups[i].user){memberarray.push(parseInt(groups[i].user[user].id))}
//print("Members: "+groups[i].user)
var result2 = tx.executeSql('INSERT INTO groups VALUES (?,?,?,?)', [login.username,groups[i].name,groups[i].gid,JSON.stringify(memberarray)])}
callback()
});
})}
function listFriends(login,database,callback){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
var result = tx.executeSql('SELECT * from contacts WHERE username="'+login.username+'" AND isFriend=1'); // check for friends
var contactlist=[];
for (var i=0;i<result.rows.length;i++){
contactlist.push(result.rows.item(i))
}
callback(contactlist)
});
}
function deleteGroup(login,database,rootwindow,group, callback){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
Helperjs.friendicaPostRequest(login,"/api/friendica/group_delete?gid="+group.gid+"&name="+group.groupname,"","POST",rootwindow, function (obj){
var deletereturn=JSON.parse(obj);
if(deletereturn.success){
db.transaction( function(tx) {
var result = tx.executeSql('DELETE from groups where username="'+login.username+'" AND groupname="'+group.name+'"'); // delete group
callback()
});
}})}
function getFriendsTimeline(login,database,contacts,onlynew,rootwindow,callback){
// retrieve and return timeline since last news, return contacts which are not friends and older than 2 days for update (friends can be updated in Contactstab)
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
var parameter = "?count=50";
if(onlynew){db.transaction( function(tx) {
var result = tx.executeSql('SELECT status_id from news WHERE username="'+login.username+'" AND messagetype=0 ORDER BY status_id DESC LIMIT 1'); // check for last news id
try{parameter=parameter+"&since_id="+result.rows.item(0).status_id;}catch(e){};})}
var newContacts=[];
Helperjs.friendicaRequest(login,"/api/statuses/friends_timeline"+parameter, rootwindow,function (obj){//print("newsjs "+JSON.stringify(contacts));
var news=JSON.parse(obj);
var newContacts=findNewContacts(news,contacts);
callback(news,newContacts)
})}
function getCurrentContacts(login,database,callback){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
var contactlist=[];
db.transaction( function(tx) {
var result = tx.executeSql('SELECT url from contacts WHERE username="'+login.username+'" AND isFriend=1'); // check for friends
for (var i=0;i<result.rows.length;i++){
contactlist.push(result.rows.item(i).url )
//print(result.rows.item(i).url)
}
var lastDate=Date.now()-604800000;// 7 days old
//print('SELECT url from contacts WHERE username="'+login.username+'" AND isFriend=0 AND imageAge>'+lastDate);
var result2 = tx.executeSql('SELECT url from contacts WHERE username="'+login.username+'" AND isFriend=0 AND imageAge > '+lastDate);
for (var j=0;j<result2.rows.length;j++){
contactlist.push(result2.rows.item(j).url )
}
})
callback(contactlist)
}
function findNewContacts(news,contacts){
var newContacts=[];
for (var i=0;i<news.length;i++){
var url=news[i].user.url;
if(contacts.indexOf(url)==-1 && !(inArray(newContacts,"url",url))){
news[i].user.isFriend=0;
newContacts.push(news[i].user);
}
if (news[i].friendica_activities.like.length>0){
for (var j=0;j<news[i].friendica_activities.like.length;j++){
var like_url=news[i].friendica_activities.like[j].url;
if(contacts.indexOf(like_url)==-1 && !(inArray(newContacts,"url",like_url))){
news[i].friendica_activities.like[j].isFriend=0;
newContacts.push(news[i].friendica_activities.like[j]);
}
}
}
if (news[i].friendica_activities.dislike.length>0){
for (var k=0;j<news[k].friendica_activities.dislike.length;k++){
var dislike_url=news[i].friendica_activities.dislike[k].url;
if(contacts.indexOf(dislike_url)==-1 && !(inArray(newContacts,"url",dislike_url))){
news[i].friendica_activities.dislike[k].isFriend=0;
newContacts.push(news[i].friendica_activities.dislike[k]);
}
}
}
var owner_url=news[i].friendica_owner.url;
if(contacts.indexOf(owner_url)==-1 && !(inArray(newContacts,"url",owner_url))){
news[i].friendica_owner.isFriend=0;
newContacts.push(news[i].friendica_owner);
}
}
return newContacts
}
function storeNews(login,database,news,rootwindow,callback){
// save news after contacts download, call next function
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
for (var i=0;i<news.length;i++){
//print('store news data for ' + news[i].id+JSON.stringify(news[i].friendica_activities));
//var ausdruck=news[i];
var likearray=[]; for (var user in news[i].friendica_activities.like){likearray.push(news[i].friendica_activities.like[user].url)}
var dislikearray=[]; for (var user in news[i].friendica_activities.dislike){dislikearray.push(news[i].friendica_activities.dislike[user].url)}
var attendyesarray=[]; for (var user in news[i].friendica_activities.attendyes){attendyesarray.push(news[i].friendica_activities.attendyes[user].url)}
var attendnoarray=[]; for (var user in news[i].friendica_activities.attendno){attendnoarray.push(news[i].friendica_activities.attendno[user].url)}
var attendmaybearray=[]; for (var user in news[i].friendica_activities.attendmaybe){attendmaybearray.push(news[i].friendica_activities.attendmaybe[user].url)}
var friendica_activities=[likearray,dislikearray,attendyesarray,attendnoarray,attendmaybearray]
var attachments="";if (news[i].attachments){attachments=Qt.btoa(JSON.stringify(news[i].attachments))}
db.transaction( function(tx) {
var result = tx.executeSql('SELECT * from news where username="'+login.username+'" AND status_id = "'+news[i].id+'" AND messagetype=0'); // check for news id
if(result.rows.length === 1) {// use update
//print(news[i].id +' news exists, update it'+'UPDATE news SET username="'+login.username+'", messagetype=0, text="'+Qt.btoa(news[i].text)+'", created_at="'+Date.parse(cleanDate(news[i].created_at))+'", in_reply_to_status_id="'+news[i].in_reply_to_status_id+'", source="'+news[i].source+'", status_id="'+news[i].id+'", in_reply_to_user_id="'+news[i].in_reply_to_user_id+'", geo="'+news[i].geo+'", favorited="'+news[i].favorited+'", uid="'+news[i].user.id+'", statusnet_html="'+Qt.btoa(news[i].status_html)+'", statusnet_conversation_id="'+news[i].statusnet_conversation_id+'",friendica_activities="'+Qt.btoa(JSON.stringify(friendica_activities))+'",attachments="'+attachments+'",friendica_owner="'+news[i].friendica_owner.url+'" where username="'+login.username+'" AND status_id="'+news[i].status_id+'" AND messagetype=0')
result = tx.executeSql('UPDATE news SET username="'+login.username+'", messagetype=0, text="'+Qt.btoa(news[i].text)+'", created_at="'+Date.parse(cleanDate(news[i].created_at))+'", in_reply_to_status_id="'+news[i].in_reply_to_status_id+'", source="'+news[i].source+'", status_id="'+news[i].id+'", in_reply_to_user_id="'+news[i].in_reply_to_user_id+'", geo="'+news[i].geo+'", favorited="'+news[i].favorited+'", uid="'+news[i].user.id+'", statusnet_html="'+Qt.btoa(news[i].status_html)+'", statusnet_conversation_id="'+news[i].statusnet_conversation_id+'",friendica_activities="'+Qt.btoa(JSON.stringify(friendica_activities))+'",attachments="'+attachments+'",friendica_owner="'+news[i].friendica_owner.url+'" where username="'+login.username+'" AND status_id="'+news[i].status_id+'" AND messagetype=0');
} else {// use insert
result = tx.executeSql('INSERT INTO news (username,messagetype,text,created_at,in_reply_to_status_id,source,status_id,in_reply_to_user_id,geo,favorited,uid,statusnet_html,statusnet_conversation_id,friendica_activities,friendica_activities_self,attachments,friendica_owner) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', [login.username,0,Qt.btoa(news[i].text),Date.parse(cleanDate(news[i].created_at)), news[i].in_reply_to_status_id, news[i].source, news[i].id,news[i].in_reply_to_user_id,news[i].geo,news[i].favorited, news[i].user.id,Qt.btoa(news[i].statusnet_html),news[i].statusnet_conversation_id, Qt.btoa(JSON.stringify(friendica_activities)),"[]",attachments,news[i].friendica_owner.url])}})
}
getDirectMessage(login,database,rootwindow,callback)
}
function getDirectMessage(login,database,rootwindow,callback){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
var parameter = "";
db.transaction( function(tx) {
var result = tx.executeSql('SELECT status_id from news WHERE username="'+login.username+'" AND messagetype=1 ORDER BY status_id DESC LIMIT 1'); // check for last news id
try{parameter="&since_id="+result.rows.item(0).status_id;}catch(e){};})
Helperjs.friendicaRequest(login,"/api/direct_messages/all"+parameter,rootwindow, function (obj){
var messages=JSON.parse(obj);
for (var i=0;i<messages.length;i++){
//print('store message data for '+JSON.stringify(messages[i]));
db.transaction( function(tx) {
var result = tx.executeSql('SELECT * from news where username= "'+login.username+'" AND status_id = "'+messages[i].id+'" AND messagetype=1'); // check for news id
if(result.rows.length === 1) {// use update
print(messages[i].id +' directmessage exists, update it')
result = tx.executeSql('UPDATE news SET username="'+login.username+'", messagetype=1, text="'+Qt.btoa(messages[i].text)+'", created_at="'+Date.parse(cleanDate(messages[i].created_at))+'", source="Friendica", status_id="'+messages[i].id+'", uid="'+messages[i].sender.id+'", statusnet_html="'+Qt.btoa(messages[i].text)+'", statusnet_conversation_id="'+messages[i].friendica_parent_uri+'" where username="'+login.username+'" AND status_id="'+messages[i].status_id+'" AND messagetype=1');
} else {// use insert
result = tx.executeSql('INSERT INTO news (username,messagetype,text,created_at,source,status_id,uid,statusnet_html,statusnet_conversation_id) VALUES (?,?,?,?,?,?,?,?,?)', [login.username,1,Qt.btoa(messages[i].text),Date.parse(cleanDate(messages[i].created_at)), "Friendica", messages[i].id, messages[i].sender.id,Qt.btoa(messages[i].text),messages[i].friendica_parent_uri])}
});
}
})
if(login.newsViewType=="Timeline"){newsfromdb(database,login.username,callback)}
else{chatsfromdb(database,login.username,callback)}
}
function getNotifications(login,database,rootwindow,callback){
Helperjs.friendicaRequest(login,"/api/friendica/notifications", rootwindow,function (obj){
var messages=JSON.parse(obj);
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
for (var i=0;i<messages.length;i++){
//print('store message data for '+JSON.stringify(messages[i]));
db.transaction( function(tx) {
var result = tx.executeSql('SELECT * from news where username="'+login.username+'" AND status_id = "'+messages[i].id+'" AND messagetype=2'); // check for news id
if(result.rows.length === 1) {// use update
print(messages[i].id +' Notification exists, update it')
result = tx.executeSql('UPDATE news SET username="'+login.username+'", messagetype=2, text="'+Qt.btoa(messages[i].msg_html)+'", created_at="'+Date.parse(messages[i].date)+'", source="Friendica", status_id="'+messages[i].id+'", uid="'+messages[i].uid+'", statusnet_html="'+Qt.btoa(messages[i].msg_html)+'", statusnet_conversation_id="'+messages[i].parent+'" where username="'+login.username+'" AND status_id="'+messages[i].id+'" AND messagetype=2');
} else {// use insert
result = tx.executeSql('INSERT INTO news (username,messagetype,text,created_at,source,status_id, uid,statusnet_html,statusnet_conversation_id) VALUES (?,?,?,?,?,?,?,?,?)', [login.username,2,Qt.btoa(messages[i].msg_html),Date.parse(messages[i].date),"Friendica", messages[i].id, messages[i].uid,Qt.btoa(messages[i].msg_html),messages[i].parent])}
});
}
db.transaction( function(tx) {
var newsrs=tx.executeSql('select * from news WHERE username="'+login.username+'" AND messagetype=2 ORDER BY status_id DESC LIMIT 20');
var newsArray=[];
for(var j = 0; j < newsrs.rows.length; j++) {
newsArray.push(newsrs.rows.item(j));
callback(newsArray);
}
})
})}
function getActivitiesUserData(database,username,userUrlArray){//print(JSON.stringify(userUrlArray));
var helpArray=[];
for (var i=0;i<userUrlArray.length;i++){
Helperjs.readData(database,"contacts",username,function(userdata){
helpArray.push(userdata[0]);
},"url",userUrlArray[i]);
}
return helpArray
}
function newsfromdb(database,username,callback,contact,stop_time){
// return news before stop_time (used by More button), in brackets of 20 entries, or by specified contact
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
if (!stop_time){var stop="";
try{var rs = tx.executeSql('select created_at from news WHERE username="'+username+'" ORDER BY created_at DESC LIMIT 1');
stop="<="+rs.rows.item(0).created_at}catch(e){stop="<99999999999999"}}
else{var stop="<"+stop_time}
var contactfilter="";if(contact){contactfilter=" AND uid='"+contact+"'"}
//print('select * from news WHERE username="'+username+'" AND created_at'+stop+contactfilter+' ORDER BY created_at DESC LIMIT 20');
var newsrs=tx.executeSql('select * from news WHERE username="'+username+'" AND created_at'+stop+contactfilter+' ORDER BY created_at DESC LIMIT 20');
var newsArray=[];
for(var i = 0; i < newsrs.rows.length; i++) {
newsArray.push(newsrs.rows.item(i));
newsArray[i]=fetchUsersForNews(database,username,newsArray[i])
}
callback(newsArray)});
}
function fetchUsersForNews(database,username,news){//print(JSON.stringify(news))
Helperjs.readData(database,"contacts",username,function(userdata){
news.user=userdata[0];
//print("Fetch user"+JSON.stringify(news.user));
},"id",news.uid);
if(news.in_reply_to_user_id){
Helperjs.readData(database,"contacts",username,function(replytodata){
news.reply_user=replytodata[0];
//print("Fetch reply to"+JSON.stringify(news.reply_user));
},"id",news.in_reply_to_user_id);
}
if (news.messagetype==0){
for(var j=0;j<news.friendica_activities.length;j++)
{var friendicaArray=JSON.parse(Qt.atob(news.friendica_activities));
// print("Array: "+friendicaArray[1]);
news.like=getActivitiesUserData(database,username,friendicaArray[0]);
news.dislike=getActivitiesUserData(database,username,friendicaArray[1]);
news.attendyes=getActivitiesUserData(database,username,friendicaArray[2]);
news.attendno=getActivitiesUserData(database,username,friendicaArray[3]);
news.attendmaybe=getActivitiesUserData(database,username,friendicaArray[4]);
}
Helperjs.readData(database,"contacts",username,function(friendica_owner_data){
news.friendica_owner_object=friendica_owner_data[0];
//print("Fetch friendica_owner"+JSON.stringify(news.friendica_owner));
},"url",news.friendica_owner);
}
return news
}
function deleteNews(login,database,newsid,messagetype,rootwindow,callback){
var api="" ;
if (messagetype==0){ api="/api/statuses/destroy?id="}
else if (messagetype==1){ api="/api/direct_messages/destroy?id="}
else if (messagetype==2){ api="/api/friendica/notifications/seen?id="}
Helperjs.friendicaPostRequest(login,api+newsid,"","POST", rootwindow,function (obj){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {var result = tx.executeSql('DELETE from news where username="'+login.username+'" AND messagetype='+messagetype+' AND status_id ='+newsid); // delete news id
callback(obj)
});
})}
function retweetNews(login,database,newsid,rootwindow,callback){
Helperjs.friendicaPostRequest(login,"/api/statuses/retweet?id="+newsid,"","POST", rootwindow,function (obj){
var answer=JSON.parse(obj);
if(answer.status.error){Helperjs.showMessage("Repost",answer.status.code,rootwindow);}
else{Helperjs.showMessage("Repost",obj,rootwindow)}
})}
function favorite(login,favorite,newsid,rootwindow){
// toggle favorites
if(favorite){ Helperjs.friendicaPostRequest(login,"/api/favorites/create?id="+newsid,"","POST", rootwindow,function (obj){
})}
else {Helperjs.friendicaPostRequest(login,"/api/favorites/destroy?id="+newsid, "POST",rootwindow,function (obj){
})}
}
function likerequest(login,database,verb,newsid,rootwindow){
Helperjs.friendicaPostRequest(login,"/api/friendica/activity/"+verb+"?id="+newsid, "","POST",rootwindow,function (obj){
if (obj=='"ok"'){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
var currentActivities_rs=tx.executeSql('select friendica_activities_self from news WHERE username="'+login.username+'" AND status_id='+newsid) ;
var currentActivities=JSON.parse(currentActivities_rs.rows.item(0).friendica_activities_self);
//print(verb+"currentActivities "+JSON.stringify(currentActivities));
if ((verb=="like")&&(currentActivities.indexOf(1)==-1)){ currentActivities.push(1);
if (currentActivities.indexOf(2)!=-1){currentActivities.splice(currentActivities.indexOf(2),1)}
}
if ((verb=="dislike")&&(currentActivities.indexOf(2)==-1)){ currentActivities.push(2);
if (currentActivities.indexOf(1)!=-1){currentActivities.splice(currentActivities.indexOf(1),1)}
}
if (verb=="unlike"){ if (currentActivities.indexOf(1)!=-1){currentActivities.splice(currentActivities.indexOf(1),1)}}
if (verb=="undislike"){ if (currentActivities.indexOf(2)!=-1){currentActivities.splice(currentActivities.indexOf(2),1)}}
//print(JSON.stringify(currentActivities));
var result = tx.executeSql('UPDATE news SET friendica_activities_self ="'+JSON.stringify(currentActivities)+'" where username="'+login.username+'" AND status_id ='+newsid);
})}
else{print("likerequest"+obj)}})
}
function like(login,database,toggle,verb,newsid,rootwindow){
if(verb=="like"&& toggle==1){likerequest(login,database,"like",newsid,rootwindow);
}
if(verb=="dislike"&& toggle==1){likerequest(login,database,"dislike",newsid,rootwindow);
}
if(toggle==0){
likerequest(login,database,"un"+verb,newsid,rootwindow);}
}
function attend(login,database,attend,newsid,rootwindow,callback){
Helperjs.friendicaPostRequest(login,"/api/friendica/activity/attend"+attend+"?id="+newsid, "","POST",rootwindow,function (obj){
//print("attend: "+attend+obj);
if (obj=='"ok"')
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
var currentActivities_rs=tx.executeSql('select friendica_activities_self from news WHERE username="'+login.username+'" AND status_id='+newsid) ;
var currentActivities=JSON.parse(currentActivities_rs.rows.item(0).friendica_activities_self);
if ((attend=="yes")&&(currentActivities.indexOf(3)==-1)){
currentActivities.push(3);
if (currentActivities.indexOf(4)!=-1){currentActivities.splice(currentActivities.indexOf(4),1)}
if (currentActivities.indexOf(5)!=-1){currentActivities.splice(currentActivities.indexOf(5),1)}
//print(JSON.stringify(currentActivities));
}
if ((attend=="no")&&(currentActivities.indexOf(4)==-1)){
currentActivities.push(4);
if (currentActivities.indexOf(3)!=-1){currentActivities.splice(currentActivities.indexOf(3),1)}
if (currentActivities.indexOf(5)!=-1){currentActivities.splice(currentActivities.indexOf(5),1)}
}
if ((attend=="maybe")&&(currentActivities.indexOf(5)==-1)){
currentActivities.push(5);
if (currentActivities.indexOf(3)!=-1){currentActivities.splice(currentActivities.indexOf(3),1)}
if (currentActivities.indexOf(4)!=-1){currentActivities.splice(currentActivities.indexOf(4),1)}
}
var result = tx.executeSql('UPDATE news SET friendica_activities_self ="'+JSON.stringify(currentActivities)+'" where username="'+login.username+'" AND status_id ='+newsid);
callback();
})})}
function requestConversation(login,database,newsid,contacts,rootwindow,callback){
Helperjs.friendicaRequest(login,"/api/conversation/show?id="+newsid,rootwindow, function (obj){
var news=JSON.parse(obj);
var newContacts=findNewContacts(news,contacts);
// storeNews(login,database,news,rootwindow,callback)
callback(news,newContacts)
})}
function conversationfromdb(database,user,conversationId,callback){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
var newsrs=tx.executeSql('select * from news WHERE username="'+user+'" AND statusnet_conversation_id="'+conversationId+'" ORDER BY created_at ASC');
var newsArray=[];
for(var i = 0; i < newsrs.rows.length; i++) {
newsArray.push(newsrs.rows.item(i));
newsArray[i]=fetchUsersForNews(database,user,newsArray[i])
}
callback(newsArray);} )}
function requestFavorites(login,database,contacts,rootwindow,callback){
Helperjs.friendicaRequest(login,"/api/favorites",rootwindow, function (obj){
//print(obj+JSON.stringify(obj));
var news=JSON.parse(obj);
var newContacts=findNewContacts(news,contacts);
// storeNews(login,database,news,rootwindow,callback)
callback(news,newContacts)
})}
function favoritesfromdb(database,user,callback){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
//print('select * from news WHERE username="'+user+'" AND favorited=1 ORDER BY status_id DESC');
var newsrs=tx.executeSql('select * from news WHERE username="'+user+'" AND favorited=1 ORDER BY status_id DESC');
var newsArray=[];
for(var i = 0; i < newsrs.rows.length; i++) {
newsArray.push(newsrs.rows.item(i));
newsArray[i]=fetchUsersForNews(database,user,newsArray[i]);
callback(newsArray);
}})}
function chatsfromdb(database,user,callback,stop_time){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
if (!stop_time){var stop="";
try{var rs = tx.executeSql('select created_at from news WHERE username="'+username+'" ORDER BY created_at DESC LIMIT 1');
stop="<="+rs.rows.item(0).created_at}catch(e){stop="<99999999999999"}}
else{var stop="<"+stop_time}
var conversationsrs=tx.executeSql('select DISTINCT statusnet_conversation_id from news WHERE username="'+user+'" AND created_at'+stop+' ORDER BY created_at DESC LIMIT 20'); //+' ORDER BY created_at DESC LIMIT 20');
var conversations=[];
for(var i = 0; i < conversationsrs.rows.length; i++) {
conversations.push(conversationsrs.rows.item(i).statusnet_conversation_id);
}
var newsArray=[];
for(var j = 0; j< conversations.length; j++) {
var newsrs=tx.executeSql('select * from news WHERE username="'+user+'" AND statusnet_conversation_id="'+conversations[j] +'" ORDER BY created_at ASC');
var helpernews=newsrs.rows.item(0);
helpernews.newscount=newsrs.rows.length;
helpernews=fetchUsersForNews(database,user,helpernews);
//var chatArray=[];
// for (var k=0;k<newsrs.rows.length;k++){
// var helperchat=newsrs.rows.item(k);
// helperchat=fetchUsersForNews(database,user,helperchat);
// chatArray.push(helperchat)
//}
//helpernews.chatArray=chatArray;
newsArray.push(helpernews);
}
callback(newsArray);
})}
function inArray(list, prop, val) {
if (list.length > 0 ) {
for (var i in list) {if (list[i][prop] == val) {
return true;
}
}
} return false;
}
function cleanDate(date){
var cleanedDate= date.slice(0,3)+", "+date.slice(8,11)+date.slice(4,7)+date.slice(25,30)+date.slice(10,25);
return cleanedDate
}