Fix multiserver

This commit is contained in:
Rodrigo Nascimento 2017-08-08 23:27:22 -03:00
parent 2eee28797a
commit 86597c505c
6 changed files with 42 additions and 21 deletions

View File

@ -4,9 +4,24 @@ import realm from './realm';
export { Accounts } from 'react-native-meteor'; 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) { export function connect(cb) {
const currentServer = realm.objects('servers').filtered('current = true')[0]; const url = `${ RocketChat.currentServer }/websocket`;
const url = `${ currentServer.id }/websocket`;
Meteor.connect(url); Meteor.connect(url);
@ -23,6 +38,7 @@ export function connect(cb) {
const setting = { const setting = {
_id: item._id _id: item._id
}; };
setting._server = {id: RocketChat.currentServer};
if (typeof item.value === 'string') { if (typeof item.value === 'string') {
setting.value = item.value; setting.value = item.value;
} }
@ -40,6 +56,7 @@ export function connect(cb) {
realm.write(() => { realm.write(() => {
const message = ddbMessage.fields.args[0]; const message = ddbMessage.fields.args[0];
message.temp = false; message.temp = false;
message._server = {id: RocketChat.currentServer};
realm.create('messages', message, true); realm.create('messages', message, true);
}); });
}, 1000); }, 1000);
@ -66,6 +83,7 @@ export function loadSubscriptions(cb) {
// if (typeof item.value === 'string') { // if (typeof item.value === 'string') {
// subscription.value = item.value; // subscription.value = item.value;
// } // }
subscription._server = {id: RocketChat.currentServer};
realm.create('subscriptions', subscription, true); realm.create('subscriptions', subscription, true);
}); });
}); });
@ -83,6 +101,7 @@ export function loadMessagesForRoom(rid) {
realm.write(() => { realm.write(() => {
data.messages.forEach((message) => { data.messages.forEach((message) => {
message.temp = false; message.temp = false;
message._server = {id: RocketChat.currentServer};
realm.create('messages', message, true); realm.create('messages', message, true);
}); });
}); });
@ -103,6 +122,7 @@ export function sendMessage(rid, msg, cb) {
ts: new Date(), ts: new Date(),
_updatedAt: new Date(), _updatedAt: new Date(),
temp: true, temp: true,
_server: {id: RocketChat.currentServer},
u: { u: {
_id: user._id, _id: user._id,
username: user.username username: user.username

View File

@ -14,6 +14,7 @@ const settingsSchema = {
primaryKey: '_id', primaryKey: '_id',
properties: { properties: {
_id: 'string', _id: 'string',
_server: 'servers',
value: { type: 'string', optional: true } value: { type: 'string', optional: true }
} }
}; };
@ -23,6 +24,7 @@ const subscriptionSchema = {
primaryKey: '_id', primaryKey: '_id',
properties: { properties: {
_id: 'string', _id: 'string',
_server: 'servers',
t: 'string', t: 'string',
ts: { type: 'date', optional: true }, ts: { type: 'date', optional: true },
ls: { type: 'date', optional: true }, ls: { type: 'date', optional: true },
@ -45,6 +47,7 @@ const usersSchema = {
primaryKey: '_id', primaryKey: '_id',
properties: { properties: {
_id: 'string', _id: 'string',
_server: 'servers',
username: 'string', username: 'string',
name: { type: 'string', optional: true } name: { type: 'string', optional: true }
} }
@ -55,6 +58,7 @@ const messagesSchema = {
primaryKey: '_id', primaryKey: '_id',
properties: { properties: {
_id: 'string', _id: 'string',
_server: 'servers',
msg: { type: 'string', optional: true }, msg: { type: 'string', optional: true },
rid: 'string', rid: 'string',
ts: 'date', ts: 'date',
@ -79,7 +83,7 @@ export default realm;
realm.write(() => { realm.write(() => {
const allSettins = realm.objects('settings'); const allSettins = realm.objects('settings');
realm.delete(allSettins); 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'));

View File

@ -23,6 +23,12 @@ const styles = StyleSheet.create({
} }
}); });
Accounts.onLogin(() => {
loadSubscriptions(() => {
navigate('Rooms');
});
});
export default class LoginView extends React.Component { export default class LoginView extends React.Component {
static propTypes = { static propTypes = {
navigation: PropTypes.object.isRequired navigation: PropTypes.object.isRequired
@ -40,13 +46,7 @@ export default class LoginView extends React.Component {
password: '' password: ''
}; };
const { navigate } = this.props.navigation; navigate = this.props.navigation.navigate;
Accounts.onLogin(() => {
loadSubscriptions(() => {
navigate('Rooms');
});
});
this.submit = () => { this.submit = () => {
loginWithPassword({ username: this.state.username }, this.state.password); loginWithPassword({ username: this.state.username }, this.state.password);

View File

@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { View, KeyboardAvoidingView, TextInput, FlatList, StyleSheet, Platform } from 'react-native'; import { View, KeyboardAvoidingView, TextInput, FlatList, StyleSheet, Platform } from 'react-native';
// import Markdown from 'react-native-simple-markdown'; // import Markdown from 'react-native-simple-markdown';
import realm from '../lib/realm'; import realm from '../lib/realm';
import { loadMessagesForRoom, sendMessage } from '../lib/meteor'; import RocketChat, { loadMessagesForRoom, sendMessage } from '../lib/meteor';
import Message from '../components/Message'; import Message from '../components/Message';
@ -80,7 +80,7 @@ export default class RoomView extends React.Component {
realm.removeListener('change', this.updateState); 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 = () => { updateState = () => {
this.setState({ this.setState({

View File

@ -2,6 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { View, FlatList, StyleSheet } from 'react-native'; import { View, FlatList, StyleSheet } from 'react-native';
import realm from '../lib/realm'; import realm from '../lib/realm';
import RocketChat from '../lib/meteor';
import RoomItem from '../components/RoomItem'; import RoomItem from '../components/RoomItem';
@ -26,13 +27,12 @@ export default class RoomsListView extends React.Component {
super(props); super(props);
this.state = { this.state = {
dataSource: realm.objects('subscriptions').sorted('name') dataSource: realm.objects('subscriptions').filtered('_server.id = $0', RocketChat.currentServer).sorted('name')
}; };
} }
_onPressItem = (id) => { _onPressItem = (id) => {
const { navigate } = this.props.navigation; const { navigate } = this.props.navigation;
console.log('pressed', id);
navigate('Room', { sid: id }); navigate('Room', { sid: id });
} }

View File

@ -4,7 +4,7 @@ import Zeroconf from 'react-native-zeroconf';
import { View, Text, SectionList, Button, StyleSheet } from 'react-native'; import { View, Text, SectionList, Button, StyleSheet } from 'react-native';
import realm from '../lib/realm'; import realm from '../lib/realm';
import { connect } from '../lib/meteor'; import RocketChat, { connect } from '../lib/meteor';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
view: { view: {
@ -128,10 +128,7 @@ export default class ListServerView extends React.Component {
onPressItem(item) { onPressItem(item) {
const { navigate } = this.props.navigation; const { navigate } = this.props.navigation;
realm.write(() => { RocketChat.currentServer = item.id;
realm.objects('servers').filtered('current = true').forEach(server => (server.current = false));
realm.create('servers', { id: item.id, current: true }, true);
});
connect(() => { connect(() => {
navigate('Login'); navigate('Login');