aboutsummaryrefslogtreecommitdiffstats
path: root/src/models/Recipe.ts
blob: 959b43fd931fc4cff26665211d1c81085c6556b0 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import semver from 'semver';
import { pathExistsSync } from 'fs-extra';
import { join } from 'path';
import { DEFAULT_SERVICE_SETTINGS } from '../config';
import { ifUndefinedString, ifUndefinedBoolean } from '../jsUtils';

interface IRecipe {
  id: string;
  name: string;
  version: string;
  aliases?: string[];
  path: string;
  config: {
    serviceURL?: string;
    hasDirectMessages?: boolean;
    hasIndirectMessages?: boolean;
    hasNotificationSound?: boolean;
    hasTeamId?: boolean;
    hasCustomUrl?: boolean;
    hasHostedOption?: boolean;
    urlInputPrefix?: string;
    urlInputSuffix?: string;
    disablewebsecurity?: boolean;
    autoHibernate?: boolean;
    partition?: string;
    message?: string;
    allowFavoritesDelineationInUnreadCount?: boolean;
  };
}

export default class Recipe {
  id: string = '';

  name: string = '';

  description = '';

  version: string = '';

  aliases: string[] = [];

  serviceURL: string = '';

  hasDirectMessages: boolean = DEFAULT_SERVICE_SETTINGS.hasDirectMessages;

  hasIndirectMessages: boolean = DEFAULT_SERVICE_SETTINGS.hasIndirectMessages;

  hasNotificationSound: boolean = DEFAULT_SERVICE_SETTINGS.hasNotificationSound;

  hasTeamId: boolean = DEFAULT_SERVICE_SETTINGS.hasTeamId;

  hasCustomUrl: boolean = DEFAULT_SERVICE_SETTINGS.hasCustomUrl;

  hasHostedOption: boolean = DEFAULT_SERVICE_SETTINGS.hasHostedOption;

  urlInputPrefix: string = '';

  urlInputSuffix: string = '';

  message: string = '';

  allowFavoritesDelineationInUnreadCount: boolean =
    DEFAULT_SERVICE_SETTINGS.allowFavoritesDelineationInUnreadCount;

  disablewebsecurity: boolean = DEFAULT_SERVICE_SETTINGS.disablewebsecurity;

  // TODO: Is this even used?
  autoHibernate: boolean = DEFAULT_SERVICE_SETTINGS.autoHibernate;

  path: string = '';

  partition: string = '';

  // TODO: Need to reconcile which of these are optional/mandatory
  constructor(data: IRecipe) {
    if (!data) {
      throw new Error('Recipe config not valid');
    }

    if (!data.id) {
      // Ferdi 4 recipes do not have an Id
      throw new Error(`Recipe '${data.name}' requires Id`);
    }

    if (!semver.valid(data.version)) {
      throw new Error(
        `Version ${data.version} of recipe '${data.name}' is not a valid semver version`,
      );
    }

    // from the recipe
    this.id = ifUndefinedString(data.id, this.id);
    this.name = ifUndefinedString(data.name, this.name);
    this.version = ifUndefinedString(data.version, this.version);
    this.aliases = data.aliases || this.aliases;
    this.serviceURL = ifUndefinedString(
      data.config.serviceURL,
      this.serviceURL,
    );
    this.hasDirectMessages = ifUndefinedBoolean(
      data.config.hasDirectMessages,
      this.hasDirectMessages,
    );
    this.hasIndirectMessages = ifUndefinedBoolean(
      data.config.hasIndirectMessages,
      this.hasIndirectMessages,
    );
    this.hasNotificationSound = ifUndefinedBoolean(
      data.config.hasNotificationSound,
      this.hasNotificationSound,
    );
    this.hasTeamId = ifUndefinedBoolean(data.config.hasTeamId, this.hasTeamId);
    this.hasCustomUrl = ifUndefinedBoolean(
      data.config.hasCustomUrl,
      this.hasCustomUrl,
    );
    this.hasHostedOption = ifUndefinedBoolean(
      data.config.hasHostedOption,
      this.hasHostedOption,
    );
    this.urlInputPrefix = ifUndefinedString(
      data.config.urlInputPrefix,
      this.urlInputPrefix,
    );
    this.urlInputSuffix = ifUndefinedString(
      data.config.urlInputSuffix,
      this.urlInputSuffix,
    );
    this.disablewebsecurity = ifUndefinedBoolean(
      data.config.disablewebsecurity,
      this.disablewebsecurity,
    );
    this.autoHibernate = ifUndefinedBoolean(
      data.config.autoHibernate,
      this.autoHibernate,
    );
    this.message = ifUndefinedString(data.config.message, this.message);
    this.allowFavoritesDelineationInUnreadCount = ifUndefinedBoolean(
      data.config.allowFavoritesDelineationInUnreadCount,
      this.allowFavoritesDelineationInUnreadCount,
    );

    // computed
    this.path = data.path;
    this.partition = ifUndefinedString(data.config.partition, this.partition);
  }

  // TODO: Need to remove this if its not used anywhere
  get author(): string[] {
    return [];
  }

  get hasDarkMode(): boolean {
    return pathExistsSync(join(this.path, 'darkmode.css'));
  }
}