aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/server/follows.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-12 17:53:50 +0100
committerChocobozzz <me@florianbigard.com>2017-12-13 16:50:33 +0100
commit3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch)
treee5ca358287fca6ecacce83defcf23af1e8e9f419 /server/controllers/api/server/follows.ts
parentc893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff)
downloadPeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst
PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip
Move models to typescript-sequelize
Diffstat (limited to 'server/controllers/api/server/follows.ts')
-rw-r--r--server/controllers/api/server/follows.ts54
1 files changed, 27 insertions, 27 deletions
diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts
index c2fb37c39..913998e3a 100644
--- a/server/controllers/api/server/follows.ts
+++ b/server/controllers/api/server/follows.ts
@@ -1,24 +1,24 @@
1import * as express from 'express' 1import * as express from 'express'
2import { UserRight } from '../../../../shared/models/users/user-right.enum' 2import { UserRight } from '../../../../shared/models/users'
3import { getFormattedObjects } from '../../../helpers' 3import { getAccountFromWebfinger, getFormattedObjects, getServerAccount, logger, retryTransactionWrapper } from '../../../helpers'
4import { retryTransactionWrapper } from '../../../helpers/database-utils' 4import { sequelizeTypescript, SERVER_ACCOUNT_NAME } from '../../../initializers'
5import { logger } from '../../../helpers/logger' 5import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub'
6import { getServerAccount } from '../../../helpers/utils' 6import { sendUndoFollow } from '../../../lib/activitypub/send'
7import { getAccountFromWebfinger } from '../../../helpers/webfinger'
8import { SERVER_ACCOUNT_NAME } from '../../../initializers/constants'
9import { database as db } from '../../../initializers/database'
10import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub/account'
11import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo'
12import { sendFollow } from '../../../lib/index' 7import { sendFollow } from '../../../lib/index'
13import { asyncMiddleware, paginationValidator, removeFollowingValidator, setFollowersSort, setPagination } from '../../../middlewares' 8import {
14import { authenticate } from '../../../middlewares/oauth' 9 asyncMiddleware,
15import { setBodyHostsPort } from '../../../middlewares/servers' 10 authenticate,
16import { setFollowingSort } from '../../../middlewares/sort' 11 ensureUserHasRight,
17import { ensureUserHasRight } from '../../../middlewares/user-right' 12 paginationValidator,
18import { followValidator } from '../../../middlewares/validators/follows' 13 removeFollowingValidator,
19import { followersSortValidator, followingSortValidator } from '../../../middlewares/validators/sort' 14 setBodyHostsPort,
20import { AccountInstance } from '../../../models/account/account-interface' 15 setFollowersSort,
21import { AccountFollowInstance } from '../../../models/index' 16 setFollowingSort,
17 setPagination
18} from '../../../middlewares'
19import { followersSortValidator, followingSortValidator, followValidator } from '../../../middlewares/validators'
20import { AccountModel } from '../../../models/account/account'
21import { AccountFollowModel } from '../../../models/account/account-follow'
22 22
23const serverFollowsRouter = express.Router() 23const serverFollowsRouter = express.Router()
24 24
@@ -63,14 +63,14 @@ export {
63 63
64async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) { 64async function listFollowing (req: express.Request, res: express.Response, next: express.NextFunction) {
65 const serverAccount = await getServerAccount() 65 const serverAccount = await getServerAccount()
66 const resultList = await db.AccountFollow.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) 66 const resultList = await AccountFollowModel.listFollowingForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort)
67 67
68 return res.json(getFormattedObjects(resultList.data, resultList.total)) 68 return res.json(getFormattedObjects(resultList.data, resultList.total))
69} 69}
70 70
71async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) { 71async function listFollowers (req: express.Request, res: express.Response, next: express.NextFunction) {
72 const serverAccount = await getServerAccount() 72 const serverAccount = await getServerAccount()
73 const resultList = await db.AccountFollow.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort) 73 const resultList = await AccountFollowModel.listFollowersForApi(serverAccount.id, req.query.start, req.query.count, req.query.sort)
74 74
75 return res.json(getFormattedObjects(resultList.data, resultList.total)) 75 return res.json(getFormattedObjects(resultList.data, resultList.total))
76} 76}
@@ -110,14 +110,14 @@ async function followRetry (req: express.Request, res: express.Response, next: e
110 return res.status(204).end() 110 return res.status(204).end()
111} 111}
112 112
113async function follow (fromAccount: AccountInstance, targetAccount: AccountInstance, targetAlreadyInDB: boolean) { 113async function follow (fromAccount: AccountModel, targetAccount: AccountModel, targetAlreadyInDB: boolean) {
114 try { 114 try {
115 await db.sequelize.transaction(async t => { 115 await sequelizeTypescript.transaction(async t => {
116 if (targetAlreadyInDB === false) { 116 if (targetAlreadyInDB === false) {
117 await saveAccountAndServerIfNotExist(targetAccount, t) 117 await saveAccountAndServerIfNotExist(targetAccount, t)
118 } 118 }
119 119
120 const [ accountFollow ] = await db.AccountFollow.findOrCreate({ 120 const [ accountFollow ] = await AccountFollowModel.findOrCreate({
121 where: { 121 where: {
122 accountId: fromAccount.id, 122 accountId: fromAccount.id,
123 targetAccountId: targetAccount.id 123 targetAccountId: targetAccount.id
@@ -145,9 +145,9 @@ async function follow (fromAccount: AccountInstance, targetAccount: AccountInsta
145} 145}
146 146
147async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) { 147async function removeFollow (req: express.Request, res: express.Response, next: express.NextFunction) {
148 const follow: AccountFollowInstance = res.locals.follow 148 const follow: AccountFollowModel = res.locals.follow
149 149
150 await db.sequelize.transaction(async t => { 150 await sequelizeTypescript.transaction(async t => {
151 if (follow.state === 'accepted') await sendUndoFollow(follow, t) 151 if (follow.state === 'accepted') await sendUndoFollow(follow, t)
152 152
153 await follow.destroy({ transaction: t }) 153 await follow.destroy({ transaction: t })
@@ -164,7 +164,7 @@ async function removeFollow (req: express.Request, res: express.Response, next:
164 164
165async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) { 165async function loadLocalOrGetAccountFromWebfinger (name: string, host: string) {
166 let loadedFromDB = true 166 let loadedFromDB = true
167 let account = await db.Account.loadByNameAndHost(name, host) 167 let account = await AccountModel.loadByNameAndHost(name, host)
168 168
169 if (!account) { 169 if (!account) {
170 const nameWithDomain = name + '@' + host 170 const nameWithDomain = name + '@' + host