vn-verdnaturachat/app/lib/meteor.js

170 lines
3.9 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-09 02:27:22 +00:00
const RocketChat = {
get currentServer() {
const current = realm.objects('servers').filtered('current = true')[0];
return current && current.id;
},
set currentServer(server) {
realm.write(() => {
2017-08-09 13:12:00 +00:00
realm.objects('servers').filtered('current = true').forEach(item => (item.current = false));
2017-08-09 02:27:22 +00:00
realm.create('servers', { id: server, current: true }, true);
});
}
2017-08-09 13:12:00 +00:00
};
2017-08-09 02:27:22 +00:00
export default RocketChat;
2017-08-09 16:19:17 +00:00
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);
});
});
2017-08-09 18:15:44 +00:00
Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/subscriptions-changed`, false);
2017-08-09 16:19:17 +00:00
});
});
2017-08-03 18:23:43 +00:00
export function connect(cb) {
2017-08-09 02:27:22 +00:00
const url = `${ RocketChat.currentServer }/websocket`;
2017-08-03 18:23:43 +00:00
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
};
2017-08-09 13:12:00 +00:00
setting._server = { id: RocketChat.currentServer };
2017-08-03 18:23:43 +00:00
if (typeof item.value === 'string') {
setting.value = item.value;
}
realm.create('settings', setting, true);
});
});
cb();
});
2017-08-07 00:34:35 +00:00
2017-08-09 01:40:55 +00:00
Meteor.ddp.on('changed', (ddbMessage) => {
2017-08-07 00:34:35 +00:00
console.log('changed', ddbMessage);
if (ddbMessage.collection === 'stream-room-messages') {
2017-08-09 16:19:17 +00:00
realm.write(() => {
const message = ddbMessage.fields.args[0];
message.temp = false;
message._server = { id: RocketChat.currentServer };
realm.create('messages', message, true);
});
2017-08-07 00:34:35 +00:00
}
2017-08-09 18:15:44 +00:00
if (ddbMessage.collection === 'stream-notify-user') {
realm.write(() => {
const data = ddbMessage.fields.args[1];
data._server = { id: RocketChat.currentServer };
realm.create('subscriptions', data, true);
});
}
2017-08-07 00:34:35 +00:00
});
2017-08-03 18:23:43 +00:00
});
}
export function loginWithPassword(selector, password, cb) {
2017-08-09 01:40:55 +00:00
Meteor.loginWithPassword(selector, password, () => cb && cb());
2017-08-07 00:34:35 +00:00
}
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;
// }
2017-08-09 13:12:00 +00:00
subscription._server = { id: RocketChat.currentServer };
2017-08-07 00:34:35 +00:00
realm.create('subscriptions', subscription, true);
2017-08-03 18:23:43 +00:00
});
});
2017-08-07 00:34:35 +00:00
2017-08-09 01:40:55 +00:00
return 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);
}
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-09 13:12:00 +00:00
message._server = { id: RocketChat.currentServer };
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);
}
2017-08-09 13:12:00 +00:00
export function sendMessage(rid, msg) {
2017-08-07 00:34:35 +00:00
const _id = Random.id();
const user = Meteor.user();
realm.write(() => {
realm.create('messages', {
_id,
rid,
msg,
2017-08-09 01:40:55 +00:00
ts: new Date(),
_updatedAt: new Date(),
2017-08-07 00:34:35 +00:00
temp: true,
2017-08-09 13:12:00 +00:00
_server: { id: RocketChat.currentServer },
2017-08-07 00:34:35 +00:00
u: {
_id: user._id,
username: user.username
}
}, true);
});
2017-08-09 13:12:00 +00:00
return new Promise((resolve, reject) => {
Meteor.call('sendMessage', { _id, rid, msg }, (error, result) => {
if (error) {
return reject(error);
}
return resolve(result);
});
});
2017-08-04 00:34:37 +00:00
}