verdnatura-chat/app/meteor.js

74 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-08-03 18:23:43 +00:00
import Meteor from 'react-native-meteor';
2017-08-05 18:16:32 +00:00
import realm from './realm';
2017-08-03 18:23:43 +00:00
export function connect(cb) {
const currentServer = realm.objects('servers').filtered('current = true')[0];
const url = `${ currentServer.id }/websocket`;
console.log('CONNECTING TO', url);
Meteor.connect(url);
2017-08-05 18:16:32 +00:00
Meteor.ddp.on('connected', () => {
2017-08-03 18:23:43 +00:00
console.log('connected');
2017-08-05 18:16:32 +00:00
Meteor.call('public-settings/get', (err, data) => {
2017-08-03 18:23:43 +00:00
if (err) {
console.error(err);
}
realm.write(() => {
2017-08-05 18:16:32 +00:00
data.forEach((item) => {
2017-08-03 18:23:43 +00:00
const setting = {
_id: item._id
};
if (typeof item.value === 'string') {
setting.value = item.value;
}
realm.create('settings', setting, true);
});
});
cb();
});
});
}
export function loginWithPassword(selector, password, cb) {
2017-08-05 18:16:32 +00:00
Meteor.loginWithPassword(selector, password, () => {
Meteor.call('subscriptions/get', (err, data) => {
2017-08-03 18:23:43 +00:00
if (err) {
console.error(err);
}
realm.write(() => {
2017-08-05 18:16:32 +00:00
data.forEach((subscription) => {
2017-08-03 18:23:43 +00:00
// const subscription = {
// _id: item._id
// };
// if (typeof item.value === 'string') {
// subscription.value = item.value;
// }
realm.create('subscriptions', subscription, true);
});
});
});
cb();
});
}
2017-08-04 00:34:37 +00:00
export function loadMessagesForRoom(rid) {
2017-08-05 18:16:32 +00:00
Meteor.call('loadHistory', rid, null, 50, (err, data) => {
2017-08-04 00:34:37 +00:00
if (err) {
console.error(err);
}
console.log(data);
realm.write(() => {
2017-08-05 18:16:32 +00:00
data.messages.forEach((message) => {
2017-08-04 00:34:37 +00:00
realm.create('messages', message, true);
});
});
});
}