File size: 2,817 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

import { type IMessage } from 'omni-shared';

enum ChatMessageStorageTypes {
  User,
  Omni,
  AsyncJob
}

// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class ChatUtils {
  static GetMessageStorageType(payload: object): ChatMessageStorageTypes {
    // @ts-ignore
    if (payload.from !== undefined && payload.from === 'omni') {
      return ChatMessageStorageTypes.Omni;
    }
    // @ts-ignore
    else if (payload.sender !== undefined && payload.sender === 'me') {
      return ChatMessageStorageTypes.User;
    } else {
      return ChatMessageStorageTypes.AsyncJob;
    }
  }

  static async IsValidImageUrl(url: string): Promise<boolean> {
    try {
      const res = await fetch(url, { method: 'HEAD' });
      if (res.ok) {
        return res.headers.get('Content-Type')?.startsWith('image') ?? false;
      }

      return false;
    } catch (e) {
      return false;
    }
  }

  static CreateAsyncJobServerPayload(message: string, ts: number): object {
    return { message, sender: 'asyncjob', ts };
  }

  static IsAsyncJobMessage(payload: object): boolean {
    try {
      // until we have more defined message types
      const tryServerMessage: IMessage = payload as IMessage;
      return tryServerMessage.body?.content[0]?.type === 'omni/job';
    } catch (e) {
      return false;
    }
  }

  static GetAsyncJobIdFromMessage(payload: object): string | null {
    try {
      // until we have more defined message types
      const tryServerMessage: IMessage = payload as IMessage;
      return tryServerMessage.body.content[0].value;
    } catch (e) {
      return null;
    }
  }

  static ShouldPersist(payload: object): boolean {
    // always allow user local
    if (
      this.GetMessageStorageType(payload) === ChatMessageStorageTypes.User ||
      this.GetMessageStorageType(payload) === ChatMessageStorageTypes.AsyncJob
    ) {
      return true;
    }
    // allow all texts
    const tryServerMessage: IMessage = payload as IMessage;
    // accept images
    if (ChatUtils.HasChatContentProperty(tryServerMessage, 'images')) {
      return true;
    }
    // skip commands
    if (ChatUtils.HasChatContentProperty(tryServerMessage, 'commands')) {
      return false;
    }
    // accept texts
    if (ChatUtils.HasChatContentProperty(tryServerMessage, 'text')) {
      return true;
    }
    // @ts-ignore accept all other texts
    if (payload.body?.content !== undefined) {
      return true;
    }
    return false;
  }

  static HasChatContentProperty(payload: object, name: string): boolean {
    try {
      // @ts-ignore
      return payload.body.attachments[name]?.length > 0;
    } catch (e) {
      return false;
    }
  }
}

export { ChatMessageStorageTypes, ChatUtils };