diff options
author | Chocobozzz <me@florianbigard.com> | 2018-02-21 16:44:18 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-02-21 16:44:43 +0100 |
commit | e8cb44090e654fda339506dccfcec7fea8722723 (patch) | |
tree | 2a36d264cf44cededff0e272919316a3feabcbc1 /server | |
parent | 276d03ed1a469fd4e3579f92392b6f9a1567d1ca (diff) | |
download | PeerTube-e8cb44090e654fda339506dccfcec7fea8722723.tar.gz PeerTube-e8cb44090e654fda339506dccfcec7fea8722723.tar.zst PeerTube-e8cb44090e654fda339506dccfcec7fea8722723.zip |
Add links to comment mentions
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/services.ts | 9 | ||||
-rw-r--r-- | server/helpers/custom-validators/accounts.ts | 11 | ||||
-rw-r--r-- | server/helpers/logger.ts | 2 | ||||
-rw-r--r-- | server/middlewares/validators/account.ts | 23 | ||||
-rw-r--r-- | server/models/account/account.ts | 37 |
5 files changed, 77 insertions, 5 deletions
diff --git a/server/controllers/services.ts b/server/controllers/services.ts index 3ac78a5df..c272edccd 100644 --- a/server/controllers/services.ts +++ b/server/controllers/services.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { CONFIG, EMBED_SIZE, PREVIEWS_SIZE } from '../initializers' | 2 | import { CONFIG, EMBED_SIZE, PREVIEWS_SIZE } from '../initializers' |
3 | import { asyncMiddleware, oembedValidator } from '../middlewares' | 3 | import { asyncMiddleware, oembedValidator } from '../middlewares' |
4 | import { accountsNameWithHostGetValidator } from '../middlewares/validators' | ||
4 | import { VideoModel } from '../models/video/video' | 5 | import { VideoModel } from '../models/video/video' |
5 | 6 | ||
6 | const servicesRouter = express.Router() | 7 | const servicesRouter = express.Router() |
@@ -9,6 +10,10 @@ servicesRouter.use('/oembed', | |||
9 | asyncMiddleware(oembedValidator), | 10 | asyncMiddleware(oembedValidator), |
10 | generateOEmbed | 11 | generateOEmbed |
11 | ) | 12 | ) |
13 | servicesRouter.use('/redirect/accounts/:nameWithHost', | ||
14 | asyncMiddleware(accountsNameWithHostGetValidator), | ||
15 | redirectToAccountUrl | ||
16 | ) | ||
12 | 17 | ||
13 | // --------------------------------------------------------------------------- | 18 | // --------------------------------------------------------------------------- |
14 | 19 | ||
@@ -62,3 +67,7 @@ function generateOEmbed (req: express.Request, res: express.Response, next: expr | |||
62 | 67 | ||
63 | return res.json(json) | 68 | return res.json(json) |
64 | } | 69 | } |
70 | |||
71 | function redirectToAccountUrl (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
72 | return res.redirect(res.locals.account.Actor.url) | ||
73 | } | ||
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index a46ffc162..cc8641d6b 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts | |||
@@ -31,6 +31,16 @@ function isLocalAccountNameExist (name: string, res: Response) { | |||
31 | return isAccountExist(promise, res) | 31 | return isAccountExist(promise, res) |
32 | } | 32 | } |
33 | 33 | ||
34 | function isAccountNameWithHostExist (nameWithDomain: string, res: Response) { | ||
35 | const [ accountName, host ] = nameWithDomain.split('@') | ||
36 | |||
37 | let promise: Bluebird<AccountModel> | ||
38 | if (!host) promise = AccountModel.loadLocalByName(accountName) | ||
39 | else promise = AccountModel.loadLocalByNameAndHost(accountName, host) | ||
40 | |||
41 | return isAccountExist(promise, res) | ||
42 | } | ||
43 | |||
34 | async function isAccountExist (p: Bluebird<AccountModel>, res: Response) { | 44 | async function isAccountExist (p: Bluebird<AccountModel>, res: Response) { |
35 | const account = await p | 45 | const account = await p |
36 | 46 | ||
@@ -53,5 +63,6 @@ export { | |||
53 | isAccountIdExist, | 63 | isAccountIdExist, |
54 | isLocalAccountNameExist, | 64 | isLocalAccountNameExist, |
55 | isAccountDescriptionValid, | 65 | isAccountDescriptionValid, |
66 | isAccountNameWithHostExist, | ||
56 | isAccountNameValid | 67 | isAccountNameValid |
57 | } | 68 | } |
diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts index 7d1d72f29..a4e5b58a4 100644 --- a/server/helpers/logger.ts +++ b/server/helpers/logger.ts | |||
@@ -59,7 +59,7 @@ const logger = new winston.createLogger({ | |||
59 | ) | 59 | ) |
60 | }), | 60 | }), |
61 | new winston.transports.Console({ | 61 | new winston.transports.Console({ |
62 | handleExcegiptions: true, | 62 | handleExceptions: true, |
63 | humanReadableUnhandledException: true, | 63 | humanReadableUnhandledException: true, |
64 | format: winston.format.combine( | 64 | format: winston.format.combine( |
65 | timestampFormatter, | 65 | timestampFormatter, |
diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts index ebc2fcf2d..0c4b7051d 100644 --- a/server/middlewares/validators/account.ts +++ b/server/middlewares/validators/account.ts | |||
@@ -1,6 +1,11 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { param } from 'express-validator/check' | 2 | import { param } from 'express-validator/check' |
3 | import { isAccountIdExist, isAccountNameValid, isLocalAccountNameExist } from '../../helpers/custom-validators/accounts' | 3 | import { |
4 | isAccountIdExist, | ||
5 | isAccountNameValid, | ||
6 | isAccountNameWithHostExist, | ||
7 | isLocalAccountNameExist | ||
8 | } from '../../helpers/custom-validators/accounts' | ||
4 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' | 9 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' |
5 | import { logger } from '../../helpers/logger' | 10 | import { logger } from '../../helpers/logger' |
6 | import { areValidationErrors } from './utils' | 11 | import { areValidationErrors } from './utils' |
@@ -31,9 +36,23 @@ const accountsGetValidator = [ | |||
31 | } | 36 | } |
32 | ] | 37 | ] |
33 | 38 | ||
39 | const accountsNameWithHostGetValidator = [ | ||
40 | param('nameWithHost').exists().withMessage('Should have an account name with host'), | ||
41 | |||
42 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
43 | logger.debug('Checking accountsNameWithHostGetValidator parameters', { parameters: req.params }) | ||
44 | |||
45 | if (areValidationErrors(req, res)) return | ||
46 | if (!await isAccountNameWithHostExist(req.params.nameWithHost, res)) return | ||
47 | |||
48 | return next() | ||
49 | } | ||
50 | ] | ||
51 | |||
34 | // --------------------------------------------------------------------------- | 52 | // --------------------------------------------------------------------------- |
35 | 53 | ||
36 | export { | 54 | export { |
37 | localAccountValidator, | 55 | localAccountValidator, |
38 | accountsGetValidator | 56 | accountsGetValidator, |
57 | accountsNameWithHostGetValidator | ||
39 | } | 58 | } |
diff --git a/server/models/account/account.ts b/server/models/account/account.ts index bc7595a0e..c5955ef3b 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts | |||
@@ -157,7 +157,6 @@ export class AccountModel extends Model<AccountModel> { | |||
157 | static loadLocalByName (name: string) { | 157 | static loadLocalByName (name: string) { |
158 | const query = { | 158 | const query = { |
159 | where: { | 159 | where: { |
160 | name, | ||
161 | [ Sequelize.Op.or ]: [ | 160 | [ Sequelize.Op.or ]: [ |
162 | { | 161 | { |
163 | userId: { | 162 | userId: { |
@@ -170,7 +169,41 @@ export class AccountModel extends Model<AccountModel> { | |||
170 | } | 169 | } |
171 | } | 170 | } |
172 | ] | 171 | ] |
173 | } | 172 | }, |
173 | include: [ | ||
174 | { | ||
175 | model: ActorModel, | ||
176 | required: true, | ||
177 | where: { | ||
178 | preferredUsername: name | ||
179 | } | ||
180 | } | ||
181 | ] | ||
182 | } | ||
183 | |||
184 | return AccountModel.findOne(query) | ||
185 | } | ||
186 | |||
187 | static loadLocalByNameAndHost (name: string, host: string) { | ||
188 | const query = { | ||
189 | include: [ | ||
190 | { | ||
191 | model: ActorModel, | ||
192 | required: true, | ||
193 | where: { | ||
194 | preferredUsername: name | ||
195 | }, | ||
196 | include: [ | ||
197 | { | ||
198 | model: ServerModel, | ||
199 | required: true, | ||
200 | where: { | ||
201 | host | ||
202 | } | ||
203 | } | ||
204 | ] | ||
205 | } | ||
206 | ] | ||
174 | } | 207 | } |
175 | 208 | ||
176 | return AccountModel.findOne(query) | 209 | return AccountModel.findOne(query) |