diff options
Diffstat (limited to 'ui/lib/apiServer.js')
| -rw-r--r-- | ui/lib/apiServer.js | 148 |
1 files changed, 75 insertions, 73 deletions
diff --git a/ui/lib/apiServer.js b/ui/lib/apiServer.js index 5c6e5ef..3714b63 100644 --- a/ui/lib/apiServer.js +++ b/ui/lib/apiServer.js @@ -2,122 +2,124 @@ import axios from 'axios'; import { channelsList, logins, messages } from '$lib/store'; export const apiServer = axios.create({ - baseURL: '/api/', - validateStatus: () => true, + baseURL: '/api/', + validateStatus: () => true }); export async function boot() { - return apiServer.get('/boot'); + return apiServer.get('/boot'); } export async function setup(name, password) { - return apiServer.post('/setup', { name, password }); + return apiServer.post('/setup', { name, password }); } export async function logIn(name, password) { - return apiServer.post('/auth/login', { name, password }); + return apiServer.post('/auth/login', { name, password }); } export async function logOut() { - return apiServer.post('/auth/logout', {}); + return apiServer.post('/auth/logout', {}); } export async function changePassword(password, to) { - return apiServer.post('/password', { password, to }); + return apiServer.post('/password', { password, to }); } export async function createChannel(name) { - return apiServer.post('/channels', { name }); + return apiServer.post('/channels', { name }); } export async function postToChannel(channelId, body) { - return apiServer.post(`/channels/${channelId}`, { body }); + return apiServer.post(`/channels/${channelId}`, { body }); } export async function deleteMessage(messageId) { - // TODO + // TODO } export async function createInvite() { - return apiServer.post(`/invite`, {}); + return apiServer.post(`/invite`, {}); } export async function getInvite(inviteId) { - return apiServer.get(`/invite/${inviteId}`); + return apiServer.get(`/invite/${inviteId}`); } export async function acceptInvite(inviteId, username, password) { - const data = { - name: username, - password, - }; - return apiServer.post(`/invite/${inviteId}`, data); + 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; + 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; - } + 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; - } + 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)); - break; - case 'deleted': - messages.update((value) => value.deleteMessage(data.id)); - break; - } + switch (data.event) { + case 'sent': + messages.update((value) => + value.addMessage(data.channel, data.id, data.at, data.sender, data.body) + ); + break; + case 'deleted': + messages.update((value) => value.deleteMessage(data.id)); + break; + } } |
