aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-12-26 10:36:24 +0100
committerChocobozzz <chocobozzz@cpy.re>2019-01-09 11:15:15 +0100
commitcef534ed53e4518fe0acf581bfe880788d42fc36 (patch)
tree115b51ea5136849a2336d44915c7780649f25dc2 /server/controllers
parent1de1d05f4c61fe059fa5e24e79c92582f0e7e4b3 (diff)
downloadPeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.tar.gz
PeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.tar.zst
PeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.zip
Add user notification base code
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/api/users/index.ts2
-rw-r--r--server/controllers/api/users/my-notifications.ts84
-rw-r--r--server/controllers/api/videos/abuse.ts3
-rw-r--r--server/controllers/api/videos/blacklist.ts14
-rw-r--r--server/controllers/api/videos/comment.ts3
-rw-r--r--server/controllers/api/videos/index.ts10
-rw-r--r--server/controllers/feeds.ts2
-rw-r--r--server/controllers/tracker.ts4
8 files changed, 116 insertions, 6 deletions
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts
index bc24792a2..98be46ea2 100644
--- a/server/controllers/api/users/index.ts
+++ b/server/controllers/api/users/index.ts
@@ -39,6 +39,7 @@ import { meRouter } from './me'
39import { deleteUserToken } from '../../../lib/oauth-model' 39import { deleteUserToken } from '../../../lib/oauth-model'
40import { myBlocklistRouter } from './my-blocklist' 40import { myBlocklistRouter } from './my-blocklist'
41import { myVideosHistoryRouter } from './my-history' 41import { myVideosHistoryRouter } from './my-history'
42import { myNotificationsRouter } from './my-notifications'
42 43
43const auditLogger = auditLoggerFactory('users') 44const auditLogger = auditLoggerFactory('users')
44 45
@@ -55,6 +56,7 @@ const askSendEmailLimiter = new RateLimit({
55}) 56})
56 57
57const usersRouter = express.Router() 58const usersRouter = express.Router()
59usersRouter.use('/', myNotificationsRouter)
58usersRouter.use('/', myBlocklistRouter) 60usersRouter.use('/', myBlocklistRouter)
59usersRouter.use('/', myVideosHistoryRouter) 61usersRouter.use('/', myVideosHistoryRouter)
60usersRouter.use('/', meRouter) 62usersRouter.use('/', meRouter)
diff --git a/server/controllers/api/users/my-notifications.ts b/server/controllers/api/users/my-notifications.ts
new file mode 100644
index 000000000..cef1d237c
--- /dev/null
+++ b/server/controllers/api/users/my-notifications.ts
@@ -0,0 +1,84 @@
1import * as express from 'express'
2import 'multer'
3import {
4 asyncMiddleware,
5 asyncRetryTransactionMiddleware,
6 authenticate,
7 paginationValidator,
8 setDefaultPagination,
9 setDefaultSort,
10 userNotificationsSortValidator
11} from '../../../middlewares'
12import { UserModel } from '../../../models/account/user'
13import { getFormattedObjects } from '../../../helpers/utils'
14import { UserNotificationModel } from '../../../models/account/user-notification'
15import { meRouter } from './me'
16import {
17 markAsReadUserNotificationsValidator,
18 updateNotificationSettingsValidator
19} from '../../../middlewares/validators/user-notifications'
20import { UserNotificationSetting } from '../../../../shared/models/users'
21import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting'
22
23const myNotificationsRouter = express.Router()
24
25meRouter.put('/me/notification-settings',
26 authenticate,
27 updateNotificationSettingsValidator,
28 asyncRetryTransactionMiddleware(updateNotificationSettings)
29)
30
31myNotificationsRouter.get('/me/notifications',
32 authenticate,
33 paginationValidator,
34 userNotificationsSortValidator,
35 setDefaultSort,
36 setDefaultPagination,
37 asyncMiddleware(listUserNotifications)
38)
39
40myNotificationsRouter.post('/me/notifications/read',
41 authenticate,
42 markAsReadUserNotificationsValidator,
43 asyncMiddleware(markAsReadUserNotifications)
44)
45
46export {
47 myNotificationsRouter
48}
49
50// ---------------------------------------------------------------------------
51
52async function updateNotificationSettings (req: express.Request, res: express.Response) {
53 const user: UserModel = res.locals.oauth.token.User
54 const body: UserNotificationSetting = req.body
55
56 const query = {
57 where: {
58 userId: user.id
59 }
60 }
61
62 await UserNotificationSettingModel.update({
63 newVideoFromSubscription: body.newVideoFromSubscription,
64 newCommentOnMyVideo: body.newCommentOnMyVideo
65 }, query)
66
67 return res.status(204).end()
68}
69
70async function listUserNotifications (req: express.Request, res: express.Response) {
71 const user: UserModel = res.locals.oauth.token.User
72
73 const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort)
74
75 return res.json(getFormattedObjects(resultList.data, resultList.total))
76}
77
78async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
79 const user: UserModel = res.locals.oauth.token.User
80
81 await UserNotificationModel.markAsRead(user.id, req.body.ids)
82
83 return res.status(204).end()
84}
diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts
index d0c81804b..fe0a95cd5 100644
--- a/server/controllers/api/videos/abuse.ts
+++ b/server/controllers/api/videos/abuse.ts
@@ -22,6 +22,7 @@ import { VideoModel } from '../../../models/video/video'
22import { VideoAbuseModel } from '../../../models/video/video-abuse' 22import { VideoAbuseModel } from '../../../models/video/video-abuse'
23import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger' 23import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger'
24import { UserModel } from '../../../models/account/user' 24import { UserModel } from '../../../models/account/user'
25import { Notifier } from '../../../lib/notifier'
25 26
26const auditLogger = auditLoggerFactory('abuse') 27const auditLogger = auditLoggerFactory('abuse')
27const abuseVideoRouter = express.Router() 28const abuseVideoRouter = express.Router()
@@ -117,6 +118,8 @@ async function reportVideoAbuse (req: express.Request, res: express.Response) {
117 await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance) 118 await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance)
118 } 119 }
119 120
121 Notifier.Instance.notifyOnNewVideoAbuse(videoAbuseInstance)
122
120 auditLogger.create(reporterAccount.Actor.getIdentifier(), new VideoAbuseAuditView(videoAbuseInstance.toFormattedJSON())) 123 auditLogger.create(reporterAccount.Actor.getIdentifier(), new VideoAbuseAuditView(videoAbuseInstance.toFormattedJSON()))
121 124
122 return videoAbuseInstance 125 return videoAbuseInstance
diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts
index 7f803c8e9..9ef08812b 100644
--- a/server/controllers/api/videos/blacklist.ts
+++ b/server/controllers/api/videos/blacklist.ts
@@ -16,6 +16,8 @@ import {
16} from '../../../middlewares' 16} from '../../../middlewares'
17import { VideoBlacklistModel } from '../../../models/video/video-blacklist' 17import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
18import { sequelizeTypescript } from '../../../initializers' 18import { sequelizeTypescript } from '../../../initializers'
19import { Notifier } from '../../../lib/notifier'
20import { VideoModel } from '../../../models/video/video'
19 21
20const blacklistRouter = express.Router() 22const blacklistRouter = express.Router()
21 23
@@ -67,13 +69,18 @@ async function addVideoToBlacklist (req: express.Request, res: express.Response)
67 reason: body.reason 69 reason: body.reason
68 } 70 }
69 71
70 await VideoBlacklistModel.create(toCreate) 72 const blacklist = await VideoBlacklistModel.create(toCreate)
73 blacklist.Video = videoInstance
74
75 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
76
77 logger.info('Video %s blacklisted.', res.locals.video.uuid)
78
71 return res.type('json').status(204).end() 79 return res.type('json').status(204).end()
72} 80}
73 81
74async function updateVideoBlacklistController (req: express.Request, res: express.Response) { 82async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
75 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel 83 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
76 logger.info(videoBlacklist)
77 84
78 if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason 85 if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
79 86
@@ -92,11 +99,14 @@ async function listBlacklist (req: express.Request, res: express.Response, next:
92 99
93async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { 100async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
94 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel 101 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
102 const video: VideoModel = res.locals.video
95 103
96 await sequelizeTypescript.transaction(t => { 104 await sequelizeTypescript.transaction(t => {
97 return videoBlacklist.destroy({ transaction: t }) 105 return videoBlacklist.destroy({ transaction: t })
98 }) 106 })
99 107
108 Notifier.Instance.notifyOnVideoUnblacklist(video)
109
100 logger.info('Video %s removed from blacklist.', res.locals.video.uuid) 110 logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
101 111
102 return res.type('json').status(204).end() 112 return res.type('json').status(204).end()
diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts
index 3875c8f79..70c1148ba 100644
--- a/server/controllers/api/videos/comment.ts
+++ b/server/controllers/api/videos/comment.ts
@@ -26,6 +26,7 @@ import { VideoCommentModel } from '../../../models/video/video-comment'
26import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' 26import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
27import { AccountModel } from '../../../models/account/account' 27import { AccountModel } from '../../../models/account/account'
28import { UserModel } from '../../../models/account/user' 28import { UserModel } from '../../../models/account/user'
29import { Notifier } from '../../../lib/notifier'
29 30
30const auditLogger = auditLoggerFactory('comments') 31const auditLogger = auditLoggerFactory('comments')
31const videoCommentRouter = express.Router() 32const videoCommentRouter = express.Router()
@@ -119,6 +120,7 @@ async function addVideoCommentThread (req: express.Request, res: express.Respons
119 }, t) 120 }, t)
120 }) 121 })
121 122
123 Notifier.Instance.notifyOnNewComment(comment)
122 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) 124 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
123 125
124 return res.json({ 126 return res.json({
@@ -140,6 +142,7 @@ async function addVideoCommentReply (req: express.Request, res: express.Response
140 }, t) 142 }, t)
141 }) 143 })
142 144
145 Notifier.Instance.notifyOnNewComment(comment)
143 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON())) 146 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
144 147
145 return res.json({ comment: comment.toFormattedJSON() }).end() 148 return res.json({ comment: comment.toFormattedJSON() }).end()
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 00a1302d1..94ed08fed 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -7,7 +7,8 @@ import { logger } from '../../../helpers/logger'
7import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' 7import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
8import { getFormattedObjects, getServerActor } from '../../../helpers/utils' 8import { getFormattedObjects, getServerActor } from '../../../helpers/utils'
9import { 9import {
10 CONFIG, MIMETYPES, 10 CONFIG,
11 MIMETYPES,
11 PREVIEWS_SIZE, 12 PREVIEWS_SIZE,
12 sequelizeTypescript, 13 sequelizeTypescript,
13 THUMBNAILS_SIZE, 14 THUMBNAILS_SIZE,
@@ -57,6 +58,7 @@ import { videoImportsRouter } from './import'
57import { resetSequelizeInstance } from '../../../helpers/database-utils' 58import { resetSequelizeInstance } from '../../../helpers/database-utils'
58import { move } from 'fs-extra' 59import { move } from 'fs-extra'
59import { watchingRouter } from './watching' 60import { watchingRouter } from './watching'
61import { Notifier } from '../../../lib/notifier'
60 62
61const auditLogger = auditLoggerFactory('videos') 63const auditLogger = auditLoggerFactory('videos')
62const videosRouter = express.Router() 64const videosRouter = express.Router()
@@ -262,6 +264,7 @@ async function addVideo (req: express.Request, res: express.Response) {
262 } 264 }
263 265
264 await federateVideoIfNeeded(video, true, t) 266 await federateVideoIfNeeded(video, true, t)
267 Notifier.Instance.notifyOnNewVideo(video)
265 268
266 auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) 269 auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON()))
267 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) 270 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
@@ -293,6 +296,7 @@ async function updateVideo (req: express.Request, res: express.Response) {
293 const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON()) 296 const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON())
294 const videoInfoToUpdate: VideoUpdate = req.body 297 const videoInfoToUpdate: VideoUpdate = req.body
295 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE 298 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
299 const wasUnlistedVideo = videoInstance.privacy === VideoPrivacy.UNLISTED
296 300
297 // Process thumbnail or create it from the video 301 // Process thumbnail or create it from the video
298 if (req.files && req.files['thumbnailfile']) { 302 if (req.files && req.files['thumbnailfile']) {
@@ -363,6 +367,10 @@ async function updateVideo (req: express.Request, res: express.Response) {
363 const isNewVideo = wasPrivateVideo && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE 367 const isNewVideo = wasPrivateVideo && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE
364 await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t) 368 await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
365 369
370 if (wasUnlistedVideo || wasPrivateVideo) {
371 Notifier.Instance.notifyOnNewVideo(videoInstanceUpdated)
372 }
373
366 auditLogger.update( 374 auditLogger.update(
367 getAuditIdFromRes(res), 375 getAuditIdFromRes(res),
368 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), 376 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts
index ccb9b6029..960085af1 100644
--- a/server/controllers/feeds.ts
+++ b/server/controllers/feeds.ts
@@ -56,7 +56,7 @@ async function generateVideoCommentsFeed (req: express.Request, res: express.Res
56 56
57 // Adding video items to the feed, one at a time 57 // Adding video items to the feed, one at a time
58 comments.forEach(comment => { 58 comments.forEach(comment => {
59 const link = CONFIG.WEBSERVER.URL + '/videos/watch/' + comment.Video.uuid + ';threadId=' + comment.getThreadId() 59 const link = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath()
60 60
61 feed.addItem({ 61 feed.addItem({
62 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`, 62 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts
index 9bc7586d1..53f1653b5 100644
--- a/server/controllers/tracker.ts
+++ b/server/controllers/tracker.ts
@@ -59,7 +59,7 @@ const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
59trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) 59trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
60trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' })) 60trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
61 61
62function createWebsocketServer (app: express.Application) { 62function createWebsocketTrackerServer (app: express.Application) {
63 const server = http.createServer(app) 63 const server = http.createServer(app)
64 const wss = new WebSocketServer({ server: server, path: '/tracker/socket' }) 64 const wss = new WebSocketServer({ server: server, path: '/tracker/socket' })
65 wss.on('connection', function (ws, req) { 65 wss.on('connection', function (ws, req) {
@@ -76,7 +76,7 @@ function createWebsocketServer (app: express.Application) {
76 76
77export { 77export {
78 trackerRouter, 78 trackerRouter,
79 createWebsocketServer 79 createWebsocketTrackerServer
80} 80}
81 81
82// --------------------------------------------------------------------------- 82// ---------------------------------------------------------------------------