Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 10 additions & 7 deletions app/livechat/client/views/app/tabbar/visitorNavigation.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { Mongo } from 'meteor/mongo';
import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';
import moment from 'moment';

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

const visitorNavigationHistory = new Mongo.Collection('visitor_navigation_history');
import { APIClient } from '../../../../../utils/client';

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();
}
},

Expand All @@ -30,10 +29,14 @@ Template.visitorNavigation.helpers({
},
});

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

if (currentData && currentData.rid) {
this.pageVisited = this.subscribe('livechat:visitorPageVisited', { rid: currentData.rid });
const { pages } = await APIClient.v1.get(`livechat/visitors.pagesVisited?roomId=${ currentData.rid }`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use pagination here, otherwise, only the first fetch will be displayed.

this.isLoading.set(false);
this.pages.set(pages);
}
});
16 changes: 16 additions & 0 deletions app/livechat/imports/server/rest/visitors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { check } from 'meteor/check';

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

API.v1.addRoute('livechat/visitors.pagesVisited', { authRequired: true }, {
get() {
check(this.queryParams, {
roomId: String,
});

const pages = Promise.await(findVisitedPages({ userId: this.userId, roomId: this.queryParams.roomId }));

return API.v1.success(pages);
},
});
1 change: 1 addition & 0 deletions app/livechat/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ import '../imports/server/rest/rooms.js';
import '../imports/server/rest/appearance.js';
import '../imports/server/rest/triggers.js';
import '../imports/server/rest/integrations.js';
import '../imports/server/rest/visitors.js';
16 changes: 16 additions & 0 deletions app/livechat/server/api/lib/visitors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission';
import { Messages, LivechatRooms } from '../../../../models/server/raw';

export async function findVisitedPages({ userId, roomId }) {
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');
}
return {
pages: await Messages.findByRoomIdAndType(room._id, 'livechat_navigation_history').toArray(),
};
}
1 change: 1 addition & 0 deletions app/livechat/server/publications/visitorPageVisited.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Meteor } from 'meteor/meteor';
import { hasPermission } from '../../../authorization';
import { LivechatRooms, Messages } from '../../../models';

console.warn('The publication "livechat:visitorPageVisited" is deprecated and will be removed after version v3.0.0');
Meteor.publish('livechat:visitorPageVisited', function({ rid: roomId }) {
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 @@ -20,4 +20,15 @@ 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);
}
}
54 changes: 54 additions & 0 deletions tests/end-to-end/api/livechat/visitors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getCredentials, api, request, credentials } from '../../../data/api-data.js';
import { updatePermission, updateSetting } from '../../../data/permissions.helper';

describe('LIVECHAT - visitors', function() {
this.retries(0);

before((done) => getCredentials(done));

before((done) => {
updateSetting('Livechat_enabled', true).then(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?roomId=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 query param is not provided', (done) => {
updatePermission('view-l-room', ['admin']).then(() => {
request.get(api('livechat/visitors.pagesVisited'))
.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?roomId=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');
})
.end(done);
});
});
});
});