Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions app/livechat/client/views/app/tabbar/visitorNavigation.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,79 @@
import { Mongo } from 'meteor/mongo';
import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';
import moment from 'moment';
import _ from 'underscore';

import { ChatRoom } from '../../../../../models';
import { t } from '../../../../../utils';
import './visitorNavigation.html';
import { APIClient } from '../../../../../utils/client';

const visitorNavigationHistory = new Mongo.Collection('visitor_navigation_history');
const ITEMS_COUNT = 50;

Template.visitorNavigation.helpers({
loadingNavigation() {
return !Template.instance().pageVisited.ready();
return Template.instance().isLoading.get();
},

pages() {
const room = ChatRoom.findOne({ _id: this.rid }, { fields: { 'v.token': 1 } });

if (room) {
return visitorNavigationHistory.find({ rid: room._id }, { sort: { ts: -1 } });
return Template.instance().pages.get();
}
},

onTableScroll() {
const instance = Template.instance();
return function(currentTarget) {
if (
currentTarget.offsetHeight + currentTarget.scrollTop
>= currentTarget.scrollHeight - 100
) {
return instance.limit.set(instance.limit.get() + 50);
}
};
},

pageTitle() {
return this.navigation.page.title || t('Empty_title');
},

accessDateTime() {
return moment(this.ts).format('L LTS');
},

});

Template.visitorNavigation.onCreated(function() {
Template.visitorNavigation.events({
'scroll .visitor-scroll': _.throttle(function(e, instance) {
if (e.target.scrollTop >= (e.target.scrollHeight - e.target.clientHeight)) {
const pages = instance.pages.get();
if (instance.total.get() <= pages.length) {
return;
}
return instance.offset.set(instance.offset.get() + ITEMS_COUNT);
}
}, 200),
});

Template.visitorNavigation.onCreated(async function() {
const currentData = Template.currentData();
this.isLoading = new ReactiveVar(true);
this.pages = new ReactiveVar([]);
this.offset = new ReactiveVar(0);
this.ready = new ReactiveVar(true);
this.total = new ReactiveVar(0);

if (currentData && currentData.rid) {
this.pageVisited = this.subscribe('livechat:visitorPageVisited', { rid: currentData.rid });
}
this.autorun(async () => {
this.isLoading.set(true);
const offset = this.offset.get();
if (currentData && currentData.rid) {
const { pages, total } = await APIClient.v1.get(`livechat/visitors.pagesVisited/${ currentData.rid }?count=${ ITEMS_COUNT }&offset=${ offset }`);
this.isLoading.set(false);
this.total.set(total);
this.pages.set(this.pages.get().concat(pages));
}
this.isLoading.set(false);
});
});
26 changes: 25 additions & 1 deletion app/livechat/imports/server/rest/visitors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import { check } from 'meteor/check';

import { API } from '../../../../api';
import { findVisitorInfo } from '../../../server/api/lib/visitors';
import { findVisitorInfo, findVisitedPages } from '../../../server/api/lib/visitors';

API.v1.addRoute('livechat/visitors.info', { authRequired: true }, {
get() {
Expand All @@ -14,3 +15,26 @@ API.v1.addRoute('livechat/visitors.info', { authRequired: true }, {
return API.v1.success(visitor);
},
});

API.v1.addRoute('livechat/visitors.pagesVisited/:roomId', { authRequired: true }, {
get() {
check(this.urlParams, {
roomId: String,
});
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();


const pages = Promise.await(findVisitedPages({
userId: this.userId,
roomId: this.urlParams.roomId,
pagination: {
offset,
count,
sort,
},
}));

return API.v1.success(pages);
},
});
28 changes: 27 additions & 1 deletion app/livechat/server/api/lib/visitors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission';
import { LivechatVisitors } from '../../../../models/server/raw';
import { LivechatVisitors, Messages, LivechatRooms } from '../../../../models/server/raw';

export async function findVisitorInfo({ userId, visitorId }) {
if (!await hasPermissionAsync(userId, 'view-l-room')) {
Expand All @@ -15,3 +15,29 @@ export async function findVisitorInfo({ userId, visitorId }) {
visitor,
};
}

export async function findVisitedPages({ userId, roomId, pagination: { offset, count, sort } }) {
if (!await hasPermissionAsync(userId, 'view-l-room')) {
throw new Error('error-not-authorized');
}
const room = await LivechatRooms.findOneById(roomId);
if (!room) {
throw new Error('invalid-room');
}
const cursor = await Messages.findByRoomIdAndType(room._id, 'livechat_navigation_history', {
sort: sort || { ts: -1 },
skip: offset,
limit: count,
});

const total = await cursor.count();

const pages = await cursor.toArray();

return {
pages,
count: pages.length,
offset,
total,
};
}
1 change: 1 addition & 0 deletions app/livechat/server/publications/visitorPageVisited.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { hasPermission } from '../../../authorization';
import { LivechatRooms, Messages } from '../../../models';

Meteor.publish('livechat:visitorPageVisited', function({ rid: roomId }) {
console.warn('The publication "livechat:visitorPageVisited" is deprecated and will be removed after version v3.0.0');
if (!this.userId) {
return this.error(new Meteor.Error('error-not-authorized', 'Not authorized', { publish: 'livechat:visitorPageVisited' }));
}
Expand Down
11 changes: 11 additions & 0 deletions app/models/server/raw/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ export class MessagesRaw extends BaseRaw {
return this.find(query, options);
}

findByRoomIdAndType(roomId, type, options) {
const query = {
rid: roomId,
t: type,
};

if (options == null) { options = {}; }

return this.find(query, options);
}

findSnippetedByRoom(roomId, options) {
const query = {
_hidden: { $ne: true },
Expand Down
45 changes: 45 additions & 0 deletions tests/end-to-end/api/livechat/visitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,49 @@ describe('LIVECHAT - visitors', function() {
.end(done);
});
});

describe('livechat/visitors.pagesVisited', () => {
it('should return an "unauthorized error" when the user does not have the necessary permission', (done) => {
updatePermission('view-l-room', []).then(() => {
request.get(api('livechat/visitors.pagesVisited/room-id'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
expect(res.body.error).to.be.equal('error-not-authorized');
})
.end(done);
});
});
it('should return an "error" when the roomId param is not provided', (done) => {
updatePermission('view-l-room', ['admin']).then(() => {
request.get(api('livechat/visitors.pagesVisited/room-id'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res) => {
expect(res.body).to.have.property('success', false);
})
.end(done);
});
});
it('should return an array of pages', (done) => {
updatePermission('view-l-room', ['admin'])
.then(() => {
request.get(api('livechat/visitors.pagesVisited/GENERAL'))
.set(credentials)
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body.pages).to.be.an('array');
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
expect(res.body).to.have.property('count');
})
.end(done);
});
});
});
});