diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-06 12:26:58 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-06 12:26:58 +0100 |
commit | 73471b1a52f242e86364ffb077ea6cadb3b07ae2 (patch) | |
tree | 43dbb7748e281f8d80f15326f489cdea10ec857d /server/controllers/api/users/my-subscriptions.ts | |
parent | c22419dd265c0c7185bf4197a1cb286eb3d8ebc0 (diff) | |
parent | f5305c04aae14467d6f957b713c5a902275cbb89 (diff) | |
download | PeerTube-73471b1a52f242e86364ffb077ea6cadb3b07ae2.tar.gz PeerTube-73471b1a52f242e86364ffb077ea6cadb3b07ae2.tar.zst PeerTube-73471b1a52f242e86364ffb077ea6cadb3b07ae2.zip |
Merge branch 'release/v1.2.0'
Diffstat (limited to 'server/controllers/api/users/my-subscriptions.ts')
-rw-r--r-- | server/controllers/api/users/my-subscriptions.ts | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/server/controllers/api/users/my-subscriptions.ts b/server/controllers/api/users/my-subscriptions.ts new file mode 100644 index 000000000..accca6d52 --- /dev/null +++ b/server/controllers/api/users/my-subscriptions.ts | |||
@@ -0,0 +1,170 @@ | |||
1 | import * as express from 'express' | ||
2 | import 'multer' | ||
3 | import { getFormattedObjects } from '../../../helpers/utils' | ||
4 | import { CONFIG, sequelizeTypescript } from '../../../initializers' | ||
5 | import { | ||
6 | asyncMiddleware, | ||
7 | asyncRetryTransactionMiddleware, | ||
8 | authenticate, | ||
9 | commonVideosFiltersValidator, | ||
10 | paginationValidator, | ||
11 | setDefaultPagination, | ||
12 | setDefaultSort, | ||
13 | userSubscriptionAddValidator, | ||
14 | userSubscriptionGetValidator | ||
15 | } from '../../../middlewares' | ||
16 | import { areSubscriptionsExistValidator, userSubscriptionsSortValidator, videosSortValidator } from '../../../middlewares/validators' | ||
17 | import { UserModel } from '../../../models/account/user' | ||
18 | import { VideoModel } from '../../../models/video/video' | ||
19 | import { buildNSFWFilter } from '../../../helpers/express-utils' | ||
20 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' | ||
21 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | ||
22 | import { JobQueue } from '../../../lib/job-queue' | ||
23 | import { logger } from '../../../helpers/logger' | ||
24 | |||
25 | const mySubscriptionsRouter = express.Router() | ||
26 | |||
27 | mySubscriptionsRouter.get('/me/subscriptions/videos', | ||
28 | authenticate, | ||
29 | paginationValidator, | ||
30 | videosSortValidator, | ||
31 | setDefaultSort, | ||
32 | setDefaultPagination, | ||
33 | commonVideosFiltersValidator, | ||
34 | asyncMiddleware(getUserSubscriptionVideos) | ||
35 | ) | ||
36 | |||
37 | mySubscriptionsRouter.get('/me/subscriptions/exist', | ||
38 | authenticate, | ||
39 | areSubscriptionsExistValidator, | ||
40 | asyncMiddleware(areSubscriptionsExist) | ||
41 | ) | ||
42 | |||
43 | mySubscriptionsRouter.get('/me/subscriptions', | ||
44 | authenticate, | ||
45 | paginationValidator, | ||
46 | userSubscriptionsSortValidator, | ||
47 | setDefaultSort, | ||
48 | setDefaultPagination, | ||
49 | asyncMiddleware(getUserSubscriptions) | ||
50 | ) | ||
51 | |||
52 | mySubscriptionsRouter.post('/me/subscriptions', | ||
53 | authenticate, | ||
54 | userSubscriptionAddValidator, | ||
55 | asyncMiddleware(addUserSubscription) | ||
56 | ) | ||
57 | |||
58 | mySubscriptionsRouter.get('/me/subscriptions/:uri', | ||
59 | authenticate, | ||
60 | userSubscriptionGetValidator, | ||
61 | getUserSubscription | ||
62 | ) | ||
63 | |||
64 | mySubscriptionsRouter.delete('/me/subscriptions/:uri', | ||
65 | authenticate, | ||
66 | userSubscriptionGetValidator, | ||
67 | asyncRetryTransactionMiddleware(deleteUserSubscription) | ||
68 | ) | ||
69 | |||
70 | // --------------------------------------------------------------------------- | ||
71 | |||
72 | export { | ||
73 | mySubscriptionsRouter | ||
74 | } | ||
75 | |||
76 | // --------------------------------------------------------------------------- | ||
77 | |||
78 | async function areSubscriptionsExist (req: express.Request, res: express.Response) { | ||
79 | const uris = req.query.uris as string[] | ||
80 | const user = res.locals.oauth.token.User as UserModel | ||
81 | |||
82 | const handles = uris.map(u => { | ||
83 | let [ name, host ] = u.split('@') | ||
84 | if (host === CONFIG.WEBSERVER.HOST) host = null | ||
85 | |||
86 | return { name, host, uri: u } | ||
87 | }) | ||
88 | |||
89 | const results = await ActorFollowModel.listSubscribedIn(user.Account.Actor.id, handles) | ||
90 | |||
91 | const existObject: { [id: string ]: boolean } = {} | ||
92 | for (const handle of handles) { | ||
93 | const obj = results.find(r => { | ||
94 | const server = r.ActorFollowing.Server | ||
95 | |||
96 | return r.ActorFollowing.preferredUsername === handle.name && | ||
97 | ( | ||
98 | (!server && !handle.host) || | ||
99 | (server.host === handle.host) | ||
100 | ) | ||
101 | }) | ||
102 | |||
103 | existObject[handle.uri] = obj !== undefined | ||
104 | } | ||
105 | |||
106 | return res.json(existObject) | ||
107 | } | ||
108 | |||
109 | async function addUserSubscription (req: express.Request, res: express.Response) { | ||
110 | const user = res.locals.oauth.token.User as UserModel | ||
111 | const [ name, host ] = req.body.uri.split('@') | ||
112 | |||
113 | const payload = { | ||
114 | name, | ||
115 | host, | ||
116 | followerActorId: user.Account.Actor.id | ||
117 | } | ||
118 | |||
119 | JobQueue.Instance.createJob({ type: 'activitypub-follow', payload }) | ||
120 | .catch(err => logger.error('Cannot create follow job for subscription %s.', req.body.uri, err)) | ||
121 | |||
122 | return res.status(204).end() | ||
123 | } | ||
124 | |||
125 | function getUserSubscription (req: express.Request, res: express.Response) { | ||
126 | const subscription: ActorFollowModel = res.locals.subscription | ||
127 | |||
128 | return res.json(subscription.ActorFollowing.VideoChannel.toFormattedJSON()) | ||
129 | } | ||
130 | |||
131 | async function deleteUserSubscription (req: express.Request, res: express.Response) { | ||
132 | const subscription: ActorFollowModel = res.locals.subscription | ||
133 | |||
134 | await sequelizeTypescript.transaction(async t => { | ||
135 | return subscription.destroy({ transaction: t }) | ||
136 | }) | ||
137 | |||
138 | return res.type('json').status(204).end() | ||
139 | } | ||
140 | |||
141 | async function getUserSubscriptions (req: express.Request, res: express.Response) { | ||
142 | const user = res.locals.oauth.token.User as UserModel | ||
143 | const actorId = user.Account.Actor.id | ||
144 | |||
145 | const resultList = await ActorFollowModel.listSubscriptionsForApi(actorId, req.query.start, req.query.count, req.query.sort) | ||
146 | |||
147 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
148 | } | ||
149 | |||
150 | async function getUserSubscriptionVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
151 | const user = res.locals.oauth.token.User as UserModel | ||
152 | const resultList = await VideoModel.listForApi({ | ||
153 | start: req.query.start, | ||
154 | count: req.query.count, | ||
155 | sort: req.query.sort, | ||
156 | includeLocalVideos: false, | ||
157 | categoryOneOf: req.query.categoryOneOf, | ||
158 | licenceOneOf: req.query.licenceOneOf, | ||
159 | languageOneOf: req.query.languageOneOf, | ||
160 | tagsOneOf: req.query.tagsOneOf, | ||
161 | tagsAllOf: req.query.tagsAllOf, | ||
162 | nsfw: buildNSFWFilter(res, req.query.nsfw), | ||
163 | filter: req.query.filter as VideoFilter, | ||
164 | withFiles: false, | ||
165 | followerActorId: user.Account.Actor.id, | ||
166 | user | ||
167 | }) | ||
168 | |||
169 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | ||
170 | } | ||