2017-11-09 19:51:58 +03:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
|
|
|
import {
|
|
|
|
isUserUsernameValid,
|
|
|
|
isAccountPublicKeyValid,
|
|
|
|
isAccountUrlValid,
|
|
|
|
isAccountPrivateKeyValid,
|
|
|
|
isAccountFollowersCountValid,
|
|
|
|
isAccountFollowingCountValid,
|
|
|
|
isAccountInboxValid,
|
|
|
|
isAccountOutboxValid,
|
|
|
|
isAccountSharedInboxValid,
|
|
|
|
isAccountFollowersValid,
|
|
|
|
isAccountFollowingValid,
|
|
|
|
activityPubContextify
|
|
|
|
} from '../../helpers'
|
|
|
|
|
2017-11-13 19:39:41 +03:00
|
|
|
import { addMethodsToModel, getSort } from '../utils'
|
2017-11-09 19:51:58 +03:00
|
|
|
import {
|
|
|
|
AccountInstance,
|
|
|
|
AccountAttributes,
|
|
|
|
|
|
|
|
AccountMethods
|
|
|
|
} from './account-interface'
|
2017-11-13 19:39:41 +03:00
|
|
|
import { sendDeleteAccount } from '../../lib/activitypub/send-request'
|
2017-11-15 12:10:41 +03:00
|
|
|
import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
2017-11-09 19:51:58 +03:00
|
|
|
|
|
|
|
let Account: Sequelize.Model<AccountInstance, AccountAttributes>
|
2017-11-15 13:00:25 +03:00
|
|
|
let loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
|
2017-11-09 19:51:58 +03:00
|
|
|
let load: AccountMethods.Load
|
2017-11-13 19:39:41 +03:00
|
|
|
let loadApplication: AccountMethods.LoadApplication
|
2017-11-09 19:51:58 +03:00
|
|
|
let loadByUUID: AccountMethods.LoadByUUID
|
|
|
|
let loadByUrl: AccountMethods.LoadByUrl
|
2017-11-14 19:31:26 +03:00
|
|
|
let loadLocalByName: AccountMethods.LoadLocalByName
|
|
|
|
let loadByNameAndHost: AccountMethods.LoadByNameAndHost
|
2017-11-09 19:51:58 +03:00
|
|
|
let listOwned: AccountMethods.ListOwned
|
|
|
|
let isOwned: AccountMethods.IsOwned
|
|
|
|
let toActivityPubObject: AccountMethods.ToActivityPubObject
|
2017-11-13 19:39:41 +03:00
|
|
|
let toFormattedJSON: AccountMethods.ToFormattedJSON
|
2017-11-09 19:51:58 +03: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 12:57:56 +03:00
|
|
|
nameValid: value => {
|
2017-11-09 19:51:58 +03:00
|
|
|
const res = isUserUsernameValid(value)
|
2017-11-14 12:57:56 +03:00
|
|
|
if (res === false) throw new Error('Name is not valid.')
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
url: {
|
2017-11-14 12:57:56 +03:00
|
|
|
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
|
2017-11-09 19:51:58 +03:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
urlValid: value => {
|
|
|
|
const res = isAccountUrlValid(value)
|
|
|
|
if (res === false) throw new Error('URL is not valid.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
publicKey: {
|
2017-11-14 12:57:56 +03:00
|
|
|
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
|
2017-11-09 19:51:58 +03:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
publicKeyValid: value => {
|
|
|
|
const res = isAccountPublicKeyValid(value)
|
|
|
|
if (res === false) throw new Error('Public key is not valid.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
privateKey: {
|
2017-11-14 12:57:56 +03:00
|
|
|
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
|
2017-11-14 19:31:26 +03:00
|
|
|
allowNull: true,
|
2017-11-09 19:51:58 +03: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 12:57:56 +03:00
|
|
|
followingCountValid: value => {
|
2017-11-09 19:51:58 +03:00
|
|
|
const res = isAccountFollowingCountValid(value)
|
|
|
|
if (res === false) throw new Error('Following count is not valid.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
inboxUrl: {
|
2017-11-14 12:57:56 +03:00
|
|
|
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
|
2017-11-09 19:51:58 +03: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 12:57:56 +03:00
|
|
|
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
|
2017-11-09 19:51:58 +03: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 12:57:56 +03:00
|
|
|
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
|
2017-11-09 19:51:58 +03: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 12:57:56 +03:00
|
|
|
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
|
2017-11-09 19:51:58 +03: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 12:57:56 +03:00
|
|
|
type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
|
2017-11-09 19:51:58 +03: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 13:00:25 +03:00
|
|
|
fields: [ 'serverId' ]
|
2017-11-09 19:51:58 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'userId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'applicationId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
2017-11-15 13:00:25 +03:00
|
|
|
fields: [ 'name', 'serverId', 'applicationId' ],
|
2017-11-09 19:51:58 +03:00
|
|
|
unique: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
hooks: { afterDestroy }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const classMethods = [
|
|
|
|
associate,
|
2017-11-15 13:00:25 +03:00
|
|
|
loadAccountByServerAndUUID,
|
2017-11-13 19:39:41 +03:00
|
|
|
loadApplication,
|
2017-11-09 19:51:58 +03:00
|
|
|
load,
|
|
|
|
loadByUUID,
|
2017-11-13 20:48:28 +03:00
|
|
|
loadByUrl,
|
2017-11-14 19:31:26 +03:00
|
|
|
loadLocalByName,
|
|
|
|
loadByNameAndHost,
|
2017-11-15 12:10:41 +03:00
|
|
|
listOwned
|
2017-11-09 19:51:58 +03:00
|
|
|
]
|
|
|
|
const instanceMethods = [
|
|
|
|
isOwned,
|
|
|
|
toActivityPubObject,
|
2017-11-13 19:39:41 +03:00
|
|
|
toFormattedJSON,
|
2017-11-09 19:51:58 +03:00
|
|
|
getFollowerSharedInboxUrls,
|
|
|
|
getFollowingUrl,
|
|
|
|
getFollowersUrl,
|
|
|
|
getPublicKeyUrl
|
|
|
|
]
|
|
|
|
addMethodsToModel(Account, classMethods, instanceMethods)
|
|
|
|
|
|
|
|
return Account
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function associate (models) {
|
2017-11-15 13:00:25 +03:00
|
|
|
Account.belongsTo(models.Server, {
|
2017-11-09 19:51:58 +03:00
|
|
|
foreignKey: {
|
2017-11-15 13:00:25 +03:00
|
|
|
name: 'serverId',
|
2017-11-09 19:51:58 +03:00
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
|
|
|
|
Account.belongsTo(models.User, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'userId',
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
|
|
|
|
Account.belongsTo(models.Application, {
|
|
|
|
foreignKey: {
|
2017-11-14 12:57:56 +03:00
|
|
|
name: 'applicationId',
|
2017-11-09 19:51:58 +03:00
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
|
|
|
|
Account.hasMany(models.VideoChannel, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'accountId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade',
|
|
|
|
hooks: true
|
|
|
|
})
|
|
|
|
|
2017-11-14 12:57:56 +03:00
|
|
|
Account.hasMany(models.AccountFollow, {
|
2017-11-09 19:51:58 +03:00
|
|
|
foreignKey: {
|
|
|
|
name: 'accountId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
|
2017-11-14 12:57:56 +03:00
|
|
|
Account.hasMany(models.AccountFollow, {
|
2017-11-09 19:51:58 +03:00
|
|
|
foreignKey: {
|
|
|
|
name: 'targetAccountId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
2017-11-15 18:28:35 +03:00
|
|
|
as: 'followers',
|
2017-11-09 19:51:58 +03:00
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function afterDestroy (account: AccountInstance) {
|
|
|
|
if (account.isOwned()) {
|
2017-11-13 19:39:41 +03:00
|
|
|
return sendDeleteAccount(account, undefined)
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2017-11-13 19:39:41 +03:00
|
|
|
toFormattedJSON = function (this: AccountInstance) {
|
2017-11-15 13:00:25 +03:00
|
|
|
let host = CONFIG.WEBSERVER.HOST
|
|
|
|
let score: number
|
|
|
|
|
|
|
|
if (this.Server) {
|
|
|
|
host = this.Server.host
|
|
|
|
score = this.Server.score as number
|
|
|
|
}
|
2017-11-15 12:10:41 +03:00
|
|
|
|
2017-11-13 19:39:41 +03:00
|
|
|
const json = {
|
|
|
|
id: this.id,
|
2017-11-15 12:10:41 +03:00
|
|
|
host,
|
2017-11-15 13:00:25 +03:00
|
|
|
score,
|
|
|
|
name: this.name,
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt
|
2017-11-13 19:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2017-11-09 19:51:58 +03:00
|
|
|
toActivityPubObject = function (this: AccountInstance) {
|
2017-11-15 13:00:25 +03:00
|
|
|
const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
|
2017-11-09 19:51:58 +03: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 13:00:25 +03:00
|
|
|
return this.serverId === null
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getFollowerSharedInboxUrls = function (this: AccountInstance) {
|
|
|
|
const query: Sequelize.FindOptions<AccountAttributes> = {
|
|
|
|
attributes: [ 'sharedInboxUrl' ],
|
|
|
|
include: [
|
|
|
|
{
|
2017-11-14 12:57:56 +03:00
|
|
|
model: Account['sequelize'].models.AccountFollow,
|
2017-11-14 19:31:26 +03:00
|
|
|
required: true,
|
|
|
|
as: 'followers',
|
2017-11-09 19:51:58 +03:00
|
|
|
where: {
|
|
|
|
targetAccountId: this.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return Account.findAll(query)
|
|
|
|
.then(accounts => accounts.map(a => a.sharedInboxUrl))
|
|
|
|
}
|
|
|
|
|
|
|
|
getFollowingUrl = function (this: AccountInstance) {
|
2017-11-15 12:10:41 +03:00
|
|
|
return this.url + '/following'
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getFollowersUrl = function (this: AccountInstance) {
|
|
|
|
return this.url + '/followers'
|
|
|
|
}
|
|
|
|
|
|
|
|
getPublicKeyUrl = function (this: AccountInstance) {
|
|
|
|
return this.url + '#main-key'
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ STATICS ------------------------------
|
|
|
|
|
|
|
|
listOwned = function () {
|
|
|
|
const query: Sequelize.FindOptions<AccountAttributes> = {
|
|
|
|
where: {
|
2017-11-15 13:00:25 +03:00
|
|
|
serverId: null
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Account.findAll(query)
|
|
|
|
}
|
|
|
|
|
2017-11-13 19:39:41 +03:00
|
|
|
loadApplication = function () {
|
|
|
|
return Account.findOne({
|
|
|
|
include: [
|
|
|
|
{
|
2017-11-14 19:31:26 +03:00
|
|
|
model: Account['sequelize'].models.Application,
|
2017-11-13 19:39:41 +03:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2017-11-09 19:51:58 +03: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 19:31:26 +03:00
|
|
|
loadLocalByName = function (name: string) {
|
2017-11-09 19:51:58 +03:00
|
|
|
const query: Sequelize.FindOptions<AccountAttributes> = {
|
|
|
|
where: {
|
|
|
|
name,
|
2017-11-14 19:31:26 +03: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 19:39:41 +03:00
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
2017-11-15 13:00:25 +03:00
|
|
|
model: Account['sequelize'].models.Server,
|
2017-11-14 19:31:26 +03:00
|
|
|
required: true,
|
2017-11-13 19:39:41 +03:00
|
|
|
where: {
|
|
|
|
host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Account.findOne(query)
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:48:28 +03:00
|
|
|
loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
|
2017-11-09 19:51:58 +03:00
|
|
|
const query: Sequelize.FindOptions<AccountAttributes> = {
|
|
|
|
where: {
|
|
|
|
url
|
2017-11-13 20:48:28 +03:00
|
|
|
},
|
|
|
|
transaction
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Account.findOne(query)
|
|
|
|
}
|
|
|
|
|
2017-11-15 13:00:25 +03:00
|
|
|
loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
|
2017-11-09 19:51:58 +03:00
|
|
|
const query: Sequelize.FindOptions<AccountAttributes> = {
|
|
|
|
where: {
|
2017-11-15 13:00:25 +03:00
|
|
|
serverId,
|
2017-11-09 19:51:58 +03:00
|
|
|
uuid
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
return Account.find(query)
|
|
|
|
}
|