2017-06-07 08:56:42 +03:00
|
|
|
Qt.include("Mastodon.js")
|
|
|
|
WorkerScript.onMessage = function(msg) {
|
|
|
|
console.log("Action > " + msg.action)
|
|
|
|
console.log("Model > " + msg.model)
|
|
|
|
console.log("Mode > " + msg.mode)
|
|
|
|
console.log("Conf > " + JSON.stringify(msg.conf))
|
|
|
|
console.log("Params > " + JSON.stringify(msg.params))
|
|
|
|
if (!msg.conf.login){
|
|
|
|
console.log("Not loggedin")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var API = MastodonAPI({ instance: msg.conf.instance, api_user_token: msg.conf.api_user_token});
|
|
|
|
if (msg.method === "POST"){
|
|
|
|
API.post(msg.action, msg.params, function(data) {
|
|
|
|
for (var i in data) {
|
|
|
|
if (data.hasOwnProperty(i)) {
|
|
|
|
console.log(JSON.stringify(data[i]))
|
|
|
|
WorkerScript.sendMessage({ 'action': msg.action, 'success': true, key: i, "data": data[i]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
API.get(msg.action, msg.params, function(data) {
|
|
|
|
var items = [];
|
|
|
|
for (var i in data) {
|
|
|
|
if (data.hasOwnProperty(i)) {
|
2017-06-07 17:09:41 +03:00
|
|
|
if(msg.action === "accounts/search") {
|
|
|
|
var item = parseAccounts(data[i]);
|
|
|
|
items.push(item)
|
|
|
|
} else if(msg.action === "notifications") {
|
2017-06-07 08:56:42 +03:00
|
|
|
console.log("Is notification... parsing...")
|
|
|
|
var item = parseNotification(data[i]);
|
|
|
|
items.push(item)
|
|
|
|
} else if (data[i].hasOwnProperty("content")){
|
|
|
|
console.log("Is toot... parsing...")
|
|
|
|
var item = parseToot(data[i]);
|
2017-06-13 17:45:52 +03:00
|
|
|
item['id'] = item['status_id']
|
2017-06-07 08:56:42 +03:00
|
|
|
items.push(item)
|
|
|
|
} else {
|
|
|
|
WorkerScript.sendMessage({ 'action': msg.action, 'success': true, key: i, "data": data[i]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(msg.model)
|
|
|
|
addDataToModel(msg.model, msg.mode, items)
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
//WorkerScript.sendMessage({ 'notifyNewItems': length - i })
|
|
|
|
function addDataToModel (model, mode, items){
|
|
|
|
var length = items.length;
|
|
|
|
console.log("Fetched > " +length)
|
|
|
|
|
|
|
|
if (mode === "append") {
|
|
|
|
model.append(items)
|
|
|
|
} else if (mode === "prepend") {
|
|
|
|
for(var i = length-1; i >= 0 ; i--){
|
|
|
|
model.insert(0,items[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
model.sync()
|
|
|
|
}
|
2017-06-13 17:45:52 +03:00
|
|
|
function parseAccounts(collection, prefix, data){
|
|
|
|
var res = collection;
|
|
|
|
|
|
|
|
res[prefix + 'account_id'] = data["id"]
|
|
|
|
res[prefix + 'account_username'] = data["username"]
|
|
|
|
res[prefix + 'account_acct'] = data["acct"]
|
|
|
|
res[prefix + 'account_display_name'] = data["display_name"]
|
|
|
|
res[prefix + 'account_locked'] = data["locked"]
|
|
|
|
res[prefix + 'account_created_at'] = data["created_at"]
|
|
|
|
res[prefix + 'account_avatar'] = data["avatar"]
|
|
|
|
|
|
|
|
// /console.log(JSON.stringify(res))
|
|
|
|
return (res);
|
2017-06-07 17:09:41 +03:00
|
|
|
}
|
|
|
|
|
2017-06-07 08:56:42 +03:00
|
|
|
function parseNotification(data){
|
2017-06-13 17:45:52 +03:00
|
|
|
//console.log(JSON.stringify(data))
|
2017-06-07 08:56:42 +03:00
|
|
|
var item = {
|
|
|
|
id: data.id,
|
|
|
|
type: data.type,
|
2017-06-13 17:45:52 +03:00
|
|
|
created_at: new Date(data.created_at)
|
2017-06-07 08:56:42 +03:00
|
|
|
};
|
|
|
|
switch (item['type']){
|
|
|
|
case "mention":
|
2017-06-07 17:09:41 +03:00
|
|
|
item = parseToot(data.status)
|
|
|
|
item['typeIcon'] = "image://theme/icon-s-retweet"
|
2017-06-07 08:56:42 +03:00
|
|
|
item['typeIcon'] = "image://theme/icon-s-alarm"
|
|
|
|
break;
|
|
|
|
case "reblog":
|
2017-06-07 17:09:41 +03:00
|
|
|
item = parseToot(data.status)
|
2017-06-13 17:45:52 +03:00
|
|
|
item = parseAccounts(item, "reblog_", data["account"])
|
|
|
|
item = parseAccounts(item, "", data["status"]["account"])
|
|
|
|
item['status_reblog'] = true;
|
2017-06-07 17:09:41 +03:00
|
|
|
item['type'] = "reblog";
|
2017-06-07 08:56:42 +03:00
|
|
|
item['typeIcon'] = "image://theme/icon-s-retweet"
|
|
|
|
break;
|
|
|
|
case "favourite":
|
2017-06-07 17:09:41 +03:00
|
|
|
item = parseToot(data.status)
|
2017-06-13 17:45:52 +03:00
|
|
|
item = parseAccounts(item, "reblog_", data["account"])
|
|
|
|
item = parseAccounts(item, "", data["status"]["account"])
|
|
|
|
item['status_reblog'] = true;
|
2017-06-07 08:56:42 +03:00
|
|
|
item['typeIcon'] = "image://theme/icon-s-favorite"
|
2017-06-07 17:09:41 +03:00
|
|
|
item['type'] = "favourite";
|
2017-06-13 17:45:52 +03:00
|
|
|
//item['retweetScreenName'] = item['reblog_account_username'];
|
2017-06-07 08:56:42 +03:00
|
|
|
break;
|
|
|
|
case "follow":
|
2017-06-07 17:09:41 +03:00
|
|
|
item['type'] = "follow";
|
2017-06-13 17:45:52 +03:00
|
|
|
item = parseAccounts(item, "", data["account"])
|
|
|
|
item = parseAccounts(item, "reblog_", data["account"])
|
|
|
|
item['content'] = data['account']['note']
|
2017-06-07 08:56:42 +03:00
|
|
|
item['typeIcon'] = "image://theme/icon-s-installed"
|
2017-06-13 17:45:52 +03:00
|
|
|
|
2017-06-07 08:56:42 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
item['typeIcon'] = "image://theme/icon-s-sailfish"
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:45:52 +03:00
|
|
|
item['id'] = data.id
|
2017-06-07 08:56:42 +03:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:45:52 +03:00
|
|
|
function collect() {
|
|
|
|
var ret = {};
|
|
|
|
var len = arguments.length;
|
|
|
|
for (var i=0; i<len; i++) {
|
|
|
|
for (p in arguments[i]) {
|
|
|
|
if (arguments[i].hasOwnProperty(p)) {
|
|
|
|
ret[p] = arguments[i][p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2017-06-07 08:56:42 +03:00
|
|
|
function parseToot (data){
|
|
|
|
//console.log(JSON.stringify(data))
|
|
|
|
var item = {};
|
2017-06-13 17:45:52 +03:00
|
|
|
item['status_id'] = data["id"]
|
|
|
|
item['status_uri'] = data["uri"]
|
|
|
|
item['status_in_reply_to_id'] = data["in_reply_to_id"]
|
|
|
|
item['status_in_reply_to_account_id'] = data["in_reply_to_account_id"]
|
|
|
|
item['status_reblog'] = data["reblog"] ? true : false
|
|
|
|
item['status_content'] = data["content"]
|
|
|
|
item['status_created_at'] = item['created_at'] = new Date(data["created_at"]);
|
|
|
|
item['status_reblogs_count'] = data["reblogs_count"]
|
|
|
|
item['status_favourites_count'] = data["favourites_count"]
|
|
|
|
item['status_reblogged'] = data["reblogged"]
|
|
|
|
item['status_favourited'] = data["favourited"]
|
|
|
|
item['status_sensitive'] = data["sensitive"]
|
|
|
|
item['status_spoiler_text'] = data["spoiler_text"]
|
|
|
|
item['status_visibility'] = data["visibility"]
|
|
|
|
|
|
|
|
|
|
|
|
//item.concat(parseAccounts("", data["account"]));
|
|
|
|
//item = collect(item, );
|
|
|
|
|
|
|
|
if(item['status_reblog']){
|
|
|
|
item['type'] = "reblog";
|
|
|
|
item['typeIcon'] = "image://theme/icon-s-retweet"
|
|
|
|
item = parseAccounts(item, "", data['reblog']["account"])
|
|
|
|
item = parseAccounts(item, "reblog_", data["account"])
|
|
|
|
} else {
|
|
|
|
item = parseAccounts(item, "", data["account"])
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//item['application_name'] = data["application"]["name"]
|
|
|
|
//item['application_website'] = data["application"]["website"]
|
|
|
|
// account
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*item['type'] = "";
|
2017-06-07 08:56:42 +03:00
|
|
|
item['retweetScreenName'] = '';
|
|
|
|
item['isVerified'] = false;
|
|
|
|
item['isReblog'] = false;
|
|
|
|
item['favourited'] = data['favourited'];
|
|
|
|
item['reblogged'] = data['reblogged'];
|
|
|
|
item['muted'] = data['muted'];
|
2017-06-13 17:45:52 +03:00
|
|
|
item['status_reblogs_count'] = data['reblogs_count'];
|
|
|
|
item['status_favourites_count'] = data['favourites_count'];
|
2017-06-07 08:56:42 +03:00
|
|
|
|
|
|
|
if(data['id']){
|
2017-06-13 17:45:52 +03:00
|
|
|
item['status_id'] = data['id'];
|
2017-06-07 08:56:42 +03:00
|
|
|
}
|
|
|
|
if(data['created_at']){
|
2017-06-13 17:45:52 +03:00
|
|
|
item['status_created_at'] = data['created_at'];
|
2017-06-07 08:56:42 +03:00
|
|
|
}
|
|
|
|
if(data['account']){
|
2017-06-13 17:45:52 +03:00
|
|
|
item['status_account_id'] = data['account']['id'];
|
|
|
|
item['status_account_username'] = data['account']['acct'];
|
|
|
|
item['status_account_display_name'] = data['account']['display_name'];
|
|
|
|
item['status_account_locked'] = data['account']['locked'];
|
|
|
|
item['status_account_avatar'] = data['account']['avatar'];
|
2017-06-07 08:56:42 +03:00
|
|
|
}
|
|
|
|
if(data['reblog']){
|
|
|
|
item['retweetScreenName'] = data['account']['username'];
|
2017-06-07 17:09:41 +03:00
|
|
|
item['type'] = "reblog";
|
2017-06-07 08:56:42 +03:00
|
|
|
item['reblog_id'] = data['reblog']['id'];
|
|
|
|
item['account_id'] = data['reblog']['account']['id'];
|
2017-06-07 17:09:41 +03:00
|
|
|
item['account_username'] = data['reblog']['account']['username'];
|
|
|
|
item['account_display_name'] = data['reblog']['account']['display_name'];
|
2017-06-07 08:56:42 +03:00
|
|
|
item['account_locked'] = data['reblog']['account']['locked'];
|
|
|
|
item['account_avatar'] = data['reblog']['account']['avatar'];
|
|
|
|
|
2017-06-13 17:45:52 +03:00
|
|
|
item['status_reblogs_count'] = data['reblog']['reblogs_count'];
|
|
|
|
item['status_favourites_count'] = data['reblog']['favourites_count'];
|
|
|
|
item['status_favourited'] = data['reblog']['favourited'];
|
|
|
|
item['status_reblogged'] = data['reblog']['reblogged'];
|
|
|
|
item['status_muted'] = data['reblog']['muted'];
|
2017-06-07 08:56:42 +03:00
|
|
|
}
|
2017-06-13 17:45:52 +03:00
|
|
|
*/
|
2017-06-07 08:56:42 +03:00
|
|
|
item['content'] = data['content'].replace(/(<([^>]+)>)/ig,"");
|
2017-06-13 17:45:52 +03:00
|
|
|
/*for(var i = 0; i < data['tags'].length ; i++){
|
|
|
|
var tag = data['tags'][i]['name'];
|
|
|
|
console.log(tag)
|
|
|
|
item['content'] = item['content'].replaceAll('#'+tag, '<a href="#'+tag+'">'+tag+'</a>')
|
|
|
|
}*/
|
|
|
|
|
2017-06-07 08:56:42 +03:00
|
|
|
item['content'] = item['content'].split(" ")
|
|
|
|
for(var i = 0; i < item['content'].length ; i++){
|
|
|
|
if(item['content'][i][0] === "#"){
|
2017-06-07 17:09:41 +03:00
|
|
|
item['content'][i] = '<a href="'+item['content'][i]+'">'+item['content'][i]+'</a>';
|
2017-06-07 08:56:42 +03:00
|
|
|
}
|
|
|
|
if(item['content'][i][0] === "@"){
|
2017-06-07 17:09:41 +03:00
|
|
|
item['content'][i] = '<a href="'+item['content'][i]+'">'+item['content'][i]+'</a>';
|
2017-06-07 08:56:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
item['content'] = item['content'].join(" ").autoLink()
|
|
|
|
|
2017-06-13 17:45:52 +03:00
|
|
|
console.log(JSON.stringify(item))
|
2017-06-07 08:56:42 +03:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|