From 86597c505cbe81e557d31e752b946e2ab90bbf91 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Tue, 8 Aug 2017 23:27:22 -0300 Subject: [PATCH] Fix multiserver --- app/lib/meteor.js | 24 ++++++++++++++++++++++-- app/lib/realm.js | 10 +++++++--- app/views/login.js | 14 +++++++------- app/views/room.js | 4 ++-- app/views/roomsList.js | 4 ++-- app/views/serverList.js | 7 ++----- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/app/lib/meteor.js b/app/lib/meteor.js index 1a8cef96..bfe06a55 100644 --- a/app/lib/meteor.js +++ b/app/lib/meteor.js @@ -4,9 +4,24 @@ 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(server => (server.current = false)); + realm.create('servers', { id: server, current: true }, true); + }); + } +} + +export default RocketChat; + export function connect(cb) { - const currentServer = realm.objects('servers').filtered('current = true')[0]; - const url = `${ currentServer.id }/websocket`; + const url = `${ RocketChat.currentServer }/websocket`; Meteor.connect(url); @@ -23,6 +38,7 @@ export function connect(cb) { const setting = { _id: item._id }; + setting._server = {id: RocketChat.currentServer}; if (typeof item.value === 'string') { setting.value = item.value; } @@ -40,6 +56,7 @@ export function connect(cb) { realm.write(() => { const message = ddbMessage.fields.args[0]; message.temp = false; + message._server = {id: RocketChat.currentServer}; realm.create('messages', message, true); }); }, 1000); @@ -66,6 +83,7 @@ export function loadSubscriptions(cb) { // if (typeof item.value === 'string') { // subscription.value = item.value; // } + subscription._server = {id: RocketChat.currentServer}; realm.create('subscriptions', subscription, true); }); }); @@ -83,6 +101,7 @@ export function loadMessagesForRoom(rid) { realm.write(() => { data.messages.forEach((message) => { message.temp = false; + message._server = {id: RocketChat.currentServer}; realm.create('messages', message, true); }); }); @@ -103,6 +122,7 @@ export function sendMessage(rid, msg, cb) { ts: new Date(), _updatedAt: new Date(), temp: true, + _server: {id: RocketChat.currentServer}, u: { _id: user._id, username: user.username diff --git a/app/lib/realm.js b/app/lib/realm.js index d33201d8..a6c68ea2 100644 --- a/app/lib/realm.js +++ b/app/lib/realm.js @@ -14,6 +14,7 @@ const settingsSchema = { primaryKey: '_id', properties: { _id: 'string', + _server: 'servers', value: { type: 'string', optional: true } } }; @@ -23,6 +24,7 @@ const subscriptionSchema = { primaryKey: '_id', properties: { _id: 'string', + _server: 'servers', t: 'string', ts: { type: 'date', optional: true }, ls: { type: 'date', optional: true }, @@ -45,6 +47,7 @@ const usersSchema = { primaryKey: '_id', properties: { _id: 'string', + _server: 'servers', username: 'string', name: { type: 'string', optional: true } } @@ -55,6 +58,7 @@ const messagesSchema = { primaryKey: '_id', properties: { _id: 'string', + _server: 'servers', msg: { type: 'string', optional: true }, rid: 'string', ts: 'date', @@ -79,7 +83,7 @@ export default realm; realm.write(() => { const allSettins = realm.objects('settings'); realm.delete(allSettins); + + // realm.create('servers', {id: 'https://demo.rocket.chat', current: false}, true); + // realm.create('servers', {id: 'http://localhost:3000', current: false}, true); }); - - -console.log(realm.objects('servers')); diff --git a/app/views/login.js b/app/views/login.js index 1f72183e..401893cf 100644 --- a/app/views/login.js +++ b/app/views/login.js @@ -23,6 +23,12 @@ const styles = StyleSheet.create({ } }); +Accounts.onLogin(() => { + loadSubscriptions(() => { + navigate('Rooms'); + }); +}); + export default class LoginView extends React.Component { static propTypes = { navigation: PropTypes.object.isRequired @@ -40,13 +46,7 @@ export default class LoginView extends React.Component { password: '' }; - const { navigate } = this.props.navigation; - - Accounts.onLogin(() => { - loadSubscriptions(() => { - navigate('Rooms'); - }); - }); + navigate = this.props.navigation.navigate; this.submit = () => { loginWithPassword({ username: this.state.username }, this.state.password); diff --git a/app/views/room.js b/app/views/room.js index efaf4dd1..f41ac41c 100644 --- a/app/views/room.js +++ b/app/views/room.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import { View, KeyboardAvoidingView, TextInput, FlatList, StyleSheet, Platform } from 'react-native'; // import Markdown from 'react-native-simple-markdown'; import realm from '../lib/realm'; -import { loadMessagesForRoom, sendMessage } from '../lib/meteor'; +import RocketChat, { loadMessagesForRoom, sendMessage } from '../lib/meteor'; import Message from '../components/Message'; @@ -80,7 +80,7 @@ export default class RoomView extends React.Component { realm.removeListener('change', this.updateState); } - getMessages = () => realm.objects('messages').filtered('rid = $0', this.rid).sorted('ts', true) + getMessages = () => realm.objects('messages').filtered('_server.id = $0 AND rid = $1', RocketChat.currentServer, this.rid).sorted('ts', true) updateState = () => { this.setState({ diff --git a/app/views/roomsList.js b/app/views/roomsList.js index 0b489d84..da948d37 100644 --- a/app/views/roomsList.js +++ b/app/views/roomsList.js @@ -2,6 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { View, FlatList, StyleSheet } from 'react-native'; import realm from '../lib/realm'; +import RocketChat from '../lib/meteor'; import RoomItem from '../components/RoomItem'; @@ -26,13 +27,12 @@ export default class RoomsListView extends React.Component { super(props); this.state = { - dataSource: realm.objects('subscriptions').sorted('name') + dataSource: realm.objects('subscriptions').filtered('_server.id = $0', RocketChat.currentServer).sorted('name') }; } _onPressItem = (id) => { const { navigate } = this.props.navigation; - console.log('pressed', id); navigate('Room', { sid: id }); } diff --git a/app/views/serverList.js b/app/views/serverList.js index 4c0c9116..13b65800 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 { connect } from '../lib/meteor'; +import RocketChat, { connect } from '../lib/meteor'; const styles = StyleSheet.create({ view: { @@ -128,10 +128,7 @@ export default class ListServerView extends React.Component { onPressItem(item) { const { navigate } = this.props.navigation; - realm.write(() => { - realm.objects('servers').filtered('current = true').forEach(server => (server.current = false)); - realm.create('servers', { id: item.id, current: true }, true); - }); + RocketChat.currentServer = item.id; connect(() => { navigate('Login');