Rename meteor.js to rocketchat.js

This commit is contained in:
Rodrigo Nascimento 2017-08-09 17:18:00 -03:00
parent 8d52209935
commit 4c5adc5dc9
7 changed files with 186 additions and 186 deletions

View File

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

175
app/lib/rocketchat.js Normal file
View File

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

View File

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

View File

@ -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 = () => (
<View style={styles.separator} />

View File

@ -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 {
<View style={[styles.bannerContainer, { backgroundColor: 'orange' }]}>
<Text style={[styles.bannerText, { color: '#a00' }]}>Authenticating...</Text>
</View>
)
);
}
}

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

View File

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