2017-08-09 20:18:00 +00:00
|
|
|
import Meteor from 'react-native-meteor';
|
|
|
|
import Random from 'react-native-meteor/lib/Random';
|
2017-08-13 01:35:09 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
2017-08-13 23:02:46 +00:00
|
|
|
import { hashPassword } from 'react-native-meteor/lib/utils';
|
2017-08-13 01:35:09 +00:00
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
import RNFetchBlob from 'react-native-fetch-blob';
|
2017-09-21 17:08:00 +00:00
|
|
|
import reduxStore from './createStore';
|
2017-08-13 23:02:46 +00:00
|
|
|
import settingsType from '../constants/settings';
|
2017-08-09 20:18:00 +00:00
|
|
|
import realm from './realm';
|
2017-08-13 23:02:46 +00:00
|
|
|
import * as actions from '../actions';
|
2017-08-17 16:55:47 +00:00
|
|
|
import { disconnect, connectSuccess } from '../actions/connect';
|
2017-08-15 19:28:46 +00:00
|
|
|
|
2017-08-09 20:18:00 +00:00
|
|
|
export { Accounts } from 'react-native-meteor';
|
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
const call = (method, ...params) => new Promise((resolve, reject) => {
|
|
|
|
Meteor.call(method, ...params, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
2017-11-07 20:25:04 +00:00
|
|
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
2017-08-11 18:18:09 +00:00
|
|
|
|
2017-08-13 01:35:09 +00:00
|
|
|
const RocketChat = {
|
2017-08-10 16:25:50 +00:00
|
|
|
createChannel({ name, users, type }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call(type ? 'createChannel' : 'createPrivateGroup', name, users, type, (err, res) => (err ? reject(err) : resolve(res)));
|
|
|
|
});
|
|
|
|
},
|
2017-08-13 01:35:09 +00:00
|
|
|
|
|
|
|
async getUserToken() {
|
|
|
|
try {
|
|
|
|
return await AsyncStorage.getItem(TOKEN_KEY);
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(`AsyncStorage error: ${ error.message }`);
|
|
|
|
}
|
|
|
|
},
|
2017-09-01 19:42:50 +00:00
|
|
|
async testServer(url) {
|
|
|
|
if (/^(https?:\/\/)?(((\w|[0-9])+(\.(\w|[0-9-_])+)+)|localhost)(:\d+)?$/.test(url)) {
|
|
|
|
const response = await fetch(url, { method: 'HEAD' });
|
|
|
|
if (response.status === 200 && response.headers.get('x-instance-id') != null && response.headers.get('x-instance-id').length) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error({ error: 'invalid server' });
|
|
|
|
},
|
2017-08-18 21:30:16 +00:00
|
|
|
connect(_url) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const url = `${ _url }/websocket`;
|
2017-08-09 20:18:00 +00:00
|
|
|
|
2017-08-17 16:55:47 +00:00
|
|
|
Meteor.connect(url, { autoConnect: true, autoReconnect: true });
|
|
|
|
Meteor.ddp.on('disconnected', () => {
|
|
|
|
reduxStore.dispatch(disconnect());
|
|
|
|
});
|
2017-08-18 21:30:16 +00:00
|
|
|
Meteor.ddp.on('connected', () => {
|
2017-08-17 20:15:24 +00:00
|
|
|
reduxStore.dispatch(connectSuccess());
|
|
|
|
resolve();
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
2017-08-17 16:55:47 +00:00
|
|
|
Meteor.ddp.on('connected', () => {
|
|
|
|
Meteor.call('public-settings/get', (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
|
2017-08-17 16:55:47 +00:00
|
|
|
const settings = {};
|
2017-08-09 20:18:00 +00:00
|
|
|
realm.write(() => {
|
2017-08-17 16:55:47 +00:00
|
|
|
data.forEach((item) => {
|
|
|
|
const setting = {
|
|
|
|
_id: item._id
|
|
|
|
};
|
2017-09-01 19:42:50 +00:00
|
|
|
setting._server = { id: reduxStore.getState().server.server };
|
2017-08-17 16:55:47 +00:00
|
|
|
if (settingsType[item.type]) {
|
|
|
|
setting[settingsType[item.type]] = item.value;
|
|
|
|
realm.create('settings', setting, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
settings[item._id] = item.value;
|
|
|
|
});
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
2017-08-17 16:55:47 +00:00
|
|
|
reduxStore.dispatch(actions.setAllSettings(settings));
|
|
|
|
});
|
|
|
|
|
|
|
|
Meteor.ddp.on('changed', (ddbMessage) => {
|
|
|
|
if (ddbMessage.collection === 'stream-room-messages') {
|
|
|
|
realm.write(() => {
|
|
|
|
const message = ddbMessage.fields.args[0];
|
|
|
|
message.temp = false;
|
2017-09-01 19:42:50 +00:00
|
|
|
message._server = { id: reduxStore.getState().server.server };
|
2017-08-17 16:55:47 +00:00
|
|
|
realm.create('messages', message, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ddbMessage.collection === 'stream-notify-user') {
|
|
|
|
realm.write(() => {
|
|
|
|
const data = ddbMessage.fields.args[1];
|
2017-09-01 19:42:50 +00:00
|
|
|
data._server = { id: reduxStore.getState().server.server };
|
2017-08-17 16:55:47 +00:00
|
|
|
realm.create('subscriptions', data, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
2017-08-18 21:30:16 +00:00
|
|
|
})
|
|
|
|
.catch(e => console.error(e));
|
2017-08-09 20:18:00 +00:00
|
|
|
},
|
|
|
|
|
2017-08-17 16:55:47 +00:00
|
|
|
login(params, callback) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-16 23:29:12 +00:00
|
|
|
Meteor._startLoggingIn();
|
2017-08-17 01:09:44 +00:00
|
|
|
return Meteor.call('login', params, (err, result) => {
|
2017-08-16 23:29:12 +00:00
|
|
|
Meteor._endLoggingIn();
|
|
|
|
Meteor._handleLoginCallback(err, result);
|
2017-08-18 21:30:16 +00:00
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
2017-08-16 23:29:12 +00:00
|
|
|
if (typeof callback === 'function') {
|
|
|
|
callback(err, result);
|
|
|
|
}
|
|
|
|
});
|
2017-08-13 23:02:46 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-13 13:35:01 +00:00
|
|
|
me({ server, token, userId }) {
|
|
|
|
return fetch(`${ server }/api/v1/me`, {
|
|
|
|
method: 'get',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Auth-Token': token,
|
|
|
|
'X-User-Id': userId
|
|
|
|
}
|
|
|
|
}).then(response => response.json());
|
|
|
|
},
|
|
|
|
|
2017-11-07 16:28:02 +00:00
|
|
|
register({ credentials }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('registerUser', credentials, (err, userId) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
resolve(userId);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setUsername({ credentials }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('setUsername', credentials.username, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-10 13:42:02 +00:00
|
|
|
forgotPassword(email) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('sendForgotPasswordEmail', email, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-08-14 14:15:37 +00:00
|
|
|
loginWithPassword({ username, password, code }, callback) {
|
2017-08-13 23:02:46 +00:00
|
|
|
let params = {};
|
|
|
|
const state = reduxStore.getState();
|
|
|
|
|
|
|
|
if (state.settings.LDAP_Enable) {
|
|
|
|
params = {
|
|
|
|
ldap: true,
|
|
|
|
username,
|
|
|
|
ldapPass: password,
|
|
|
|
ldapOptions: {}
|
|
|
|
};
|
|
|
|
} else if (state.settings.CROWD_Enable) {
|
|
|
|
params = {
|
|
|
|
crowd: true,
|
|
|
|
username,
|
|
|
|
crowdPassword: password
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
params = {
|
|
|
|
password: hashPassword(password),
|
|
|
|
user: {
|
|
|
|
username
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-17 01:09:44 +00:00
|
|
|
if (typeof username === 'string' && username.indexOf('@') !== -1) {
|
|
|
|
params.user = { email: username };
|
2017-08-13 23:02:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 00:31:22 +00:00
|
|
|
if (code) {
|
|
|
|
params = {
|
|
|
|
totp: {
|
|
|
|
login: params,
|
|
|
|
code
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:29:12 +00:00
|
|
|
return this.login(params, callback);
|
2017-08-09 20:18:00 +00:00
|
|
|
},
|
|
|
|
|
2017-11-08 20:23:46 +00:00
|
|
|
// loadRooms(cb) {
|
|
|
|
// console.warn('a');
|
|
|
|
// Meteor.call('rooms/get', (err, data) => {
|
|
|
|
// if (err) {
|
|
|
|
// console.error(err);
|
|
|
|
// }
|
|
|
|
// console.warn(`rooms ${ data.length }`);
|
|
|
|
// if (data.length) {
|
|
|
|
// realm.write(() => {
|
|
|
|
// data.forEach((room) => {
|
|
|
|
// room._server = { id: reduxStore.getState().server.server };
|
|
|
|
// realm.create('rooms', room, true);
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return cb && cb();
|
|
|
|
// });
|
|
|
|
// },
|
|
|
|
|
2017-08-09 20:18:00 +00:00
|
|
|
loadSubscriptions(cb) {
|
|
|
|
Meteor.call('subscriptions/get', (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2017-08-11 18:18:09 +00:00
|
|
|
if (data.length) {
|
|
|
|
realm.write(() => {
|
|
|
|
data.forEach((subscription) => {
|
|
|
|
// const subscription = {
|
|
|
|
// _id: item._id
|
|
|
|
// };
|
|
|
|
// if (typeof item.value === 'string') {
|
|
|
|
// subscription.value = item.value;
|
|
|
|
// }
|
2017-09-01 19:42:50 +00:00
|
|
|
subscription._server = { id: reduxStore.getState().server.server };
|
2017-08-14 18:02:53 +00:00
|
|
|
// write('subscriptions', subscription);
|
2017-08-11 18:18:09 +00:00
|
|
|
realm.create('subscriptions', subscription, true);
|
|
|
|
});
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
2017-08-11 18:18:09 +00:00
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
|
|
|
|
return cb && cb();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-08-10 23:21:46 +00:00
|
|
|
loadMessagesForRoom(rid, end, cb) {
|
2017-08-17 06:28:41 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('loadHistory', rid, end, 20, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
if (cb) {
|
|
|
|
cb({ end: true });
|
|
|
|
}
|
|
|
|
return reject(err);
|
2017-08-10 23:21:46 +00:00
|
|
|
}
|
2017-08-17 20:15:24 +00:00
|
|
|
if (data && data.messages.length) {
|
2017-08-17 06:28:41 +00:00
|
|
|
realm.write(() => {
|
|
|
|
data.messages.forEach((message) => {
|
|
|
|
message.temp = false;
|
2017-09-01 19:42:50 +00:00
|
|
|
message._server = { id: reduxStore.getState().server.server };
|
2017-08-17 06:28:41 +00:00
|
|
|
// write('messages', message);
|
|
|
|
realm.create('messages', message, true);
|
|
|
|
});
|
2017-08-11 18:18:09 +00:00
|
|
|
});
|
2017-08-17 06:28:41 +00:00
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
|
2017-08-17 06:28:41 +00:00
|
|
|
if (cb) {
|
2017-08-17 20:15:24 +00:00
|
|
|
if (data && data.messages.length < 20) {
|
2017-08-17 06:28:41 +00:00
|
|
|
cb({ end: true });
|
|
|
|
} else {
|
|
|
|
cb({ end: false });
|
|
|
|
}
|
2017-08-10 23:21:46 +00:00
|
|
|
}
|
2017-08-17 06:28:41 +00:00
|
|
|
resolve();
|
|
|
|
Meteor.subscribe('stream-room-messages', rid, false);
|
|
|
|
});
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
getMessage(rid, msg = {}) {
|
2017-08-09 20:18:00 +00:00
|
|
|
const _id = Random.id();
|
2017-08-21 00:11:46 +00:00
|
|
|
// console.log('reduxStore.getState().login.id ', reduxStore.getState().login);
|
2017-08-15 19:28:46 +00:00
|
|
|
const message = {
|
|
|
|
_id,
|
|
|
|
rid,
|
|
|
|
msg,
|
|
|
|
ts: new Date(),
|
|
|
|
_updatedAt: new Date(),
|
|
|
|
temp: true,
|
2017-09-01 19:42:50 +00:00
|
|
|
_server: { id: reduxStore.getState().server.server },
|
2017-08-15 19:28:46 +00:00
|
|
|
u: {
|
2017-08-21 00:11:46 +00:00
|
|
|
_id: reduxStore.getState().login.user.id || '1',
|
|
|
|
username: reduxStore.getState().login.user.id
|
2017-08-15 19:28:46 +00:00
|
|
|
}
|
|
|
|
};
|
2017-08-09 20:18:00 +00:00
|
|
|
|
|
|
|
realm.write(() => {
|
2017-08-15 19:28:46 +00:00
|
|
|
realm.create('messages', message, true);
|
|
|
|
// write('messages', message, true);
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
2017-08-15 19:28:46 +00:00
|
|
|
return message;
|
|
|
|
},
|
|
|
|
sendMessage(rid, msg) {
|
|
|
|
const tempMessage = this.getMessage(rid, msg);
|
|
|
|
return call('sendMessage', { _id: tempMessage._id, rid, msg });
|
2017-08-10 16:16:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
spotlight(search, usernames) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('spotlight', search, usernames, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
createDirectMessage(username) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('createDirectMessage', username, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-08-11 18:18:09 +00:00
|
|
|
readMessages(rid) {
|
|
|
|
return call('readMessages', rid);
|
|
|
|
},
|
2017-08-10 16:16:32 +00:00
|
|
|
joinRoom(rid) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('joinRoom', rid, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
2017-08-10 20:09:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
"name":"yXfExLErmNR5eNPx7.png"
|
|
|
|
"size":961
|
|
|
|
"type":"image/png"
|
|
|
|
"rid":"GENERAL"
|
|
|
|
"description":""
|
|
|
|
"store":"fileSystem"
|
|
|
|
*/
|
2017-08-15 19:28:46 +00:00
|
|
|
_ufsCreate(fileInfo) {
|
|
|
|
// return call('ufsCreate', fileInfo);
|
2017-08-10 20:09:54 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('ufsCreate', fileInfo, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// ["ZTE8CKHJt7LATv7Me","fileSystem","e8E96b2819"
|
2017-08-15 19:28:46 +00:00
|
|
|
_ufsComplete(fileId, store, token) {
|
2017-08-10 20:09:54 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('ufsComplete', fileId, store, token, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
- "GENERAL"
|
|
|
|
- {
|
|
|
|
"type":"image/png",
|
|
|
|
"size":961,
|
|
|
|
"name":"yXfExLErmNR5eNPx7.png",
|
|
|
|
"description":"",
|
|
|
|
"url":"/ufs/fileSystem/ZTE8CKHJt7LATv7Me/yXfExLErmNR5eNPx7.png"
|
|
|
|
}
|
|
|
|
*/
|
2017-08-15 19:28:46 +00:00
|
|
|
_sendFileMessage(rid, data, msg = {}) {
|
2017-08-10 20:09:54 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-15 19:28:46 +00:00
|
|
|
Meteor.call('sendFileMessage', rid, null, data, msg, (error, result) => {
|
2017-08-10 20:09:54 +00:00
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 14:25:17 +00:00
|
|
|
},
|
2017-08-15 19:28:46 +00:00
|
|
|
async sendFileMessage(rid, fileInfo, data) {
|
|
|
|
const placeholder = RocketChat.getMessage(rid, 'Sending an image');
|
|
|
|
try {
|
|
|
|
const result = await RocketChat._ufsCreate({ ...fileInfo, rid });
|
|
|
|
|
|
|
|
await RNFetchBlob.fetch('POST', result.url, {
|
|
|
|
'Content-Type': 'application/octet-stream'
|
|
|
|
}, data);
|
|
|
|
|
|
|
|
const completeRresult = await RocketChat._ufsComplete(result.fileId, fileInfo.store, result.token);
|
|
|
|
|
|
|
|
return await RocketChat._sendFileMessage(completeRresult.rid, {
|
|
|
|
_id: completeRresult._id,
|
|
|
|
type: completeRresult.type,
|
|
|
|
size: completeRresult.size,
|
|
|
|
name: completeRresult.name,
|
|
|
|
url: completeRresult.path
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return e;
|
|
|
|
} finally {
|
|
|
|
realm.write(() => {
|
|
|
|
const msg = realm.objects('messages').filtered('_id = $0', placeholder._id);
|
|
|
|
realm.delete(msg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2017-08-17 02:06:22 +00:00
|
|
|
getRooms() {
|
|
|
|
return Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => {
|
2017-09-21 17:08:00 +00:00
|
|
|
const { server, login } = reduxStore.getState();
|
2017-11-08 20:23:46 +00:00
|
|
|
const data = subscriptions.map((subscription) => {
|
|
|
|
subscription._updatedAt = (rooms.find(room => room._id === subscription.rid) || {})._updatedAt;
|
2017-09-21 17:08:00 +00:00
|
|
|
subscription._server = { id: server.server };
|
2017-08-17 02:06:22 +00:00
|
|
|
return subscription;
|
|
|
|
});
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2017-08-17 02:06:22 +00:00
|
|
|
realm.write(() => {
|
|
|
|
data.forEach((subscription) => {
|
|
|
|
realm.create('subscriptions', subscription, true);
|
|
|
|
});
|
|
|
|
});
|
2017-09-21 17:08:00 +00:00
|
|
|
Meteor.subscribe('stream-notify-user', `${ login.user.id }/subscriptions-changed`, false);
|
2017-08-17 02:06:22 +00:00
|
|
|
return data;
|
2017-09-21 17:08:00 +00:00
|
|
|
});
|
2017-08-17 02:06:22 +00:00
|
|
|
},
|
2017-11-07 20:25:04 +00:00
|
|
|
logout({ server }) {
|
|
|
|
Meteor.logout();
|
|
|
|
AsyncStorage.removeItem(TOKEN_KEY);
|
|
|
|
AsyncStorage.removeItem(`${ TOKEN_KEY }-${ server }`);
|
2017-08-09 20:18:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RocketChat;
|