diff --git a/app/lib/meteor.js b/app/lib/meteor.js
deleted file mode 100644
index 0ce5a5ef..00000000
--- a/app/lib/meteor.js
+++ /dev/null
@@ -1,173 +0,0 @@
-import Meteor from 'react-native-meteor';
-import Random from 'react-native-meteor/lib/Random';
-import realm from './realm';
-
-export { Accounts } from 'react-native-meteor';
-
-const RocketChat = {
- get currentServer() {
- const current = realm.objects('servers').filtered('current = true')[0];
- 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);
- });
- }
-};
-
-export default RocketChat;
-
-Meteor.Accounts.onLogin(() => {
- Meteor.call('subscriptions/get', (err, data) => {
- if (err) {
- console.error(err);
- }
-
- realm.write(() => {
- data.forEach((subscription) => {
- // const subscription = {
- // _id: item._id
- // };
- // if (typeof item.value === 'string') {
- // subscription.value = item.value;
- // }
- subscription._server = { id: RocketChat.currentServer };
- realm.create('subscriptions', subscription, true);
- });
- });
- Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/subscriptions-changed`, false);
- });
-});
-
-export function 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);
- }
-
- realm.write(() => {
- data.forEach((item) => {
- const setting = {
- _id: item._id
- };
- setting._server = { id: RocketChat.currentServer };
- if (typeof item.value === 'string') {
- setting.value = item.value;
- }
- realm.create('settings', setting, true);
- });
- });
-
- cb();
- });
-
- Meteor.ddp.on('changed', (ddbMessage) => {
- console.log('changed', ddbMessage);
- if (ddbMessage.collection === 'stream-room-messages') {
- realm.write(() => {
- const message = ddbMessage.fields.args[0];
- message.temp = false;
- message._server = { id: RocketChat.currentServer };
- realm.create('messages', message, true);
- });
- }
-
- if (ddbMessage.collection === 'stream-notify-user') {
- realm.write(() => {
- const data = ddbMessage.fields.args[1];
- data._server = { id: RocketChat.currentServer };
- realm.create('subscriptions', data, true);
- });
- }
- });
- });
-}
-
-export function loginWithPassword(selector, password, cb) {
- Meteor.loginWithPassword(selector, password, () => cb && cb());
-}
-
-export function loadSubscriptions(cb) {
- Meteor.call('subscriptions/get', (err, data) => {
- if (err) {
- console.error(err);
- }
-
- realm.write(() => {
- data.forEach((subscription) => {
- // const subscription = {
- // _id: item._id
- // };
- // if (typeof item.value === 'string') {
- // subscription.value = item.value;
- // }
- subscription._server = { id: RocketChat.currentServer };
- realm.create('subscriptions', subscription, true);
- });
- });
-
- return cb && cb();
- });
-}
-
-export function loadMessagesForRoom(rid, cb) {
- Meteor.call('loadHistory', rid, null, 50, (err, data) => {
- if (err) {
- console.error(err);
- }
-
- realm.write(() => {
- data.messages.forEach((message) => {
- message.temp = false;
- message._server = { id: RocketChat.currentServer };
- realm.create('messages', message, true);
- });
- });
-
- if (cb) {
- cb();
- }
- });
-
- Meteor.subscribe('stream-room-messages', rid, false);
-}
-
-export function sendMessage(rid, msg) {
- const _id = Random.id();
- const user = Meteor.user();
-
- realm.write(() => {
- 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);
- });
- });
-}
diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js
new file mode 100644
index 00000000..3ed431b5
--- /dev/null
+++ b/app/lib/rocketchat.js
@@ -0,0 +1,175 @@
+import Meteor from 'react-native-meteor';
+import Random from 'react-native-meteor/lib/Random';
+import realm from './realm';
+
+export { Accounts } from 'react-native-meteor';
+
+const RocketChat = {
+ get currentServer() {
+ const current = realm.objects('servers').filtered('current = true')[0];
+ 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);
+ });
+ },
+
+ 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);
+ }
+
+ realm.write(() => {
+ data.forEach((item) => {
+ const setting = {
+ _id: item._id
+ };
+ setting._server = { id: RocketChat.currentServer };
+ if (typeof item.value === 'string') {
+ setting.value = item.value;
+ }
+ realm.create('settings', setting, true);
+ });
+ });
+
+ if (cb) {
+ cb();
+ }
+ });
+
+ Meteor.ddp.on('changed', (ddbMessage) => {
+ console.log('changed', ddbMessage);
+ if (ddbMessage.collection === 'stream-room-messages') {
+ realm.write(() => {
+ const message = ddbMessage.fields.args[0];
+ message.temp = false;
+ message._server = { id: RocketChat.currentServer };
+ realm.create('messages', message, true);
+ });
+ }
+
+ if (ddbMessage.collection === 'stream-notify-user') {
+ realm.write(() => {
+ const data = ddbMessage.fields.args[1];
+ data._server = { id: RocketChat.currentServer };
+ realm.create('subscriptions', data, true);
+ });
+ }
+ });
+ });
+ },
+
+ loginWithPassword(selector, password, cb) {
+ Meteor.loginWithPassword(selector, password, () => cb && cb());
+ },
+
+ loadSubscriptions(cb) {
+ Meteor.call('subscriptions/get', (err, data) => {
+ if (err) {
+ console.error(err);
+ }
+
+ realm.write(() => {
+ data.forEach((subscription) => {
+ // const subscription = {
+ // _id: item._id
+ // };
+ // if (typeof item.value === 'string') {
+ // subscription.value = item.value;
+ // }
+ subscription._server = { id: RocketChat.currentServer };
+ realm.create('subscriptions', subscription, true);
+ });
+ });
+
+ return cb && cb();
+ });
+ },
+
+ loadMessagesForRoom(rid, cb) {
+ Meteor.call('loadHistory', rid, null, 50, (err, data) => {
+ if (err) {
+ console.error(err);
+ }
+
+ realm.write(() => {
+ data.messages.forEach((message) => {
+ message.temp = false;
+ message._server = { id: RocketChat.currentServer };
+ realm.create('messages', message, true);
+ });
+ });
+
+ if (cb) {
+ cb();
+ }
+ });
+
+ Meteor.subscribe('stream-room-messages', rid, false);
+ },
+
+ sendMessage(rid, msg) {
+ const _id = Random.id();
+ const user = Meteor.user();
+
+ realm.write(() => {
+ 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);
+ });
+ });
+ }
+};
+
+export default RocketChat;
+
+Meteor.Accounts.onLogin(() => {
+ Meteor.call('subscriptions/get', (err, data) => {
+ if (err) {
+ console.error(err);
+ }
+
+ realm.write(() => {
+ data.forEach((subscription) => {
+ // const subscription = {
+ // _id: item._id
+ // };
+ // if (typeof item.value === 'string') {
+ // subscription.value = item.value;
+ // }
+ subscription._server = { id: RocketChat.currentServer };
+ realm.create('subscriptions', subscription, true);
+ });
+ });
+ Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/subscriptions-changed`, false);
+ });
+});
diff --git a/app/views/login.js b/app/views/login.js
index 01445905..962d733a 100644
--- a/app/views/login.js
+++ b/app/views/login.js
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import { TextInput, StyleSheet } from 'react-native';
-import { loginWithPassword } from '../lib/meteor';
+import RocketChat from '../lib/rocketchat';
import KeyboardView from '../components/KeyboardView';
@@ -42,7 +42,7 @@ export default class LoginView extends React.Component {
};
this.submit = () => {
- loginWithPassword({ username: this.state.username }, this.state.password, () => {
+ RocketChat.loginWithPassword({ username: this.state.username }, this.state.password, () => {
this.props.navigation.dispatch({ type: 'Navigation/BACK' });
});
};
diff --git a/app/views/room.js b/app/views/room.js
index 1548ab94..1fd2832a 100644
--- a/app/views/room.js
+++ b/app/views/room.js
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { Text, View, FlatList, StyleSheet } from 'react-native';
// import Markdown from 'react-native-simple-markdown';
import realm from '../lib/realm';
-import RocketChat, { loadMessagesForRoom, sendMessage } from '../lib/meteor';
+import RocketChat from '../lib/rocketchat';
import Message from '../components/Message';
import MessageBox from '../components/MessageBox';
@@ -54,7 +54,7 @@ export default class RoomView extends React.Component {
}
componentWillMount() {
- loadMessagesForRoom(this.rid, () => {
+ RocketChat.loadMessagesForRoom(this.rid, () => {
this.setState({
...this.state,
loaded: true
@@ -76,7 +76,7 @@ export default class RoomView extends React.Component {
});
};
- sendMessage = message => sendMessage(this.rid, message);
+ sendMessage = message => RocketChat.sendMessage(this.rid, message);
renderSeparator = () => (
diff --git a/app/views/roomsList.js b/app/views/roomsList.js
index f76bddcf..e7f07c36 100644
--- a/app/views/roomsList.js
+++ b/app/views/roomsList.js
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { Text, View, FlatList, StyleSheet } from 'react-native';
import Meteor from 'react-native-meteor';
import realm from '../lib/realm';
-import RocketChat, { connect } from '../lib/meteor';
+import RocketChat from '../lib/rocketchat';
import RoomItem from '../components/RoomItem';
@@ -75,9 +75,7 @@ export default class RoomsListView extends React.Component {
navigation = this.props.navigation;
if (RocketChat.currentServer) {
- connect(() => {
- // navigation.navigate('Login');
- });
+ RocketChat.connect();
} else {
navigation.navigate('ListServerModal');
}
@@ -127,7 +125,7 @@ export default class RoomsListView extends React.Component {
Authenticating...
- )
+ );
}
}
diff --git a/app/views/serverList.js b/app/views/serverList.js
index 6396bac9..a149a6f4 100644
--- a/app/views/serverList.js
+++ b/app/views/serverList.js
@@ -4,7 +4,7 @@ import Zeroconf from 'react-native-zeroconf';
import { View, Text, SectionList, Button, StyleSheet } from 'react-native';
import realm from '../lib/realm';
-import RocketChat, { connect } from '../lib/meteor';
+import RocketChat from '../lib/rocketchat';
const styles = StyleSheet.create({
view: {
@@ -88,7 +88,7 @@ export default class ListServerView extends React.Component {
onPressItem(item) {
RocketChat.currentServer = item.id;
- connect(() => {});
+ RocketChat.connect();
this.props.navigation.dispatch({ type: 'Navigation/BACK' });
}
diff --git a/app/views/serverNew.js b/app/views/serverNew.js
index eb3f9580..3a933e8f 100644
--- a/app/views/serverNew.js
+++ b/app/views/serverNew.js
@@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import { TextInput, StyleSheet } from 'react-native';
-import RocketChat from '../lib/meteor';
+import RocketChat from '../lib/rocketchat';
import KeyboardView from '../components/KeyboardView';