AccountMethods
} from './account-interface'
-import LoadApplication = AccountMethods.LoadApplication
import { sendDeleteAccount } from '../../lib/activitypub/send-request'
+import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
let Account: Sequelize.Model<AccountInstance, AccountAttributes>
let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID
let loadApplication: AccountMethods.LoadApplication
let loadByUUID: AccountMethods.LoadByUUID
let loadByUrl: AccountMethods.LoadByUrl
-let loadLocalAccountByNameAndPod: AccountMethods.LoadLocalAccountByNameAndPod
+let loadLocalByName: AccountMethods.LoadLocalByName
+let loadByNameAndHost: AccountMethods.LoadByNameAndHost
let listOwned: AccountMethods.ListOwned
-let listFollowerUrlsForApi: AccountMethods.ListFollowerUrlsForApi
-let listFollowingUrlsForApi: AccountMethods.ListFollowingUrlsForApi
+let listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi
+let listAcceptedFollowingUrlsForApi: AccountMethods.ListAcceptedFollowingUrlsForApi
let listFollowingForApi: AccountMethods.ListFollowingForApi
let listFollowersForApi: AccountMethods.ListFollowersForApi
let isOwned: AccountMethods.IsOwned
type: DataTypes.STRING,
allowNull: false,
validate: {
- usernameValid: value => {
+ nameValid: value => {
const res = isUserUsernameValid(value)
- if (res === false) throw new Error('Username is not valid.')
+ if (res === false) throw new Error('Name is not valid.')
}
}
},
url: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
allowNull: false,
validate: {
urlValid: value => {
}
},
publicKey: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
allowNull: false,
validate: {
publicKeyValid: value => {
}
},
privateKey: {
- type: DataTypes.STRING,
- allowNull: false,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
+ allowNull: true,
validate: {
privateKeyValid: value => {
const res = isAccountPrivateKeyValid(value)
type: DataTypes.INTEGER,
allowNull: false,
validate: {
- followersCountValid: value => {
+ followingCountValid: value => {
const res = isAccountFollowingCountValid(value)
if (res === false) throw new Error('Following count is not valid.')
}
}
},
inboxUrl: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
allowNull: false,
validate: {
inboxUrlValid: value => {
}
},
outboxUrl: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
allowNull: false,
validate: {
outboxUrlValid: value => {
}
},
sharedInboxUrl: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
allowNull: false,
validate: {
sharedInboxUrlValid: value => {
}
},
followersUrl: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
allowNull: false,
validate: {
followersUrlValid: value => {
}
},
followingUrl: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
allowNull: false,
validate: {
followingUrlValid: value => {
loadApplication,
load,
loadByUUID,
- loadLocalAccountByNameAndPod,
+ loadByUrl,
+ loadLocalByName,
+ loadByNameAndHost,
listOwned,
- listFollowerUrlsForApi,
- listFollowingUrlsForApi,
+ listAcceptedFollowerUrlsForApi,
+ listAcceptedFollowingUrlsForApi,
listFollowingForApi,
listFollowersForApi
]
Account.belongsTo(models.Application, {
foreignKey: {
- name: 'userId',
+ name: 'applicationId',
allowNull: true
},
onDelete: 'cascade'
hooks: true
})
- Account.hasMany(models.AccountFollower, {
+ Account.hasMany(models.AccountFollow, {
foreignKey: {
name: 'accountId',
allowNull: false
onDelete: 'cascade'
})
- Account.hasMany(models.AccountFollower, {
+ Account.hasMany(models.AccountFollow, {
foreignKey: {
name: 'targetAccountId',
allowNull: false
attributes: [ 'sharedInboxUrl' ],
include: [
{
- model: Account['sequelize'].models.AccountFollower,
+ model: Account['sequelize'].models.AccountFollow,
+ required: true,
+ as: 'followers',
where: {
targetAccountId: this.id
}
return Account.findAll(query)
}
-listFollowerUrlsForApi = function (id: number, start: number, count?: number) {
- return createListFollowForApiQuery('followers', id, start, count)
+listAcceptedFollowerUrlsForApi = function (id: number, start: number, count?: number) {
+ return createListAcceptedFollowForApiQuery('followers', id, start, count)
}
-listFollowingUrlsForApi = function (id: number, start: number, count?: number) {
- return createListFollowForApiQuery('following', id, start, count)
+listAcceptedFollowingUrlsForApi = function (id: number, start: number, count?: number) {
+ return createListAcceptedFollowForApiQuery('following', id, start, count)
}
listFollowingForApi = function (id: number, start: number, count: number, sort: string) {
include: [
{
model: Account['sequelize'].models.Account,
- as: 'following',
+ as: 'accountFollowing',
required: true,
include: [ Account['sequelize'].models.Pod ]
}
include: [
{
model: Account['sequelize'].models.Account,
- as: 'followers',
+ as: 'accountFollowers',
required: true,
include: [ Account['sequelize'].models.Pod ]
}
return Account.findOne({
include: [
{
- model: Account['sequelize'].model.Application,
+ model: Account['sequelize'].models.Application,
required: true
}
]
return Account.findOne(query)
}
-loadLocalAccountByNameAndPod = function (name: string, host: string) {
+loadLocalByName = function (name: string) {
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
name,
- userId: {
- [Sequelize.Op.ne]: null
- }
+ [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
},
include: [
{
model: Account['sequelize'].models.Pod,
+ required: true,
where: {
host
}
return Account.findOne(query)
}
-loadByUrl = function (url: string) {
+loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
url
- }
+ },
+ transaction
}
return Account.findOne(query)
// ------------------------------ UTILS ------------------------------
-async function createListFollowForApiQuery (type: 'followers' | 'following', id: number, start: number, count?: number) {
+async function createListAcceptedFollowForApiQuery (type: 'followers' | 'following', id: number, start: number, count?: number) {
let firstJoin: string
let secondJoin: string
for (const selection of selections) {
let query = 'SELECT ' + selection + ' FROM "Account" ' +
- 'INNER JOIN "AccountFollower" ON "AccountFollower"."' + firstJoin + '" = "Account"."id" ' +
+ 'INNER JOIN "AccountFollow" ON "AccountFollow"."' + firstJoin + '" = "Account"."id" ' +
'INNER JOIN "Account" AS "Follows" ON "Followers"."id" = "Follows"."' + secondJoin + '" ' +
- 'WHERE "Account"."id" = $id ' +
+ 'WHERE "Account"."id" = $id AND "AccountFollow"."state" = \'accepted\' ' +
'LIMIT ' + start
if (count !== undefined) query += ', ' + count