PeerTube/server/models/account/account.ts

430 lines
10 KiB
TypeScript
Raw Normal View History

2017-11-09 17:51:58 +01:00
import * as Sequelize from 'sequelize'
import {
2017-11-17 15:52:26 +01:00
activityPubContextify,
2017-11-09 17:51:58 +01:00
isAccountFollowersCountValid,
2017-11-17 15:52:26 +01:00
isAccountFollowersValid,
2017-11-09 17:51:58 +01:00
isAccountFollowingCountValid,
2017-11-17 15:52:26 +01:00
isAccountFollowingValid,
2017-11-09 17:51:58 +01:00
isAccountInboxValid,
isAccountOutboxValid,
2017-11-17 15:52:26 +01:00
isAccountPrivateKeyValid,
isAccountPublicKeyValid,
2017-11-09 17:51:58 +01:00
isAccountSharedInboxValid,
2017-11-17 15:52:26 +01:00
isAccountUrlValid,
isUserUsernameValid
2017-11-09 17:51:58 +01:00
} from '../../helpers'
import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
2017-11-20 09:43:39 +01:00
import { sendDeleteAccount } from '../../lib/activitypub/send/send-delete'
2017-11-17 15:52:26 +01:00
import { addMethodsToModel } from '../utils'
import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
2017-11-09 17:51:58 +01:00
let Account: Sequelize.Model<AccountInstance, AccountAttributes>
let load: AccountMethods.Load
2017-11-13 17:39:41 +01:00
let loadApplication: AccountMethods.LoadApplication
2017-11-09 17:51:58 +01:00
let loadByUUID: AccountMethods.LoadByUUID
let loadByUrl: AccountMethods.LoadByUrl
2017-11-14 17:31:26 +01:00
let loadLocalByName: AccountMethods.LoadLocalByName
let loadByNameAndHost: AccountMethods.LoadByNameAndHost
2017-11-09 17:51:58 +01:00
let isOwned: AccountMethods.IsOwned
let toActivityPubObject: AccountMethods.ToActivityPubObject
2017-11-13 17:39:41 +01:00
let toFormattedJSON: AccountMethods.ToFormattedJSON
2017-11-09 17:51:58 +01:00
let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
let getFollowingUrl: AccountMethods.GetFollowingUrl
let getFollowersUrl: AccountMethods.GetFollowersUrl
let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
{
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
validate: {
isUUID: 4
}
},
name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
2017-11-14 10:57:56 +01:00
nameValid: value => {
2017-11-09 17:51:58 +01:00
const res = isUserUsernameValid(value)
2017-11-14 10:57:56 +01:00
if (res === false) throw new Error('Name is not valid.')
2017-11-09 17:51:58 +01:00
}
}
},
url: {
2017-11-14 10:57:56 +01:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
2017-11-09 17:51:58 +01:00
allowNull: false,
validate: {
urlValid: value => {
const res = isAccountUrlValid(value)
if (res === false) throw new Error('URL is not valid.')
}
}
},
publicKey: {
2017-11-14 10:57:56 +01:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
2017-11-16 18:40:50 +01:00
allowNull: true,
2017-11-09 17:51:58 +01:00
validate: {
publicKeyValid: value => {
const res = isAccountPublicKeyValid(value)
if (res === false) throw new Error('Public key is not valid.')
}
}
},
privateKey: {
2017-11-14 10:57:56 +01:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
2017-11-14 17:31:26 +01:00
allowNull: true,
2017-11-09 17:51:58 +01:00
validate: {
privateKeyValid: value => {
const res = isAccountPrivateKeyValid(value)
if (res === false) throw new Error('Private key is not valid.')
}
}
},
followersCount: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
followersCountValid: value => {
const res = isAccountFollowersCountValid(value)
if (res === false) throw new Error('Followers count is not valid.')
}
}
},
followingCount: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
2017-11-14 10:57:56 +01:00
followingCountValid: value => {
2017-11-09 17:51:58 +01:00
const res = isAccountFollowingCountValid(value)
if (res === false) throw new Error('Following count is not valid.')
}
}
},
inboxUrl: {
2017-11-14 10:57:56 +01:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
2017-11-09 17:51:58 +01:00
allowNull: false,
validate: {
inboxUrlValid: value => {
const res = isAccountInboxValid(value)
if (res === false) throw new Error('Inbox URL is not valid.')
}
}
},
outboxUrl: {
2017-11-14 10:57:56 +01:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
2017-11-09 17:51:58 +01:00
allowNull: false,
validate: {
outboxUrlValid: value => {
const res = isAccountOutboxValid(value)
if (res === false) throw new Error('Outbox URL is not valid.')
}
}
},
sharedInboxUrl: {
2017-11-14 10:57:56 +01:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
2017-11-09 17:51:58 +01:00
allowNull: false,
validate: {
sharedInboxUrlValid: value => {
const res = isAccountSharedInboxValid(value)
if (res === false) throw new Error('Shared inbox URL is not valid.')
}
}
},
followersUrl: {
2017-11-14 10:57:56 +01:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
2017-11-09 17:51:58 +01:00
allowNull: false,
validate: {
followersUrlValid: value => {
const res = isAccountFollowersValid(value)
if (res === false) throw new Error('Followers URL is not valid.')
}
}
},
followingUrl: {
2017-11-14 10:57:56 +01:00
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
2017-11-09 17:51:58 +01:00
allowNull: false,
validate: {
followingUrlValid: value => {
const res = isAccountFollowingValid(value)
if (res === false) throw new Error('Following URL is not valid.')
}
}
}
},
{
indexes: [
{
fields: [ 'name' ]
},
{
2017-11-15 11:00:25 +01:00
fields: [ 'serverId' ]
2017-11-09 17:51:58 +01:00
},
{
fields: [ 'userId' ],
unique: true
},
{
fields: [ 'applicationId' ],
unique: true
},
{
2017-11-15 11:00:25 +01:00
fields: [ 'name', 'serverId', 'applicationId' ],
2017-11-09 17:51:58 +01:00
unique: true
}
],
hooks: { afterDestroy }
}
)
const classMethods = [
associate,
2017-11-13 17:39:41 +01:00
loadApplication,
2017-11-09 17:51:58 +01:00
load,
loadByUUID,
2017-11-13 18:48:28 +01:00
loadByUrl,
2017-11-14 17:31:26 +01:00
loadLocalByName,
2017-11-23 17:36:15 +01:00
loadByNameAndHost
2017-11-09 17:51:58 +01:00
]
const instanceMethods = [
isOwned,
toActivityPubObject,
2017-11-13 17:39:41 +01:00
toFormattedJSON,
2017-11-09 17:51:58 +01:00
getFollowerSharedInboxUrls,
getFollowingUrl,
getFollowersUrl,
getPublicKeyUrl
]
addMethodsToModel(Account, classMethods, instanceMethods)
return Account
}
// ---------------------------------------------------------------------------
function associate (models) {
2017-11-15 11:00:25 +01:00
Account.belongsTo(models.Server, {
2017-11-09 17:51:58 +01:00
foreignKey: {
2017-11-15 11:00:25 +01:00
name: 'serverId',
2017-11-09 17:51:58 +01:00
allowNull: true
},
onDelete: 'cascade'
})
Account.belongsTo(models.User, {
foreignKey: {
name: 'userId',
allowNull: true
},
onDelete: 'cascade'
})
Account.belongsTo(models.Application, {
foreignKey: {
2017-11-14 10:57:56 +01:00
name: 'applicationId',
2017-11-09 17:51:58 +01:00
allowNull: true
},
onDelete: 'cascade'
})
Account.hasMany(models.VideoChannel, {
foreignKey: {
name: 'accountId',
allowNull: false
},
onDelete: 'cascade',
hooks: true
})
2017-11-14 10:57:56 +01:00
Account.hasMany(models.AccountFollow, {
2017-11-09 17:51:58 +01:00
foreignKey: {
name: 'accountId',
allowNull: false
},
onDelete: 'cascade'
})
2017-11-14 10:57:56 +01:00
Account.hasMany(models.AccountFollow, {
2017-11-09 17:51:58 +01:00
foreignKey: {
name: 'targetAccountId',
allowNull: false
},
2017-11-15 16:28:35 +01:00
as: 'followers',
2017-11-09 17:51:58 +01:00
onDelete: 'cascade'
})
}
function afterDestroy (account: AccountInstance) {
if (account.isOwned()) {
2017-11-13 17:39:41 +01:00
return sendDeleteAccount(account, undefined)
2017-11-09 17:51:58 +01:00
}
return undefined
}
2017-11-13 17:39:41 +01:00
toFormattedJSON = function (this: AccountInstance) {
2017-11-15 11:00:25 +01:00
let host = CONFIG.WEBSERVER.HOST
let score: number
if (this.Server) {
host = this.Server.host
score = this.Server.score as number
}
2017-11-13 17:39:41 +01:00
const json = {
id: this.id,
host,
2017-11-15 11:00:25 +01:00
score,
name: this.name,
createdAt: this.createdAt,
updatedAt: this.updatedAt
2017-11-13 17:39:41 +01:00
}
return json
}
2017-11-09 17:51:58 +01:00
toActivityPubObject = function (this: AccountInstance) {
2017-11-15 11:00:25 +01:00
const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
2017-11-09 17:51:58 +01:00
const json = {
type,
id: this.url,
following: this.getFollowingUrl(),
followers: this.getFollowersUrl(),
inbox: this.inboxUrl,
outbox: this.outboxUrl,
preferredUsername: this.name,
url: this.url,
name: this.name,
endpoints: {
sharedInbox: this.sharedInboxUrl
},
uuid: this.uuid,
publicKey: {
id: this.getPublicKeyUrl(),
owner: this.url,
publicKeyPem: this.publicKey
}
}
return activityPubContextify(json)
}
isOwned = function (this: AccountInstance) {
2017-11-15 11:00:25 +01:00
return this.serverId === null
2017-11-09 17:51:58 +01:00
}
getFollowerSharedInboxUrls = function (this: AccountInstance) {
const query: Sequelize.FindOptions<AccountAttributes> = {
attributes: [ 'sharedInboxUrl' ],
include: [
{
2017-11-14 10:57:56 +01:00
model: Account['sequelize'].models.AccountFollow,
2017-11-14 17:31:26 +01:00
required: true,
as: 'followers',
2017-11-09 17:51:58 +01:00
where: {
targetAccountId: this.id
}
}
]
}
return Account.findAll(query)
.then(accounts => accounts.map(a => a.sharedInboxUrl))
}
getFollowingUrl = function (this: AccountInstance) {
return this.url + '/following'
2017-11-09 17:51:58 +01:00
}
getFollowersUrl = function (this: AccountInstance) {
return this.url + '/followers'
}
getPublicKeyUrl = function (this: AccountInstance) {
return this.url + '#main-key'
}
// ------------------------------ STATICS ------------------------------
2017-11-13 17:39:41 +01:00
loadApplication = function () {
return Account.findOne({
include: [
{
2017-11-14 17:31:26 +01:00
model: Account['sequelize'].models.Application,
2017-11-13 17:39:41 +01:00
required: true
}
]
})
2017-11-09 17:51:58 +01:00
}
load = function (id: number) {
return Account.findById(id)
}
loadByUUID = function (uuid: string) {
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
uuid
}
}
return Account.findOne(query)
}
2017-11-14 17:31:26 +01:00
loadLocalByName = function (name: string) {
2017-11-09 17:51:58 +01:00
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
name,
2017-11-14 17:31:26 +01:00
[Sequelize.Op.or]: [
{
userId: {
[Sequelize.Op.ne]: null
}
},
{
applicationId: {
[Sequelize.Op.ne]: null
}
}
]
}
}
return Account.findOne(query)
}
loadByNameAndHost = function (name: string, host: string) {
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
name
2017-11-13 17:39:41 +01:00
},
include: [
{
2017-11-15 11:00:25 +01:00
model: Account['sequelize'].models.Server,
2017-11-14 17:31:26 +01:00
required: true,
2017-11-13 17:39:41 +01:00
where: {
host
}
}
]
2017-11-09 17:51:58 +01:00
}
return Account.findOne(query)
}
2017-11-13 18:48:28 +01:00
loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
2017-11-09 17:51:58 +01:00
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
url
2017-11-13 18:48:28 +01:00
},
transaction
2017-11-09 17:51:58 +01:00
}
return Account.findOne(query)
}