Created
November 9, 2020 04:57
-
-
Save supertopoz/8d923f4202f64103b5b4a220f091299c to your computer and use it in GitHub Desktop.
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
class SearchUsers { | |
constructor(sb) { | |
this.sb = sb; | |
this.lookupDictionary = {}; | |
this.userDictionary = {}; | |
} | |
addQueryUsersToDictionary(users, searchTerm) { | |
if (users.length === 0) { | |
this.lookupDictionary[searchTerm] = {}; | |
return; | |
} | |
users.forEach((user) => { | |
this.userDictionary[user.userId] = user; | |
for (var i = 1; i <= 10; i++) { | |
let dictionaryKey = user.nickname.slice(0, i).toLowerCase(); | |
const userId = user.userId; | |
let entry = {}; | |
entry[userId] = true; | |
if (this.lookupDictionary[dictionaryKey]) { | |
entry = this.lookupDictionary[dictionaryKey]; | |
entry[userId] = true; | |
} | |
this.lookupDictionary[dictionaryKey] = entry; | |
} | |
}); | |
} | |
querySendbirdForNicknames(searchTerm) { | |
return new Promise((resolve, reject) => { | |
let query = this.sb.createApplicationUserListQuery(); | |
try { | |
query.nicknameStartsWithFilter = searchTerm; | |
query.limit = 100; | |
} catch (e) { | |
console.log(e); | |
return; | |
} | |
const fetch = () => { | |
if (query.hasNext) { | |
query.next((users, error) => { | |
console.log(users); | |
if (error) reject(error); | |
this.addQueryUsersToDictionary(users, searchTerm); | |
fetch(); | |
}); | |
} else { | |
resolve(this.lookupDictionary); | |
} | |
}; | |
fetch(); | |
}); | |
} | |
searchApplicationUsers(searchTerm) { | |
return new Promise((resolve, reject) => { | |
const result = { | |
usersDictionary: {}, | |
lookupDictionary: {} | |
}; | |
if (searchTerm.length <= 3) { | |
reject(null); | |
return; | |
} | |
if (this.lookupDictionary[searchTerm]) { | |
console.log("CALLED_IN"); | |
result.usersDictionary = this.userDictionary; | |
result.lookupDictionary = this.lookupDictionary[searchTerm]; | |
resolve(result); | |
return; | |
} | |
console.log("CALLED OUT"); | |
this.querySendbirdForNicknames(searchTerm) | |
.then((lookupDictionary) => { | |
result.usersDictionary = this.userDictionary; | |
result.lookupDictionary = this.lookupDictionary[searchTerm]; | |
resolve(result); | |
}) | |
.catch((error) => { | |
reject(error); | |
}); | |
}); | |
} | |
} | |
export default SearchUsers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment