2018-04-24 19:34:03 +00:00
|
|
|
import { post } from './helpers/rest';
|
|
|
|
import database from '../realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-09-28 18:57:29 +00:00
|
|
|
import store from '../createStore';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
const readMessagesREST = function readMessagesREST(rid) {
|
2018-09-28 18:57:29 +00:00
|
|
|
const { user } = store.getState().login;
|
|
|
|
const { token, id } = user;
|
2018-09-26 19:38:06 +00:00
|
|
|
const server = this.ddp.url.replace(/^ws/, 'http');
|
2018-04-24 19:34:03 +00:00
|
|
|
return post({ token, id, server }, 'subscriptions.read', { rid });
|
|
|
|
};
|
|
|
|
|
|
|
|
const readMessagesDDP = function readMessagesDDP(rid) {
|
|
|
|
try {
|
|
|
|
return this.ddp.call('readMessages', rid);
|
|
|
|
} catch (e) {
|
|
|
|
return readMessagesREST.call(this, rid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default async function readMessages(rid) {
|
|
|
|
const { database: db } = database;
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
// eslint-disable-next-line
|
2018-09-28 18:57:29 +00:00
|
|
|
const data = await (this.ddp && this.ddp.status ? readMessagesDDP.call(this, rid) : readMessagesREST.call(this, rid));
|
2018-05-18 17:55:08 +00:00
|
|
|
const [subscription] = db.objects('subscriptions').filtered('rid = $0', rid);
|
|
|
|
db.write(() => {
|
|
|
|
subscription.open = true;
|
|
|
|
subscription.alert = false;
|
|
|
|
subscription.unread = 0;
|
|
|
|
subscription.userMentions = 0;
|
|
|
|
subscription.groupMentions = 0;
|
|
|
|
subscription.ls = new Date();
|
|
|
|
subscription.lastOpen = new Date();
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
} catch (e) {
|
|
|
|
log('readMessages', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|