Commit ce48bda9 authored by lutangar's avatar lutangar
Browse files

feat: implement a naive version from the settings page

parent da14a24c
{
"singleQuote": true
}
.header-right {
background-color: black;
}
.hello-world-h4 {
width: 150px;
color: white;
}
.upload-button {
background-color: red !important;
}
function register ({ registerHook }) {
registerHook({
target: 'action:admin-plugin-settings.init',
handler: ({ npmName }) => {
console.log('loaded admin plugin settings %s', npmName)
const div = document.createElement('div')
div.innerHTML = '<p>Just after the user name</p>'
document.querySelector('#user-name-wrapper').appendChild(div)
}
})
}
export {
register
}
function register ({ registerHook, registerSettingsScript, peertubeHelpers }) {
registerHook({
target: 'action:application.init',
handler: () => onApplicationInit(peertubeHelpers)
})
registerHook({
target: 'action:auth-user.information-loaded',
handler: ({ user }) => {
console.log('User information loaded.', user)
fetch('/api/v1/users/me', {
method: 'GET',
headers: peertubeHelpers.getAuthHeader()
}).then(res => res.json())
.then(data => console.log('Hi %s.', data.username))
}
})
registerHook({
target: 'action:auth-user.logged-in',
handler: () => console.log('User logged in.')
})
registerHook({
target: 'action:auth-user.logged-out',
handler: () => console.log('User logged out.')
})
// Videos list
registerHook({
target: 'filter:api.trending-videos.videos.list.params',
handler: params => Object.assign({}, params, { sort: '-views' })
})
registerHook({
target: 'filter:api.trending-videos.videos.list.result',
handler: result => addSymbolToVideoNameResult(result, '<3')
})
registerHook({
target: 'filter:api.local-videos.videos.list.params',
handler: params => Object.assign({}, params, { sort: '-views' })
})
registerHook({
target: 'filter:api.local-videos.videos.list.result',
handler: result => addSymbolToVideoNameResult(result, ':)')
})
registerHook({
target: 'filter:api.recently-added-videos.videos.list.params',
handler: params => Object.assign({}, params, { filter: 'all-local' })
})
registerHook({
target: 'filter:api.recently-added-videos.videos.list.result',
handler: result => addSymbolToVideoNameResult(result, 'o/')
})
registerHook({
target: 'filter:api.user-subscriptions-videos.videos.list.params',
handler: params => Object.assign({}, params, { sort: '-views' })
})
registerHook({
target: 'filter:api.user-subscriptions-videos.videos.list.result',
handler: result => addSymbolToVideoNameResult(result, ':D')
})
registerHook({
target: 'filter:internal.common.svg-icons.get-content.result',
handler: (result, params) => {
if (params.name === 'syndication') {
return '<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="50"/></svg>'
}
return result
}
})
// Router hooks
registerHook({
target: 'action:router.navigation-end',
handler: params => console.log('New URL! %s.', params.path)
})
// Modal hooks
registerHook({
target: 'action:modal.video-download.shown',
handler: () => {
console.log('Video download modal shown')
document.getElementById('download-torrent').checked = true
document.getElementById('download-direct').parentElement.style.display = 'none'
}
})
// Fake hook
registerHook({
target: 'fakeHook',
handler: () => console.log('fake hook')
})
// Settings
registerSettingsScript({
isSettingHidden: options => {
if (options.setting.name === 'my-markdown-area' && options.formValues.select === '2') {
return true
}
return false
}
})
}
export {
register
}
function onApplicationInit (peertubeHelpers) {
console.log('Hello application world')
const baseStaticUrl = peertubeHelpers.getBaseStaticRoute()
const imageUrl = baseStaticUrl + '/images/chocobo.png'
const topLeftBlock = document.querySelector('.top-left-block')
topLeftBlock.style.backgroundImage = 'url(' + imageUrl + ')'
peertubeHelpers.translate('User name')
.then(translation => console.log('Translated User name by ' + translation))
peertubeHelpers.getServerConfig()
.then(config => console.log('Got server config.', config))
}
function addSymbolToVideoNameResult (result, symbol) {
result.data.forEach(v => v.name += ' ' + symbol)
return {
data: result.data,
total: result.total
}
}
function register ({ registerHook, peertubeHelpers }) {
registerHook({
target: 'action:embed.player.loaded',
handler: () => alert('video loaded')
})
console.log(peertubeHelpers.translate('toto'))
}
export {
register
}
function register ({ registerHook, peertubeHelpers }) {
registerHook({
target: 'action:login.init',
handler: () => {
console.log('Login init')
document.querySelector('.looking-for-account > div').innerHTML = 'Hello'
}
})
}
export {
register
}
function register ({ registerHook, peertubeHelpers }) {
registerHook({
target: 'action:search.init',
handler: () => console.log('Search init')
})
registerHook({
target: 'filter:api.search.videos.list.result',
handler: result => {
result.data.forEach(v => v.name += ' SEARCH')
return {
total: result.total,
data: result.data
}
}
})
registerHook({
target: 'filter:api.search.video-channels.list.result',
handler: result => {
result.data.forEach(v => v.displayName += ' :p')
return {
total: result.total,
data: result.data
}
}
})
peertubeHelpers.showModal({
title: 'my super modal',
content: 'hello, do you like my modal?',
close: true,
cancel: {
value: 'Cancel',
action: () => console.log('Canceled')
},
confirm: {
value: 'Confirm',
action: () => console.log('Confirmed')
}
})
peertubeHelpers.markdownRenderer.textMarkdownToHTML('**My Bold Text**')
.then(v => console.log(v))
}
export {
register
}
function register ({ registerHook, registerVideoField }) {
console.log('loading video edit stuff')
{
const commonOptions1 = {
name: 'hello-world-field',
label: 'Super field',
type: 'input',
default: 'hello'
}
const commonOptions2 = {
name: 'hello-world-field-2',
label: 'Super field 2',
type: 'input',
hidden: ({ liveVideo, videoToUpdate, formValues }) => {
console.log('check hidden field', { videoToUpdate, liveVideo, formValues })
return formValues.pluginData['hello-world-field'] === 'toto'
}
}
for (const type of [ 'upload', 'import-url', 'update' ]) {
registerVideoField(commonOptions1, { type })
registerVideoField(commonOptions2, { type })
}
}
{
const hooks = [
'action:video-upload.init',
'action:video-url-import.init',
'action:video-torrent-import.init',
'action:go-live.init'
]
for (const h of hooks) {
registerHook({
target: h,
handler: () => {
const event = new Event('change', {
bubbles: true,
cancelable: true,
});
const selects = document.querySelectorAll('label[for=first-step-privacy] + my-select-options')
console.log(selects)
selects.forEach(s => {
s.value = 2 // Unlisted
s.dispatchEvent(event)
})
}
})
}
}
}
export {
register
}
function register ({ registerHook, peertubeHelpers }) {
registerHook({
target: 'action:video-watch.init',
handler: () => console.log('Hello video watch world')
})
registerHook({
target: 'action:video-watch.video.loaded',
handler: ({ videojs, video, playlist }) => {
if (playlist) {
console.log('playlist loaded')
} else {
console.log('video loaded')
}
// Insert element next to the player
{
const elem = document.createElement('div')
elem.className = 'hello-world-h4'
elem.innerHTML = '<h4>Hello everybody! This is an element next to the player</h4>'
elem.style = 'background-color: red; '
document.getElementById('plugin-placeholder-player-next').appendChild(elem)
}
}
})
registerHook({
target: 'filter:api.video-watch.video.get.result',
handler: video => {
video.name += ' \o/'
return video
}
})
registerHook({
target: 'filter:api.video-watch.video-threads.list.result',
handler: result => {
result.data.forEach(c => c.text += ' THREAD')
return result
}
})
registerHook({
target: 'filter:api.video-watch.video-thread-replies.list.result',
handler: result => {
result.children.forEach(c => c.comment.text += ' REPLY DEEP 1')
return result
}
})
registerHook({
target: 'filter:internal.video-watch.player.build-options.result',
handler: (result, params) => {
console.log('Running player build options hook for video %s.', params.video.name)
result.playerOptions.common.inactivityTimeout = 10000
return result
}
})
peertubeHelpers.notifier.info('you are on the watch page', 'useless', 1000)
}
export {
register
}
{
"settings.json_categories.label": "Categories to add/delete",
"settings.json_categories.description": "Paste the content of a Json file respecting the following format:<br />\n{ \"add\": [{ \"key\": 42, \"label\": \"The meaning of life\" }], \"delete\": [1] }"
}
{
"User name": "Nom d'utilisateur",
"Admin name": "Nom d'administrateur",
"two": "deux"
"settings.json_categories.label": "Categories a modifier",
"settings.json_categories.description": "Coller le contenu d'un fichier Json en respectant le format suivant : <br />\n{ \"add\": [{ \"key\": 42, \"label\": \"Le sens de la vie\" }], \"delete\": [1] }"
}
async function register ({
registerHook,
getRouter,
const { createOnSettingsChangeHandler } = require('./server/settings');
async function register({
registerSetting,
settingsManager,
storageManager,
videoCategoryManager,
videoLicenceManager,
videoLanguageManager,
registerExternalAuth,
peertubeHelpers
peertubeHelpers,
}) {
const defaultAdmin = 'PeerTube admin'
registerHook({
target: 'action:application.listening',
handler: () => displayHelloWorld(settingsManager, defaultAdmin)
})
registerHook({
target: 'unknown-hook',
handler: () => console.log('fake hook')
})
registerHook({
target: 'filter:api.user.signup.allowed.result',
handler: (result, params) => {
if (result.allowed === false) return result
if (params && params.body.email.includes('laposte.net')) {
return { allowed: false, errorMessage: 'laposte.net emails are not allowed on this instance' }
}
return result
}
})
registerHook({
target: 'filter:api.video.post-import-url.accept.result',
handler: ({ accepted }, { video }) => {
if (!accepted) return { accepted: false }
if (video.name.includes('small video - youtube')) return { accepted: false, errorMessage: 'bad word' }
return { accepted: true }
}
})
{
const fieldName = 'hello-world-field'
registerHook({
target: 'action:api.video.updated',
handler: ({ video, body }) => {
if (!body.pluginData) return
storageManager.storeData(fieldName + '-' + video.id, body.pluginData[fieldName])
}
})
registerHook({
target: 'filter:api.video.get.result',
handler: async (video) => {
if (!video) return video
if (!video.pluginData) video.pluginData = {}
const result = await storageManager.getData(fieldName + '-' + video.id)
video.pluginData[fieldName] = result
return video
}
})
}
const { logger } = peertubeHelpers;
registerSetting({
name: 'admin-name',
label: 'Admin name',
type: 'input',
private: true,
descriptionHTML: 'Your admin name',
default: defaultAdmin
})
registerSetting({
name: 'super-checkbox',
label: 'My super checkbox',
type: 'input-checkbox',
descriptionHTML: 'Use this super checkbox with caution'
})
registerSetting({
name: 'user-name',
label: 'User name',
type: 'input',
private: false
})
registerSetting({
name: 'my-markdown-area',
label: 'Markdown text',
type: 'markdown-text',
default: false
})
registerSetting({
type: 'html',
html: '<strong class="toto">coucou</strong><br /><br />'
})
registerSetting({
name: 'select',
label: 'Super select',
type: 'select',
default: '3',
options: [
{ value: '1', label: 'one' },
{ value: '2', label: 'two' },
{ value: '3', label: 'three' }
]
})
const value = await storageManager.getData('toto')
console.log(value)
await storageManager.storeData('toto', 'hello' + new Date())
await storageManager.storeData('toto2', { toto2: [ 'user 1', 'user 2' ] })
console.log(await storageManager.getData('toto2'))
console.log(await storageManager.getData('toto2.toto2'))
videoLanguageManager.addLanguage('al_bhed', 'Al Bhed')
videoLanguageManager.deleteLanguage('fr')
videoCategoryManager.addCategory(42, 'Best category')
videoCategoryManager.deleteCategory(1) // Music
videoLicenceManager.addLicence(42, 'Best licence')
videoLicenceManager.deleteLicence(7) // Public domain
settingsManager.onSettingsChange(settings => {
peertubeHelpers.logger.info('Settings changed!', { settings })
})
const router = getRouter()
router.get('/ping', (req, res) => res.json({ message: 'pong' }))
router.post('/form/post/mirror', (req, res) => {
res.json(req.body)
})
{
const result = registerExternalAuth({
authName: 'fake-auth',
authDisplayName: () => 'fake auth',
onAuthRequest: (req, res) => {
result.userAuthenticated({
req,
res,
username: 'fake_auth_username',
email: 'fake_auth_username@example.com'
})
}
name: 'json-categories-as-text',
label: 'settings.json_categories.label',
type: 'input-textarea',
default: JSON.stringify({ add: [], delete: [] }),
descriptionHTML: 'settings.json_categories.description',
});
settingsManager.onSettingsChange(
createOnSettingsChangeHandler({
videoCategoryManager,
logger,
})
}
);
}
async function unregister () {
return
async function unregister() {
return;
}