Friendiqa/source-android/js/newsworker.js

146 lines
9.2 KiB
JavaScript
Raw Normal View History

2018-02-19 22:36:00 +01:00
// This file is part of Friendiqa
// https://github.com/lubuwest/Friendiqa
// 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
WorkerScript.onMessage = function(msg) {
2017-11-24 22:12:03 +01:00
if(msg.deleteId!==undefined)
2017-11-07 21:57:40 +01:00
{msg.model.remove(msg.deleteId);
msg.model.sync()
}
else{
2018-08-25 16:17:09 +02:00
if(msg.method=="refresh"){msg.model.clear()};
msg.model.sync()
for (var j=0;j<msg.news.length;j++){
2017-01-26 21:55:31 +01:00
if (msg.news[j]) {
var newsitemobject=msg.news[j];
2017-03-25 23:36:14 +01:00
//print("Newsitem"+JSON.stringify(newsitemobject.friendica_owner+" userid: "+newsitemobject.user.id));
2018-07-01 15:09:21 +02:00
// if (newsitemobject.messagetype==2){
// newsitemobject.user={};
// newsitemobject.user.profile_image="";
// newsitemobject.user.profile_image_url="";
// newsitemobject.user.name="";
// }
var forumname="";try{if (newsitemobject.messagetype==0&&newsitemobject.hasOwnProperty('friendica_owner')&&((newsitemobject.friendica_owner.url)!=(newsitemobject.user.url))){
2017-11-07 21:57:40 +01:00
//print(newsitemobject.friendica_owner+" Friendica Owner "+JSON.stringify(newsitemobject));
2018-07-01 15:09:21 +02:00
forumname=" via "+newsitemobject.friendica_owner.name
2017-11-07 21:57:40 +01:00
}}catch(e){print("forum name "+e)}
2017-01-26 21:55:31 +01:00
var likeText="";var dislikeText="";var attendyesText="";var attendnoText="";var attendmaybeText=""; var self={};
2018-07-01 15:09:21 +02:00
try{if (newsitemobject.messagetype==0&&newsitemobject.hasOwnProperty('friendica_activities')){
if (newsitemobject.friendica_activities.like.length>0){
if (newsitemobject.friendica_activities.like.length==1){likeText= newsitemobject.friendica_activities.like[0].name+" "+ qsTr("likes this.")}
else {likeText= newsitemobject.friendica_activities.like.length+" "+ qsTr("like this.")}
2017-01-26 21:55:31 +01:00
}
2018-07-01 15:09:21 +02:00
if (newsitemobject.friendica_activities.dislike.length>0){
if (newsitemobject.friendica_activities.dislike.length==1){dislikeText= newsitemobject.friendica_activities.dislike[0].name+" "+ qsTr("doesn't like this.")}
else {dislikeText= newsitemobject.friendica_activities.dislike.length+" "+ qsTr("don't like this.")}
2017-01-26 21:55:31 +01:00
}
2018-07-01 15:09:21 +02:00
if (newsitemobject.friendica_activities.attendyes.length>0){
if (newsitemobject.friendica_activities.attendyes.length==1){attendyesText=newsitemobject.friendica_activities.attendyes[0].name+" "+ qsTr("will attend.")}
else {attendyesText= newsitemobject.friendica_activities.attendyes.length+" "+ qsTr("persons will attend.")}
2017-01-26 21:55:31 +01:00
}
2018-07-01 15:09:21 +02:00
if (newsitemobject.friendica_activities.attendno.length>0){
if (newsitemobject.friendica_activities.attendno.length==1){attendnoText= newsitemobject.friendica_activities.attendno[0].name+" "+ qsTr("will not attend.")}
else {attendnoText= newsitemobject.friendica_activities.attendno.length+" "+ qsTr("persons will not attend.")}
2017-01-26 21:55:31 +01:00
}
2018-07-01 15:09:21 +02:00
if (newsitemobject.friendica_activities.attendmaybe.length>0){
if (newsitemobject.friendica_activities.attendmaybe.length==1){attendmaybeText= newsitemobject.friendica_activities.attendmaybe[0].name+" "+ qsTr("may attend.")}
else {attendmaybeText= newsitemobject.friendica_activities.attendmaybe.length+" "+ qsTr("persons may attend.")}
2017-01-26 21:55:31 +01:00
}
//var friendica_activities_self=JSON.parse(newsitemobject.friendica_activities_self);
2018-07-01 15:09:21 +02:00
if (newsitemobject.hasOwnProperty("friendica_activities_self")){
if (newsitemobject.friendica_activities_self.indexOf(3)!=-1){self.attending=qsTr("yes")}
if (newsitemobject.friendica_activities_self.indexOf(4)!=-1){self.attending=qsTr("no")}
if (newsitemobject.friendica_activities_self.indexOf(5)!=-1){self.attending=qsTr("maybe")}
if (newsitemobject.friendica_activities_self.indexOf(1)!=-1){self.liked=1}
if (newsitemobject.friendica_activities_self.indexOf(2)!=-1){self.disliked=1}
2018-07-01 15:09:21 +02:00
}}} catch(e){print("Activities "+e+ " "+JSON.stringify(newsitemobject.friendica_activities))}
2017-11-07 21:57:40 +01:00
var friendica_activities={likeText:likeText,dislikeText:dislikeText,attendyesText:attendyesText,attendnoText:attendnoText,attendmaybeText:attendmaybeText,self:self}
2018-07-20 21:15:54 +02:00
var attachmentList=[];
2018-08-25 16:17:09 +02:00
var videoformats=["mp4", "avi", "webm","ogg","mp3"]
2018-07-20 21:15:54 +02:00
try{if(newsitemobject.attachments){
2018-07-01 15:09:21 +02:00
var attachArray=newsitemobject.attachments;
2018-07-20 21:15:54 +02:00
for (var image in attachArray){
var attachhelper={mimetype:attachArray[image].mimetype}
var attachhelperstring=Qt.btoa(attachArray[image].url)
2018-08-25 16:17:09 +02:00
var helperstringposition=newsitemobject.statusnet_html.indexOf(attachhelperstring);
if (helperstringposition>-1){attachhelper.url=newsitemobject.statusnet_html.substring(newsitemobject.statusnet_html.lastIndexOf("http",helperstringposition),helperstringposition+attachhelperstring.length);
2018-07-20 21:15:54 +02:00
if (attachArray[image].mimetype=="image/jpeg"){attachhelper.url=attachhelper.url+".jpg"}
else if (attachArray[image].mimetype=="image/gif"){attachhelper.url=attachhelper.url+".gif"}
else if (attachArray[image].mimetype=="image/png"){attachhelper.url=attachhelper.url+".png"}
2017-05-11 22:15:34 +02:00
}
2018-07-20 21:15:54 +02:00
else {attachhelper.url=attachArray[image].url}
attachmentList.push(attachhelper)
2018-08-25 16:17:09 +02:00
newsitemobject.statusnet_html=newsitemobject.statusnet_html.replace(attachhelper.url,"")
2017-05-11 22:15:34 +02:00
}
2018-08-25 16:17:09 +02:00
}
}catch(e){print("attachment "+e)}
for (var format in videoformats){
if (newsitemobject.text.indexOf("."+videoformats[format])>-1){
var videohelper={mimetype:"video/"+videoformats[format]}
var videotext=newsitemobject.text;
while (videotext.indexOf("."+videoformats[format])>-1){
var videohelperstringposition=videotext.indexOf("."+videoformats[format]);
videohelper.url=videotext.substring(videotext.lastIndexOf("http",videohelperstringposition),videohelperstringposition+4);
videotext=videotext.substring(videohelperstringposition+4,videotext.length)
if ((attachmentList.length==0) || (attachmentList[attachmentList.length-1].url!=videohelper.url)){attachmentList.push(videohelper)}
}
}
}
2017-05-11 22:15:34 +02:00
newsitemobject.attachmentList=attachmentList;
2018-07-20 21:15:54 +02:00
2017-01-26 21:55:31 +01:00
var seconds=(msg.currentTime-newsitemobject.created_at)/1000;
2017-11-07 21:57:40 +01:00
var timestring="";
if (seconds<60) {timestring=seconds+" "+qsTr("seconds") +" "+qsTr("ago");}
else if (seconds<90){timestring=Math.round(seconds/60)+" "+qsTr("minute") +" "+qsTr("ago");}
else if (seconds<3600){timestring=Math.round(seconds/60)+" "+qsTr("minutes") +" "+qsTr("ago");}
else if (seconds<5400){timestring=Math.round(seconds/3600)+" "+qsTr("hour") +" "+qsTr("ago");}
else if (seconds<86400){timestring=Math.round(seconds/3600)+" "+qsTr("hours") +" "+qsTr("ago");}
else if (seconds<129600){timestring=Math.round(seconds/86400)+" "+qsTr("day") +" "+qsTr("ago");}
else if (seconds<3888000){timestring=Math.round(seconds/86400)+" "+qsTr("days") +" "+qsTr("ago");}
else if (seconds<5832000){timestring=Math.round(seconds/3888000)+" "+qsTr("month") +" "+qsTr("ago");}
else if (seconds<69984000){timestring=Math.round(seconds/3888000)+" "+qsTr("months") +" "+qsTr("ago");}
else {timestring=Math.round(seconds/46656000)+" "+qsTr("years") +" "+qsTr("ago");}
var data=({"newsitemobject": newsitemobject,"dateDiff":timestring,"friendica_activities":friendica_activities,"forumname":forumname})}
//print("News:"+j+msg.news.length+JSON.stringify(data));
2018-08-25 16:17:09 +02:00
if(msg.method=="append") {
msg.model.insert(j, data)}
else{
msg.model.append(data)}
2017-11-07 21:57:40 +01:00
}
if (j==msg.news.length){
//print("j: "+j+" msg.model.count: "+msg.model.count);
msg.model.sync()
2018-08-25 16:17:09 +02:00
}
2017-11-07 21:57:40 +01:00
}
2017-01-26 21:55:31 +01:00
}