2021-06-11 23:16:57 +00:00
|
|
|
import type { ChatType, ProsodyListRoomsResult } from 'shared/lib/types'
|
2021-05-10 19:04:04 +00:00
|
|
|
|
2021-06-11 12:30:46 +00:00
|
|
|
interface ActionPluginSettingsParams {
|
|
|
|
npmName: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function register ({ registerHook, registerSettingsScript, peertubeHelpers }: RegisterOptions): void {
|
|
|
|
function getBaseRoute (): string {
|
|
|
|
// NB: this will come with Peertube > 3.2.1 (3.3.0?)
|
|
|
|
if (peertubeHelpers.getBaseRouterRoute) {
|
|
|
|
return peertubeHelpers.getBaseRouterRoute()
|
|
|
|
}
|
|
|
|
// We are guessing the route with the correct plugin version with this trick:
|
|
|
|
const staticBase = peertubeHelpers.getBaseStaticRoute()
|
|
|
|
// we can't use '/plugins/livechat/router', because the loaded html page needs correct relative paths.
|
|
|
|
return staticBase.replace(/\/static.*$/, '/router')
|
|
|
|
}
|
|
|
|
|
|
|
|
registerHook({
|
|
|
|
target: 'action:admin-plugin-settings.init',
|
|
|
|
handler: ({ npmName }: ActionPluginSettingsParams) => {
|
|
|
|
if (npmName !== PLUGIN_CHAT_PACKAGE_NAME) {
|
|
|
|
console.log(`[peertube-plugin-livechat] Settings for ${npmName}, not ${PLUGIN_CHAT_PACKAGE_NAME}. Returning.`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log('[peertube-plugin-livechat] Initializing diagnostic button')
|
|
|
|
const diagButtons = document.querySelectorAll('.peertube-plugin-livechat-launch-diagnostic')
|
|
|
|
diagButtons.forEach(diagButton => {
|
|
|
|
if (diagButton.hasAttribute('href')) { return }
|
|
|
|
// TODO: use a modal instead of a target=_blank
|
|
|
|
diagButton.setAttribute('href', getBaseRoute() + '/settings/diagnostic')
|
|
|
|
diagButton.setAttribute('target', '_blank')
|
|
|
|
})
|
2021-06-11 23:16:57 +00:00
|
|
|
console.log('[peertube-plugin-livechat] Initializing prosody-list-rooms button')
|
|
|
|
const listRoomsButtons: NodeListOf<HTMLAnchorElement> =
|
|
|
|
document.querySelectorAll('.peertube-plugin-livechat-prosody-list-rooms')
|
|
|
|
listRoomsButtons.forEach(listRoomsButton => {
|
|
|
|
if (listRoomsButton.classList.contains('btn')) { return }
|
|
|
|
listRoomsButton.classList.add('btn')
|
|
|
|
listRoomsButton.classList.add('action-button')
|
|
|
|
listRoomsButton.classList.add('orange-button')
|
|
|
|
listRoomsButton.onclick = async (): Promise<void> => {
|
|
|
|
console.log('[peertube-plugin-livechat] Opening the room list...')
|
|
|
|
// TODO: use showModal (seems buggy with Peertube 3.2.1)
|
|
|
|
|
|
|
|
try {
|
|
|
|
document.querySelectorAll('.peertube-plugin-livechat-prosody-list-rooms-content')
|
|
|
|
.forEach(dom => dom.remove())
|
|
|
|
const container = document.createElement('div')
|
|
|
|
container.classList.add('peertube-plugin-livechat-prosody-list-rooms-content')
|
|
|
|
container.textContent = '...'
|
|
|
|
listRoomsButton.after(container)
|
|
|
|
|
2021-06-12 01:52:45 +00:00
|
|
|
const response = await fetch(getBaseRoute() + '/webchat/prosody-list-rooms', {
|
2021-06-11 23:16:57 +00:00
|
|
|
method: 'GET',
|
|
|
|
headers: peertubeHelpers.getAuthHeader()
|
|
|
|
})
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Response is not ok')
|
|
|
|
}
|
|
|
|
const json: ProsodyListRoomsResult = await response.json()
|
|
|
|
if (!json.ok) {
|
|
|
|
container.textContent = json.error
|
|
|
|
container.classList.add('peertube-plugin-livechat-error')
|
|
|
|
} else {
|
2021-07-19 14:51:51 +00:00
|
|
|
const rooms = json.rooms.sort((a, b) => {
|
|
|
|
const timestampA = a.lasttimestamp ?? 0
|
|
|
|
const timestampB = b.lasttimestamp ?? 0
|
|
|
|
if (timestampA === timestampB) {
|
|
|
|
return a.name.localeCompare(b.name)
|
|
|
|
} else if (timestampA < timestampB) {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
})
|
2021-06-12 01:52:45 +00:00
|
|
|
|
2021-06-11 23:16:57 +00:00
|
|
|
container.textContent = ''
|
|
|
|
const table = document.createElement('table')
|
2021-06-12 01:52:45 +00:00
|
|
|
table.classList.add('peertube-plugin-livechat-prosody-list-rooms')
|
2021-06-11 23:16:57 +00:00
|
|
|
container.append(table)
|
2021-06-12 01:52:45 +00:00
|
|
|
// TODO: translate labels.
|
2021-07-19 13:45:57 +00:00
|
|
|
const labels: any = {
|
2021-07-19 13:47:18 +00:00
|
|
|
RoomName: 'Room name',
|
|
|
|
RoomDescription: 'Room description',
|
2021-07-19 13:45:57 +00:00
|
|
|
NotFound: 'Not found',
|
2021-07-19 14:51:51 +00:00
|
|
|
Video: 'Video',
|
2021-08-05 16:25:27 +00:00
|
|
|
Channel: 'Channel',
|
2021-07-19 14:51:51 +00:00
|
|
|
LastActivity: 'Last activity'
|
2021-07-19 13:45:57 +00:00
|
|
|
}
|
2021-07-19 14:51:51 +00:00
|
|
|
|
2021-06-12 01:52:45 +00:00
|
|
|
const titleLineEl = document.createElement('tr')
|
|
|
|
const titleNameEl = document.createElement('th')
|
2021-07-19 13:47:18 +00:00
|
|
|
titleNameEl.textContent = labels.RoomName
|
2021-06-12 01:52:45 +00:00
|
|
|
const titleDescriptionEl = document.createElement('th')
|
2021-07-19 13:47:18 +00:00
|
|
|
titleDescriptionEl.textContent = labels.RoomDescription
|
2021-07-19 13:45:57 +00:00
|
|
|
const titleVideoEl = document.createElement('th')
|
2021-08-05 16:25:27 +00:00
|
|
|
titleVideoEl.textContent = `${labels.Video as string} / ${labels.Channel as string}`
|
2021-07-19 14:51:51 +00:00
|
|
|
const titleLastActivityEl = document.createElement('th')
|
|
|
|
titleLastActivityEl.textContent = labels.LastActivity
|
2021-06-12 01:52:45 +00:00
|
|
|
titleLineEl.append(titleNameEl)
|
|
|
|
titleLineEl.append(titleDescriptionEl)
|
2021-07-19 13:45:57 +00:00
|
|
|
titleLineEl.append(titleVideoEl)
|
2021-07-19 14:51:51 +00:00
|
|
|
titleLineEl.append(titleLastActivityEl)
|
2021-06-12 01:52:45 +00:00
|
|
|
table.append(titleLineEl)
|
|
|
|
rooms.forEach(room => {
|
2021-08-05 16:25:27 +00:00
|
|
|
const localpart = room.localpart
|
2021-06-11 23:16:57 +00:00
|
|
|
const lineEl = document.createElement('tr')
|
|
|
|
const nameEl = document.createElement('td')
|
|
|
|
const aEl = document.createElement('a')
|
|
|
|
aEl.textContent = room.name
|
|
|
|
aEl.target = '_blank'
|
2021-06-12 01:52:45 +00:00
|
|
|
const descriptionEl = document.createElement('td')
|
|
|
|
descriptionEl.textContent = room.description
|
2021-07-19 13:45:57 +00:00
|
|
|
const videoEl = document.createElement('td')
|
2021-07-19 14:51:51 +00:00
|
|
|
const lastActivityEl = document.createElement('td')
|
|
|
|
if (room.lasttimestamp && (typeof room.lasttimestamp === 'number')) {
|
|
|
|
const date = new Date(room.lasttimestamp * 1000)
|
|
|
|
lastActivityEl.textContent = date.toLocaleDateString() + ' ' + date.toLocaleTimeString()
|
|
|
|
}
|
2021-06-11 23:16:57 +00:00
|
|
|
nameEl.append(aEl)
|
|
|
|
lineEl.append(nameEl)
|
2021-06-12 01:52:45 +00:00
|
|
|
lineEl.append(descriptionEl)
|
2021-07-19 13:45:57 +00:00
|
|
|
lineEl.append(videoEl)
|
2021-07-19 14:51:51 +00:00
|
|
|
lineEl.append(lastActivityEl)
|
2021-06-11 23:16:57 +00:00
|
|
|
table.append(lineEl)
|
2021-07-19 13:45:57 +00:00
|
|
|
|
2021-08-05 16:25:27 +00:00
|
|
|
const channelMatches = localpart.match(/^channel\.(\d+)$/)
|
|
|
|
if (channelMatches) {
|
|
|
|
// Here we have a channel chat room
|
|
|
|
// The backend should have added informations here
|
|
|
|
// (because the Peertube API can't work with channelId...)
|
2021-08-05 16:34:38 +00:00
|
|
|
const href = getBaseRoute() + '/webchat/room/' + encodeURIComponent(localpart) + '?fromadmin=1'
|
2021-08-05 16:25:27 +00:00
|
|
|
if (room.channel?.name) {
|
|
|
|
aEl.href = href // here we know that the channel still exists, so we can open the webchat.
|
|
|
|
const aVideoEl = document.createElement('a')
|
|
|
|
aVideoEl.textContent = room.channel?.displayName ?? '-'
|
|
|
|
aVideoEl.target = '_blank'
|
|
|
|
aVideoEl.href = '/video-channels/' + room.channel.name
|
|
|
|
videoEl.append(aVideoEl)
|
|
|
|
}
|
|
|
|
} else if (/^[a-zA-A0-9-]+$/.test(localpart)) {
|
|
|
|
// localpart must be a video uuid.
|
|
|
|
const uuid = localpart
|
2021-08-05 16:34:38 +00:00
|
|
|
const href = getBaseRoute() + '/webchat/room/' + encodeURIComponent(uuid) + '?fromadmin=1'
|
2021-07-19 13:45:57 +00:00
|
|
|
const p = fetch('/api/v1/videos/' + uuid, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: peertubeHelpers.getAuthHeader()
|
|
|
|
})
|
|
|
|
p.then(async res => {
|
|
|
|
if (!res.ok) {
|
|
|
|
videoEl.textContent = labels.NotFound
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const video: Video | undefined = await res.json()
|
|
|
|
if (!video) {
|
|
|
|
videoEl.textContent = labels.NotFound
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
aEl.href = href
|
|
|
|
const aVideoEl = document.createElement('a')
|
|
|
|
aVideoEl.textContent = video.name
|
|
|
|
aVideoEl.target = '_blank'
|
|
|
|
aVideoEl.href = '/videos/watch/' + uuid
|
|
|
|
videoEl.append(aVideoEl)
|
|
|
|
}, () => {
|
|
|
|
console.error('[peertube-plugin-livechat] Failed to retrieve video ' + uuid)
|
|
|
|
})
|
|
|
|
}
|
2021-06-11 23:16:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
peertubeHelpers.notifier.error('Room list failed')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-06-11 12:30:46 +00:00
|
|
|
}
|
|
|
|
})
|
2021-06-02 10:32:00 +00:00
|
|
|
registerSettingsScript({
|
|
|
|
isSettingHidden: options => {
|
|
|
|
const name = options.setting.name
|
2021-06-02 17:54:04 +00:00
|
|
|
switch (name) {
|
|
|
|
case 'chat-type-help-disabled':
|
|
|
|
return options.formValues['chat-type'] !== ('disabled' as ChatType)
|
2021-08-05 13:41:49 +00:00
|
|
|
case 'prosody-room-type':
|
2021-06-03 09:46:11 +00:00
|
|
|
case 'prosody-port':
|
2021-06-22 11:23:01 +00:00
|
|
|
case 'prosody-peertube-uri':
|
2021-06-02 17:54:04 +00:00
|
|
|
case 'chat-type-help-builtin-prosody':
|
2021-06-11 23:16:57 +00:00
|
|
|
case 'prosody-list-rooms':
|
2021-07-13 15:40:29 +00:00
|
|
|
case 'prosody-advanced':
|
|
|
|
case 'prosody-c2s':
|
2021-06-02 17:54:04 +00:00
|
|
|
return options.formValues['chat-type'] !== ('builtin-prosody' as ChatType)
|
2021-07-13 15:40:29 +00:00
|
|
|
case 'prosody-c2s-port':
|
2021-07-14 17:11:09 +00:00
|
|
|
return !(
|
|
|
|
options.formValues['chat-type'] === ('builtin-prosody' as ChatType) &&
|
|
|
|
options.formValues['prosody-c2s'] === true
|
|
|
|
)
|
2021-06-03 09:46:11 +00:00
|
|
|
case 'chat-server':
|
|
|
|
case 'chat-room':
|
|
|
|
case 'chat-bosh-uri':
|
|
|
|
case 'chat-ws-uri':
|
2021-06-02 17:54:04 +00:00
|
|
|
case 'chat-type-help-builtin-converse':
|
|
|
|
return options.formValues['chat-type'] !== ('builtin-converse' as ChatType)
|
2021-06-03 09:46:11 +00:00
|
|
|
case 'chat-uri':
|
2021-06-02 17:54:04 +00:00
|
|
|
case 'chat-type-help-external-uri':
|
|
|
|
return options.formValues['chat-type'] !== ('external-uri' as ChatType)
|
2021-06-03 09:46:11 +00:00
|
|
|
case 'chat-style':
|
|
|
|
return options.formValues['chat-type'] === 'disabled'
|
2021-06-08 16:08:58 +00:00
|
|
|
case 'chat-per-live-video-warning':
|
|
|
|
return !(options.formValues['chat-all-lives'] === true && options.formValues['chat-per-live-video'] === true)
|
2021-06-02 17:54:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 10:32:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
2021-05-10 19:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
register
|
|
|
|
}
|