Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
feature(Tinebase): add broadcast-hub client
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliusweiss committed May 18, 2022
1 parent acd8032 commit 5c85e2d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 6 deletions.
5 changes: 5 additions & 0 deletions tine20/Tinebase/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Tinebase_Config extends Tinebase_Config_Abstract

const BROADCASTHUB = 'broadcasthub';
const BROADCASTHUB_ACTIVE = 'active';
const BROADCASTHUB_URL = 'url';
const BROADCASTHUB_REDIS = 'redis';
const BROADCASTHUB_REDIS_HOST = 'host';
const BROADCASTHUB_REDIS_PORT = 'port';
Expand Down Expand Up @@ -1723,6 +1724,10 @@ class Tinebase_Config extends Tinebase_Config_Abstract
self::TYPE => self::TYPE_BOOL,
self::CLIENTREGISTRYINCLUDE => true,
self::DEFAULT_STR => false,
],self::BROADCASTHUB_URL => [
self::TYPE => self::TYPE_STRING,
self::CLIENTREGISTRYINCLUDE => true,
self::DEFAULT_STR => '//:4003',
],
self::BROADCASTHUB_PUBSUBNAME => [
self::TYPE => self::TYPE_STRING,
Expand Down
2 changes: 1 addition & 1 deletion tine20/Tinebase/Frontend/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ public function checkAuthToken($token, $channel)
Tinebase_Model_AuthToken::class, [
['field' => Tinebase_Model_AuthToken::FLD_AUTH_TOKEN, 'operator' => 'equals', 'value' => $token]
]
))->getFirstRecord();
))->filter(Tinebase_Model_AuthToken::FLD_AUTH_TOKEN, $token,)->getFirstRecord();

if ($t && in_array($channel, $t->{Tinebase_Model_AuthToken::FLD_CHANNELS})) {
return $t->toArray();
Expand Down
83 changes: 83 additions & 0 deletions tine20/Tinebase/js/broadcastClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Tine 2.0
*
* @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
* @author Cornelius Weiß <c.weiss@metaways.de>
* @copyright Copyright (c) 2022 Metaways Infosystems GmbH (http://www.metaways.de)
*/

const randWait = async (min = 0, max = 2000) => {
return new Promise((resolve) => {
window.setTimeout(resolve, Math.round(Math.random()*max) + min)
});
}

const init = async () => {
await randWait(); // give browser time to breathe
const auth = await Tine.Tinebase.getAuthToken(['broadcasthub'], 100);
const wsUrl = Tine.Tinebase.configManager.get('broadcasthub').url;
const socket = new WebSocket(wsUrl);
let authResponse = null;

socket.onopen = async (e) => {
socket.send(auth.auth_token);
};

socket.onmessage = async (event) => {
if (!authResponse) {
authResponse = event.data;
if (authResponse !== 'AUTHORIZED') {
console.error(`[broadcastClient] not authorised: code=${event.code} ${event.data}`);
}
return;
}
try {
const data = JSON.parse(event.data);
const topicPrefix = String(data.model).split('_Model_').join('.');
const topics = [`${topicPrefix}.*`, `${topicPrefix}.${data.recordId}`];

//@TODO: can we filter out our own actions?
if (topics.filter(value => postal.subscriptions.recordchange.hasOwnProperty(value)).length) {
// we have a subscription, so let's try to load the record (NOTE: loading a record does a postal publishing)
const recordClass = Tine.Tinebase.data.RecordMgr.get(data.model);
const proxy = recordClass ? recordClass.getProxy() : null;

if (proxy) {
if (data.verb === 'delete') {
proxy.postMessage(data.verb, {[recordClass.getMeta('idProperty')]: data.recordId});
} else {
try {
await proxy.promiseLoadRecord(data.recordId);
} catch (/* serverError */ e) {
if (e.code !== 403) {
console.error(`[broadcastClient] can't load record ${data.model} ${data.recordId}`, e);
}
}
}
}
}

} catch (e) {
console.error(`[broadcastClient] error processing event: `,event, e);
}

};

socket.onclose = async (event) => {
if (event.wasClean) {
console.error(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
console.error('[broadcastClient] Connection died');
}
await randWait(5000, 10000);
init();
};

socket.onerror = async (error) => {
console.error(`[broadcastClient] error:`, error);
};
};

export default init;
10 changes: 5 additions & 5 deletions tine20/Tinebase/js/data/RecordProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ Ext.extend(Tine.Tinebase.data.RecordProxy, Ext.data.DataProxy, {
fulfill(r);
},
failure: function (error) {
reject(new Error(error));
reject(new ServerError(error));
}
}));
} catch (error) {
if (Ext.isFunction(reject)) {
reject(new Error(options));
reject(new ServerError(error));
}
}
});
Expand Down Expand Up @@ -262,7 +262,7 @@ Ext.extend(Tine.Tinebase.data.RecordProxy, Ext.data.DataProxy, {
}), additionalArguments);
} catch (error) {
if (Ext.isFunction(reject)) {
reject(new Error(options));
reject(new ServerError(error));
}
}
});
Expand Down Expand Up @@ -316,12 +316,12 @@ Ext.extend(Tine.Tinebase.data.RecordProxy, Ext.data.DataProxy, {
fulfill(r);
},
failure: function (error) {
reject(new Error(error));
reject(new ServerError(error));
}
}), additionalArguments);
} catch (error) {
if (Ext.isFunction(reject)) {
reject(new Error(options));
reject(new ServerError(error));
}
}
});
Expand Down
5 changes: 5 additions & 0 deletions tine20/Tinebase/js/tineInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/*global Ext, Tine, google, OpenLayers, Locale, */

import waitFor from 'util/waitFor.es6';
import initBroadcastClient from './broadcastClient'

var EventEmitter = require('events');

/** ------------------------- Ext.ux Initialisation ------------------------ **/
Expand Down Expand Up @@ -1067,6 +1069,9 @@ Tine.Tinebase.tineInit = {

if (Tine.Tinebase.registry.get('currentAccount')) {
Tine.Tinebase.tineInit.initAppMgr();
if (window.isMainWindow && Tine.Tinebase.configManager.get('broadcasthub')?.active) {
initBroadcastClient();
}
}

Tine.Tinebase.tineInit.initUploadMgr();
Expand Down

0 comments on commit 5c85e2d

Please sign in to comment.