File size: 1,705 Bytes
8ede856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export const CHAT_SELECTED_CONFIG_STORAGE_KEY = 'chat.selectedConfigId';

export type ChatMessageType = 'FriendMessage' | 'GroupMessage';

export interface WebchatUmoDetails {
  platformId: string;
  messageType: ChatMessageType;
  username: string;
  sessionKey: string;
  umo: string;
}

function getFromLocalStorage(key: string, fallback: string): string {
  try {
    if (typeof localStorage === 'undefined') {
      return fallback;
    }
    const value = localStorage.getItem(key);
    return value == null ? fallback : value;
  } catch {
    return fallback;
  }
}

function setToLocalStorage(key: string, value: string): void {
  try {
    if (typeof localStorage === 'undefined') {
      return;
    }
    localStorage.setItem(key, value);
  } catch {
    // Ignore storage errors (e.g. private mode / restricted storage).
  }
}

export function getStoredDashboardUsername(): string {
  return getFromLocalStorage('user', '').trim() || 'guest';
}

export function getStoredSelectedChatConfigId(): string {
  return getFromLocalStorage(CHAT_SELECTED_CONFIG_STORAGE_KEY, '').trim() || 'default';
}

export function setStoredSelectedChatConfigId(configId: string): void {
  setToLocalStorage(CHAT_SELECTED_CONFIG_STORAGE_KEY, configId);
}

export function buildWebchatUmoDetails(sessionId: string, isGroup = false): WebchatUmoDetails {
  const platformId = 'webchat';
  const username = getStoredDashboardUsername();
  const messageType: ChatMessageType = isGroup ? 'GroupMessage' : 'FriendMessage';
  const sessionKey = `${platformId}!${username}!${sessionId}`;
  return {
    platformId,
    messageType,
    username,
    sessionKey,
    umo: `${platformId}:${messageType}:${sessionKey}`
  };
}