Skip to content

Commit

Permalink
refactor(file): implement new alexandria search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Yelinz committed Dec 9, 2024
1 parent 2afbd8e commit 1312941
Show file tree
Hide file tree
Showing 22 changed files with 119 additions and 40 deletions.
12 changes: 0 additions & 12 deletions addon/adapters/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,5 @@ export default function (BaseClass) {

return ajaxOptions;
}

// Overwrite and replicate the query function,
// because ember doesnt pass adapterOptions to urlForQuery
query(_, type, query, __, options) {
let url = this.buildURL(type.modelName, null, null, "query", query);

if (options?.adapterOptions?.customEndpoint) {
url = `${this.buildURL()}/${options.adapterOptions.customEndpoint}`;
}

return this.ajax(url, "GET", { data: query });
}
};
}
15 changes: 15 additions & 0 deletions addon/adapters/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function (BaseClass) {
return class SearchResultAdapter extends BaseClass {
// Overwrite and replicate the query function,
// because ember doesnt pass adapterOptions to urlForQuery
query(_, type, query, __, options) {
let url = this.buildURL(type.modelName, null, null, "query", query);

if (options?.adapterOptions?.customEndpoint) {
url = `${this.buildURL()}/${options.adapterOptions.customEndpoint}`;
}

return this.ajax(url, "GET", { data: query });
}
};
}
1 change: 0 additions & 1 deletion addon/components/document-view.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
>
<div
class="document-view"
{{did-insert (perform this.initialiseDocumentSelection)}}
>
{{! List & Grid View }}
{{#if this.listView}}
Expand Down
47 changes: 36 additions & 11 deletions addon/components/document-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,53 @@ export default class DocumentViewComponent extends Component {

@task
*fetchDocuments() {
const documents = yield this.store.query("document", {
include: "category,files,tags",
filter: this.args.filters || {},
sort: this.sort ? `${this.sortDirection}${this.sort}` : "",
});
let documents = [];
const filter = this.args.filters || {};
if (filter.query) {
filter.only_newest = true;
const searchResult = yield this.store.query(
"search-result",
{
include: "document,matched_file",
filter,
page: { number: 1 },
},
{
adapterOptions: {
customEndpoint: "search",
},
},
);

documents = searchResult.reduce((acc, result) => {
if (!acc.some((doc) => doc.id === result.document.id)) {
acc.push(result.document);
}
return acc;
}, []);
console.log(documents);

} else {
documents = yield this.store.query("document", {
include: "category,files,tags",
filter,
sort: this.sort ? `${this.sortDirection}${this.sort}` : "",
});
}

this.initialiseDocumentSelection(documents);

return yield this.config.documentsPostProcess(documents);
}

@task
*initialiseDocumentSelection() {
initialiseDocumentSelection(docs) {
let docIds = [];
if (this.router.externalRouter.currentRoute?.queryParams?.document) {
docIds = decodeURIComponent(
this.router.externalRouter.currentRoute.queryParams.document,
).split(",");
}
if (docIds.length !== 0) {
const docs = yield this.store.query("document", {
filter: this.args.filters || {},
sort: this.sort ? `${this.sortDirection}${this.sort}` : "",
});
const selectedDocs = [...docs].filter((doc) => docIds.includes(doc.id));
selectedDocs.forEach((doc) => this.documents.selectDocument(doc));
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { tracked } from "@glimmer/tracking";
import { task } from "ember-concurrency";
import { trackedFunction } from "reactiveweb/function";

export default class DocumentViewComponent extends Component {
export default class SearchViewComponent extends Component {
@service store;
@service("alexandria-config") config;
@service("alexandria-documents") documents;
Expand All @@ -22,10 +22,10 @@ export default class DocumentViewComponent extends Component {
return [];
}

const files = await this.store.query(
"file",
const search = await this.store.query(
"search-result",
{
include: "document,renderings",
include: "document,matched_file",
filter: this.args.filters || {},
page: { number: 1 },
},
Expand All @@ -36,14 +36,7 @@ export default class DocumentViewComponent extends Component {
},
);

const documents = Array.from(
new Map(
files.map((file) => [
file.document.id,
this.store.peekRecord("document", file.document.id),
]),
).values(),
);
const documents = search.map((result) => result.document);

return await this.config.documentsPostProcess(documents);
});
Expand Down
2 changes: 1 addition & 1 deletion addon/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class IndexController extends Controller {
category: this.category,
tags: this.tags.length ? this.tags.join(",") : undefined,
marks: this.marks.length ? this.marks.join(",") : undefined,
search: this.search,
query: this.search,
activeGroup: this.activeGroup,
};

Expand Down
1 change: 1 addition & 0 deletions addon/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class SearchController extends Controller {

get filters() {
const filters = {
onlyNewest: true,
query: this.search,
};

Expand Down
11 changes: 11 additions & 0 deletions addon/models/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Model, { attr, belongsTo } from "@ember-data/model";

export default class SearchResultModel extends Model {
@attr searchRank;
@attr searchContext;
@attr fileName;
@attr documentName;

@belongsTo("document", { inverse: null, async: false }) document;
@belongsTo("matched-file", { inverse: null, async: false }) file;
}
3 changes: 3 additions & 0 deletions addon/serializers/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LocalizedSerializer } from "ember-localized-model";

export default class SearchResultSerializer extends LocalizedSerializer {}
2 changes: 1 addition & 1 deletion addon/templates/search.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div
class="alexandria-container uk-flex uk-flex-1 uk-height-1-1 uk-border uk-background-default uk-width-1 uk-flex-column uk-overflow-hidden"
>
<FileSearch
<SearchView
@filters={{this.filters}}
class="uk-border-top"
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ApplicationAdapter from "./application";

import adapterFactory from "ember-alexandria/adapters/search-result";

export default adapterFactory(ApplicationAdapter);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/models/search-result";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/serializers/search-result";
5 changes: 5 additions & 0 deletions tests/dummy/app/adapters/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ApplicationAdapter from "./application";

import adapterFactory from "ember-alexandria/adapters/search-result";

export default adapterFactory(ApplicationAdapter);
1 change: 1 addition & 0 deletions tests/dummy/app/models/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/models/search-result";
1 change: 1 addition & 0 deletions tests/dummy/app/serializers/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-alexandria/serializers/search-result";
9 changes: 9 additions & 0 deletions tests/dummy/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export default function makeServer(config) {
});

this.get("/files/multi", () => new Response(200, {}, {}));

this.get("/search", function (schema) {
const res = schema.searchResults.create({
document: schema.documents.create(),
});
const serialized = this.serialize(res);
serialized.data = [serialized.data];
return serialized;
});
},
});
}
1 change: 0 additions & 1 deletion tests/dummy/mirage/factories/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ export default Factory.extend({
name: () => faker.system.fileName(),
variant: "original",
downloadUrl: () => faker.internet.url(),
checksum: () => `sha256:${faker.git.commitSha({ length: 64 })}`,
});
16 changes: 16 additions & 0 deletions tests/dummy/mirage/factories/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { faker } from "@faker-js/faker";
import { Factory } from "miragejs";

export default Factory.extend({
fileName: () => faker.system.fileName(),
documentName: () => faker.system.fileName(),
searchRank: () => faker.number.int(),
searchContext: () => faker.word.adjective(),

afterCreate(result, server) {
result.update({
document: server.schema.documents.create(),
matchedFile: server.schema.files.create(),
});
},
});
6 changes: 6 additions & 0 deletions tests/dummy/mirage/models/search-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Model, belongsTo } from "miragejs";

export default Model.extend({
document: belongsTo(),
matchedFile: belongsTo(),
});
2 changes: 1 addition & 1 deletion tests/unit/controllers/application-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module("Unit | Controller | index", function (hooks) {
activeGroup: "group",
category: 1,
metainfo: JSON.stringify([{ key: "instance_id", value: "1" }]),
search: "test",
query: "test",
tags: undefined,
marks: undefined,
});
Expand Down

0 comments on commit 1312941

Please sign in to comment.