-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[IMPROVE] Replace livechat:externalMessages publication by REST #15643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
renatobecker-zz
merged 5 commits into
develop
from
replace-livechat-external-messages-pub
Nov 13, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a8d174a
Replace livechat:externalMessages publication by REST
02c8407
Fix lint
c7c3653
Add pagination to livechat external messages
ea54edc
Merge branch 'develop' into replace-livechat-external-messages-pub
f9ea349
Add index and remove obsolete collection
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,57 @@ | ||
| import { Template } from 'meteor/templating'; | ||
| import { ReactiveVar } from 'meteor/reactive-var'; | ||
|
|
||
| import { LivechatExternalMessage } from '../../../../lib/LivechatExternalMessage'; | ||
| import './externalSearch.html'; | ||
| import { APIClient } from '../../../../../utils/client'; | ||
|
|
||
| const MESSAGES_COUNT = 50; | ||
|
|
||
| Template.externalSearch.helpers({ | ||
| messages() { | ||
| return LivechatExternalMessage.findByRoomId(this.rid, { ts: 1 }); | ||
| return Template.instance().externalMessages.get(); | ||
| }, | ||
| hasMore() { | ||
| const instance = Template.instance(); | ||
| const externalMessages = instance.externalMessages.get(); | ||
| return instance.total.get() > externalMessages.length; | ||
| }, | ||
| }); | ||
|
|
||
| Template.externalSearch.events({ | ||
| 'click button.pick-message'(event, instance) { | ||
| event.preventDefault(); | ||
|
|
||
| $(`#chat-window-${ instance.roomId } .input-message`).val(this.msg).focus(); | ||
| $(`#chat-window-${ instance.roomId } .js-input-message`).val(this.msg).focus(); | ||
| }, | ||
| 'click .load-more'(e, t) { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| t.offset.set(t.offset.get() + MESSAGES_COUNT); | ||
| }, | ||
| 'click .load-more-livechat-external-messages'(event, instance) { | ||
| return instance.offset.set(instance.offset.get() + MESSAGES_COUNT); | ||
| }, | ||
| }); | ||
|
|
||
| Template.externalSearch.onCreated(function() { | ||
| this.roomId = null; | ||
| // console.log('externalSearch.this ->',this); | ||
| this.autorun(() => { | ||
| this.externalMessages = new ReactiveVar([]); | ||
| this.offset = new ReactiveVar(0); | ||
| this.ready = new ReactiveVar(true); | ||
| this.total = new ReactiveVar(0); | ||
|
|
||
| this.autorun(async () => { | ||
| this.ready.set(false); | ||
| const offset = this.offset.get(); | ||
| this.roomId = Template.currentData().rid; | ||
| this.subscribe('livechat:externalMessages', Template.currentData().rid); | ||
| if (this.roomId) { | ||
| const { messages, total } = await APIClient.v1.get(`livechat/messages.external/${ this.roomId }?count=${ MESSAGES_COUNT }&offset=${ offset }`); | ||
| this.total.set(total); | ||
| if (offset === 0) { | ||
| this.externalMessages.set(messages); | ||
| } else { | ||
| this.externalMessages.set(this.externalMessages.get().concat(messages)); | ||
| } | ||
| } | ||
| this.ready.set(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
|
|
||
| import { check } from 'meteor/check'; | ||
|
|
||
| import { API } from '../../../../api'; | ||
| import { findExternalMessages } from '../../../server/api/lib/messages'; | ||
|
|
||
| API.v1.addRoute('livechat/messages.external/:roomId', { authRequired: true }, { | ||
| get() { | ||
| check(this.urlParams, { | ||
| roomId: String, | ||
| }); | ||
| const { offset, count } = this.getPaginationItems(); | ||
| const { sort } = this.parseJsonQuery(); | ||
|
|
||
| const departments = Promise.await(findExternalMessages({ | ||
| roomId: this.urlParams.roomId, | ||
| pagination: { | ||
| offset, | ||
| count, | ||
| sort, | ||
| }, | ||
| })); | ||
|
|
||
| return API.v1.success(departments); | ||
| }, | ||
| }); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { LivechatExternalMessage } from '../../../../models/server/raw'; | ||
|
|
||
| export async function findExternalMessages({ roomId, pagination: { offset, count, sort } }) { | ||
| const cursor = await LivechatExternalMessage.findByRoomId(roomId, { | ||
| sort: sort || { ts: -1 }, | ||
| skip: offset, | ||
| limit: count, | ||
| }); | ||
|
|
||
| const total = await cursor.count(); | ||
|
|
||
| const messages = await cursor.toArray(); | ||
|
|
||
| return { | ||
| messages, | ||
| count: messages.length, | ||
| offset, | ||
| total, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import { Meteor } from 'meteor/meteor'; | ||
|
|
||
| import { LivechatExternalMessage } from '../../lib/LivechatExternalMessage'; | ||
| import { LivechatExternalMessage } from '../../../models/server'; | ||
|
|
||
| Meteor.publish('livechat:externalMessages', function(roomId) { | ||
| console.warn('The publication "livechat:externalMessages" is deprecated and will be removed after version v3.0.0'); | ||
| return LivechatExternalMessage.findByRoomId(roomId); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Base } from './_Base'; | ||
|
|
||
| export class LivechatExternalMessage extends Base { | ||
| constructor() { | ||
| super('livechat_external_message'); | ||
|
|
||
| this.tryEnsureIndex({ rid: 1 }); | ||
| } | ||
|
|
||
| // FIND | ||
| findByRoomId(roomId, sort = { ts: -1 }) { | ||
| const query = { rid: roomId }; | ||
|
|
||
| return this.find(query, { sort }); | ||
| } | ||
| } | ||
|
|
||
| export default new LivechatExternalMessage(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { BaseRaw } from './BaseRaw'; | ||
|
|
||
| export class LivechatExternalMessageRaw extends BaseRaw { | ||
| findByRoomId(roomId, options) { | ||
| const query = { rid: roomId }; | ||
|
|
||
| return this.find(query, options); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { getCredentials, api, request, credentials } from '../../../data/api-data.js'; | ||
| import { updateSetting } from '../../../data/permissions.helper'; | ||
|
|
||
| describe('LIVECHAT - messages', function() { | ||
| this.retries(0); | ||
|
|
||
| before((done) => getCredentials(done)); | ||
|
|
||
| before((done) => { | ||
| updateSetting('Livechat_enabled', true).then(done); | ||
| }); | ||
|
|
||
| describe('livechat/messages.external', () => { | ||
| it('should return an array of messages', (done) => { | ||
| request.get(api('livechat/messages.external/roomId')) | ||
| .set(credentials) | ||
| .expect('Content-Type', 'application/json') | ||
| .expect(200) | ||
| .expect((res) => { | ||
| expect(res.body).to.have.property('success', true); | ||
| expect(res.body.messages).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); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about creating an index for the
ridfield?It would provide more performance when finding records.