aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/OpenCompletionUseCase.ts
blob: 1b63e7c574eba79822b2eb6b9289c4dfb284f4b9 (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
import { inject, injectable } from "tsyringe";
import CachedSettingRepository from "../repositories/CachedSettingRepository";
import CompletionType from "../../shared/CompletionType";
import BookmarkRepository from "./BookmarkRepository";
import HistoryRepository from "./HistoryRepository";

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

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

@injectable()
export default class OpenCompletionUseCase {
  constructor(
    @inject('BookmarkRepository') private bookmarkRepository: BookmarkRepository,
    @inject('HistoryRepository') 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: string): Promise<BookmarkItem[]> {
    return this.bookmarkRepository.queryBookmarks(query);
  }

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