Friendiqa/source-linux/js/news.js

599 lines
36 KiB
JavaScript
Raw Normal View History

2018-02-19 22:36:00 +01:00
// This file is part of Friendiqa
2018-11-09 22:06:13 +01:00
// https://git.friendi.ca/lubuwest/Friendiqa
2018-02-19 22:36:00 +01:00
// Copyright (C) 2017 Marco R. <thomasschmidt45@gmx.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations including
// the two.
//
// You must obey the GNU General Public License in all respects for all
// of the code used other than OpenSSL. If you modify file(s) with this
// exception, you may extend this exception to your version of the
// file(s), but you are not obligated to do so. If you do not wish to do
// so, delete this exception statement from your version. If you delete
// this exception statement from all source files in the program, then
// also delete it here.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2017-01-26 21:55:31 +01:00
.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
2018-07-01 15:09:21 +02:00
Helperjs.friendicaRequest(login,"/api/statuses/friends?count=9999", rootwindow,function (obj){
2017-01-26 21:55:31 +01:00
var friends=JSON.parse(obj);
2018-10-01 21:17:54 +02:00
for (var i=0;i<friends.length;i++){
friends[i].created_at=Date.parse(cleanDate(friends[i].created_at));
friends[i].isFriend=1
}
2018-02-19 22:36:00 +01:00
//try{requestProfile(login,friends,rootwindow,function(friends_profile){callback(friends_profile)})}
//catch(e){
callback(friends)//}
2017-01-26 21:55:31 +01:00
});
}
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]);
2018-02-19 22:36:00 +01:00
Helperjs.friendicaRequest(login,"/api/friendica/group_show",rootwindow, function (obj){
2017-01-26 21:55:31 +01:00
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()
});
})}
2019-06-25 20:59:10 +02:00
function listFriends(login,database,callback,filter){
2017-11-07 21:57:40 +01:00
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
2019-06-25 20:59:10 +02:00
var filtertext='';
2019-12-10 21:12:32 +01:00
//try {filtertext=' AND screen_name like "' + filter+'%"'}catch(e){}
var filtertext=new RegExp(".*"+filter.toLowerCase()+".*")
// if (filter){filtertext=' AND (screen_name like "' + filter+'%" OR name like "' + Qt.btoa(filter)+'%")'}
2019-06-25 20:59:10 +02:00
db.transaction( function(tx) {
//var result = tx.executeSql('SELECT * from contacts WHERE username="'+login.username+'" AND isFriend>0'+filtertext);
2019-12-10 21:12:32 +01:00
var result = tx.executeSql('SELECT * from contacts WHERE username="'+login.username+'" AND isFriend>0');
2019-06-25 20:59:10 +02:00
// check for friends
var contactlist=[];
for (var i=0;i<result.rows.length;i++){
2019-12-10 21:12:32 +01:00
var contact=result.rows.item(i)
contact.name=Qt.atob(contact.name);
if(filtertext.test(contact.name.toLowerCase())|| filtertext.test(contact.screen_name.toLowerCase())){
contactlist.push(contact)}
2019-06-25 20:59:10 +02:00
}
callback(contactlist)
});
2017-11-07 21:57:40 +01:00
}
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()
});
}})}
2019-06-25 20:59:10 +02:00
function getLastNews(login,database,callback){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
var lastnewsid=0;
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');
try{lastnewsid=result.rows.item(0).status_id;}catch(e){print(e)};
callback(lastnewsid)
})
2018-07-01 15:09:21 +02:00
}
2017-11-07 21:57:40 +01:00
2018-07-01 15:09:21 +02:00
//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){
// var news=JSON.parse(obj);
// if (news.hasOwnProperty('status')){
// Helperjs.showMessage(qsTr("Error"),"API:\n" +login.server+"/api/statuses/friends_timeline"+parameter+"\n Return: \n"+obj,rootwindow)
// }
// var newContacts=findNewContacts(news,contacts);
// callback(news,newContacts)
//})}
2017-01-26 21:55:31 +01:00
function getCurrentContacts(login,database,callback){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
var contactlist=[];
db.transaction( function(tx) {
2017-03-25 23:36:14 +01:00
var result = tx.executeSql('SELECT url from contacts WHERE username="'+login.username+'" AND isFriend=1'); // check for friends
2017-01-26 21:55:31 +01:00
for (var i=0;i<result.rows.length;i++){
2017-03-25 23:36:14 +01:00
contactlist.push(result.rows.item(i).url )
//print(result.rows.item(i).url)
2017-01-26 21:55:31 +01:00
}
2017-05-11 22:15:34 +02:00
var lastDate=Date.now()-604800000;// 7 days old
2017-03-25 23:36:14 +01:00
//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);
2017-01-26 21:55:31 +01:00
for (var j=0;j<result2.rows.length;j++){
2017-03-25 23:36:14 +01:00
contactlist.push(result2.rows.item(j).url )
2017-01-26 21:55:31 +01:00
}
})
2018-07-01 15:09:21 +02:00
callback(contactlist)
2017-01-26 21:55:31 +01:00
}
2018-02-19 22:36:00 +01:00
function findNewContacts(news,contacts){
2017-01-26 21:55:31 +01:00
var newContacts=[];
for (var i=0;i<news.length;i++){
2017-03-25 23:36:14 +01:00
var url=news[i].user.url;
2017-05-11 22:15:34 +02:00
if(contacts.indexOf(url)==-1 && !(inArray(newContacts,"url",url))){
2017-01-26 21:55:31 +01:00
news[i].user.isFriend=0;
newContacts.push(news[i].user);
}
2018-07-01 15:09:21 +02:00
if (news[i].hasOwnProperty('friendica_activities') && news[i].friendica_activities.like.length>0){
2017-01-26 21:55:31 +01:00
for (var j=0;j<news[i].friendica_activities.like.length;j++){
2017-03-25 23:36:14 +01:00
var like_url=news[i].friendica_activities.like[j].url;
2017-05-11 22:15:34 +02:00
if(contacts.indexOf(like_url)==-1 && !(inArray(newContacts,"url",like_url))){
2017-01-26 21:55:31 +01:00
news[i].friendica_activities.like[j].isFriend=0;
newContacts.push(news[i].friendica_activities.like[j]);
}
}
2017-03-25 23:36:14 +01:00
}
2018-07-01 15:09:21 +02:00
if (news[i].hasOwnProperty('friendica_activities') && news[i].friendica_activities.dislike.length>0){
2017-03-25 23:36:14 +01:00
for (var k=0;j<news[k].friendica_activities.dislike.length;k++){
var dislike_url=news[i].friendica_activities.dislike[k].url;
2017-05-11 22:15:34 +02:00
if(contacts.indexOf(dislike_url)==-1 && !(inArray(newContacts,"url",dislike_url))){
2017-03-25 23:36:14 +01:00
news[i].friendica_activities.dislike[k].isFriend=0;
2017-01-26 21:55:31 +01:00
newContacts.push(news[i].friendica_activities.dislike[k]);
}
}
2017-03-25 23:36:14 +01:00
}
2018-02-19 22:36:00 +01:00
2019-06-25 20:59:10 +02:00
if(news[i].hasOwnProperty('friendica_author')){
var owner_url=news[i].friendica_author.url;
2018-07-01 15:09:21 +02:00
if(contacts.indexOf(owner_url)==-1 && !(inArray(newContacts,"url",owner_url))){
2019-06-25 20:59:10 +02:00
news[i].friendica_author.isFriend=0;
newContacts.push(news[i].friendica_author);
2018-07-01 15:09:21 +02:00
}
}
2017-01-26 21:55:31 +01:00
}
return newContacts
}
2018-07-01 15:09:21 +02:00
function storeNews(login,database,news,rootwindow){
2017-01-26 21:55:31 +01:00
// 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++){
2020-01-27 21:53:51 +01:00
//print('store news data for ' + login.username+news[i].messagetype+Qt.btoa(news[i].text)+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_author.url);
2017-03-25 23:36:14 +01:00
//var ausdruck=news[i];
2018-07-01 15:09:21 +02:00
var likearray=[];var dislikearray=[];var attendyesarray=[];var attendnoarray=[];var attendmaybearray=[];
if(news[i].hasOwnProperty('friendica_activities')){
for (var user in news[i].friendica_activities.like){likearray.push(news[i].friendica_activities.like[user].url)}
for (var user in news[i].friendica_activities.dislike){dislikearray.push(news[i].friendica_activities.dislike[user].url)}
for (var user in news[i].friendica_activities.attendyes){attendyesarray.push(news[i].friendica_activities.attendyes[user].url)}
for (var user in news[i].friendica_activities.attendno){attendnoarray.push(news[i].friendica_activities.attendno[user].url)}
for (var user in news[i].friendica_activities.attendmaybe){attendmaybearray.push(news[i].friendica_activities.attendmaybe[user].url)}
}
2017-01-26 21:55:31 +01:00
var friendica_activities=[likearray,dislikearray,attendyesarray,attendnoarray,attendmaybearray]
2017-05-11 22:15:34 +02:00
var attachments="";if (news[i].attachments){attachments=Qt.btoa(JSON.stringify(news[i].attachments))}
2017-01-26 21:55:31 +01:00
db.transaction( function(tx) {
2019-06-25 20:59:10 +02:00
var result = tx.executeSql('SELECT * from news where username="'+login.username+'" AND status_id = "'+news[i].id+'" AND messagetype='+news[i].messagetype); // check for news id
2017-01-26 21:55:31 +01:00
if(result.rows.length === 1) {// use update
2017-03-25 23:36:14 +01:00
//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')
2019-06-25 20:59:10 +02:00
result = tx.executeSql('UPDATE news SET username="'+login.username+'", messagetype='+news[i].messagetype+', text="'+Qt.btoa(news[i].text)+'", created_at="'+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_author.url+'" where username="'+login.username+'" AND status_id="'+news[i].status_id+'" AND messagetype=0');
2017-01-26 21:55:31 +01:00
} else {// use insert
2019-06-25 20:59:10 +02:00
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,news[i].messagetype,Qt.btoa(news[i].text),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_author.url])}})
2017-01-26 21:55:31 +01:00
}
2018-07-01 15:09:21 +02:00
// getDirectMessage(login,database,rootwindow,callback)
2017-01-26 21:55:31 +01:00
}
2018-07-01 15:09:21 +02:00
//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])}
// });
// }
// })
// callback()
//// 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));
// newsArray[j].statusnet_html=Qt.atob(newsArray[j].statusnet_html);
// callback(newsArray);
// }
// })
//})}
function getActivitiesUserData(allcontacts,userUrlArray){//print(JSON.stringify(userUrlArray));
2017-01-26 21:55:31 +01:00
var helpArray=[];
2017-03-25 23:36:14 +01:00
for (var i=0;i<userUrlArray.length;i++){
2018-02-19 22:36:00 +01:00
helpArray.push(objFromArray(allcontacts,"url",userUrlArray[i]));
2017-01-26 21:55:31 +01:00
}
return helpArray
}
2019-06-25 20:59:10 +02:00
function newsfromdb(database,login,messagetype,callback,contact,stop_time){
2017-01-26 21:55:31 +01:00
// 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) {
2019-06-25 20:59:10 +02:00
var result = tx.executeSql('SELECT status_id from news WHERE username="'+login.username+'" AND messagetype=0 ORDER BY status_id DESC LIMIT 1');
try{var lastid=result.rows.item(0).status_id;}catch(e){var lastid=0};
if (!stop_time){var stop="";
try{var rs = tx.executeSql('select created_at from news WHERE username="'+login.username+'" AND messagetype="'+messagetype+'" 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+"' OR friendica_owner='"+contact+"')"}
2019-12-10 21:12:32 +01:00
if (messagetype=="0"){messagetype="0,5"}
//print('select * from news WHERE username="'+login.username+'" AND messagetype IN ( '+messagetype+' ) AND created_at'+stop+contactfilter+' ORDER BY created_at DESC LIMIT 20');
var newsrs=tx.executeSql('select * from news WHERE username="'+login.username+'" AND messagetype IN ( '+messagetype+' ) AND created_at'+stop+contactfilter+' ORDER BY created_at DESC LIMIT 20');
2019-06-25 20:59:10 +02:00
var newsArray=[];
var allcontacts=getAllContacts(database,login.username);
for(var i = 0; i < newsrs.rows.length; i++) {
newsArray.push(newsrs.rows.item(i));
newsArray[i].statusnet_html=Qt.atob(newsArray[i].statusnet_html);
newsArray[i].text=Qt.atob(newsArray[i].text);
newsArray[i].id=newsArray[i].status_id;
newsArray[i].friendica_author=newsArray[i].friendica_owner
newsArray[i]=fetchUsersForNews(database,login.username,newsArray[i],allcontacts);
if (newsArray[i].attachments!="" && newsArray[i].attachments!==null){newsArray[i].attachments=JSON.parse(Qt.atob(newsArray[i].attachments))};
}
callback(newsArray,lastid)});
2017-01-26 21:55:31 +01:00
}
2019-06-25 20:59:10 +02:00
function fetchUsersForNews(database,username,news,allcontacts){//print("fetchusers "+JSON.stringify(news))
2018-02-19 22:36:00 +01:00
news.user=objFromArray(allcontacts,"id",news.uid);
if(news.in_reply_to_user_id){news.reply_user=objFromArray(allcontacts,"id",news.in_reply_to_user_id)}
2018-07-01 15:09:21 +02:00
//news.friendica_owner_object=objFromArray(allcontacts,"url",news.friendica_owner);
2019-06-25 20:59:10 +02:00
news.friendica_author=objFromArray(allcontacts,"url",news.friendica_author);
2018-07-01 15:09:21 +02:00
if (news.messagetype==0){
var friendicaArray=JSON.parse(Qt.atob(news.friendica_activities));
delete news.friendica_activities;
news.friendica_activities={};
//for(var j=0;j<friendicaArray.length;j++){
news.friendica_activities.like=getActivitiesUserData(allcontacts,friendicaArray[0]);
news.friendica_activities.dislike=getActivitiesUserData(allcontacts,friendicaArray[1]);
news.friendica_activities.attendyes=getActivitiesUserData(allcontacts,friendicaArray[2]);
news.friendica_activities.attendno=getActivitiesUserData(allcontacts,friendicaArray[3]);
news.friendica_activities.attendmaybe=getActivitiesUserData(allcontacts,friendicaArray[4]);
//}
}
return news
2017-01-26 21:55:31 +01:00
}
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="}
2017-11-07 21:57:40 +01:00
Helperjs.friendicaPostRequest(login,api+newsid,"","POST", rootwindow,function (obj){
2017-01-26 21:55:31 +01:00
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){
2018-02-19 22:36:00 +01:00
Helperjs.friendicaPostRequest(login,"/api/statuses/retweet?id="+newsid,"","POST", rootwindow,function (obj){
2017-01-26 21:55:31 +01:00
var answer=JSON.parse(obj);
2018-02-19 22:36:00 +01:00
if(answer.hasOwnProperty('status'))//('error' in answer.status)
{Helperjs.showMessage("Repost",answer.status.code,rootwindow);}
else{Helperjs.showMessage("Repost",answer.text,rootwindow)}
})
}
2017-01-26 21:55:31 +01:00
function favorite(login,favorite,newsid,rootwindow){
// toggle favorites
2017-11-07 21:57:40 +01:00
if(favorite){ Helperjs.friendicaPostRequest(login,"/api/favorites/create?id="+newsid,"","POST", rootwindow,function (obj){
2017-01-26 21:55:31 +01:00
})}
2018-07-20 21:15:54 +02:00
else {Helperjs.friendicaPostRequest(login,"/api/favorites/destroy?id="+newsid,"","POST",rootwindow,function (obj){
2017-01-26 21:55:31 +01:00
})}
}
function likerequest(login,database,verb,newsid,rootwindow){
2017-11-07 21:57:40 +01:00
Helperjs.friendicaPostRequest(login,"/api/friendica/activity/"+verb+"?id="+newsid, "","POST",rootwindow,function (obj){
2017-01-26 21:55:31 +01:00
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);
2017-05-11 22:15:34 +02:00
//print(verb+"currentActivities "+JSON.stringify(currentActivities));
2017-01-26 21:55:31 +01:00
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)}}
2017-03-25 23:36:14 +01:00
//print(JSON.stringify(currentActivities));
2017-01-26 21:55:31 +01:00
var result = tx.executeSql('UPDATE news SET friendica_activities_self ="'+JSON.stringify(currentActivities)+'" where username="'+login.username+'" AND status_id ='+newsid);
})}
2018-02-19 22:36:00 +01:00
else{}})
2017-01-26 21:55:31 +01:00
}
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){
2017-11-07 21:57:40 +01:00
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]);
2017-01-26 21:55:31 +01:00
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)){
2017-01-26 21:55:31 +01:00
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)}
2017-03-25 23:36:14 +01:00
//print(JSON.stringify(currentActivities));
2017-01-26 21:55:31 +01:00
}
if ((attend=="no")&&(currentActivities.indexOf(4)==-1)){
2017-01-26 21:55:31 +01:00
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)}
2017-01-26 21:55:31 +01:00
}
if ((attend=="maybe")&&(currentActivities.indexOf(5)==-1)){
2017-01-26 21:55:31 +01:00
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)}
2017-01-26 21:55:31 +01:00
}
2017-01-26 21:55:31 +01:00
var result = tx.executeSql('UPDATE news SET friendica_activities_self ="'+JSON.stringify(currentActivities)+'" where username="'+login.username+'" AND status_id ='+newsid);
callback();
})})}
2017-01-26 21:55:31 +01:00
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) {
2018-02-19 22:36:00 +01:00
var newsrs=tx.executeSql('select * from news WHERE username="'+user+'" AND statusnet_conversation_id="'+conversationId+'" ORDER BY created_at ASC');
var newsArray=[];
2018-07-01 15:09:21 +02:00
var allcontacts=getAllContacts(database,user);
2018-02-19 22:36:00 +01:00
for(var i = 0; i < newsrs.rows.length; i++) {
newsArray.push(newsrs.rows.item(i));
2018-07-01 15:09:21 +02:00
newsArray[i].statusnet_html=Qt.atob(newsArray[i].statusnet_html);
newsArray[i].text=Qt.atob(newsArray[i].text);
newsArray[i].id=newsArray[i].status_id;
newsArray[i]=fetchUsersForNews(database,user,newsArray[i],allcontacts);
2019-06-25 20:59:10 +02:00
if (helpernews.attachments!="" && newsArray[i].attachments!==null){newsArray[i].attachments=JSON.parse(Qt.atob(newsArray[i].attachments))};
2018-02-19 22:36:00 +01:00
}
callback(newsArray)})
}
2017-01-26 21:55:31 +01:00
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)
})}
2018-07-01 15:09:21 +02:00
//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=[];
// var allcontacts=getAllContacts(database,user);
// for(var i = 0; i < newsrs.rows.length; i++) {
// newsArray.push(newsrs.rows.item(i));
// newsArray[i].statusnet_html=Qt.atob(newsArray[i].statusnet_html);
// newsArray[i].id=newsArray[i].status_id;
// newsArray[i]=fetchUsersForNews(database,user,newsArray[i],allcontacts);
// if (newsArray[i].attachments!==null){newsArray[i].attachments=JSON.parse(Qt.atob(newsArray[i].attachments))};
// callback(newsArray);
// }})}
2017-01-26 21:55:31 +01:00
2019-06-25 20:59:10 +02:00
function chatsfromdb(database,login,messagetype,callback,stop_time){
2017-01-26 21:55:31 +01:00
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
if (!stop_time){var stop="";
2019-06-25 20:59:10 +02:00
try{var rs = tx.executeSql('select created_at from news WHERE username="'+login.username+'" AND messagetype="'+messagetype+'" ORDER BY created_at DESC LIMIT 1');
2017-01-26 21:55:31 +01:00
stop="<="+rs.rows.item(0).created_at}catch(e){stop="<99999999999999"}}
else{var stop="<"+stop_time}
2019-12-10 21:12:32 +01:00
if (messagetype=="1"){messagetype="1,5"}
var conversationsrs=tx.executeSql('select DISTINCT statusnet_conversation_id from news WHERE username="'+login.username+'" AND created_at'+stop+' AND messagetype IN ( "'+messagetype+'" ) ORDER BY created_at DESC LIMIT 20'); //+' ORDER BY created_at DESC LIMIT 20');
2019-06-25 20:59:10 +02:00
var result = tx.executeSql('SELECT status_id from news WHERE username="'+login.username+'" AND messagetype=0 ORDER BY status_id DESC LIMIT 1');
try{var lastid=result.rows.item(0).status_id;}catch(e){var lastid=0};
2017-01-26 21:55:31 +01:00
var conversations=[];
for(var i = 0; i < conversationsrs.rows.length; i++) {
conversations.push(conversationsrs.rows.item(i).statusnet_conversation_id);
}
var newsArray=[];
2019-06-25 20:59:10 +02:00
var allcontacts=getAllContacts(database,login.username);
2017-01-26 21:55:31 +01:00
for(var j = 0; j< conversations.length; j++) {
2019-06-25 20:59:10 +02:00
var newsrs=tx.executeSql('select * from news WHERE username="'+login.username+'" AND statusnet_conversation_id="'+conversations[j] +'" AND messagetype="'+messagetype+'" ORDER BY created_at ASC');
2020-01-27 21:53:51 +01:00
//print(JSON.stringify(newsrs.rows.item(0))+JSON.stringify(newsrs.rows.item(1)))
2017-11-07 21:57:40 +01:00
var helpernews=newsrs.rows.item(0);
helpernews.newscount=newsrs.rows.length;
2019-06-25 20:59:10 +02:00
helpernews=fetchUsersForNews(database,login.username,helpernews,allcontacts);
2018-07-01 15:09:21 +02:00
helpernews.statusnet_html=Qt.atob(helpernews.statusnet_html);
helpernews.text=Qt.atob(helpernews.text);
helpernews.id=helpernews.status_id;
2019-06-25 20:59:10 +02:00
if (helpernews.attachments!="" && helpernews.attachments!==null){helpernews.attachments=JSON.parse(Qt.atob(helpernews.attachments))};
2020-01-27 21:53:51 +01:00
helpernews.currentconversation=[];
for (var h = 0;h<newsrs.rows.length;h++){
var helpernews2=newsrs.rows.item(h);
helpernews2.newscount=0;
helpernews2=fetchUsersForNews(database,login.username,helpernews2,allcontacts);
helpernews2.statusnet_html=Qt.atob(helpernews2.statusnet_html);
helpernews2.text=Qt.atob(helpernews2.text);//print(h+" "+helpernews2.text)
helpernews2.id=helpernews2.status_id;
if (helpernews2.attachments!="" && helpernews2.attachments!==null){helpernews2.attachments=JSON.parse(Qt.atob(helpernews2.attachments))};
helpernews.currentconversation.push(helpernews2)
}
2017-11-07 21:57:40 +01:00
newsArray.push(helpernews);
}
2019-06-25 20:59:10 +02:00
callback(newsArray,lastid);
2017-11-07 21:57:40 +01:00
})}
2017-01-26 21:55:31 +01:00
2018-07-01 15:09:21 +02:00
function allchatsfromdb(database,user,callback){
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+'" ORDER BY created_at DESC'); //+' ORDER BY created_at DESC LIMIT 20');
var conversationIds=[];
for(var i = 0; i < conversationsrs.rows.length; i++) {
conversationIds.push(conversationsrs.rows.item(i).statusnet_conversation_id);
}
var newsArray=[];
var countArray=[];
var allcontacts=getAllContacts(database,user);
for(var j = 0; j< conversationIds.length; j++) {
var newsrs=tx.executeSql('select * from news WHERE username="'+user+'" AND statusnet_conversation_id="'+conversationIds[j] +'" ORDER BY created_at ASC');
var helpernews=newsrs.rows.item(0);
//helpernews.newscount=newsrs.rows.length;
helpernews=fetchUsersForNews(database,user,helpernews,allcontacts);
helpernews.statusnet_html=Qt.atob(helpernews.statusnet_html);
helpernews.text=Qt.atob(helpernews.text);
helpernews.id=helpernews.status_id;
2019-06-25 20:59:10 +02:00
if (helpernews.attachments!="" && helpernews.attachments!==null){helpernews.attachments=JSON.parse(Qt.atob(helpernews.attachments))};
2018-07-01 15:09:21 +02:00
newsArray.push(helpernews);
countArray.push(newsrs.rows.length)
}
var conversationsobject=({});
conversationsobject.conversationIds=conversationIds;
conversationsobject.newsArray=newsArray;
conversationsobject.countArray=countArray;
callback(conversationsobject);
})}
function oldchatfromdb(database,user,conversationId,lastpost,allcontacts,callback){
var db=Sql.LocalStorage.openDatabaseSync(database[0],database[1],database[2],database[3]);
db.transaction( function(tx) {
// var newsArray=[];
// var countArray=[];
//var allcontacts=getAllContacts(database,user);
var newsrs=tx.executeSql('select * from news WHERE username="'+user+'" AND statusnet_conversation_id="'+conversationId +'" AND status_id<'+lastpost+' ORDER BY created_at ASC');
if(newsrs.rows.length>0){var helpernews=newsrs.rows.item(0);
var newscount=newsrs.rows.length;
helpernews=fetchUsersForNews(database,user,helpernews,allcontacts);
helpernews.statusnet_html=Qt.atob(helpernews.statusnet_html);
helpernews.text=Qt.atob(helpernews.text);
helpernews.id=helpernews.status_id;
2019-06-25 20:59:10 +02:00
if (helpernews.attachments!="" && helpernews.attachments!==null){helpernews.attachments=JSON.parse(Qt.atob(helpernews.attachments))};
2018-07-01 15:09:21 +02:00
callback(helpernews,newscount);}
// var conversationobject={news:helpernews,newscount:newscount};
// return conversationobject;
})}
function getAllContacts(database,user){
var allcontacts=[];
Helperjs.readData(database,"contacts",user,function(obj){
allcontacts=obj;
for (var n in allcontacts){
allcontacts[n].name=Qt.atob(allcontacts[n].name);
allcontacts[n].description=Qt.atob(allcontacts[n].description)
}
});
return allcontacts;
}
2017-01-26 21:55:31 +01:00
function inArray(list, prop, val) {
if (list.length > 0 ) {
for (var i in list) {if (list[i][prop] == val) {
return true;
}
}
} return false;
}
2018-02-19 22:36:00 +01:00
function objFromArray(list, prop, val) {
if (list.length > 0 ) {
for (var i in list) {if (list[i][prop] == val) {
return list[i];
}
}
} return false;
}
2017-01-26 21:55:31 +01:00
function cleanDate(date){
2018-02-19 22:36:00 +01:00
var cleanedDate= date.slice(0,3)+", "+date.slice(8,11)+date.slice(4,7)+date.slice(25,30)+date.slice(10,25);
return cleanedDate
2017-01-26 21:55:31 +01:00
}