]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/activitypub/actor.ts
Fix banner error in actor json
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
index e547d2c0c19b00cfd932d318bf4e492675c96e64..19f3f7e04ef67279d33e5b66fb78284552302eb6 100644 (file)
@@ -1,5 +1,6 @@
 import { values } from 'lodash'
 import { extname } from 'path'
+import { literal, Op, Transaction } from 'sequelize'
 import {
   AllowNull,
   BelongsTo,
@@ -16,8 +17,9 @@ import {
   Table,
   UpdatedAt
 } from 'sequelize-typescript'
+import { ModelCache } from '@server/models/model-cache'
 import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub'
-import { Avatar } from '../../../shared/models/avatars/avatar.model'
+import { ActorImage } from '../../../shared/models/actors/actor-image.model'
 import { activityPubContextify } from '../../helpers/activitypub'
 import {
   isActorFollowersCountValid,
@@ -27,28 +29,34 @@ import {
   isActorPublicKeyValid
 } from '../../helpers/custom-validators/activitypub/actor'
 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
-import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
-import { AccountModel } from '../account/account'
-import { AvatarModel } from '../avatar/avatar'
-import { ServerModel } from '../server/server'
-import { isOutdated, throwIfNotValid } from '../utils'
-import { VideoChannelModel } from '../video/video-channel'
-import { ActorFollowModel } from './actor-follow'
-import { VideoModel } from '../video/video'
+import {
+  ACTIVITY_PUB,
+  ACTIVITY_PUB_ACTOR_TYPES,
+  CONSTRAINTS_FIELDS,
+  MIMETYPES,
+  SERVER_ACTOR_NAME,
+  WEBSERVER
+} from '../../initializers/constants'
 import {
   MActor,
   MActorAccountChannelId,
-  MActorAP,
+  MActorAPAccount,
+  MActorAPChannel,
   MActorFormattable,
   MActorFull,
   MActorHost,
   MActorServer,
-  MActorSummaryFormattable, MActorUrl,
+  MActorSummaryFormattable,
+  MActorUrl,
   MActorWithInboxes
-} from '../../typings/models'
-import * as Bluebird from 'bluebird'
-import { Op, Transaction, literal } from 'sequelize'
-import { ModelCache } from '@server/models/model-cache'
+} from '../../types/models'
+import { AccountModel } from '../account/account'
+import { ActorImageModel } from '../account/actor-image'
+import { ServerModel } from '../server/server'
+import { isOutdated, throwIfNotValid } from '../utils'
+import { VideoModel } from '../video/video'
+import { VideoChannelModel } from '../video/video-channel'
+import { ActorFollowModel } from './actor-follow'
 
 enum ScopeNames {
   FULL = 'FULL'
@@ -62,7 +70,6 @@ export const unusedActorAttributesForAPI = [
   'sharedInboxUrl',
   'followersUrl',
   'followingUrl',
-  'url',
   'createdAt',
   'updatedAt'
 ]
@@ -74,7 +81,8 @@ export const unusedActorAttributesForAPI = [
       required: false
     },
     {
-      model: AvatarModel,
+      model: ActorImageModel,
+      as: 'Avatar',
       required: false
     }
   ]
@@ -101,7 +109,13 @@ export const unusedActorAttributesForAPI = [
         required: false
       },
       {
-        model: AvatarModel,
+        model: ActorImageModel,
+        as: 'Avatar',
+        required: false
+      },
+      {
+        model: ActorImageModel,
+        as: 'Banner',
         required: false
       }
     ]
@@ -123,13 +137,13 @@ export const unusedActorAttributesForAPI = [
         }
       }
     },
-    // {
-    //   fields: [ 'preferredUsername' ],
-    //   unique: true,
-    //   where: {
-    //     serverId: null
-    //   }
-    // },
+    {
+      fields: [ 'preferredUsername' ],
+      unique: true,
+      where: {
+        serverId: null
+      }
+    },
     {
       fields: [ 'inboxUrl', 'sharedInboxUrl' ]
     },
@@ -147,7 +161,7 @@ export const unusedActorAttributesForAPI = [
     }
   ]
 })
