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';
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

View File

@ -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'));

View File

@ -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);

View File

@ -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({

View File

@ -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 });
}

View File

@ -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');