Merge branch 'search'

This commit is contained in:
Reto Brunner 2022-11-24 09:34:24 +01:00
commit d34b58811a
4 changed files with 24 additions and 20 deletions

View File

@ -129,13 +129,14 @@ export default defineComponent({
const oldScrollTop = ref(0);
const oldChatHeight = ref(0);
const search = computed(() => store.state.messageSearchResults);
const messages = computed(() => {
if (!search.value) {
const results = store.state.messageSearchResults?.results;
if (!results) {
return [];
}
return search.value.results;
return results;
});
const chan = computed(() => {
@ -185,14 +186,14 @@ export default defineComponent({
return new Date(previousMessage.time).getDay() !== new Date(message.time).getDay();
};
const doSearch = () => {
const clearSearchState = () => {
offset.value = 0;
store.commit("messageSearchInProgress", true);
if (!offset.value) {
store.commit("messageSearchInProgress", undefined); // Only reset if not getting offset
}
store.commit("messageSearchInProgress", false);
store.commit("messageSearchResults", null);
};
const doSearch = () => {
clearSearchState(); // this is a new search, so we need to clear anything before that
socket.emit("search", {
networkUuid: network.value?.uuid,
channelName: channel.value?.name,
@ -216,7 +217,7 @@ export default defineComponent({
networkUuid: network.value?.uuid,
channelName: channel.value?.name,
searchTerm: String(route.query.q || ""),
offset: offset.value + 1,
offset: offset.value,
});
};
@ -286,6 +287,7 @@ export default defineComponent({
onUnmounted(() => {
eventbus.off("escapekey", closeSearch);
eventbus.off("re-search", doSearch);
clearSearchState();
});
return {
@ -293,7 +295,6 @@ export default defineComponent({
loadMoreButton,
messages,
moreResultsAvailable,
search,
network,
channel,
route,

View File

@ -224,7 +224,7 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
let select =
'SELECT msg, type, time, network, channel FROM messages WHERE type = "message" AND json_extract(msg, "$.text") LIKE ? ESCAPE \'@\'';
const params = [`%${escapedSearchTerm}%`];
const params: any[] = [`%${escapedSearchTerm}%`];
if (query.networkUuid) {
select += " AND network = ? ";
@ -239,9 +239,8 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
const maxResults = 100;
select += " ORDER BY time DESC LIMIT ? OFFSET ? ";
params.push(maxResults.toString());
query.offset = parseInt(query.offset as string, 10) || 0;
params.push(String(query.offset));
params.push(maxResults);
params.push(query.offset);
return new Promise((resolve, reject) => {
this.database.all(select, params, (err, rows) => {
@ -252,8 +251,8 @@ class SqliteMessageStorage implements ISqliteMessageStorage {
searchTerm: query.searchTerm,
target: query.channelName,
networkUuid: query.networkUuid,
offset: query.offset as number,
results: parseSearchRowsToMessages(query.offset as number, rows).reverse(),
offset: query.offset,
results: parseSearchRowsToMessages(query.offset, rows).reverse(),
};
resolve(response);
}

View File

@ -26,7 +26,7 @@ export type SearchQuery = {
searchTerm: string;
networkUuid: string;
channelName: string;
offset: number | string;
offset: number;
};
export type SearchResponse =

View File

@ -167,7 +167,9 @@ describe("SQLite Message Storage", function () {
.search({
searchTerm: "msg",
networkUuid: "retrieval-order-test-network",
} as any)
channelName: "",
offset: 0,
})
.then((messages) => {
// @ts-expect-error Property 'results' does not exist on type '[]'.
expect(messages.results).to.have.lengthOf(100);
@ -192,7 +194,9 @@ describe("SQLite Message Storage", function () {
.search({
searchTerm: query,
networkUuid: "this-is-a-network-guid2",
} as any)
channelName: "",
offset: 0,
})
.then((messages) => {
// @ts-expect-error Property 'results' does not exist on type '[]'.
expect(messages.results.map((i) => i.text)).to.deep.equal(expected);