aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/stores/Service.ts
blob: 78c57cbabeac071c0e8f5690a31d56b339b06513 (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
/*
 * Copyright (C)  2022 Kristóf Marussy <kristof@marussy.com>
 *
 * This file is part of Sophie.
 *
 * Sophie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import type { UnreadCount } from '@sophie/service-shared';
import {
  service as originalService,
  ServiceSettingsSnapshotIn,
} from '@sophie/shared';
import { Instance, getSnapshot, ReferenceIdentifier } from 'mobx-state-tree';

import SettingsWithId from '../utils/SettingsWithId';
import generateId from '../utils/generateId';
import overrideProps from '../utils/overrideProps';

import { ProfileSettingsSnapshotWithId } from './Profile';
import { serviceSettings } from './ServiceSettings';

export interface ServiceConfig
  extends Omit<ServiceSettingsSnapshotIn, 'profile'> {
  id?: string | undefined;

  profile?: ReferenceIdentifier | undefined;
}

export const service = overrideProps(originalService, {
  settings: serviceSettings,
})
  .views((self) => ({
    get config(): ServiceConfig {
      const { id, settings } = self;
      return { ...getSnapshot(settings), id };
    },
  }))
  .actions((self) => ({
    setLocation({
      url,
      canGoBack,
      canGoForward,
    }: {
      url: string;
      canGoBack: boolean;
      canGoForward: boolean;
    }): void {
      self.currentUrl = url;
      self.canGoBack = canGoBack;
      self.canGoForward = canGoForward;
    },
    setTitle(title: string): void {
      self.title = title;
    },
    startedLoading(): void {
      self.state = 'loading';
    },
    finishedLoading(): void {
      if (self.state === 'loading') {
        // Do not overwrite crashed state if the service haven't been reloaded yet.
        self.state = 'loaded';
      }
    },
    crashed(): void {
      self.state = 'crashed';
    },
    setUnreadCount({ direct, indirect }: UnreadCount): void {
      if (direct !== undefined) {
        self.directMessageCount = direct;
      }
      if (indirect !== undefined) {
        self.indirectMessageCount = indirect;
      }
    },
  }));

export interface Service extends Instance<typeof service> {}

export type ServiceSettingsSnapshotWithId =
  SettingsWithId<ServiceSettingsSnapshotIn>;

export function addMissingServiceIdsAndProfiles(
  serviceConfigs: ServiceConfig[] | undefined,
  profiles: ProfileSettingsSnapshotWithId[],
): ServiceSettingsSnapshotWithId[] {
  return (serviceConfigs ?? []).map((serviceConfig) => {
    const { id, ...settings } = serviceConfig;
    const { name } = settings;
    let { profile: profileId } = settings;
    if (profileId === undefined) {
      profileId = generateId(name);
      profiles.push({ id: profileId, settings: { name } });
    }
    return {
      id: id === undefined ? generateId(name) : id,
      settings: { ...settings, profile: profileId },
    };
  });
}