summaryrefslogtreecommitdiff
path: root/ui/lib/apiServer.js
blob: 3e270fc6e947910ab726880b0dcb58c39a85ece4 (plain)
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
import axios from 'axios';
import { get } from 'svelte/store';
import { currentUser, channelsList, logins, messages } from '$lib/store';

export const apiServer = axios.create({
  baseURL: '/api/',
  validateStatus: () => true,
});

export async function boot() {
  return apiServer.get('/boot');
}

export async function setup(name, password) {
  return apiServer.post('/setup', { name, password });
}

export async function logIn(name, password) {
  return apiServer.post('/auth/login', { name, password });
}

export async function logOut() {
  return apiServer.post('/auth/logout', {});
}

export async function changePassword(password, to) {
  return apiServer.post('/password', { password, to });
}

export async function createChannel(name) {
  return apiServer.post('/channels', { name });
}

export async function postToChannel(channelId, body) {
  return apiServer.post(`/channels/${channelId}`, { body });
}

export async function deleteMessage(messageId) {
  // TODO
}

export async function createInvite() {
  return apiServer.post(`/invite`, {});
}

export async function getInvite(inviteId) {
  return apiServer.get(`/invite/${inviteId}`);
}

export async function acceptInvite(inviteId, username, password) {
  const data = {
    name: username,
    password,
  };
  return apiServer.post(`/invite/${inviteId}`, data);
}

export function subscribeToEvents(resume_point) {
  const eventsUrl = new URL('/api/events', window.location);
  eventsUrl.searchParams.append('resume_point', resume_point);
  const evtSource = new EventSource(eventsUrl.toString());
  // TODO: this should process all incoming events and store them.
  // TODO: eventually we'll need to handle expiring old info, so as not to use
  //       infinite browser memory.
  /*
   * Known message types as of now:
   * - created: a channel is created.
   *   - action: ignore.
   * - message: a message is created.
   *   - action: display message in channel.
   * - message_deleted: a message is deleted.
   *   - action: replace message with <...>.
   * - deleted: a channel is deleted.
   *   - action: remove channel from sidebar.
  */
  evtSource.onmessage = (evt) => {
    const data = JSON.parse(evt.data);

    switch (data.type) {
      case 'login':
        onLoginEvent(data);
        break;
      case 'channel':
        onChannelEvent(data);
        break;
      case 'message':
        onMessageEvent(data);
        break;
    }
  }

  return evtSource;
}

function onLoginEvent(data) {
  switch (data.event) {
    case 'created':
      logins.update((value) => value.addLogin(data.id, data.name))
      break;
  }
}

function onChannelEvent(data) {
  switch (data.event) {
    case 'created':
      channelsList.update((value) => value.addChannel(data.id, data.name))
      break;
    case 'deleted':
      channelsList.update((value) => value.deleteChannel(data.id));
      messages.update((value) => value.deleteChannel(data.id));
      break;
  }
}

function onMessageEvent(data) {
  switch (data.event) {
    case 'sent':
      messages.update((value) => value.addMessage(data.channel, data.id, data.at, data.sender, data.body));
      displayToast(data); 
      break;
    case 'deleted':
      messages.update((value) => value.deleteMessage(data.id));
      break;
  }
}

function displayToast(data) {
  // we use get throughout this as this function is not reactive, and just
  // needs the values of the stores at a moment in time.
  const currentUserId = get(currentUser).id;
  if (currentUserId === data.sender) {
    return;
  }

  const senderName = get(logins).get(data.sender);
  const channelName = get(channelsList).get(data.channel);
  const title = `${senderName} in ${channelName}`;

  const opts = {
    body: data.body,
    tag: title,
    // TODO: we need to inject the understory/hi icon in a more principled way here:
    icon: "/ui/lib/assets/logo.png",
    // TODO: support onclick bringing you to the relevant channel?
    onclick: null
  }
  new Notification(title, opts);
}