1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-07-18 09:22:18 +03:00

Emoji provider, DDG working, style improvements

This commit is contained in:
Aviral Dasgupta
2016-06-17 04:58:09 +05:30
parent 769b3f0c2a
commit b9d7743e5a
8 changed files with 127 additions and 37 deletions

View File

@ -2,31 +2,50 @@ import AutocompleteProvider from './AutocompleteProvider';
import Q from 'q';
import 'whatwg-fetch';
const DDG_REGEX = /\/ddg\w+(.+)$/;
const DDG_REGEX = /\/ddg\s+(.+)$/;
const REFERER = 'vector';
export default class DuckDuckGoProvider extends AutocompleteProvider {
static getQueryUri(query: String) {
return `http://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&t=${encodeURIComponent(REFERER)}`;
return `http://api.duckduckgo.com/?q=${encodeURIComponent(query)}`
+ `&format=json&no_redirect=1&no_html=1&t=${encodeURIComponent(REFERER)}`;
}
getCompletions(query: String) {
if(!query)
let match = DDG_REGEX.exec(query);
if(!query || !match)
return Q.when([]);
let promise = Q.defer();
fetch(DuckDuckGoProvider.getQueryUri(query), {
return fetch(DuckDuckGoProvider.getQueryUri(match[1]), {
method: 'GET'
}).then(response => {
let results = response.Results.map(result => {
return {
title: result.Text,
description: result.Result
};
})
.then(response => response.json())
.then(json => {
let results = json.Results.map(result => {
return {
title: result.Text,
description: result.Result
};
});
if(json.Answer) {
results.unshift({
title: json.Answer,
description: json.AnswerType
});
}
if(json.RelatedTopics && json.RelatedTopics.length > 0) {
results.unshift({
title: json.RelatedTopics[0].Text
});
}
if(json.AbstractText) {
results.unshift({
title: json.AbstractText
});
}
// console.log(results);
return results;
});
promise.resolve(results);
});
return promise;
}
getName() {