forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from kb0304/contacts-discovery
Contacts Discovery
- Loading branch information
Showing
13 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,14 @@ | ||
Package.describe({ | ||
name: 'rocketchat:contacts', | ||
version: '0.0.1', | ||
git: '', | ||
}); | ||
|
||
Package.onUse(function(api) { | ||
api.use('rocketchat:lib'); | ||
api.use('ecmascript'); | ||
|
||
api.addFiles('server/index.js', 'server'); | ||
api.addFiles('server/service.js', 'server'); | ||
api.addFiles('server/startup.js', 'server'); | ||
}); |
This file contains 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,114 @@ | ||
/* globals SyncedCron */ | ||
|
||
import { Meteor } from 'meteor/meteor'; | ||
import _ from 'underscore'; | ||
const service = require('./service.js'); | ||
const provider = new service.Provider(); | ||
|
||
function refreshContactsHashMap() { | ||
let phoneFieldName = ''; | ||
RocketChat.settings.get('Contacts_Phone_Custom_Field_Name', function(name, fieldName) { | ||
phoneFieldName = fieldName; | ||
}); | ||
|
||
let emailFieldName = ''; | ||
RocketChat.settings.get('Contacts_Email_Custom_Field_Name', function(name, fieldName) { | ||
emailFieldName = fieldName; | ||
}); | ||
|
||
let useDefaultEmails = false; | ||
RocketChat.settings.get('Contacts_Use_Default_Emails', function(name, fieldName) { | ||
useDefaultEmails = fieldName; | ||
}); | ||
|
||
const contacts = []; | ||
const cursor = Meteor.users.find({ active:true }); | ||
|
||
let phoneFieldArray = []; | ||
if (phoneFieldName) { | ||
phoneFieldArray = phoneFieldName.split(','); | ||
} | ||
|
||
let emailFieldArray = []; | ||
if (emailFieldName) { | ||
emailFieldArray = emailFieldName.split(','); | ||
} | ||
|
||
let dict; | ||
|
||
const phonePattern = /^\+?[1-9]\d{1,14}$/; | ||
const rfcMailPattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
cursor.forEach((user) => { | ||
const discoverable = RocketChat.getUserPreference(user, 'isPublicAccount'); | ||
if (discoverable !== false) { | ||
if (phoneFieldArray.length > 0) { | ||
dict = user; | ||
for (let i = 0;i < phoneFieldArray.length - 1;i++) { | ||
if (phoneFieldArray[i] in dict) { | ||
dict = dict[phoneFieldArray[i]]; | ||
} | ||
} | ||
let phone = dict[phoneFieldArray[phoneFieldArray.length - 1]]; | ||
if (phone && _.isString(phone)) { | ||
phone = phone.replace(/[^0-9+]|_/g, ''); | ||
if (phonePattern.test(phone)) { | ||
contacts.push({ d:phone, u:user.username }); | ||
} | ||
} | ||
|
||
} | ||
|
||
if (emailFieldArray.length > 0) { | ||
dict = user; | ||
for (let i = 0;i < emailFieldArray.length - 1;i++) { | ||
if (emailFieldArray[i] in dict) { | ||
dict = dict[emailFieldArray[i]]; | ||
} | ||
} | ||
const email = dict[emailFieldArray[emailFieldArray.length - 1]]; | ||
if (email && _.isString(email)) { | ||
if (rfcMailPattern.test(email)) { | ||
contacts.push({ d:email, u:user.username }); | ||
} | ||
} | ||
} | ||
|
||
if (useDefaultEmails && 'emails' in user) { | ||
user.emails.forEach((email) => { | ||
if (email.verified) { | ||
contacts.push({ d:email.address, u:user.username }); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
provider.setHashedMap(provider.generateHashedMap(contacts)); | ||
} | ||
|
||
Meteor.methods({ | ||
queryContacts(weakHashes) { | ||
if (!Meteor.userId()) { | ||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { | ||
method: 'queryContactse', | ||
}); | ||
} | ||
return provider.queryContacts(weakHashes); | ||
}, | ||
}); | ||
|
||
const jobName = 'Refresh_Contacts_Hashes'; | ||
|
||
Meteor.startup(() => { | ||
Meteor.defer(() => { | ||
refreshContactsHashMap(); | ||
|
||
RocketChat.settings.get('Contacts_Background_Sync_Interval', function(name, processingFrequency) { | ||
SyncedCron.remove(jobName); | ||
SyncedCron.add({ | ||
name: jobName, | ||
schedule: (parser) => parser.cron(`*/${ processingFrequency } * * * *`), | ||
job: refreshContactsHashMap, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains 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,81 @@ | ||
const crypto = require('crypto'); | ||
|
||
class ContactsProvider { | ||
|
||
constructor() { | ||
this.contactsWeakHashMap = {}; | ||
} | ||
|
||
addContact(contact, username) { | ||
const weakHash = this.getWeakHash(contact); | ||
const strongHash = this.getStrongHash(contact); | ||
|
||
if (weakHash in this.contactsWeakHashMap) { | ||
if (this.contactsWeakHashMap[weakHash].indexOf(strongHash) === -1) { | ||
this.contactsWeakHashMap[weakHash].push({ | ||
h:strongHash, | ||
u:username, | ||
}); | ||
} | ||
} else { | ||
this.contactsWeakHashMap[weakHash] = [{ h:strongHash, u:username }]; | ||
} | ||
} | ||
|
||
generateHashedMap(contacts) { | ||
const contactsWeakHashMap = {}; | ||
contacts.forEach((contact) => { | ||
const weakHash = this.getWeakHash(contact.d); | ||
const strongHash = this.getStrongHash(contact.d); | ||
if (weakHash in contactsWeakHashMap) { | ||
if (contactsWeakHashMap[weakHash].indexOf(strongHash) === -1) { | ||
contactsWeakHashMap[weakHash].push({ h:strongHash, u:contact.u }); | ||
} | ||
} else { | ||
contactsWeakHashMap[weakHash] = [{ h:strongHash, u:contact.u }]; | ||
} | ||
}); | ||
return contactsWeakHashMap; | ||
} | ||
|
||
setHashedMap(contactsWeakHashMap) { | ||
this.contactsWeakHashMap = contactsWeakHashMap; | ||
} | ||
|
||
getStrongHash(contact) { | ||
return crypto.createHash('sha1').update(contact).digest('hex'); | ||
} | ||
|
||
getWeakHash(contact) { | ||
return crypto.createHash('sha1').update(contact).digest('hex').substr(3, 6); | ||
} | ||
|
||
queryContacts(contactWeakHashList) { | ||
let result = []; | ||
contactWeakHashList.forEach((weakHash) => { | ||
if (weakHash in this.contactsWeakHashMap) { | ||
result = result.concat(this.contactsWeakHashMap[weakHash]); | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
removeContact(contact, username) { | ||
const weakHash = this.getWeakHash(contact); | ||
const strongHash = this.getStrongHash(contact); | ||
|
||
if (weakHash in this.contactsWeakHashMap && this.contactsWeakHashMap[weakHash].indexOf(strongHash) >= 0) { | ||
this.contactsWeakHashMap[weakHash].splice(this.contactsWeakHashMap[weakHash].indexOf({ h:strongHash, u:username }), 1); | ||
|
||
if (!this.contactsWeakHashMap[weakHash].length) { delete this.contactsWeakHashMap[weakHash]; } | ||
} | ||
} | ||
|
||
reset() { | ||
this.contactsWeakHashMap = {}; | ||
} | ||
} | ||
|
||
module.exports = { | ||
Provider: ContactsProvider, | ||
}; |
This file contains 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,25 @@ | ||
RocketChat.settings.addGroup('Contacts', function() { | ||
this.add('Contacts_Phone_Custom_Field_Name', '', { | ||
type: 'string', | ||
public: true, | ||
i18nDescription: 'Contacts_Phone_Custom_Field_Name_Description', | ||
}); | ||
|
||
this.add('Contacts_Use_Default_Emails', true, { | ||
type: 'boolean', | ||
public: true, | ||
i18nDescription: 'Contacts_Use_Default_Emails_Description', | ||
}); | ||
|
||
this.add('Contacts_Email_Custom_Field_Name', '', { | ||
type: 'string', | ||
public: true, | ||
i18nDescription: 'Contacts_Email_Custom_Field_Name_Description', | ||
}); | ||
|
||
this.add('Contacts_Background_Sync_Interval', 10, { | ||
type: 'int', | ||
public: true, | ||
i18nDescription: 'Contacts_Background_Sync_Interval_Description', | ||
}); | ||
}); |
This file contains 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 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 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 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 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