ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

527 lines
14 KiB

  1. async function register ({ registerHook, registerSetting, settingsManager, storageManager, peertubeHelpers }) {
  2. {
  3. registerSetting({
  4. name: 'unique-setting',
  5. label: 'Unique setting',
  6. type: 'select',
  7. options: []
  8. })
  9. registerSetting({
  10. name: 'unique-setting',
  11. label: 'Unique setting',
  12. type: 'select',
  13. options: [
  14. {
  15. value: 1,
  16. label: 'One'
  17. }
  18. ]
  19. })
  20. registerSetting({
  21. label: 'Unnamed 1',
  22. type: 'input'
  23. })
  24. registerSetting({
  25. label: 'Unnamed 2',
  26. type: 'input'
  27. })
  28. const actionHooks = [
  29. 'action:application.listening',
  30. 'action:notifier.notification.created',
  31. 'action:api.video.updated',
  32. 'action:api.video.deleted',
  33. 'action:api.video.uploaded',
  34. 'action:api.video.viewed',
  35. 'action:api.video.file-updated',
  36. 'action:api.video-channel.created',
  37. 'action:api.video-channel.updated',
  38. 'action:api.video-channel.deleted',
  39. 'action:api.live-video.created',
  40. 'action:live.video.state.updated',
  41. 'action:api.video-thread.created',
  42. 'action:api.video-comment-reply.created',
  43. 'action:api.video-comment.deleted',
  44. 'action:api.video-caption.created',
  45. 'action:api.video-caption.deleted',
  46. 'action:api.user.blocked',
  47. 'action:api.user.unblocked',
  48. 'action:api.user.registered',
  49. 'action:api.user.created',
  50. 'action:api.user.deleted',
  51. 'action:api.user.updated',
  52. 'action:api.user.oauth2-got-token',
  53. 'action:api.video-playlist-element.created'
  54. ]
  55. for (const h of actionHooks) {
  56. registerHook({
  57. target: h,
  58. handler: () => peertubeHelpers.logger.debug('Run hook %s.', h)
  59. })
  60. }
  61. for (const h of [ 'action:activity-pub.remote-video.created', 'action:activity-pub.remote-video.updated' ]) {
  62. registerHook({
  63. target: h,
  64. handler: ({ video, videoAPObject }) => {
  65. peertubeHelpers.logger.debug('Run hook %s - AP %s - video %s.', h, video.name, videoAPObject.name )
  66. }
  67. })
  68. }
  69. }
  70. registerHook({
  71. target: 'filter:api.videos.list.params',
  72. handler: obj => addToCount(obj)
  73. })
  74. registerHook({
  75. target: 'filter:api.videos.list.result',
  76. handler: obj => addToTotal(obj)
  77. })
  78. registerHook({
  79. target: 'filter:api.video-playlist.videos.list.params',
  80. handler: obj => addToCount(obj)
  81. })
  82. registerHook({
  83. target: 'filter:api.video-playlist.videos.list.result',
  84. handler: obj => addToTotal(obj)
  85. })
  86. registerHook({
  87. target: 'filter:api.accounts.videos.list.params',
  88. handler: obj => addToCount(obj)
  89. })
  90. registerHook({
  91. target: 'filter:api.accounts.videos.list.result',
  92. handler: obj => addToTotal(obj, 2)
  93. })
  94. registerHook({
  95. target: 'filter:api.video-channels.videos.list.params',
  96. handler: obj => addToCount(obj, 3)
  97. })
  98. registerHook({
  99. target: 'filter:api.video-channels.videos.list.result',
  100. handler: obj => addToTotal(obj, 3)
  101. })
  102. registerHook({
  103. target: 'filter:api.user.me.videos.list.params',
  104. handler: obj => addToCount(obj, 4)
  105. })
  106. registerHook({
  107. target: 'filter:api.user.me.videos.list.result',
  108. handler: obj => addToTotal(obj, 4)
  109. })
  110. registerHook({
  111. target: 'filter:api.user.me.get.result',
  112. handler: (result) => {
  113. result.customParam = 'Customized'
  114. return result
  115. }
  116. })
  117. registerHook({
  118. target: 'filter:api.user.me.subscription-videos.list.params',
  119. handler: obj => addToCount(obj)
  120. })
  121. registerHook({
  122. target: 'filter:api.user.me.subscription-videos.list.result',
  123. handler: obj => addToTotal(obj, 4)
  124. })
  125. registerHook({
  126. target: 'filter:api.video.get.result',
  127. handler: video => {
  128. video.name += ' <3'
  129. return video
  130. }
  131. })
  132. // ---------------------------------------------------------------------------
  133. registerHook({
  134. target: 'filter:api.video-channels.list.params',
  135. handler: obj => addToCount(obj, 1)
  136. })
  137. registerHook({
  138. target: 'filter:api.video-channels.list.result',
  139. handler: obj => addToTotal(obj, 1)
  140. })
  141. registerHook({
  142. target: 'filter:api.video-channel.get.result',
  143. handler: channel => {
  144. channel.name += ' <3'
  145. return channel
  146. }
  147. })
  148. // ---------------------------------------------------------------------------
  149. for (const hook of [ 'filter:api.video.upload.accept.result', 'filter:api.live-video.create.accept.result' ]) {
  150. registerHook({
  151. target: hook,
  152. handler: ({ accepted }, { videoBody, liveVideoBody }) => {
  153. if (!accepted) return { accepted: false }
  154. const name = videoBody
  155. ? videoBody.name
  156. : liveVideoBody.name
  157. if (name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word' }
  158. return { accepted: true }
  159. }
  160. })
  161. }
  162. registerHook({
  163. target: 'filter:api.video.update-file.accept.result',
  164. handler: ({ accepted }, { videoFile }) => {
  165. if (!accepted) return { accepted: false }
  166. if (videoFile.filename.includes('webm')) return { accepted: false, errorMessage: 'no webm' }
  167. return { accepted: true }
  168. }
  169. })
  170. registerHook({
  171. target: 'filter:api.video.pre-import-url.accept.result',
  172. handler: ({ accepted }, { videoImportBody }) => {
  173. if (!accepted) return { accepted: false }
  174. if (videoImportBody.targetUrl.includes('bad')) return { accepted: false, errorMessage: 'bad target url' }
  175. return { accepted: true }
  176. }
  177. })
  178. registerHook({
  179. target: 'filter:api.video.pre-import-torrent.accept.result',
  180. handler: ({ accepted }, { videoImportBody }) => {
  181. if (!accepted) return { accepted: false }
  182. if (videoImportBody.name.includes('bad torrent')) return { accepted: false, errorMessage: 'bad torrent' }
  183. return { accepted: true }
  184. }
  185. })
  186. registerHook({
  187. target: 'filter:api.video.post-import-url.accept.result',
  188. handler: ({ accepted }, { video }) => {
  189. if (!accepted) return { accepted: false }
  190. if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
  191. return { accepted: true }
  192. }
  193. })
  194. registerHook({
  195. target: 'filter:api.video.post-import-torrent.accept.result',
  196. handler: ({ accepted }, { video }) => {
  197. if (!accepted) return { accepted: false }
  198. if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
  199. return { accepted: true }
  200. }
  201. })
  202. registerHook({
  203. target: 'filter:api.video.user-import.accept.result',
  204. handler: ({ accepted }, { videoBody }) => {
  205. if (!accepted) return { accepted: false }
  206. if (videoBody.name === 'video 1') return { accepted: false, errorMessage: 'bad word' }
  207. return { accepted: true }
  208. }
  209. })
  210. // ---------------------------------------------------------------------------
  211. registerHook({
  212. target: 'filter:api.video-thread.create.accept.result',
  213. handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
  214. })
  215. registerHook({
  216. target: 'filter:api.video-comment-reply.create.accept.result',
  217. handler: ({ accepted }, { commentBody }) => checkCommentBadWord(accepted, commentBody)
  218. })
  219. registerHook({
  220. target: 'filter:activity-pub.remote-video-comment.create.accept.result',
  221. handler: ({ accepted }, { comment }) => checkCommentBadWord(accepted, comment)
  222. })
  223. // ---------------------------------------------------------------------------
  224. registerHook({
  225. target: 'filter:activity-pub.activity.context.build.result',
  226. handler: context => context.concat([ { recordedAt: 'https://schema.org/recordedAt' } ])
  227. })
  228. registerHook({
  229. target: 'filter:activity-pub.video.json-ld.build.result',
  230. handler: (jsonld, { video }) => ({ ...jsonld, videoName: video.name })
  231. })
  232. // ---------------------------------------------------------------------------
  233. registerHook({
  234. target: 'filter:api.video-threads.list.params',
  235. handler: obj => addToCount(obj)
  236. })
  237. registerHook({
  238. target: 'filter:api.video-threads.list.result',
  239. handler: obj => addToTotal(obj)
  240. })
  241. registerHook({
  242. target: 'filter:api.video-thread-comments.list.result',
  243. handler: obj => {
  244. obj.data.forEach(c => c.text += ' <3')
  245. return obj
  246. }
  247. })
  248. registerHook({
  249. target: 'filter:video.auto-blacklist.result',
  250. handler: (blacklisted, { video }) => {
  251. if (blacklisted) return true
  252. if (video.name.includes('please blacklist me')) return true
  253. return false
  254. }
  255. })
  256. {
  257. registerHook({
  258. target: 'filter:api.user.signup.allowed.result',
  259. handler: (result, params) => {
  260. if (params && params.body && params.body.email && params.body.email.includes('jma 1')) {
  261. return { allowed: false, errorMessage: 'No jma 1' }
  262. }
  263. return result
  264. }
  265. })
  266. registerHook({
  267. target: 'filter:api.user.request-signup.allowed.result',
  268. handler: (result, params) => {
  269. if (params && params.body && params.body.email && params.body.email.includes('jma 2')) {
  270. return { allowed: false, errorMessage: 'No jma 2' }
  271. }
  272. return result
  273. }
  274. })
  275. }
  276. registerHook({
  277. target: 'filter:api.download.torrent.allowed.result',
  278. handler: (result, params) => {
  279. if (params && params.downloadName.includes('bad torrent')) {
  280. return { allowed: false, errorMessage: 'Liu Bei' }
  281. }
  282. return result
  283. }
  284. })
  285. registerHook({
  286. target: 'filter:api.download.video.allowed.result',
  287. handler: async (result, params) => {
  288. const loggedInUser = await peertubeHelpers.user.getAuthUser(params.res)
  289. if (loggedInUser) return { allowed: true }
  290. if (params && !params.streamingPlaylist && params.video.name.includes('bad file')) {
  291. return { allowed: false, errorMessage: 'Cao Cao' }
  292. }
  293. if (params && params.streamingPlaylist && params.video.name.includes('bad playlist file')) {
  294. return { allowed: false, errorMessage: 'Sun Jian' }
  295. }
  296. return result
  297. }
  298. })
  299. // ---------------------------------------------------------------------------
  300. registerHook({
  301. target: 'filter:html.embed.video.allowed.result',
  302. handler: (result, params) => {
  303. return {
  304. allowed: false,
  305. html: 'Lu Bu'
  306. }
  307. }
  308. })
  309. registerHook({
  310. target: 'filter:html.embed.video-playlist.allowed.result',
  311. handler: (result, params) => {
  312. return {
  313. allowed: false,
  314. html: 'Diao Chan'
  315. }
  316. }
  317. })
  318. // ---------------------------------------------------------------------------
  319. registerHook({
  320. target: 'filter:html.client.json-ld.result',
  321. handler: (jsonld, context) => {
  322. if (!context || !context.video) return jsonld
  323. return Object.assign(jsonld, { recordedAt: 'http://example.com/recordedAt' })
  324. }
  325. })
  326. // ---------------------------------------------------------------------------
  327. registerHook({
  328. target: 'filter:api.server.stats.get.result',
  329. handler: (result) => {
  330. return { ...result, customStats: 14 }
  331. }
  332. })
  333. registerHook({
  334. target: 'filter:job-queue.process.params',
  335. handler: (object, context) => {
  336. if (context.type !== 'video-studio-edition') return object
  337. object.data.tasks = [
  338. {
  339. name: 'cut',
  340. options: {
  341. start: 0,
  342. end: 1
  343. }
  344. }
  345. ]
  346. return object
  347. }
  348. })
  349. registerHook({
  350. target: 'filter:transcoding.auto.resolutions-to-transcode.result',
  351. handler: (object, context) => {
  352. if (context.video.name.includes('transcode-filter')) {
  353. object = [ 100 ]
  354. }
  355. return object
  356. }
  357. })
  358. // Upload/import/live attributes
  359. for (const target of [
  360. 'filter:api.video.upload.video-attribute.result',
  361. 'filter:api.video.import-url.video-attribute.result',
  362. 'filter:api.video.import-torrent.video-attribute.result',
  363. 'filter:api.video.live.video-attribute.result',
  364. 'filter:api.video.user-import.video-attribute.result'
  365. ]) {
  366. registerHook({
  367. target,
  368. handler: (result) => {
  369. return { ...result, description: result.description + ' - ' + target }
  370. }
  371. })
  372. }
  373. {
  374. const filterHooks = [
  375. 'filter:api.search.videos.local.list.params',
  376. 'filter:api.search.videos.local.list.result',
  377. 'filter:api.search.videos.index.list.params',
  378. 'filter:api.search.videos.index.list.result',
  379. 'filter:api.search.video-channels.local.list.params',
  380. 'filter:api.search.video-channels.local.list.result',
  381. 'filter:api.search.video-channels.index.list.params',
  382. 'filter:api.search.video-channels.index.list.result',
  383. 'filter:api.search.video-playlists.local.list.params',
  384. 'filter:api.search.video-playlists.local.list.result',
  385. 'filter:api.search.video-playlists.index.list.params',
  386. 'filter:api.search.video-playlists.index.list.result',
  387. 'filter:api.overviews.videos.list.params',
  388. 'filter:api.overviews.videos.list.result',
  389. 'filter:job-queue.process.params',
  390. 'filter:job-queue.process.result'
  391. ]
  392. for (const h of filterHooks) {
  393. registerHook({
  394. target: h,
  395. handler: (obj) => {
  396. peertubeHelpers.logger.debug('Run hook %s.', h)
  397. return obj
  398. }
  399. })
  400. }
  401. }
  402. }
  403. async function unregister () {
  404. return
  405. }
  406. module.exports = {
  407. register,
  408. unregister
  409. }
  410. // ############################################################################
  411. function addToCount (obj, amount = 1) {
  412. return Object.assign({}, obj, { count: obj.count + amount })
  413. }
  414. function addToTotal (result, amount = 1) {
  415. return {
  416. data: result.data,
  417. total: result.total + amount
  418. }
  419. }
  420. function checkCommentBadWord (accepted, commentBody) {
  421. if (!accepted) return { accepted: false }
  422. if (commentBody.text.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
  423. return { accepted: true }
  424. }