2017-12-04 10:34:40 +01:00
|
|
|
import { join } from 'path'
|
2017-11-09 17:51:58 +01:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-12-12 17:53:50 +01:00
|
|
|
import {
|
|
|
|
AfterDestroy,
|
|
|
|
AllowNull,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
DataType,
|
|
|
|
Default,
|
|
|
|
ForeignKey,
|
|
|
|
HasMany,
|
|
|
|
Is,
|
|
|
|
IsUUID,
|
|
|
|
Model,
|
|
|
|
Table,
|
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
2017-12-04 10:34:40 +01:00
|
|
|
import { Avatar } from '../../../shared/models/avatars/avatar.model'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { activityPubContextify } from '../../helpers'
|
2017-11-09 17:51:58 +01:00
|
|
|
import {
|
|
|
|
isAccountFollowersCountValid,
|
|
|
|
isAccountFollowingCountValid,
|
2017-11-17 15:52:26 +01:00
|
|
|
isAccountPrivateKeyValid,
|
|
|
|
isAccountPublicKeyValid,
|
2017-12-12 17:53:50 +01:00
|
|
|
isActivityPubUrlValid
|
|
|
|
} from '../../helpers/custom-validators/activitypub'
|
|
|
|
import { isUserUsernameValid } from '../../helpers/custom-validators/users'
|
|
|
|
import { AVATARS_DIR, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
|
|
|
|
import { sendDeleteAccount } from '../../lib/activitypub/send'
|
|
|
|
import { ApplicationModel } from '../application/application'
|
|
|
|
import { AvatarModel } from '../avatar/avatar'
|
|
|
|
import { ServerModel } from '../server/server'
|
|
|
|
import { throwIfNotValid } from '../utils'
|
|
|
|
import { VideoChannelModel } from '../video/video-channel'
|
|
|
|
import { AccountFollowModel } from './account-follow'
|
|
|
|
import { UserModel } from './user'
|
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'account',
|
|
|
|
indexes: [
|
2017-11-09 17:51:58 +01:00
|
|
|
{
|
2017-12-12 17:53:50 +01:00
|
|
|
fields: [ 'name' ]
|
2017-11-09 17:51:58 +01:00
|
|
|
},
|
|
|
|
{
|
2017-12-12 17:53:50 +01:00
|
|
|
fields: [ 'serverId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'userId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'applicationId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'name', 'serverId', 'applicationId' ],
|
|
|
|
unique: true
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
]
|
2017-12-12 17:53:50 +01:00
|
|
|
})
|
|
|
|
export class AccountModel extends Model<Account> {
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Default(DataType.UUIDV4)
|
|
|
|
@IsUUID(4)
|
|
|
|
@Column(DataType.UUID)
|
|
|
|
uuid: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name'))
|
|
|
|
@Column
|
|
|
|
name: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AccountUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
|
|
|
|
url: string
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Is('AccountPublicKey', value => throwIfNotValid(value, isAccountPublicKeyValid, 'public key'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max))
|
|
|
|
publicKey: string
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Is('AccountPublicKey', value => throwIfNotValid(value, isAccountPrivateKeyValid, 'private key'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max))
|
|
|
|
privateKey: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AccountFollowersCount', value => throwIfNotValid(value, isAccountFollowersCountValid, 'followers count'))
|
|
|
|
@Column
|
|
|
|
followersCount: number
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AccountFollowersCount', value => throwIfNotValid(value, isAccountFollowingCountValid, 'following count'))
|
|
|
|
@Column
|
|
|
|
followingCount: number
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AccountInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
|
|
|
|
inboxUrl: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AccountOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
|
|
|
|
outboxUrl: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AccountSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
|
|
|
|
sharedInboxUrl: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AccountFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
|
|
|
|
followersUrl: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('AccountFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max))
|
|
|
|
followingUrl: string
|
|
|
|
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@ForeignKey(() => AvatarModel)
|
|
|
|
@Column
|
|
|
|
avatarId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => AvatarModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
Avatar: AvatarModel
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@ForeignKey(() => ServerModel)
|
|
|
|
@Column
|
|
|
|
serverId: number
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@BelongsTo(() => ServerModel, {
|
2017-11-09 17:51:58 +01:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 17:53:50 +01:00
|
|
|
Server: ServerModel
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@ForeignKey(() => UserModel)
|
|
|
|
@Column
|
|
|
|
userId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => UserModel, {
|
2017-11-09 17:51:58 +01:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 17:53:50 +01:00
|
|
|
User: UserModel
|
|
|
|
|
|
|
|
@ForeignKey(() => ApplicationModel)
|
|
|
|
@Column
|
|
|
|
applicationId: number
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@BelongsTo(() => ApplicationModel, {
|
2017-11-09 17:51:58 +01:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 17:53:50 +01:00
|
|
|
Application: ApplicationModel
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@HasMany(() => VideoChannelModel, {
|
2017-11-09 17:51:58 +01:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade',
|
|
|
|
hooks: true
|
|
|
|
})
|
2017-12-12 17:53:50 +01:00
|
|
|
VideoChannels: VideoChannelModel[]
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@HasMany(() => AccountFollowModel, {
|
2017-11-09 17:51:58 +01:00
|
|
|
foreignKey: {
|
|
|
|
name: 'accountId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 17:53:50 +01:00
|
|
|
AccountFollowing: AccountFollowModel[]
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@HasMany(() => AccountFollowModel, {
|
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'
|
|
|
|
})
|
2017-12-12 17:53:50 +01:00
|
|
|
AccountFollowers: AccountFollowModel[]
|
2017-12-04 10:34:40 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@AfterDestroy
|
|
|
|
static sendDeleteIfOwned (instance: AccountModel) {
|
|
|
|
if (instance.isOwned()) {
|
|
|
|
return sendDeleteAccount(instance, undefined)
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return undefined
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static loadApplication () {
|
|
|
|
return AccountModel.findOne({
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ApplicationModel,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static load (id: number) {
|
|
|
|
return AccountModel.findById(id)
|
|
|
|
}
|
2017-12-04 10:34:40 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static loadByUUID (uuid: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
uuid
|
|
|
|
}
|
2017-12-04 10:34:40 +01:00
|
|
|
}
|
2017-11-15 11:00:25 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return AccountModel.findOne(query)
|
2017-11-15 11:00:25 +01:00
|
|
|
}
|
2017-11-15 10:10:41 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static loadLocalByName (name: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
name,
|
|
|
|
[ Sequelize.Op.or ]: [
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
[ Sequelize.Op.ne ]: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
applicationId: {
|
|
|
|
[ Sequelize.Op.ne ]: null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 17:39:41 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return AccountModel.findOne(query)
|
|
|
|
}
|
2017-11-13 17:39:41 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static loadByNameAndHost (name: string, host: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
name
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ServerModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
2017-12-12 17:53:50 +01:00
|
|
|
|
|
|
|
return AccountModel.findOne(query)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
url
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return AccountModel.findOne(query)
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
followersUrl: {
|
|
|
|
[ Sequelize.Op.in ]: followersUrls
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
2017-12-12 17:53:50 +01:00
|
|
|
},
|
|
|
|
transaction
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return AccountModel.findAll(query)
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
toFormattedJSON () {
|
|
|
|
let host = CONFIG.WEBSERVER.HOST
|
|
|
|
let score: number
|
|
|
|
let avatar: Avatar = null
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
if (this.Avatar) {
|
|
|
|
avatar = {
|
|
|
|
path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
|
|
|
|
createdAt: this.Avatar.createdAt,
|
|
|
|
updatedAt: this.Avatar.updatedAt
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
if (this.Server) {
|
|
|
|
host = this.Server.host
|
|
|
|
score = this.Server.score
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
uuid: this.uuid,
|
|
|
|
host,
|
|
|
|
score,
|
|
|
|
name: this.name,
|
|
|
|
followingCount: this.followingCount,
|
|
|
|
followersCount: this.followersCount,
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt,
|
|
|
|
avatar
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
toActivityPubObject () {
|
|
|
|
const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
|
|
|
|
|
|
|
|
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
|
2017-11-13 17:39:41 +01:00
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
2017-12-12 17:53:50 +01:00
|
|
|
|
|
|
|
return activityPubContextify(json)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
isOwned () {
|
|
|
|
return this.serverId === null
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
|
|
|
|
const query = {
|
|
|
|
attributes: [ 'sharedInboxUrl' ],
|
|
|
|
include: [
|
2017-11-14 17:31:26 +01:00
|
|
|
{
|
2017-12-12 17:53:50 +01:00
|
|
|
model: AccountFollowModel,
|
|
|
|
required: true,
|
|
|
|
as: 'followers',
|
|
|
|
where: {
|
|
|
|
targetAccountId: this.id
|
2017-11-14 17:31:26 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-12 17:53:50 +01:00
|
|
|
],
|
|
|
|
transaction: t
|
2017-11-14 17:31:26 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return AccountModel.findAll(query)
|
|
|
|
.then(accounts => accounts.map(a => a.sharedInboxUrl))
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
getFollowingUrl () {
|
|
|
|
return this.url + '/following'
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
getFollowersUrl () {
|
|
|
|
return this.url + '/followers'
|
2017-11-24 13:41:10 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
getPublicKeyUrl () {
|
|
|
|
return this.url + '#main-key'
|
|
|
|
}
|
2017-11-24 13:41:10 +01:00
|
|
|
}
|