AccountMethods
} from './account-interface'
import { sendDeleteAccount } from '../../lib/activitypub/send-request'
-import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
+import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
let Account: Sequelize.Model<AccountInstance, AccountAttributes>
-let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID
+let loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
let load: AccountMethods.Load
let loadApplication: AccountMethods.LoadApplication
let loadByUUID: AccountMethods.LoadByUUID
let loadLocalByName: AccountMethods.LoadLocalByName
let loadByNameAndHost: AccountMethods.LoadByNameAndHost
let listOwned: AccountMethods.ListOwned
-let listAcceptedFollowerUrlsForApi: AccountMethods.ListAcceptedFollowerUrlsForApi
-let listAcceptedFollowingUrlsForApi: AccountMethods.ListAcceptedFollowingUrlsForApi
-let listFollowingForApi: AccountMethods.ListFollowingForApi
-let listFollowersForApi: AccountMethods.ListFollowersForApi
let isOwned: AccountMethods.IsOwned
let toActivityPubObject: AccountMethods.ToActivityPubObject
let toFormattedJSON: AccountMethods.ToFormattedJSON
fields: [ 'name' ]
},
{
- fields: [ 'podId' ]
+ fields: [ 'serverId' ]
},
{
fields: [ 'userId' ],
unique: true
},
{
- fields: [ 'name', 'podId' ],
+ fields: [ 'name', 'serverId', 'applicationId' ],
unique: true
}
],
const classMethods = [
associate,
- loadAccountByPodAndUUID,
+ loadAccountByServerAndUUID,
loadApplication,
load,
loadByUUID,
loadByUrl,
loadLocalByName,
loadByNameAndHost,
- listOwned,
- listAcceptedFollowerUrlsForApi,
- listAcceptedFollowingUrlsForApi,
- listFollowingForApi,
- listFollowersForApi
+ listOwned
]
const instanceMethods = [
isOwned,
// ---------------------------------------------------------------------------
function associate (models) {
- Account.belongsTo(models.Pod, {
+ Account.belongsTo(models.Server, {
foreignKey: {
- name: 'podId',
+ name: 'serverId',
allowNull: true
},
onDelete: 'cascade'
name: 'accountId',
allowNull: false
},
- as: 'following',
onDelete: 'cascade'
})
name: 'targetAccountId',
allowNull: false
},
- as: 'followers',
onDelete: 'cascade'
})
}
}
toFormattedJSON = function (this: AccountInstance) {
+ let host = CONFIG.WEBSERVER.HOST
+ let score: number
+
+ if (this.Server) {
+ host = this.Server.host
+ score = this.Server.score as number
+ }
+
const json = {
id: this.id,
- host: this.Pod.host,
- name: this.name
+ host,
+ score,
+ name: this.name,
+ createdAt: this.createdAt,
+ updatedAt: this.updatedAt
}
return json
}
toActivityPubObject = function (this: AccountInstance) {
- const type = this.podId ? 'Application' as 'Application' : 'Person' as 'Person'
+ const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person'
const json = {
type,
}
isOwned = function (this: AccountInstance) {
- return this.podId === null
+ return this.serverId === null
}
getFollowerSharedInboxUrls = function (this: AccountInstance) {
}
getFollowingUrl = function (this: AccountInstance) {
- return this.url + '/followers'
+ return this.url + '/following'
}
getFollowersUrl = function (this: AccountInstance) {
listOwned = function () {
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
- podId: null
+ serverId: null
}
}
return Account.findAll(query)
}
-listAcceptedFollowerUrlsForApi = function (id: number, start: number, count?: number) {
- return createListAcceptedFollowForApiQuery('followers', 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) {
- const query = {
- distinct: true,
- offset: start,
- limit: count,
- order: [ getSort(sort) ],
- include: [
- {
- model: Account['sequelize'].models.AccountFollow,
- required: true,
- as: 'following',
- include: [
- {
- model: Account['sequelize'].models.Account,
- as: 'accountFollowing',
- required: true,
- include: [ Account['sequelize'].models.Pod ]
- }
- ]
- }
- ]
- }
-
- return Account.findAndCountAll(query).then(({ rows, count }) => {
- return {
- data: rows,
- total: count
- }
- })
-}
-
-listFollowersForApi = function (id: number, start: number, count: number, sort: string) {
- const query = {
- distinct: true,
- offset: start,
- limit: count,
- order: [ getSort(sort) ],
- include: [
- {
- model: Account['sequelize'].models.AccountFollow,
- required: true,
- as: 'followers',
- include: [
- {
- model: Account['sequelize'].models.Account,
- as: 'accountFollowers',
- required: true,
- include: [ Account['sequelize'].models.Pod ]
- }
- ]
- }
- ]
- }
-
- return Account.findAndCountAll(query).then(({ rows, count }) => {
- return {
- data: rows,
- total: count
- }
- })
-}
-
loadApplication = function () {
return Account.findOne({
include: [
},
include: [
{
- model: Account['sequelize'].models.Pod,
+ model: Account['sequelize'].models.Server,
required: true,
where: {
host
return Account.findOne(query)
}
-loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
+loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
const query: Sequelize.FindOptions<AccountAttributes> = {
where: {
- podId,
+ serverId,
uuid
},
transaction
return Account.find(query)
}
-
-// ------------------------------ UTILS ------------------------------
-
-async function createListAcceptedFollowForApiQuery (type: 'followers' | 'following', id: number, start: number, count?: number) {
- let firstJoin: string
- let secondJoin: string
-
- if (type === 'followers') {
- firstJoin = 'targetAccountId'
- secondJoin = 'accountId'
- } else {
- firstJoin = 'accountId'
- secondJoin = 'targetAccountId'
- }
-
- const selections = [ '"Followers"."url" AS "url"', 'COUNT(*) AS "total"' ]
- const tasks: Promise<any>[] = []
-
- for (const selection of selections) {
- let query = 'SELECT ' + selection + ' FROM "Account" ' +
- 'INNER JOIN "AccountFollow" ON "AccountFollow"."' + firstJoin + '" = "Account"."id" ' +
- 'INNER JOIN "Account" AS "Follows" ON "Followers"."id" = "Follows"."' + secondJoin + '" ' +
- 'WHERE "Account"."id" = $id AND "AccountFollow"."state" = \'accepted\' ' +
- 'LIMIT ' + start
-
- if (count !== undefined) query += ', ' + count
-
- const options = {
- bind: { id },
- type: Sequelize.QueryTypes.SELECT
- }
- tasks.push(Account['sequelize'].query(query, options))
- }
-
- const [ followers, [ { total } ]] = await Promise.all(tasks)
- const urls: string[] = followers.map(f => f.url)
-
- return {
- data: urls,
- total: parseInt(total, 10)
- }
-}