harbour-tooter/qml/lib/API.js

218 lines
8.6 KiB
JavaScript
Raw Permalink Normal View History

2017-06-01 17:13:51 +03:00
.pragma library
.import QtQuick.LocalStorage 2.0 as LS
2020-05-04 10:26:41 +03:00
var db = LS.LocalStorage.openDatabaseSync("tooterb", "", "harbour-tooterb", 100000);
2017-06-01 17:13:51 +03:00
var conf = {};
var mediator = (function(){
var subscribe = function(channel, fn){
if(!mediator.channels[channel]) mediator.channels[channel] = [];
mediator.channels[channel].push({ context : this, callback : fn });
return this;
};
2020-06-22 21:13:05 +03:00
2017-06-01 17:13:51 +03:00
var publish = function(channel){
if(!mediator.channels[channel]) return false;
var args = Array.prototype.slice.call(arguments, 1);
for(var i = 0, l = mediator.channels[channel].length; i < l; i++){
var subscription = mediator.channels[channel][i];
subscription.callback.apply(subscription.context.args);
};
return this;
};
2020-06-22 21:13:05 +03:00
2017-06-01 17:13:51 +03:00
return {
channels : {},
publish : publish,
subscribe : subscribe,
installTo : function(obj){
obj.subscribe = subscribe;
obj.publish = publish;
}
};
}());
2020-06-22 21:13:05 +03:00
2017-06-01 17:13:51 +03:00
var init = function(){
console.log("db.version: "+db.version);
if(db.version === '') {
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS settings ('
+ ' key TEXT UNIQUE, '
+ ' value TEXT '
2020-07-03 11:45:03 +03:00
+ ');');
2017-06-01 17:13:51 +03:00
//tx.executeSql('INSERT INTO settings (key, value) VALUES (?, ?)', ["conf", "{}"]);
});
db.changeVersion('', '0.1', function(tx) {
});
}
db.transaction(function(tx) {
var rs = tx.executeSql('SELECT * FROM settings;');
console.log("READING CONF FROM DB")
for (var i = 0; i < rs.rows.length; i++) {
//var json = JSON.parse(rs.rows.item(i).value);
console.log(rs.rows.item(i).key+" \t > \t "+rs.rows.item(i).value)
conf[rs.rows.item(i).key] = JSON.parse(rs.rows.item(i).value)
}
console.log("END OF READING")
console.log(JSON.stringify(conf));
mediator.publish('confLoaded', { loaded: true});
});
};
function saveData() {
console.log("SAVING CONF TO DB")
2017-06-01 17:13:51 +03:00
db.transaction(function(tx) {
for (var key in conf) {
if (conf.hasOwnProperty(key)){
console.log(key + "\t>\t"+conf[key]);
if (typeof conf[key] === "object" && conf[key] === null) {
tx.executeSql('DELETE FROM settings WHERE key=? ', [key])
} else {
tx.executeSql('INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?) ', [key, JSON.stringify(conf[key])])
}
2017-06-01 17:13:51 +03:00
}
}
console.log("END OF SAVING")
2017-06-01 17:13:51 +03:00
});
}
var tootParser = function(data){
console.log(data)
var ret = {};
ret.id = data.id
ret.content = data.content
ret.created_at = data.created_at
ret.in_reply_to_account_id = data.in_reply_to_account_id
ret.in_reply_to_id = data.in_reply_to_id
ret.user_id = data.account.id
ret.user_locked = data.account.locked
ret.username = data.account.username
ret.display_name = data.account.display_name
ret.avatar_static = data.account.avatar_static
ret.favourited = data.favourited ? true : false
ret.status_favourites_count = data.favourites_count ? data.favourites_count : 0
2017-06-01 17:13:51 +03:00
ret.reblog = data.reblog ? true : false
ret.reblogged = data.reblogged ? true : false
ret.status_reblogs_count = data.reblogs_count ? data.reblogs_count : false
2017-06-01 17:13:51 +03:00
ret.bookmarked = data.bookmarked ? true : false
2017-06-01 17:13:51 +03:00
ret.muted = data.muted ? true : false
ret.sensitive = data.sensitive ? true : false
ret.visibility = data.visibility ? data.visibility : false
console.log(ret)
}
2020-06-22 21:13:05 +03:00
2017-06-01 17:13:51 +03:00
var test = 1;
Qt.include("Mastodon.js")
2017-06-01 17:13:51 +03:00
var modelTLhome = Qt.createQmlObject('import QtQuick 2.0; ListModel { }', Qt.application, 'InternalQmlObject');
var modelTLpublic = Qt.createQmlObject('import QtQuick 2.0; ListModel { }', Qt.application, 'InternalQmlObject');
2018-05-24 12:14:02 +03:00
var modelTLlocal = Qt.createQmlObject('import QtQuick 2.0; ListModel { }', Qt.application, 'InternalQmlObject');
var modelTLnotifications = Qt.createQmlObject('import QtQuick 2.0; ListModel { }', Qt.application, 'InternalQmlObject');
2017-06-16 17:45:04 +03:00
var modelTLsearch = Qt.createQmlObject('import QtQuick 2.0; ListModel { }', Qt.application, 'InternalQmlObject');
var modelTLbookmarks = Qt.createQmlObject('import QtQuick 2.0; ListModel { }', Qt.application, 'InternalQmlObject');
var notificationsList = []
var notificationGenerator = function(item){
var notification;
switch (item.urgency){
case "normal":
2020-05-04 10:26:41 +03:00
notification = Qt.createQmlObject('import org.nemomobile.notifications 1.0; Notification { category: "x-harbour.tooterb.activity"; appName: "Tooter β"; itemCount: 1; remoteActions: [ { "name": "default", "displayName": "Do something", "icon": "icon-s-certificates", "service": "ba.dysko.harbour.tooterb", "path": "/", "iface": "ba.dysko.harbour.tooterb", "method": "openapp", "arguments": [ "'+item.service+'", "'+item.key+'" ] }]; urgency: Notification.Normal; }', Qt.application, 'InternalQmlObject');
break;
case "critical":
2020-05-04 10:26:41 +03:00
notification = Qt.createQmlObject('import org.nemomobile.notifications 1.0; Notification { appName: "Tooter β"; itemCount: 1; remoteActions: [ { "name": "default", "displayName": "Do something", "icon": "icon-s-certificates", "service": "ba.dysko.harbour.tooterb", "path": "/", "iface": "ba.dysko.harbour.tooterb", "method": "openapp", "arguments": [ "'+item.service+'", "'+item.key+'" ] }]; urgency: Notification.Critical; }', Qt.application, 'InternalQmlObject');
break;
default:
2020-05-04 10:26:41 +03:00
notification = Qt.createQmlObject('import org.nemomobile.notifications 1.0; Notification { category: "x-harbour.tooterb.activity"; appName: "Tooter β"; itemCount: 1; remoteActions: [ { "name": "default", "displayName": "Do something", "icon": "icon-s-certificates", "service": "ba.dysko.harbour.tooterb", "path": "/", "iface": "ba.dysko.harbour.tooterb", "method": "openapp", "arguments": [ "'+item.service+'", "'+item.key+'" ] }]; urgency: Notification.Low; }', Qt.application, 'InternalQmlObject');
}
console.log(JSON.stringify(notification.remoteActions[0].arguments))
2020-05-04 10:26:41 +03:00
//Notifications.notify("Tooter β", "serverinfo.serverTitle", " new activity", false, "2015-10-15 00:00:00", "aaa")
notification.timestamp = item.timestamp
notification.summary = item.summary
notification.body = item.body
if(item.previewBody)
notification.previewBody = item.previewBody;
else
notification.previewBody = item.body;
if(item.previewSummary)
notification.previewSummary = item.previewSummary;
else
notification.previewSummary = item.summary
if(notification.replacesId){ notification.replacesId = 0 }
notification.publish()
}
var notifier = function(item){
2017-06-01 17:13:51 +03:00
item.content = item.content.replace(/(<([^>]+)>)/ig,"").replaceAll("&quot;", "\"")
var msg;
switch (item.type){
case "favourite":
msg = {
urgency: "normal",
timestamp: item.created_at,
summary: (item.reblog_account_display_name !== "" ? item.reblog_account_display_name : '@'+item.reblog_account_username) + ' ' + qsTr("favourited"),
body: item.content,
service: 'toot',
key: item.id
}
break;
2020-06-22 21:13:05 +03:00
case "follow":
msg = {
urgency: "critical",
timestamp: item.created_at,
summary: (item.account_display_name !== "" ? item.account_display_name : '@'+item.account_username),
body: qsTr("followed you"),
service: 'profile',
key: item.account_username
}
break;
case "reblog":
msg = {
urgency: "low",
timestamp: item.created_at,
2017-07-20 13:14:16 +03:00
summary: (item.reblog_account_display_name !== "" ? item.reblog_account_display_name : '@'+item.reblog_account_username) + ' ' + qsTr("boosted"),
body: item.content,
service: 'toot',
key: item.id
}
break;
2020-06-22 21:13:05 +03:00
case "mention":
msg = {
urgency: "critical",
timestamp: item.created_at,
summary: (item.account_display_name !== "" ? item.account_display_name : '@'+item.account_username) + ' ' + qsTr("said"),
body: item.content,
previewBody: (item.account_display_name !== "" ? item.account_display_name : '@'+item.account_username) + ' ' + qsTr("said") + ': ' + item.content,
service: 'toot',
key: item.id
}
break;
2020-06-22 21:13:05 +03:00
default:
//console.log(JSON.stringify(messageObject.data))
return;
}
notificationGenerator(msg)
}
2017-06-01 17:13:51 +03:00
2017-06-01 17:13:51 +03:00
var api;
function func() {
console.log(api)
}