vn-verdnaturachat/app/meteor.js

120 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-08-03 18:23:43 +00:00
import Meteor from 'react-native-meteor';
2017-08-07 00:34:35 +00:00
import Random from 'react-native-meteor/lib/Random';
2017-08-05 18:16:32 +00:00
import realm from './realm';
2017-08-03 18:23:43 +00:00
2017-08-07 00:34:35 +00:00
export { Accounts } from 'react-native-meteor';
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`;
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();
});
2017-08-07 00:34:35 +00:00
Meteor.ddp.on("changed", ddbMessage => {
console.log('changed', ddbMessage);
if (ddbMessage.collection === 'stream-room-messages') {
setTimeout(function() {
realm.write(() => {
const message = ddbMessage.fields.args[0];
message.temp = false;
realm.create('messages', message, true);
});
}, 1000)
}
});
2017-08-03 18:23:43 +00:00
});
}
export function loginWithPassword(selector, password, cb) {
2017-08-07 00:34:35 +00:00
Meteor.loginWithPassword(selector, password, (err, data) => {
cb && cb();
});
}
2017-08-03 18:23:43 +00:00
2017-08-07 00:34:35 +00:00
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;
// }
realm.create('subscriptions', subscription, true);
2017-08-03 18:23:43 +00:00
});
});
2017-08-07 00:34:35 +00:00
cb && cb();
2017-08-03 18:23:43 +00:00
});
}
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-07 00:34:35 +00:00
message.temp = false;
2017-08-04 00:34:37 +00:00
realm.create('messages', message, true);
});
});
});
2017-08-07 00:34:35 +00:00
Meteor.subscribe('stream-room-messages', rid, false);
}
export function sendMessage(rid, msg, cb) {
const _id = Random.id();
const user = Meteor.user();
realm.write(() => {
realm.create('messages', {
_id,
rid,
msg,
ts: new Date,
_updatedAt: new Date,
temp: true,
u: {
_id: user._id,
username: user.username
}
}, true);
});
Meteor.call('sendMessage', {_id, rid, msg}, (err, data) => {
cb && cb();
});
2017-08-04 00:34:37 +00:00
}