-export class ActorModel extends Model<ActorModel> {
+export class ActorModel extends Model {
 
   @AllowNull(false)
   @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
@@ -214,18 +228,35 @@ export class ActorModel extends Model<ActorModel> {
   @UpdatedAt
   updatedAt: Date
 
-  @ForeignKey(() => AvatarModel)
+  @ForeignKey(() => ActorImageModel)
   @Column
   avatarId: number
 
-  @BelongsTo(() => AvatarModel, {
+  @ForeignKey(() => ActorImageModel)
+  @Column
+  bannerId: number
+
+  @BelongsTo(() => ActorImageModel, {
+    foreignKey: {
+      name: 'avatarId',
+      allowNull: true
+    },
+    as: 'Avatar',
+    onDelete: 'set null',
+    hooks: true
+  })
+  Avatar: ActorImageModel
+
+  @BelongsTo(() => ActorImageModel, {
     foreignKey: {
+      name: 'bannerId',
       allowNull: true
     },
+    as: 'Banner',
     onDelete: 'set null',
     hooks: true
   })
-  Avatar: AvatarModel
+  Banner: ActorImageModel
 
   @HasMany(() => ActorFollowModel, {
     foreignKey: {
@@ -277,15 +308,15 @@ export class ActorModel extends Model<ActorModel> {
   })
   VideoChannel: VideoChannelModel
 
-  static load (id: number): Bluebird<MActor> {
+  static load (id: number): Promise<MActor> {
     return ActorModel.unscoped().findByPk(id)
   }
 
-  static loadFull (id: number): Bluebird<MActorFull> {
+  static loadFull (id: number): Promise<MActorFull> {
     return ActorModel.scope(ScopeNames.FULL).findByPk(id)
   }
 
-  static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird<MActor> {
+  static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Promise<MActor> {
     const query = {
       include: [
         {
@@ -329,7 +360,7 @@ export class ActorModel extends Model<ActorModel> {
       .then(a => !!a)
   }
 
-  static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird<MActorFull[]> {
+  static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise<MActorFull[]> {
     const query = {
       where: {
         followersUrl: {
@@ -342,7 +373,7 @@ export class ActorModel extends Model<ActorModel> {
     return ActorModel.scope(ScopeNames.FULL).findAll(query)
   }
 
-  static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
+  static loadLocalByName (preferredUsername: string, transaction?: Transaction): Promise<MActorFull> {
     const fun = () => {
       const query = {
         where: {
@@ -365,7 +396,7 @@ export class ActorModel extends Model<ActorModel> {
     })
   }
 
-  static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> {
+  static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Promise<MActorUrl> {
     const fun = () => {
       const query = {
         attributes: [ 'url' ],
@@ -389,7 +420,7 @@ export class ActorModel extends Model<ActorModel> {
     })
   }
 
-  static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
+  static loadByNameAndHost (preferredUsername: string, host: string): Promise<MActorFull> {
     const query = {
       where: {
         preferredUsername
@@ -408,7 +439,7 @@ export class ActorModel extends Model<ActorModel> {
     return ActorModel.scope(ScopeNames.FULL).findOne(query)
   }
 
-  static loadByUrl (url: string, transaction?: Transaction): Bluebird<MActorAccountChannelId> {
+  static loadByUrl (url: string, transaction?: Transaction): Promise<MActorAccountChannelId> {
     const query = {
       where: {
         url
@@ -431,7 +462,7 @@ export class ActorModel extends Model<ActorModel> {
     return ActorModel.unscoped().findOne(query)
   }
 
-  static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird<MActorFull> {
+  static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Promise<MActorFull> {
     const query = {
       where: {
         url
@@ -462,7 +493,7 @@ export class ActorModel extends Model<ActorModel> {
     }, { where, transaction })
   }
 
-  static loadAccountActorByVideoId (videoId: number): Bluebird<MActor> {
+  static loadAccountActorByVideoId (videoId: number): Promise<MActor> {
     const query = {
       include: [
         {
@@ -497,7 +528,7 @@ export class ActorModel extends Model<ActorModel> {
   }
 
   toFormattedSummaryJSON (this: MActorSummaryFormattable) {
-    let avatar: Avatar = null
+    let avatar: ActorImage = null
     if (this.Avatar) {
       avatar = this.Avatar.toFormattedJSON()
     }
@@ -513,29 +544,51 @@ export class ActorModel extends Model<ActorModel> {
   toFormattedJSON (this: MActorFormattable) {
     const base = this.toFormattedSummaryJSON()
 
+    let banner: ActorImage = null
+    if (this.Banner) {
+      banner = this.Banner.toFormattedJSON()
+    }
+
     return Object.assign(base, {
       id: this.id,
       hostRedundancyAllowed: this.getRedundancyAllowed(),
       followingCount: this.followingCount,
       followersCount: this.followersCount,
+      banner,
       createdAt: this.createdAt,
       updatedAt: this.updatedAt
     })
   }
 
-  toActivityPubObject (this: MActorAP, name: string) {
+  toActivityPubObject (this: MActorAPChannel | MActorAPAccount, name: string) {
     let icon: ActivityIconObject
+    let image: ActivityIconObject
 
     if (this.avatarId) {
       const extension = extname(this.Avatar.filename)
 
       icon = {
         type: 'Image',
-        mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
+        mediaType: MIMETYPES.IMAGE.EXT_MIMETYPE[extension],
+        height: this.Avatar.height,
+        width: this.Avatar.width,
         url: this.getAvatarUrl()
       }
     }
 
+    if (this.bannerId) {
+      const banner = (this as MActorAPChannel).Banner
+      const extension = extname(banner.filename)
+
+      image = {
+        type: 'Image',
+        mediaType: MIMETYPES.IMAGE.EXT_MIMETYPE[extension],
+        height: banner.height,
+        width: banner.width,
+        url: this.getBannerUrl()
+      }
+    }
+
     const json = {
       type: this.type,
       id: this.url,
@@ -555,7 +608,8 @@ export class ActorModel extends Model<ActorModel> {
         owner: this.url,
         publicKeyPem: this.publicKey
       },
-      icon
+      icon,
+      image
     }
 
     return activityPubContextify(json)
@@ -625,6 +679,12 @@ export class ActorModel extends Model<ActorModel> {
     return WEBSERVER.URL + this.Avatar.getStaticPath()
   }
 
+  getBannerUrl () {
+    if (!this.bannerId) return undefined
+
+    return WEBSERVER.URL + this.Banner.getStaticPath()
+  }
+
   isOutdated () {
     if (this.isOwned()) return false