1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-11-18 16:50:08 +00:00
Tims-Chat/files_wcf/js/Bastelstu.be/Chat/Room.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-08-16 22:30:59 +00:00
/*
2021-02-04 22:04:35 +00:00
* Copyright (c) 2010-2021 Tim Düsterhus.
2018-08-16 22:30:59 +00:00
*
* Use of this software is governed by the Business Source License
* included in the LICENSE file.
*
2021-03-05 16:19:27 +00:00
* Change Date: 2025-03-05
2018-08-16 22:30:59 +00:00
*
* On the date above, in accordance with the Business Source
* License, use of this software will be governed by version 2
* or later of the General Public License.
*/
define([
'Bastelstu.be/PromiseWrap/Ajax',
'WoltLabSuite/Core/Core',
'./User',
], function (Ajax, Core, User) {
'use strict'
2020-11-01 16:41:19 +00:00
2018-08-16 22:30:59 +00:00
const DEPENDENCIES = ['sessionID', 'roomID']
/**
* Represents a chat room.
*/
class Room {
constructor(sessionID, roomID) {
this.sessionID = sessionID
this.roomID = roomID
}
/**
* Sends a request to join the room.
*
* @returns {Promise}
*/
async join() {
const payload = {
className: 'chat\\data\\room\\RoomAction',
actionName: 'join',
parameters: { roomID: this.roomID, sessionID: this.sessionID },
}
return Ajax.api(this, payload)
}
/**
* Sends a request to leave the room.
*
* @param {boolean} unload Send a beacon if true'ish and a regular AJAX request otherwise.
*/
leave(unload = false) {
const payload = {
className: 'chat\\data\\room\\RoomAction',
actionName: 'leave',
parameters: { roomID: this.roomID, sessionID: this.sessionID },
}
if (unload && FormData && (navigator.sendBeacon || window.fetch)) {
// Ordinary AJAX requests are unreliable during unload:
// Use navigator.sendBeacon if available, otherwise hope
// for the best and clean up based on a time out.
const url = WSC_API_URL + 'index.php?ajax-proxy/&t=' + SECURITY_TOKEN
const formData = new FormData()
Core.serialize(payload)
.split('&')
.map((item) => item.split('='))
.map((item) => item.map(decodeURIComponent))
.forEach((item) => formData.append(item[0], item[1]))
if (navigator.sendBeacon) {
navigator.sendBeacon(url, formData)
}
if (window.fetch) {
fetch(url, {
method: 'POST',
keepalive: true,
redirect: 'follow',
body: formData,
})
}
return Promise.resolve()
} else {
return Ajax.api(this, payload)
}
}
/**
* Sends a request to retrieve the userIDs inhabiting this room.
*
* @returns {Promise}
*/
async getUsers() {
const payload = {
className: 'chat\\data\\room\\RoomAction',
actionName: 'getUsers',
objectIDs: [this.roomID],
}
const result = await Ajax.api(this, payload)
return Object.values(result.returnValues).map((user) => new User(user))
}
_ajaxSetup() {
return { silent: true, ignoreError: true }
}
}
Room.DEPENDENCIES = DEPENDENCIES
return Room
})