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-13 23:02:46 +00:00
|
|
|
import reduxStore from '../lib/createStore';
|
|
|
|
import settingsType from '../constants/settings';
|
2017-08-09 20:18:00 +00:00
|
|
|
import realm from './realm';
|
2017-08-11 18:18:09 +00:00
|
|
|
import debounce from '../utils/debounce';
|
2017-08-13 23:02:46 +00:00
|
|
|
import * as actions from '../actions';
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const write = (() => {
|
|
|
|
const cache = [];
|
|
|
|
const run = debounce(() => {
|
|
|
|
if (!cache.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
realm.write(() => {
|
|
|
|
cache.forEach(([name, obj]) => {
|
|
|
|
realm.create(name, obj, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// cache = [];
|
|
|
|
}, 1000);
|
|
|
|
return (name, obj) => {
|
|
|
|
cache.push([name, obj]);
|
|
|
|
run();
|
|
|
|
};
|
|
|
|
})();
|
2017-08-10 16:25:50 +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
|
|
|
|
2017-08-09 20:18:00 +00:00
|
|
|
get currentServer() {
|
2017-08-11 19:57:09 +00:00
|
|
|
const current = realm.objects('servers').filtered('current = true').slice(0, 1)[0];
|
2017-08-09 20:18:00 +00:00
|
|
|
return current && current.id;
|
|
|
|
},
|
|
|
|
|
|
|
|
set currentServer(server) {
|
|
|
|
realm.write(() => {
|
|
|
|
realm.objects('servers').filtered('current = true').forEach(item => (item.current = false));
|
|
|
|
realm.create('servers', { id: server, current: true }, true);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-08-13 01:35:09 +00:00
|
|
|
async getUserToken() {
|
|
|
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
|
|
|
try {
|
|
|
|
return await AsyncStorage.getItem(TOKEN_KEY);
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(`AsyncStorage error: ${ error.message }`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-08-09 20:18:00 +00:00
|
|
|
connect(cb) {
|
|
|
|
const url = `${ RocketChat.currentServer }/websocket`;
|
|
|
|
|
|
|
|
Meteor.connect(url);
|
|
|
|
|
|
|
|
Meteor.ddp.on('connected', () => {
|
|
|
|
console.log('connected');
|
|
|
|
|
|
|
|
Meteor.call('public-settings/get', (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
2017-08-13 23:02:46 +00:00
|
|
|
const settings = {};
|
2017-08-09 20:18:00 +00:00
|
|
|
realm.write(() => {
|
|
|
|
data.forEach((item) => {
|
|
|
|
const setting = {
|
|
|
|
_id: item._id
|
|
|
|
};
|
|
|
|
setting._server = { id: RocketChat.currentServer };
|
2017-08-13 23:02:46 +00:00
|
|
|
if (settingsType[item.type]) {
|
|
|
|
setting[settingsType[item.type]] = item.value;
|
|
|
|
realm.create('settings', setting, true);
|
2017-08-09 20:18:00 +00:00
|
|
|
}
|
2017-08-13 23:02:46 +00:00
|
|
|
|
|
|
|
settings[item._id] = item.value;
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
|
|
|
});
|
2017-08-13 23:02:46 +00:00
|
|
|
reduxStore.dispatch(actions.setAllSettings(settings));
|
2017-08-09 20:18:00 +00:00
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Meteor.ddp.on('changed', (ddbMessage) => {
|
2017-08-10 20:09:54 +00:00
|
|
|
// console.log('changed', ddbMessage);
|
2017-08-09 20:18:00 +00:00
|
|
|
if (ddbMessage.collection === 'stream-room-messages') {
|
|
|
|
realm.write(() => {
|
|
|
|
const message = ddbMessage.fields.args[0];
|
|
|
|
message.temp = false;
|
|
|
|
message._server = { id: RocketChat.currentServer };
|
2017-08-11 18:18:09 +00:00
|
|
|
// write('messages', message);
|
2017-08-09 20:18:00 +00:00
|
|
|
realm.create('messages', message, true);
|
|
|
|
});
|
|
|
|
}
|
2017-08-13 01:35:09 +00:00
|
|
|
|
2017-08-11 18:18:09 +00:00
|
|
|
this.subCache = this.subCache || {};
|
|
|
|
this.roomCache = this.roomCache || {};
|
|
|
|
this.cache = {};
|
2017-08-09 20:18:00 +00:00
|
|
|
if (ddbMessage.collection === 'stream-notify-user') {
|
2017-08-11 18:18:09 +00:00
|
|
|
const data = ddbMessage.fields.args[1];
|
|
|
|
let key;
|
|
|
|
if (ddbMessage.fields.eventName && ddbMessage.fields.eventName.indexOf('rooms-changed') > -1) {
|
|
|
|
this.roomCache[data._id] = data;
|
|
|
|
key = data._id;
|
|
|
|
} else {
|
|
|
|
this.subCache[data.rid] = data;
|
|
|
|
key = data.rid;
|
|
|
|
delete this.subCache[key]._updatedAt;
|
|
|
|
}
|
|
|
|
this.cache[key] = this.cache[key] ||
|
|
|
|
setTimeout(() => {
|
|
|
|
this.subCache[key] = this.subCache[key] || realm.objects('subscriptions').filtered('rid = $0', key).slice(0, 1)[0];
|
|
|
|
if (this.roomCache[key]) {
|
|
|
|
this.subCache[key]._updatedAt = this.roomCache[key]._updatedAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
write('subscriptions', this.subCache[key]);
|
|
|
|
delete this.subCache[key];
|
|
|
|
delete this.roomCache[key];
|
|
|
|
delete this.cache[key];
|
|
|
|
}, 550);
|
2017-08-09 20:18:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-08-13 23:02:46 +00:00
|
|
|
login(params, callback) {
|
|
|
|
Meteor._startLoggingIn();
|
|
|
|
Meteor.call('login', params, (err, result) => {
|
|
|
|
Meteor._endLoggingIn();
|
|
|
|
|
|
|
|
Meteor._handleLoginCallback(err, result);
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof username === 'string') {
|
|
|
|
if (username.indexOf('@') !== -1) {
|
|
|
|
params.user = { email: username };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 00:31:22 +00:00
|
|
|
if (code) {
|
|
|
|
params = {
|
|
|
|
totp: {
|
|
|
|
login: params,
|
|
|
|
code
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-13 23:02:46 +00:00
|
|
|
this.login(params, callback);
|
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;
|
|
|
|
// }
|
|
|
|
subscription._server = { id: RocketChat.currentServer };
|
|
|
|
write('subscriptions', subscription);
|
|
|
|
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) {
|
|
|
|
Meteor.call('loadHistory', rid, end, 20, (err, data) => {
|
2017-08-09 20:18:00 +00:00
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
2017-08-10 23:21:46 +00:00
|
|
|
if (cb) {
|
|
|
|
cb({ end: true });
|
|
|
|
}
|
|
|
|
return;
|
2017-08-09 20:18:00 +00:00
|
|
|
}
|
2017-08-11 18:18:09 +00:00
|
|
|
if (data.messages.length) {
|
|
|
|
realm.write(() => {
|
|
|
|
data.messages.forEach((message) => {
|
|
|
|
message.temp = false;
|
|
|
|
message._server = { id: RocketChat.currentServer };
|
|
|
|
// write('messages', message);
|
|
|
|
realm.create('messages', message, 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
|
|
|
|
|
|
|
if (cb) {
|
2017-08-10 23:21:46 +00:00
|
|
|
if (data.messages.length < 20) {
|
|
|
|
cb({ end: true });
|
|
|
|
} else {
|
|
|
|
cb({ end: false });
|
|
|
|
}
|
2017-08-09 20:18:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Meteor.subscribe('stream-room-messages', rid, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
sendMessage(rid, msg) {
|
|
|
|
const _id = Random.id();
|
|
|
|
const user = Meteor.user();
|
|
|
|
|
|
|
|
realm.write(() => {
|
2017-08-11 18:18:09 +00:00
|
|
|
// write('messages', {
|
|
|
|
// _id,
|
|
|
|
// rid,
|
|
|
|
// msg,
|
|
|
|
// ts: new Date(),
|
|
|
|
// _updatedAt: new Date(),
|
|
|
|
// temp: true,
|
|
|
|
// _server: { id: RocketChat.currentServer },
|
|
|
|
// u: {
|
|
|
|
// _id: user._id,
|
|
|
|
// username: user.username
|
|
|
|
// }
|
|
|
|
// });
|
2017-08-09 20:18:00 +00:00
|
|
|
realm.create('messages', {
|
|
|
|
_id,
|
|
|
|
rid,
|
|
|
|
msg,
|
|
|
|
ts: new Date(),
|
|
|
|
_updatedAt: new Date(),
|
|
|
|
temp: true,
|
|
|
|
_server: { id: RocketChat.currentServer },
|
|
|
|
u: {
|
|
|
|
_id: user._id,
|
|
|
|
username: user.username
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('sendMessage', { _id, rid, msg }, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
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"
|
|
|
|
*/
|
|
|
|
ufsCreate(fileInfo) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('ufsCreate', fileInfo, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// ["ZTE8CKHJt7LATv7Me","fileSystem","e8E96b2819"
|
|
|
|
ufsComplete(fileId, store, token) {
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
sendFileMessage(rid, message) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Meteor.call('sendFileMessage', rid, null, message, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve(result);
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 14:25:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
return AsyncStorage.clear();
|
2017-08-09 20:18:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RocketChat;
|
2017-08-13 01:35:09 +00:00
|
|
|
|
2017-08-13 23:02:46 +00:00
|
|
|
if (RocketChat.currentServer) {
|
|
|
|
reduxStore.dispatch(actions.setCurrentServer(RocketChat.currentServer));
|
|
|
|
}
|
|
|
|
|
2017-08-09 20:18:00 +00:00
|
|
|
Meteor.Accounts.onLogin(() => {
|
2017-08-11 18:18:09 +00:00
|
|
|
Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => {
|
|
|
|
subscriptions = subscriptions.sort((s1, s2) => (s1.rid > s2.rid ? 1 : -1));
|
|
|
|
rooms = rooms.sort((s1, s2) => (s1._id > s2._id ? 1 : -1));
|
|
|
|
const data = subscriptions.map((subscription, index) => {
|
|
|
|
subscription._updatedAt = rooms[index]._updatedAt;
|
|
|
|
return subscription;
|
|
|
|
});
|
2017-08-11 19:57:09 +00:00
|
|
|
Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/subscriptions-changed`, false);
|
|
|
|
Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/rooms-changed`, false);
|
2017-08-09 20:18:00 +00:00
|
|
|
realm.write(() => {
|
|
|
|
data.forEach((subscription) => {
|
2017-08-11 18:18:09 +00:00
|
|
|
// const subscription = {
|
|
|
|
// _id: item._id
|
|
|
|
// };
|
|
|
|
// if (typeof item.value === 'string') {
|
|
|
|
// subscription.value = item.value;
|
|
|
|
// }
|
2017-08-09 20:18:00 +00:00
|
|
|
subscription._server = { id: RocketChat.currentServer };
|
2017-08-11 18:18:09 +00:00
|
|
|
// write('subscriptions', subscription);
|
2017-08-09 20:18:00 +00:00
|
|
|
realm.create('subscriptions', subscription, true);
|
|
|
|
});
|
|
|
|
});
|
2017-08-11 18:18:09 +00:00
|
|
|
}).then(() => {
|
|
|
|
console.log('subscriptions done.');
|
2017-08-09 20:18:00 +00:00
|
|
|
});
|
|
|
|
});
|
2017-08-14 00:31:22 +00:00
|
|
|
|
|
|
|
// Use for logout
|
|
|
|
// AsyncStorage.clear();
|