parent
214a7e2386
commit
9caf0cddbf
@ -0,0 +1,86 @@ |
||||
const hideMessagesOfType = []; |
||||
|
||||
RocketChat.settings.get(/Message_HideType_.+/, function(key, value) { |
||||
const type = key.replace('Message_HideType_', ''); |
||||
const types = type === 'mute_unmute' ? ['user-muted', 'user-unmuted'] : [type]; |
||||
|
||||
return types.forEach((type) => { |
||||
const index = hideMessagesOfType.indexOf(type); |
||||
|
||||
if (value === true && index === -1) { |
||||
return hideMessagesOfType.push(type); |
||||
} |
||||
|
||||
if (index > -1) { |
||||
return hideMessagesOfType.splice(index, 1); |
||||
} |
||||
}); |
||||
}); |
||||
|
||||
RocketChat.loadMessageHistory = function loadMessageHistory({ userId, rid, end, limit = 20, ls }) { |
||||
const options = { |
||||
sort: { |
||||
ts: -1 |
||||
}, |
||||
limit |
||||
}; |
||||
|
||||
if (!RocketChat.settings.get('Message_ShowEditedStatus')) { |
||||
options.fields = { |
||||
editedAt: 0 |
||||
}; |
||||
} |
||||
|
||||
let records; |
||||
if (end != null) { |
||||
records = RocketChat.models.Messages.findVisibleByRoomIdBeforeTimestampNotContainingTypes(rid, end, hideMessagesOfType, options).fetch(); |
||||
} else { |
||||
records = RocketChat.models.Messages.findVisibleByRoomIdNotContainingTypes(rid, hideMessagesOfType, options).fetch(); |
||||
} |
||||
|
||||
const UI_Use_Real_Name = RocketChat.settings.get('UI_Use_Real_Name') === true; |
||||
|
||||
const messages = records.map((message) => { |
||||
message.starred = _.findWhere(message.starred, { |
||||
_id: userId |
||||
}); |
||||
if (message.u && message.u._id && UI_Use_Real_Name) { |
||||
const user = RocketChat.models.Users.findOneById(message.u._id); |
||||
message.u.name = user && user.name; |
||||
} |
||||
if (message.mentions && message.mentions.length && UI_Use_Real_Name) { |
||||
message.mentions.forEach((mention) => { |
||||
const user = RocketChat.models.Users.findOneById(mention._id); |
||||
mention.name = user && user.name; |
||||
}); |
||||
} |
||||
return message; |
||||
}); |
||||
|
||||
let unreadNotLoaded = 0; |
||||
let firstUnread; |
||||
|
||||
if (ls != null) { |
||||
const firstMessage = messages[messages.length - 1]; |
||||
|
||||
if ((firstMessage != null ? firstMessage.ts : undefined) > ls) { |
||||
delete options.limit; |
||||
|
||||
const unreadMessages = RocketChat.models.Messages.findVisibleByRoomIdBetweenTimestampsNotContainingTypes(rid, ls, firstMessage.ts, hideMessagesOfType, { |
||||
limit: 1, |
||||
sort: { |
||||
ts: 1 |
||||
} |
||||
}); |
||||
|
||||
firstUnread = unreadMessages.fetch()[0]; |
||||
unreadNotLoaded = unreadMessages.count(); |
||||
} |
||||
} |
||||
|
||||
return { |
||||
messages, |
||||
firstUnread, |
||||
unreadNotLoaded |
||||
}; |
||||
}; |
@ -1,21 +1,18 @@ |
||||
this.visitorId = new ReactiveVar(null); |
||||
import visitor from '../../imports/client/visitor'; |
||||
|
||||
Meteor.startup(() => { |
||||
if (!localStorage.getItem('rocketChatLivechat')) { |
||||
localStorage.setItem('rocketChatLivechat', Random.id()); |
||||
} else { |
||||
Tracker.autorun(c => { |
||||
if (!Meteor.userId() && visitor.getToken()) { |
||||
if (!visitor.getId() && visitor.getToken()) { |
||||
Meteor.call('livechat:loginByToken', visitor.getToken(), (err, result) => { |
||||
if (result && result.token) { |
||||
Meteor.loginWithToken(result.token, () => { |
||||
c.stop(); |
||||
}); |
||||
if (result && result._id) { |
||||
visitor.setId(result._id); |
||||
c.stop(); |
||||
} |
||||
}); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
this.visitorId.set(localStorage.getItem('rocketChatLivechat')); |
||||
}); |
||||
|
@ -0,0 +1,13 @@ |
||||
import LivechatVisitors from '../models/LivechatVisitors'; |
||||
|
||||
Meteor.methods({ |
||||
'livechat:loadHistory'({ token, rid, end, limit = 20, ls}) { |
||||
const visitor = LivechatVisitors.getVisitorByToken(token, { fields: { _id: 1 } }); |
||||
|
||||
if (!visitor) { |
||||
return; |
||||
} |
||||
|
||||
return RocketChat.loadMessageHistory({ userId: visitor._id, rid, end, limit, ls }); |
||||
} |
||||
}); |
@ -1,28 +1,34 @@ |
||||
import LivechatVisitors from '../models/LivechatVisitors'; |
||||
|
||||
Meteor.methods({ |
||||
'livechat:loginByToken'(token) { |
||||
const user = RocketChat.models.Users.getVisitorByToken(token, { fields: { _id: 1 } }); |
||||
const user = LivechatVisitors.getVisitorByToken(token, { fields: { _id: 1 } }); |
||||
|
||||
if (!user) { |
||||
return; |
||||
} |
||||
|
||||
const stampedToken = Accounts._generateStampedLoginToken(); |
||||
const hashStampedToken = Accounts._hashStampedToken(stampedToken); |
||||
|
||||
const updateUser = { |
||||
$set: { |
||||
services: { |
||||
resume: { |
||||
loginTokens: [ hashStampedToken ] |
||||
} |
||||
} |
||||
} |
||||
return { |
||||
_id: user._id |
||||
}; |
||||
|
||||
Meteor.users.update(user._id, updateUser); |
||||
// const stampedToken = Accounts._generateStampedLoginToken();
|
||||
// const hashStampedToken = Accounts._hashStampedToken(stampedToken);
|
||||
|
||||
return { |
||||
token: stampedToken.token |
||||
}; |
||||
// const updateUser = {
|
||||
// $set: {
|
||||
// services: {
|
||||
// resume: {
|
||||
// loginTokens: [ hashStampedToken ]
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
|
||||
// Meteor.users.update(user._id, updateUser);
|
||||
|
||||
// return {
|
||||
// token: stampedToken.token
|
||||
// };
|
||||
} |
||||
}); |
||||
|
@ -1,22 +1,20 @@ |
||||
Meteor.methods({ |
||||
'livechat:registerGuest'({ token, name, email, department } = {}) { |
||||
const stampedToken = Accounts._generateStampedLoginToken(); |
||||
const hashStampedToken = Accounts._hashStampedToken(stampedToken); |
||||
// const stampedToken = Accounts._generateStampedLoginToken();
|
||||
// const hashStampedToken = Accounts._hashStampedToken(stampedToken);
|
||||
|
||||
const userId = RocketChat.Livechat.registerGuest.call(this, { |
||||
token, |
||||
name, |
||||
email, |
||||
department, |
||||
loginToken: hashStampedToken |
||||
department |
||||
}); |
||||
|
||||
// update visited page history to not expire
|
||||
RocketChat.models.LivechatPageVisited.keepHistoryForToken(token); |
||||
|
||||
return { |
||||
userId, |
||||
token: stampedToken.token |
||||
userId |
||||
}; |
||||
} |
||||
}); |
||||
|
@ -1,16 +1,35 @@ |
||||
import LivechatVisitors from '../models/LivechatVisitors'; |
||||
|
||||
Meteor.methods({ |
||||
sendMessageLivechat(message) { |
||||
check(message.rid, String); |
||||
check(message.token, String); |
||||
sendMessageLivechat({ token, _id, rid, msg }) { |
||||
check(token, String); |
||||
check(_id, String); |
||||
check(rid, String); |
||||
check(msg, String); |
||||
|
||||
const guest = Meteor.users.findOne(Meteor.userId(), { |
||||
const guest = LivechatVisitors.getVisitorByToken(token, { |
||||
fields: { |
||||
name: 1, |
||||
username: 1, |
||||
department: 1 |
||||
department: 1, |
||||
token: 1 |
||||
} |
||||
}); |
||||
|
||||
return RocketChat.Livechat.sendMessage({ guest, message }); |
||||
console.log('guest ->', guest); |
||||
|
||||
if (!guest) { |
||||
throw new Meteor.Error('invalid-token'); |
||||
} |
||||
|
||||
return RocketChat.Livechat.sendMessage({ |
||||
guest, |
||||
message: { |
||||
_id, |
||||
rid, |
||||
msg, |
||||
token |
||||
} |
||||
}); |
||||
} |
||||
}); |
||||
|
@ -0,0 +1,148 @@ |
||||
class LivechatVisitors extends RocketChat.models._Base { |
||||
constructor() { |
||||
super('livechat_visitor'); |
||||
} |
||||
|
||||
/** |
||||
* Gets visitor by token |
||||
* @param {string} token - Visitor token |
||||
*/ |
||||
getVisitorByToken(token, options) { |
||||
const query = { |
||||
token |
||||
}; |
||||
|
||||
return this.findOne(query, options); |
||||
} |
||||
|
||||
/** |
||||
* Gets visitor by token |
||||
* @param {string} token - Visitor token |
||||
*/ |
||||
findVisitorByToken(token) { |
||||
const query = { |
||||
token |
||||
}; |
||||
|
||||
return this.find(query); |
||||
} |
||||
|
||||
updateLivechatDataByToken(token, key, value, overwrite = true) { |
||||
const query = { |
||||
token |
||||
}; |
||||
|
||||
if (!overwrite) { |
||||
const user = this.findOne(query, { fields: { livechatData: 1 } }); |
||||
if (user.livechatData && typeof user.livechatData[key] !== 'undefined') { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
const update = { |
||||
$set: { |
||||
[`livechatData.${ key }`]: value |
||||
} |
||||
}; |
||||
|
||||
return this.update(query, update); |
||||
} |
||||
|
||||
/** |
||||
* Find a visitor by their phone number |
||||
* @return {object} User from db |
||||
*/ |
||||
findOneVisitorByPhone(phone) { |
||||
const query = { |
||||
'phone.phoneNumber': phone |
||||
}; |
||||
|
||||
return this.findOne(query); |
||||
} |
||||
|
||||
/** |
||||
* Get the next visitor name |
||||
* @return {string} The next visitor name |
||||
*/ |
||||
getNextVisitorUsername() { |
||||
const settingsRaw = RocketChat.models.Settings.model.rawCollection(); |
||||
const findAndModify = Meteor.wrapAsync(settingsRaw.findAndModify, settingsRaw); |
||||
|
||||
const query = { |
||||
_id: 'Livechat_guest_count' |
||||
}; |
||||
|
||||
const update = { |
||||
$inc: { |
||||
value: 1 |
||||
} |
||||
}; |
||||
|
||||
const livechatCount = findAndModify(query, null, update); |
||||
|
||||
return `guest-${ livechatCount.value.value + 1 }`; |
||||
} |
||||
|
||||
updateById(_id, update) { |
||||
return this.update({ _id }, update); |
||||
} |
||||
|
||||
saveGuestById(_id, data) { |
||||
const setData = {}; |
||||
const unsetData = {}; |
||||
|
||||
if (data.name) { |
||||
if (!_.isEmpty(s.trim(data.name))) { |
||||
setData.name = s.trim(data.name); |
||||
} else { |
||||
unsetData.name = 1; |
||||
} |
||||
} |
||||
|
||||
if (data.email) { |
||||
if (!_.isEmpty(s.trim(data.email))) { |
||||
setData.visitorEmails = [ |
||||
{ address: s.trim(data.email) } |
||||
]; |
||||
} else { |
||||
unsetData.visitorEmails = 1; |
||||
} |
||||
} |
||||
|
||||
if (data.phone) { |
||||
if (!_.isEmpty(s.trim(data.phone))) { |
||||
setData.phone = [ |
||||
{ phoneNumber: s.trim(data.phone) } |
||||
]; |
||||
} else { |
||||
unsetData.phone = 1; |
||||
} |
||||
} |
||||
|
||||
const update = {}; |
||||
|
||||
if (!_.isEmpty(setData)) { |
||||
update.$set = setData; |
||||
} |
||||
|
||||
if (!_.isEmpty(unsetData)) { |
||||
update.$unset = unsetData; |
||||
} |
||||
|
||||
if (_.isEmpty(update)) { |
||||
return true; |
||||
} |
||||
|
||||
return this.update({ _id }, update); |
||||
} |
||||
|
||||
findOneGuestByEmailAddress(emailAddress) { |
||||
const query = { |
||||
'visitorEmails.address': new RegExp(`^${ s.escapeRegExp(emailAddress) }$`, 'i') |
||||
}; |
||||
|
||||
return this.findOne(query); |
||||
} |
||||
} |
||||
|
||||
export default new LivechatVisitors(); |
Loading…
Reference in new issue