peertube-theme-nctv-dark/peertube-plugin-hello-world/main.js

185 lines
4.4 KiB
JavaScript
Raw Normal View History

2019-07-26 12:45:53 +00:00
async function register ({
registerHook,
2020-04-10 12:42:34 +00:00
getRouter,
2019-07-26 12:45:53 +00:00
registerSetting,
settingsManager,
storageManager,
videoCategoryManager,
videoLicenceManager,
2020-04-28 12:58:40 +00:00
videoLanguageManager,
registerExternalAuth,
peertubeHelpers
2019-07-26 12:45:53 +00:00
}) {
2019-07-16 09:53:38 +00:00
const defaultAdmin = 'PeerTube admin'
registerHook({
target: 'action:application.listening',
handler: () => displayHelloWorld(settingsManager, defaultAdmin)
})
2019-07-22 14:40:52 +00:00
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 }
}
})
2020-08-21 12:45:18 +00:00
{
2021-06-30 14:44:42 +00:00
for (const fieldName of [ 'hello-world-field', 'hello-world-field-2', 'hello-world-field-3' ]) {
registerHook({
target: 'action:api.video.updated',
handler: ({ video, body }) => {
if (!body.pluginData) return
2020-08-21 12:45:18 +00:00
2021-06-30 14:44:42 +00:00
storageManager.storeData(fieldName + '-' + video.id, body.pluginData[fieldName])
}
})
2020-08-21 12:45:18 +00:00
2021-06-30 14:44:42 +00:00
registerHook({
target: 'filter:api.video.get.result',
handler: async (video) => {
if (!video) return video
2020-08-21 12:45:18 +00:00
2021-06-30 14:44:42 +00:00
if (!video.pluginData) video.pluginData = {}
2020-08-21 12:45:18 +00:00
2021-06-30 14:44:42 +00:00
const result = await storageManager.getData(fieldName + '-' + video.id)
video.pluginData[fieldName] = result
2020-08-21 12:45:18 +00:00
2021-06-30 14:44:42 +00:00
return video
}
})
}
2020-08-21 12:45:18 +00:00
}
2019-07-16 09:53:38 +00:00
registerSetting({
name: 'admin-name',
label: 'Admin name',
type: 'input',
2019-07-26 07:42:52 +00:00
private: true,
2021-04-09 13:19:32 +00:00
descriptionHTML: 'Your admin name',
2019-07-16 09:53:38 +00:00
default: defaultAdmin
})
2021-04-09 13:19:32 +00:00
registerSetting({
name: 'super-checkbox',
label: 'My super checkbox',
type: 'input-checkbox',
descriptionHTML: 'Use this super checkbox with caution'
})
2019-07-26 07:42:52 +00:00
registerSetting({
name: 'user-name',
label: 'User name',
type: 'input',
private: false
})
2020-04-24 12:25:43 +00:00
registerSetting({
name: 'my-markdown-area',
label: 'Markdown text',
type: 'markdown-text',
default: false
})
2021-04-09 13:19:32 +00:00
registerSetting({
type: 'html',
2021-04-22 09:35:02 +00:00
html: '<strong class="toto">coucou</strong><br /><br />'
2021-04-09 13:19:32 +00:00
})
registerSetting({
name: 'select',
label: 'Super select',
type: 'select',
default: '3',
options: [
{ value: '1', label: 'one' },
{ value: '2', label: 'two' },
{ value: '3', label: 'three' }
]
})
2019-07-16 09:53:38 +00:00
const value = await storageManager.getData('toto')
console.log(value)
await storageManager.storeData('toto', 'hello' + new Date())
2020-04-10 12:42:34 +00:00
await storageManager.storeData('toto2', { toto2: [ 'user 1', 'user 2' ] })
console.log(await storageManager.getData('toto2'))
console.log(await storageManager.getData('toto2.toto2'))
2019-07-26 12:45:53 +00:00
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
2020-04-10 12:42:34 +00:00
settingsManager.onSettingsChange(settings => {
peertubeHelpers.logger.info('Settings changed!', { settings })
})
2020-04-10 12:42:34 +00:00
const router = getRouter()
router.get('/ping', (req, res) => res.json({ message: 'pong' }))
router.post('/form/post/mirror', (req, res) => {
res.json(req.body)
})
2020-04-28 12:58:40 +00:00
{
const result = registerExternalAuth({
authName: 'fake-auth',
2020-04-30 08:14:51 +00:00
authDisplayName: () => 'fake auth',
2020-04-28 12:58:40 +00:00
onAuthRequest: (req, res) => {
result.userAuthenticated({
req,
res,
username: 'fake_auth_username',
email: 'fake_auth_username@example.com'
})
}
})
}
2019-07-16 09:53:38 +00:00
}
async function unregister () {
return
}
module.exports = {
register,
unregister
}
// ############################################################################
async function displayHelloWorld (settingsManager, defaultAdmin) {
let value = await settingsManager.getSetting('admin-name')
if (!value) value = defaultAdmin
console.log('hello world ' + value)
}