2017-06-07 08:56:42 +03:00
|
|
|
Qt.include("Mastodon.js")
|
2017-07-20 13:14:16 +03:00
|
|
|
var loadImages = true;
|
2017-06-07 08:56:42 +03:00
|
|
|
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))
|
2017-06-15 01:48:53 +03:00
|
|
|
|
|
|
|
// order notifications in ASC order
|
|
|
|
function orderNotifications(items){
|
|
|
|
for (var i = items.length-1; i > 0; i--){
|
2017-10-25 17:30:23 +03:00
|
|
|
if (items[i].id > 0 ) //msg.conf.notificationLastID)
|
2017-06-15 01:48:53 +03:00
|
|
|
WorkerScript.sendMessage({ 'fireNotification': true, "data": items[i]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 02:07:39 +03:00
|
|
|
if (!msg.conf || !msg.conf.login){
|
2017-06-07 08:56:42 +03:00
|
|
|
console.log("Not loggedin")
|
|
|
|
return;
|
|
|
|
}
|
2017-07-20 13:14:16 +03:00
|
|
|
if (typeof msg.conf['loadImages'] !== "undefined")
|
|
|
|
loadImages = msg.conf['loadImages']
|
|
|
|
|
2017-06-07 08:56:42 +03:00
|
|
|
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) {
|
2017-07-04 02:07:39 +03:00
|
|
|
if (msg.bgAction){
|
|
|
|
console.log(JSON.stringify(data))
|
|
|
|
} else if (msg.action === "statuses"){
|
2017-06-16 17:45:04 +03:00
|
|
|
// status posted
|
|
|
|
if(msg.model){
|
2017-07-04 02:07:39 +03:00
|
|
|
var item = parseToot(data);
|
|
|
|
msg.model.append(item)
|
2017-06-16 17:45:04 +03:00
|
|
|
msg.model.sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
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]})
|
|
|
|
}
|
2017-06-07 08:56:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
API.get(msg.action, msg.params, function(data) {
|
|
|
|
var items = [];
|
|
|
|
for (var i in data) {
|
2017-06-14 17:42:28 +03:00
|
|
|
var item;
|
2017-06-07 08:56:42 +03:00
|
|
|
if (data.hasOwnProperty(i)) {
|
2017-06-07 17:09:41 +03:00
|
|
|
if(msg.action === "accounts/search") {
|
2017-10-26 17:23:16 +03:00
|
|
|
item = parseAccounts([], "", data[i]);
|
|
|
|
console.log(JSON.stringify(data[i]))
|
2017-06-07 17:09:41 +03:00
|
|
|
items.push(item)
|
2017-06-15 17:03:20 +03:00
|
|
|
|
2017-06-07 17:09:41 +03:00
|
|
|
} else if(msg.action === "notifications") {
|
2017-06-15 17:03:20 +03:00
|
|
|
// notification
|
|
|
|
//console.log("Is notification... parsing...")
|
2017-06-14 17:42:28 +03:00
|
|
|
item = parseNotification(data[i]);
|
2017-06-07 08:56:42 +03:00
|
|
|
items.push(item)
|
2017-06-15 17:03:20 +03:00
|
|
|
|
|
|
|
} else if(msg.action.indexOf("statuses") >-1 && msg.action.indexOf("context") >-1 && i === "ancestors") {
|
|
|
|
// status ancestors toots - conversation
|
|
|
|
console.log("ancestors")
|
|
|
|
for (var j = 0; j < data[i].length; j ++) {
|
|
|
|
item = parseToot(data[i][j]);
|
|
|
|
item['id'] = item['status_id']
|
|
|
|
items.push(item)
|
|
|
|
console.log(JSON.stringify(data[i][j]))
|
|
|
|
}
|
|
|
|
addDataToModel (msg.model, "prepend", items);
|
|
|
|
items = [];
|
|
|
|
|
|
|
|
//console.log(JSON.stringify(i))
|
|
|
|
} else if(msg.action.indexOf("statuses") >-1 && msg.action.indexOf("context") >-1 && i === "descendants") {
|
|
|
|
// status ancestors toots - conversation
|
|
|
|
console.log("descendants")
|
|
|
|
for (var j = 0; j < data[i].length; j ++) {
|
|
|
|
item = parseToot(data[i][j]);
|
|
|
|
item['id'] = item['status_id']
|
2017-06-22 15:49:39 +03:00
|
|
|
if (typeof item['attachments'] === "undefined")
|
|
|
|
item['attachments'] = [];
|
2017-06-15 17:03:20 +03:00
|
|
|
items.push(item)
|
|
|
|
console.log(JSON.stringify(data[i][j]))
|
|
|
|
}
|
|
|
|
addDataToModel (msg.model, "append", items);
|
|
|
|
items = [];
|
|
|
|
|
2017-06-07 08:56:42 +03:00
|
|
|
} else if (data[i].hasOwnProperty("content")){
|
2017-06-15 17:03:20 +03:00
|
|
|
//console.log("Is toot... parsing...")
|
2017-06-14 17:42:28 +03:00
|
|
|
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]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 17:03:20 +03:00
|
|
|
if(msg.model && items.length)
|
2017-06-07 08:56:42 +03:00
|
|
|
addDataToModel(msg.model, msg.mode, items)
|
2017-06-16 17:45:04 +03:00
|
|
|
/*if(msg.action === "notifications")
|
|
|
|
orderNotifications(items)*/
|
2017-06-07 08:56:42 +03:00
|
|
|
});
|
|
|
|
}
|
2017-06-15 01:48:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-07 08:56:42 +03:00
|
|
|
//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,
|
2017-06-16 17:45:04 +03:00
|
|
|
type: data.type,
|
|
|
|
attachments: []
|
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"
|
2017-06-14 17:42:28 +03:00
|
|
|
item['type'] = "mention";
|
2017-06-07 08:56:42 +03:00
|
|
|
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-16 17:45:04 +03:00
|
|
|
item['attachments'] = []
|
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-14 17:57:44 +03:00
|
|
|
item['created_at'] = new Date(data.created_at)
|
2017-06-16 17:45:04 +03:00
|
|
|
item['section'] = getDate(data["created_at"])
|
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-16 17:45:04 +03:00
|
|
|
function getDate(dateStr){
|
|
|
|
var ts = new Date(dateStr);
|
|
|
|
return new Date(ts.getFullYear(), ts.getMonth(), ts.getDate(), 0, 0, 0)
|
|
|
|
}
|
2017-06-07 08:56:42 +03:00
|
|
|
function parseToot (data){
|
|
|
|
//console.log(JSON.stringify(data))
|
|
|
|
var item = {};
|
2017-10-20 16:20:33 +03:00
|
|
|
|
2017-07-04 02:07:39 +03:00
|
|
|
item['type'] = "toot"
|
2017-06-15 17:03:20 +03:00
|
|
|
item['highlight'] = false
|
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"]);
|
2017-06-16 17:45:04 +03:00
|
|
|
item['section'] = getDate(data["created_at"]);
|
2017-07-04 17:57:50 +03:00
|
|
|
item['reblogs_count'] = data["reblogs_count"]
|
|
|
|
item['favourites_count'] = data["favourites_count"]
|
|
|
|
item['reblogged'] = data["reblogged"]
|
|
|
|
item['favourited'] = data["favourited"]
|
2017-06-13 17:45:52 +03:00
|
|
|
item['status_sensitive'] = data["sensitive"]
|
|
|
|
item['status_spoiler_text'] = data["spoiler_text"]
|
|
|
|
item['status_visibility'] = data["visibility"]
|
|
|
|
|
|
|
|
|
|
|
|
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"])
|
|
|
|
}
|
2017-06-16 17:45:04 +03:00
|
|
|
item['content'] = data['content'].replaceAll('</span><span class="invisible">', '').replaceAll('<span class="invisible">', '').replaceAll('</span><span class="ellipsis">', '');
|
2017-06-16 01:56:29 +03:00
|
|
|
item['attachments'] = [];
|
2017-11-01 02:27:48 +03:00
|
|
|
|
2017-07-20 13:14:16 +03:00
|
|
|
|
2017-06-16 01:56:29 +03:00
|
|
|
for(var i = 0; i < data['media_attachments'].length ; i++){
|
|
|
|
var attachments = data['media_attachments'][i];
|
|
|
|
item['content'] = item['content'].replaceAll(attachments['text_url'], '')
|
2017-10-26 17:23:16 +03:00
|
|
|
var tmp = {
|
|
|
|
id: attachments['id'],
|
|
|
|
id: attachments['id'],
|
|
|
|
type: attachments['type'],
|
2017-10-26 17:38:07 +03:00
|
|
|
url: attachments['remote_url'] && typeof attachments['remote_url'] == "string" ? attachments['remote_url'] : attachments['url'] ,
|
2017-10-26 17:23:16 +03:00
|
|
|
preview_url: loadImages ? attachments['preview_url'] : ''
|
|
|
|
}
|
2017-11-01 02:27:48 +03:00
|
|
|
//console.log("-----------------------------------")
|
|
|
|
//console.log(JSON.stringify(attachments))
|
|
|
|
//console.log(typeof attachments['remote_url'])
|
|
|
|
//console.log(JSON.stringify(tmp))
|
|
|
|
//console.log("-----------------------------------")
|
2017-10-26 17:23:16 +03:00
|
|
|
item['attachments'].push(tmp)
|
2017-06-16 01:56:29 +03:00
|
|
|
}
|
2017-06-07 08:56:42 +03:00
|
|
|
return item;
|
|
|
|
}
|