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.
|
|
|
|
*
|
2022-03-10 17:56:18 +00:00
|
|
|
* Change Date: 2026-03-10
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
define([
|
|
|
|
'Bastelstu.be/PromiseWrap/Ajax',
|
|
|
|
'WoltLabSuite/Core/Core',
|
|
|
|
'./User',
|
|
|
|
], function (Ajax, Core, User) {
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const DEPENDENCIES = ['sessionID', 'roomID']
|
2018-08-16 22:30:59 +00:00
|
|
|
/**
|
|
|
|
* Represents a chat room.
|
|
|
|
*/
|
|
|
|
class Room {
|
|
|
|
constructor(sessionID, roomID) {
|
|
|
|
this.sessionID = sessionID
|
2020-11-01 16:41:19 +00:00
|
|
|
this.roomID = roomID
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a request to join the room.
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
async join() {
|
2020-11-01 16:41:19 +00:00
|
|
|
const payload = {
|
|
|
|
className: 'chat\\data\\room\\RoomAction',
|
|
|
|
actionName: 'join',
|
|
|
|
parameters: { roomID: this.roomID, sessionID: this.sessionID },
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
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) {
|
2020-11-01 16:41:19 +00:00
|
|
|
const payload = {
|
|
|
|
className: 'chat\\data\\room\\RoomAction',
|
|
|
|
actionName: 'leave',
|
|
|
|
parameters: { roomID: this.roomID, sessionID: this.sessionID },
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
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)
|
2020-11-01 16:41:19 +00:00
|
|
|
.split('&')
|
|
|
|
.map((item) => item.split('='))
|
|
|
|
.map((item) => item.map(decodeURIComponent))
|
|
|
|
.forEach((item) => formData.append(item[0], item[1]))
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
if (navigator.sendBeacon) {
|
|
|
|
navigator.sendBeacon(url, formData)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.fetch) {
|
2020-11-01 16:41:19 +00:00
|
|
|
fetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
keepalive: true,
|
|
|
|
redirect: 'follow',
|
|
|
|
body: formData,
|
|
|
|
})
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve()
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
return Ajax.api(this, payload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a request to retrieve the userIDs inhabiting this room.
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
async getUsers() {
|
2020-11-01 16:41:19 +00:00
|
|
|
const payload = {
|
|
|
|
className: 'chat\\data\\room\\RoomAction',
|
|
|
|
actionName: 'getUsers',
|
|
|
|
objectIDs: [this.roomID],
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
const result = await Ajax.api(this, payload)
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
return Object.values(result.returnValues).map((user) => new User(user))
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ajaxSetup() {
|
2020-11-01 16:41:19 +00:00
|
|
|
return { silent: true, ignoreError: true }
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Room.DEPENDENCIES = DEPENDENCIES
|
|
|
|
|
|
|
|
return Room
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|