]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/fixtures/peertube-plugin-test/main.js
7c53f6afe49dcafb78804525d376c1e9d6d6f55a
[github/Chocobozzz/PeerTube.git] / server / tests / fixtures / peertube-plugin-test / main.js
1 async function register ({ registerHook, registerSetting, settingsManager, storageManager, peertubeHelpers }) {
2 const actionHooks = [
3 'action:application.listening',
4
5 'action:api.video.updated',
6 'action:api.video.deleted',
7 'action:api.video.uploaded',
8 'action:api.video.viewed',
9
10 'action:api.video-thread.created',
11 'action:api.video-comment-reply.created',
12 'action:api.video-comment.deleted'
13 ]
14
15 for (const h of actionHooks) {
16 registerHook({
17 target: h,
18 handler: () => peertubeHelpers.logger.debug('Run hook %s.', h)
19 })
20 }
21
22 registerHook({
23 target: 'filter:api.videos.list.params',
24 handler: obj => addToCount(obj)
25 })
26
27 registerHook({
28 target: 'filter:api.videos.list.result',
29 handler: obj => addToTotal(obj)
30 })
31
32 registerHook({
33 target: 'filter:api.video.get.result',
34 handler: video => {
35 video.name += ' <3'
36
37 return video
38 }
39 })
40
41 registerHook({
42 target: 'filter:api.video.upload.accept.result',
43 handler: ({ accepted }, { videoBody }) => {
44 if (!accepted) return { accepted: false }
45 if (videoBody.name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
46
47 return { accepted: true }
48 }
49 })
50
51 registerHook({
52 target: 'filter:api.video-thread.create.accept.result',
53 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
54 })
55
56 registerHook({
57 target: 'filter:api.video-comment-reply.create.accept.result',
58 handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
59 })
60
61 registerHook({
62 target: 'filter:api.video-threads.list.params',
63 handler: obj => addToCount(obj)
64 })
65
66 registerHook({
67 target: 'filter:api.video-threads.list.result',
68 handler: obj => addToTotal(obj)
69 })
70
71 registerHook({
72 target: 'filter:api.video-thread-comments.list.result',
73 handler: obj => {
74 obj.data.forEach(c => c.text += ' <3')
75
76 return obj
77 }
78 })
79
80 registerHook({
81 target: 'filter:video.auto-blacklist.result',
82 handler: (blacklisted, { video }) => {
83 if (blacklisted) return true
84 if (video.name.includes('please blacklist me')) return true
85
86 return false
87 }
88 })
89 }
90
91 async function unregister () {
92 return
93 }
94
95 module.exports = {
96 register,
97 unregister
98 }
99
100 // ############################################################################
101
102 function addToCount (obj) {
103 return Object.assign({}, obj, { count: obj.count + 1 })
104 }
105
106 function addToTotal (result) {
107 return {
108 data: result.data,
109 total: result.total + 1
110 }
111 }
112
113 function checkCommentBadWord (accepted, commentBody) {
114 if (!accepted) return { accepted: false }
115 if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
116
117 return { accepted: true }
118 }