Moving javascript driver back to platform (#3613)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2b0fcd378c
Коммит
1641370fbe
1670
webapp/client/client.jsx
Обычный файл
1670
webapp/client/client.jsx
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
101
webapp/client/web_client.jsx
Обычный файл
101
webapp/client/web_client.jsx
Обычный файл
@@ -0,0 +1,101 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
import Client from './client.jsx';
|
||||
import TeamStore from '../stores/team_store.jsx';
|
||||
import BrowserStore from '../stores/browser_store.jsx';
|
||||
import * as GlobalActions from 'actions/global_actions.jsx';
|
||||
|
||||
import request from 'superagent';
|
||||
|
||||
const HTTP_UNAUTHORIZED = 401;
|
||||
|
||||
class WebClientClass extends Client {
|
||||
constructor() {
|
||||
super();
|
||||
this.enableLogErrorsToConsole(true);
|
||||
TeamStore.addChangeListener(this.onTeamStoreChanged);
|
||||
}
|
||||
|
||||
onTeamStoreChanged = () => {
|
||||
this.setTeamId(TeamStore.getCurrentId());
|
||||
}
|
||||
|
||||
track = (category, action, label, property, value) => {
|
||||
if (global.window && global.window.analytics) {
|
||||
global.window.analytics.track(action, {category, label, property, value});
|
||||
}
|
||||
}
|
||||
|
||||
trackPage = () => {
|
||||
if (global.window && global.window.analytics) {
|
||||
global.window.analytics.page();
|
||||
}
|
||||
}
|
||||
|
||||
handleError = (err, res) => { // eslint-disable-line no-unused-vars
|
||||
if (err.status === HTTP_UNAUTHORIZED && res.req.url !== '/api/v3/users/login') {
|
||||
GlobalActions.emitUserLoggedOutEvent('/login');
|
||||
}
|
||||
}
|
||||
|
||||
// not sure why but super.login doesn't work if using an () => arrow functions.
|
||||
// I think this might be a webpack issue.
|
||||
webLogin(loginId, password, token, success, error) {
|
||||
this.login(
|
||||
loginId,
|
||||
password,
|
||||
token,
|
||||
(data) => {
|
||||
this.track('api', 'api_users_login_success', '', 'login_id', loginId);
|
||||
BrowserStore.signalLogin();
|
||||
|
||||
if (success) {
|
||||
success(data);
|
||||
}
|
||||
},
|
||||
(err) => {
|
||||
this.track('api', 'api_users_login_fail', '', 'login_id', loginId);
|
||||
if (error) {
|
||||
error(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
webLoginByLdap(loginId, password, token, success, error) {
|
||||
this.loginByLdap(
|
||||
loginId,
|
||||
password,
|
||||
token,
|
||||
(data) => {
|
||||
this.track('api', 'api_users_login_success', '', 'login_id', loginId);
|
||||
BrowserStore.signalLogin();
|
||||
|
||||
if (success) {
|
||||
success(data);
|
||||
}
|
||||
},
|
||||
(err) => {
|
||||
this.track('api', 'api_users_login_fail', '', 'login_id', loginId);
|
||||
if (error) {
|
||||
error(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getYoutubeVideoInfo(googleKey, videoId, success, error) {
|
||||
request.get('https://www.googleapis.com/youtube/v3/videos').
|
||||
query({part: 'snippet', id: videoId, key: googleKey}).
|
||||
end((err, res) => {
|
||||
if (err) {
|
||||
return error(err);
|
||||
}
|
||||
return success(res.body);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var WebClient = new WebClientClass();
|
||||
export default WebClient;
|
||||
7
webapp/client/web_websocket_client.jsx
Обычный файл
7
webapp/client/web_websocket_client.jsx
Обычный файл
@@ -0,0 +1,7 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
import WebSocketClient from './websocket_client.jsx';
|
||||
|
||||
var WebClient = new WebSocketClient();
|
||||
export default WebClient;
|
||||
165
webapp/client/websocket_client.jsx
Обычный файл
165
webapp/client/websocket_client.jsx
Обычный файл
@@ -0,0 +1,165 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
const MAX_WEBSOCKET_FAILS = 7;
|
||||
const MIN_WEBSOCKET_RETRY_TIME = 3000; // 3 sec
|
||||
const MAX_WEBSOCKET_RETRY_TIME = 300000; // 5 mins
|
||||
|
||||
export default class WebSocketClient {
|
||||
constructor() {
|
||||
this.conn = null;
|
||||
this.sequence = 1;
|
||||
this.connectFailCount = 0;
|
||||
this.manuallyClosed = false;
|
||||
this.eventCallback = null;
|
||||
this.responseCallbacks = {};
|
||||
this.reconnectCallback = null;
|
||||
this.errorCallback = null;
|
||||
this.closeCallback = null;
|
||||
}
|
||||
|
||||
initialize(connectionUrl) {
|
||||
if (this.conn) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.connectFailCount === 0) {
|
||||
console.log('websocket connecting to ' + connectionUrl); //eslint-disable-line no-console
|
||||
}
|
||||
|
||||
this.manuallyClosed = false;
|
||||
|
||||
this.conn = new WebSocket(connectionUrl);
|
||||
|
||||
this.conn.onopen = () => {
|
||||
if (this.reconnectCallback) {
|
||||
this.reconnectCallback();
|
||||
}
|
||||
|
||||
if (this.connectFailCount > 0) {
|
||||
console.log('websocket re-established connection'); //eslint-disable-line no-console
|
||||
}
|
||||
|
||||
this.connectFailCount = 0;
|
||||
};
|
||||
|
||||
this.conn.onclose = () => {
|
||||
this.conn = null;
|
||||
this.sequence = 1;
|
||||
|
||||
if (this.connectFailCount === 0) {
|
||||
console.log('websocket closed'); //eslint-disable-line no-console
|
||||
}
|
||||
|
||||
if (this.manuallyClosed) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.connectFailCount++;
|
||||
|
||||
if (this.closeCallback) {
|
||||
this.closeCallback(this.connectFailCount);
|
||||
}
|
||||
|
||||
let retryTime = MIN_WEBSOCKET_RETRY_TIME;
|
||||
|
||||
// If we've failed a bunch of connections then start backing off
|
||||
if (this.connectFailCount > MAX_WEBSOCKET_FAILS) {
|
||||
retryTime = MIN_WEBSOCKET_RETRY_TIME * this.connectFailCount * this.connectFailCount;
|
||||
if (retryTime > MAX_WEBSOCKET_RETRY_TIME) {
|
||||
retryTime = MAX_WEBSOCKET_RETRY_TIME;
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(
|
||||
() => {
|
||||
this.initialize(connectionUrl);
|
||||
},
|
||||
retryTime
|
||||
);
|
||||
};
|
||||
|
||||
this.conn.onerror = (evt) => {
|
||||
if (this.connectFailCount <= 1) {
|
||||
console.log('websocket error'); //eslint-disable-line no-console
|
||||
console.log(evt); //eslint-disable-line no-console
|
||||
}
|
||||
|
||||
if (this.errorCallback) {
|
||||
this.errorCallback(evt);
|
||||
}
|
||||
};
|
||||
|
||||
this.conn.onmessage = (evt) => {
|
||||
const msg = JSON.parse(evt.data);
|
||||
if (msg.seq_reply) {
|
||||
if (msg.error) {
|
||||
console.log(msg); //eslint-disable-line no-console
|
||||
}
|
||||
|
||||
if (this.responseCallbacks[msg.seq_reply]) {
|
||||
this.responseCallbacks[msg.seq_reply](msg);
|
||||
Reflect.deleteProperty(this.responseCallbacks, msg.seq_reply);
|
||||
}
|
||||
} else if (this.eventCallback) {
|
||||
this.eventCallback(msg);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
setEventCallback(callback) {
|
||||
this.eventCallback = callback;
|
||||
}
|
||||
|
||||
setReconnectCallback(callback) {
|
||||
this.reconnectCallback = callback;
|
||||
}
|
||||
|
||||
setErrorCallback(callback) {
|
||||
this.errorCallback = callback;
|
||||
}
|
||||
|
||||
setCloseCallback(callback) {
|
||||
this.closeCallback = callback;
|
||||
}
|
||||
|
||||
close() {
|
||||
this.manuallyClosed = true;
|
||||
this.connectFailCount = 0;
|
||||
this.sequence = 1;
|
||||
if (this.conn && this.conn.readyState === WebSocket.OPEN) {
|
||||
this.conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
sendMessage(action, data, responseCallback) {
|
||||
const msg = {
|
||||
action,
|
||||
seq: this.sequence++,
|
||||
data
|
||||
};
|
||||
|
||||
if (responseCallback) {
|
||||
this.responseCallbacks[msg.seq] = responseCallback;
|
||||
}
|
||||
|
||||
if (this.conn && this.conn.readyState === WebSocket.OPEN) {
|
||||
this.conn.send(JSON.stringify(msg));
|
||||
} else if (!this.conn || this.conn.readyState === WebSocket.Closed) {
|
||||
this.conn = null;
|
||||
this.initialize();
|
||||
}
|
||||
}
|
||||
|
||||
userTyping(channelId, parentId) {
|
||||
const data = {};
|
||||
data.channel_id = channelId;
|
||||
data.parent_id = parentId;
|
||||
|
||||
this.sendMessage('user_typing', data);
|
||||
}
|
||||
|
||||
getStatuses(callback) {
|
||||
this.sendMessage('get_statuses', null, callback);
|
||||
}
|
||||
}
|
||||
Ссылка в новой задаче
Block a user