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

159 lines
3.8 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
{
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
}
})
}
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,
2019-07-16 09:53:38 +00:00
default: defaultAdmin
})
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
})
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)
}