aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/fixtures/peertube-plugin-test-four/main.js
blob: edbb883e2c74920e237a61d3e45ed779fdceab25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
async function register ({
  peertubeHelpers,
  registerHook,
  getRouter
}) {
  const logger = peertubeHelpers.logger

  logger.info('Hello world from plugin four')

  {
    const username = 'root'
    const results = await peertubeHelpers.database.query(
      'SELECT "email" from "user" WHERE "username" = $username',
      {
        type: 'SELECT',
        bind: { username }
      }
    )

    logger.info('root email is ' + results[0]['email'])
  }

  {
    registerHook({
      target: 'action:api.video.viewed',
      handler: async ({ video }) => {
        const videoFromDB1 = await peertubeHelpers.videos.loadByUrl(video.url)
        const videoFromDB2 = await peertubeHelpers.videos.loadByIdOrUUID(video.id)
        const videoFromDB3 = await peertubeHelpers.videos.loadByIdOrUUID(video.uuid)

        if (videoFromDB1.uuid !== videoFromDB2.uuid || videoFromDB2.uuid !== videoFromDB3.uuid) return

        logger.info('video from DB uuid is %s.', videoFromDB1.uuid)

        await peertubeHelpers.videos.removeVideo(video.id)

        logger.info('Video deleted by plugin four.')
      }
    })
  }

  {
    const serverActor = await peertubeHelpers.server.getServerActor()
    logger.info('server actor name is %s', serverActor.preferredUsername)
  }

  {
    logger.info('server url is %s', peertubeHelpers.config.getWebserverUrl())
  }

  {
    const actions = {
      blockServer,
      unblockServer,
      blockAccount,
      unblockAccount,
      blacklist,
      unblacklist
    }

    const router = getRouter()
    router.post('/commander', async (req, res) => {
      try {
        await actions[req.body.command](peertubeHelpers, req.body)

        res.sendStatus(204)
      } catch (err) {
        logger.error('Error in commander.', { err })
        res.sendStatus(500)
      }
    })

    router.get('/server-config', async (req, res) => {
      const serverConfig = await peertubeHelpers.config.getServerConfig()

      return res.json({ serverConfig })
    })

    router.get('/static-route', async (req, res) => {
      const staticRoute = peertubeHelpers.plugin.getBaseStaticRoute()

      return res.json({ staticRoute })
    })

    router.get('/router-route', async (req, res) => {
      const routerRoute = peertubeHelpers.plugin.getBaseRouterRoute()

      return res.json({ routerRoute })
    })

    router.get('/user', async (req, res) => {
      const user = await peertubeHelpers.user.getAuthUser(res)
      if (!user) return res.sendStatus(404)

      const isAdmin = user.role === 0
      const isModerator = user.role === 1
      const isUser = user.role === 2

      return res.json({
        username: user.username,
        displayName: user.Account.name,
        isAdmin,
        isModerator,
        isUser
      })
    })

    router.get('/video-files/:id', async (req, res) => {
      const details = await peertubeHelpers.videos.getFiles(req.params.id)
      if (!details) return res.sendStatus(404)

      return res.json(details)
    })
  }

}

async function unregister () {
  return
}

module.exports = {
  register,
  unregister
}

// ###########################################################################

async function blockServer (peertubeHelpers, body) {
  const serverActor = await peertubeHelpers.server.getServerActor()

  await peertubeHelpers.moderation.blockServer({ byAccountId: serverActor.Account.id, hostToBlock: body.hostToBlock })
}

async function unblockServer (peertubeHelpers, body) {
  const serverActor = await peertubeHelpers.server.getServerActor()

  await peertubeHelpers.moderation.unblockServer({ byAccountId: serverActor.Account.id, hostToUnblock: body.hostToUnblock })
}

async function blockAccount (peertubeHelpers, body) {
  const serverActor = await peertubeHelpers.server.getServerActor()

  await peertubeHelpers.moderation.blockAccount({ byAccountId: serverActor.Account.id, handleToBlock: body.handleToBlock })
}

async function unblockAccount (peertubeHelpers, body) {
  const serverActor = await peertubeHelpers.server.getServerActor()

  await peertubeHelpers.moderation.unblockAccount({ byAccountId: serverActor.Account.id, handleToUnblock: body.handleToUnblock })
}

async function blacklist (peertubeHelpers, body) {
  await peertubeHelpers.moderation.blacklistVideo({
    videoIdOrUUID: body.videoUUID,
    createOptions: body
  })
}

async function unblacklist (peertubeHelpers, body) {
  await peertubeHelpers.moderation.unblacklistVideo({ videoIdOrUUID: body.videoUUID })
}