]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account.ts
Add follow tabs
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
CommitLineData
e4f97bab
C
1import * as Sequelize from 'sequelize'
2
3import {
4 isUserUsernameValid,
5 isAccountPublicKeyValid,
6 isAccountUrlValid,
7 isAccountPrivateKeyValid,
8 isAccountFollowersCountValid,
9 isAccountFollowingCountValid,
10 isAccountInboxValid,
11 isAccountOutboxValid,
12 isAccountSharedInboxValid,
13 isAccountFollowersValid,
14 isAccountFollowingValid,
15 activityPubContextify
16} from '../../helpers'
17
7a7724e6 18import { addMethodsToModel, getSort } from '../utils'
e4f97bab
C
19import {
20 AccountInstance,
21 AccountAttributes,
22
23 AccountMethods
24} from './account-interface'
7a7724e6 25import { sendDeleteAccount } from '../../lib/activitypub/send-request'
51548b31 26import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
e4f97bab
C
27
28let Account: Sequelize.Model<AccountInstance, AccountAttributes>
29let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID
30let load: AccountMethods.Load
7a7724e6 31let loadApplication: AccountMethods.LoadApplication
e4f97bab
C
32let loadByUUID: AccountMethods.LoadByUUID
33let loadByUrl: AccountMethods.LoadByUrl
350e31d6
C
34let loadLocalByName: AccountMethods.LoadLocalByName
35let loadByNameAndHost: AccountMethods.LoadByNameAndHost
e4f97bab 36let listOwned: AccountMethods.ListOwned
e4f97bab
C
37let isOwned: AccountMethods.IsOwned
38let toActivityPubObject: AccountMethods.ToActivityPubObject
7a7724e6 39let toFormattedJSON: AccountMethods.ToFormattedJSON
e4f97bab
C
40let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
41let getFollowingUrl: AccountMethods.GetFollowingUrl
42let getFollowersUrl: AccountMethods.GetFollowersUrl
43let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
44
45export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
46 Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
47 {
48 uuid: {
49 type: DataTypes.UUID,
50 defaultValue: DataTypes.UUIDV4,
51 allowNull: false,
52 validate: {
53 isUUID: 4
54 }
55 },
56 name: {
57 type: DataTypes.STRING,
58 allowNull: false,
59 validate: {
e34c85e5 60 nameValid: value => {
e4f97bab 61 const res = isUserUsernameValid(value)
e34c85e5 62 if (res === false) throw new Error('Name is not valid.')
e4f97bab
C
63 }
64 }
65 },
66 url: {
e34c85e5 67 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
e4f97bab
C
68 allowNull: false,
69 validate: {
70 urlValid: value => {
71 const res = isAccountUrlValid(value)
72 if (res === false) throw new Error('URL is not valid.')
73 }
74 }
75 },
76 publicKey: {
e34c85e5 77 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
e4f97bab
C
78 allowNull: false,
79 validate: {
80 publicKeyValid: value => {
81 const res = isAccountPublicKeyValid(value)
82 if (res === false) throw new Error('Public key is not valid.')
83 }
84 }
85 },
86 privateKey: {
e34c85e5 87 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
350e31d6 88 allowNull: true,
e4f97bab
C
89 validate: {
90 privateKeyValid: value => {
91 const res = isAccountPrivateKeyValid(value)
92 if (res === false) throw new Error('Private key is not valid.')
93 }
94 }
95 },
96 followersCount: {
97 type: DataTypes.INTEGER,
98 allowNull: false,
99 validate: {
100 followersCountValid: value => {
101 const res = isAccountFollowersCountValid(value)
102 if (res === false) throw new Error('Followers count is not valid.')
103 }
104 }
105 },
106 followingCount: {
107 type: DataTypes.INTEGER,
108 allowNull: false,
109 validate: {
e34c85e5 110 followingCountValid: value => {
e4f97bab
C
111 const res = isAccountFollowingCountValid(value)
112 if (res === false) throw new Error('Following count is not valid.')
113 }
114 }
115 },
116 inboxUrl: {
e34c85e5 117 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
e4f97bab
C
118 allowNull: false,
119 validate: {
120 inboxUrlValid: value => {
121 const res = isAccountInboxValid(value)
122 if (res === false) throw new Error('Inbox URL is not valid.')
123 }
124 }
125 },
126 outboxUrl: {
e34c85e5 127 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
e4f97bab
C
128 allowNull: false,
129 validate: {
130 outboxUrlValid: value => {
131 const res = isAccountOutboxValid(value)
132 if (res === false) throw new Error('Outbox URL is not valid.')
133 }
134 }
135 },
136 sharedInboxUrl: {
e34c85e5 137 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
e4f97bab
C
138 allowNull: false,
139 validate: {
140 sharedInboxUrlValid: value => {
141 const res = isAccountSharedInboxValid(value)
142 if (res === false) throw new Error('Shared inbox URL is not valid.')
143 }
144 }
145 },
146 followersUrl: {
e34c85e5 147 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
e4f97bab
C
148 allowNull: false,
149 validate: {
150 followersUrlValid: value => {
151 const res = isAccountFollowersValid(value)
152 if (res === false) throw new Error('Followers URL is not valid.')
153 }
154 }
155 },
156 followingUrl: {
e34c85e5 157 type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
e4f97bab
C
158 allowNull: false,
159 validate: {
160 followingUrlValid: value => {
161 const res = isAccountFollowingValid(value)
162 if (res === false) throw new Error('Following URL is not valid.')
163 }
164 }
165 }
166 },
167 {
168 indexes: [
169 {
170 fields: [ 'name' ]
171 },
172 {
173 fields: [ 'podId' ]
174 },
175 {
176 fields: [ 'userId' ],
177 unique: true
178 },
179 {
180 fields: [ 'applicationId' ],
181 unique: true
182 },
183 {
51548b31 184 fields: [ 'name', 'podId', 'applicationId' ],
e4f97bab
C
185 unique: true
186 }
187 ],
188 hooks: { afterDestroy }
189 }
190 )
191
192 const classMethods = [
193 associate,
194 loadAccountByPodAndUUID,
7a7724e6 195 loadApplication,
e4f97bab
C
196 load,
197 loadByUUID,
ce548a10 198 loadByUrl,
350e31d6
C
199 loadLocalByName,
200 loadByNameAndHost,
51548b31 201 listOwned
e4f97bab
C
202 ]
203 const instanceMethods = [
204 isOwned,
205 toActivityPubObject,
7a7724e6 206 toFormattedJSON,
e4f97bab
C
207 getFollowerSharedInboxUrls,
208 getFollowingUrl,
209 getFollowersUrl,
210 getPublicKeyUrl
211 ]
212 addMethodsToModel(Account, classMethods, instanceMethods)
213
214 return Account
215}
216
217// ---------------------------------------------------------------------------
218
219function associate (models) {
220 Account.belongsTo(models.Pod, {
221 foreignKey: {
222 name: 'podId',
223 allowNull: true
224 },
225 onDelete: 'cascade'
226 })
227
228 Account.belongsTo(models.User, {
229 foreignKey: {
230 name: 'userId',
231 allowNull: true
232 },
233 onDelete: 'cascade'
234 })
235
236 Account.belongsTo(models.Application, {
237 foreignKey: {
e34c85e5 238 name: 'applicationId',
e4f97bab
C
239 allowNull: true
240 },
241 onDelete: 'cascade'
242 })
243
244 Account.hasMany(models.VideoChannel, {
245 foreignKey: {
246 name: 'accountId',
247 allowNull: false
248 },
249 onDelete: 'cascade',
250 hooks: true
251 })
252
e34c85e5 253 Account.hasMany(models.AccountFollow, {
e4f97bab
C
254 foreignKey: {
255 name: 'accountId',
256 allowNull: false
257 },
7a7724e6 258 as: 'following',
e4f97bab
C
259 onDelete: 'cascade'
260 })
261
e34c85e5 262 Account.hasMany(models.AccountFollow, {
e4f97bab
C
263 foreignKey: {
264 name: 'targetAccountId',
265 allowNull: false
266 },
7a7724e6 267 as: 'followers',
e4f97bab
C
268 onDelete: 'cascade'
269 })
270}
271
272function afterDestroy (account: AccountInstance) {
273 if (account.isOwned()) {
7a7724e6 274 return sendDeleteAccount(account, undefined)
e4f97bab
C
275 }
276
277 return undefined
278}
279
7a7724e6 280toFormattedJSON = function (this: AccountInstance) {
51548b31
C
281 let host = this.Pod ? this.Pod.host : CONFIG.WEBSERVER.HOST
282
7a7724e6
C
283 const json = {
284 id: this.id,
51548b31 285 host,
7a7724e6
C
286 name: this.name
287 }
288
289 return json
290}
291
e4f97bab 292toActivityPubObject = function (this: AccountInstance) {
571389d4 293 const type = this.podId ? 'Application' as 'Application' : 'Person' as 'Person'
e4f97bab
C
294
295 const json = {
296 type,
297 id: this.url,
298 following: this.getFollowingUrl(),
299 followers: this.getFollowersUrl(),
300 inbox: this.inboxUrl,
301 outbox: this.outboxUrl,
302 preferredUsername: this.name,
303 url: this.url,
304 name: this.name,
305 endpoints: {
306 sharedInbox: this.sharedInboxUrl
307 },
308 uuid: this.uuid,
309 publicKey: {
310 id: this.getPublicKeyUrl(),
311 owner: this.url,
312 publicKeyPem: this.publicKey
313 }
314 }
315
316 return activityPubContextify(json)
317}
318
319isOwned = function (this: AccountInstance) {
320 return this.podId === null
321}
322
323getFollowerSharedInboxUrls = function (this: AccountInstance) {
324 const query: Sequelize.FindOptions<AccountAttributes> = {
325 attributes: [ 'sharedInboxUrl' ],
326 include: [
327 {
e34c85e5 328 model: Account['sequelize'].models.AccountFollow,
350e31d6
C
329 required: true,
330 as: 'followers',
e4f97bab
C
331 where: {
332 targetAccountId: this.id
333 }
334 }
335 ]
336 }
337
338 return Account.findAll(query)
339 .then(accounts => accounts.map(a => a.sharedInboxUrl))
340}
341
342getFollowingUrl = function (this: AccountInstance) {
51548b31 343 return this.url + '/following'
e4f97bab
C
344}
345
346getFollowersUrl = function (this: AccountInstance) {
347 return this.url + '/followers'
348}
349
350getPublicKeyUrl = function (this: AccountInstance) {
351 return this.url + '#main-key'
352}
353
354// ------------------------------ STATICS ------------------------------
355
356listOwned = function () {
357 const query: Sequelize.FindOptions<AccountAttributes> = {
358 where: {
359 podId: null
360 }
361 }
362
363 return Account.findAll(query)
364}
365
7a7724e6
C
366loadApplication = function () {
367 return Account.findOne({
368 include: [
369 {
350e31d6 370 model: Account['sequelize'].models.Application,
7a7724e6
C
371 required: true
372 }
373 ]
374 })
e4f97bab
C
375}
376
377load = function (id: number) {
378 return Account.findById(id)
379}
380
381loadByUUID = function (uuid: string) {
382 const query: Sequelize.FindOptions<AccountAttributes> = {
383 where: {
384 uuid
385 }
386 }
387
388 return Account.findOne(query)
389}
390
350e31d6 391loadLocalByName = function (name: string) {
e4f97bab
C
392 const query: Sequelize.FindOptions<AccountAttributes> = {
393 where: {
394 name,
350e31d6
C
395 [Sequelize.Op.or]: [
396 {
397 userId: {
398 [Sequelize.Op.ne]: null
399 }
400 },
401 {
402 applicationId: {
403 [Sequelize.Op.ne]: null
404 }
405 }
406 ]
407 }
408 }
409
410 return Account.findOne(query)
411}
412
413loadByNameAndHost = function (name: string, host: string) {
414 const query: Sequelize.FindOptions<AccountAttributes> = {
415 where: {
416 name
7a7724e6
C
417 },
418 include: [
419 {
420 model: Account['sequelize'].models.Pod,
350e31d6 421 required: true,
7a7724e6
C
422 where: {
423 host
424 }
425 }
426 ]
e4f97bab
C
427 }
428
429 return Account.findOne(query)
430}
431
ce548a10 432loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
e4f97bab
C
433 const query: Sequelize.FindOptions<AccountAttributes> = {
434 where: {
435 url
ce548a10
C
436 },
437 transaction
e4f97bab
C
438 }
439
440 return Account.findOne(query)
441}
442
443loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
444 const query: Sequelize.FindOptions<AccountAttributes> = {
445 where: {
446 podId,
447 uuid
448 },
449 transaction
450 }
451
452 return Account.find(query)
453}