aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/CompletionUseCase.ts
blob: fd3c279a52fa335535da2c66a8fea43810c80337 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { inject, injectable } from "tsyringe";
import HistoryRepository from "./HistoryRepository";
import BookmarkRepository from "./BookmarkRepository";
import CachedSettingRepository from "../repositories/CachedSettingRepository";
import CompletionType from "../../shared/CompletionType";

export type BookmarkItem = {
  title: string
  url: string
}

export type HistoryItem = {
  title: string
  url: string
}

@injectable()
export default class CompletionUseCase {
  constructor(
    private bookmarkRepository: BookmarkRepository,
    private historyRepository: HistoryRepository,
    @inject("CachedSettingRepository")
    private cachedSettingRepository: CachedSettingRepository,
  ) {
  }

  async getCompletionTypes(): Promise<CompletionType[]> {
    const settings = await this.cachedSettingRepository.get();
    const types: CompletionType[] = [];
    for (const c of settings.properties.complete) {
      switch (c) {
      case 's':
        types.push(CompletionType.SearchEngines);
        break;
      case 'h':
        types.push(CompletionType.History);
        break;
      case 'b':
        types.push(CompletionType.Bookmarks);
        break;
      }
      // ignore invalid characters in the complete property
    }
    return types;
  }

  async requestSearchEngines(query: string): Promise<string[]> {
    const settings = await this.cachedSettingRepository.get();
    return Object.keys(settings.search.engines)
      .filter(key => key.startsWith(query))
  }

  requestBookmarks(query: any): Promise<BookmarkItem[]> {
    return this.bookmarkRepository.queryBookmarks(query);
  }

  async requestHistory(query: string): Promise<HistoryItem[]> {
    return this.historyRepository.queryHistories(query);
  }
}