diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-29 19:10:13 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-29 19:10:13 +0100 |
commit | c5911fd347c76e8bdc05ea9f3ee9efed4a58c236 (patch) | |
tree | b8d287daca6c45305090cbec9da97d1155f275bd /server/lib | |
parent | 8b0d42ee372de6589796be26b83e5bffb1b69cdf (diff) | |
download | PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.tar.gz PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.tar.zst PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.zip |
Begin to add avatar to actors
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/activitypub/actor.ts | 87 | ||||
-rw-r--r-- | server/lib/activitypub/url.ts | 2 |
2 files changed, 59 insertions, 30 deletions
diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index e590dc72d..e557896e8 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts | |||
@@ -1,16 +1,20 @@ | |||
1 | import * as Bluebird from 'bluebird' | 1 | import * as Bluebird from 'bluebird' |
2 | import { join } from 'path' | ||
2 | import { Transaction } from 'sequelize' | 3 | import { Transaction } from 'sequelize' |
3 | import * as url from 'url' | 4 | import * as url from 'url' |
5 | import * as uuidv4 from 'uuid/v4' | ||
4 | import { ActivityPubActor, ActivityPubActorType } from '../../../shared/models/activitypub' | 6 | import { ActivityPubActor, ActivityPubActorType } from '../../../shared/models/activitypub' |
5 | import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects' | 7 | import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects' |
6 | import { isRemoteActorValid } from '../../helpers/custom-validators/activitypub/actor' | 8 | import { isRemoteActorValid } from '../../helpers/custom-validators/activitypub/actor' |
9 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | ||
7 | import { retryTransactionWrapper } from '../../helpers/database-utils' | 10 | import { retryTransactionWrapper } from '../../helpers/database-utils' |
8 | import { logger } from '../../helpers/logger' | 11 | import { logger } from '../../helpers/logger' |
9 | import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto' | 12 | import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto' |
10 | import { doRequest } from '../../helpers/requests' | 13 | import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' |
11 | import { CONFIG, sequelizeTypescript } from '../../initializers' | 14 | import { CONFIG, sequelizeTypescript } from '../../initializers' |
12 | import { AccountModel } from '../../models/account/account' | 15 | import { AccountModel } from '../../models/account/account' |
13 | import { ActorModel } from '../../models/activitypub/actor' | 16 | import { ActorModel } from '../../models/activitypub/actor' |
17 | import { AvatarModel } from '../../models/avatar/avatar' | ||
14 | import { ServerModel } from '../../models/server/server' | 18 | import { ServerModel } from '../../models/server/server' |
15 | import { VideoChannelModel } from '../../models/video/video-channel' | 19 | import { VideoChannelModel } from '../../models/video/video-channel' |
16 | 20 | ||
@@ -62,6 +66,32 @@ async function getOrCreateActorAndServerAndModel (actorUrl: string, recurseIfNee | |||
62 | return actor | 66 | return actor |
63 | } | 67 | } |
64 | 68 | ||
69 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string, uuid?: string) { | ||
70 | return new ActorModel({ | ||
71 | type, | ||
72 | url, | ||
73 | preferredUsername, | ||
74 | uuid, | ||
75 | publicKey: null, | ||
76 | privateKey: null, | ||
77 | followersCount: 0, | ||
78 | followingCount: 0, | ||
79 | inboxUrl: url + '/inbox', | ||
80 | outboxUrl: url + '/outbox', | ||
81 | sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox', | ||
82 | followersUrl: url + '/followers', | ||
83 | followingUrl: url + '/following' | ||
84 | }) | ||
85 | } | ||
86 | |||
87 | export { | ||
88 | getOrCreateActorAndServerAndModel, | ||
89 | buildActorInstance, | ||
90 | setAsyncActorKeys | ||
91 | } | ||
92 | |||
93 | // --------------------------------------------------------------------------- | ||
94 | |||
65 | function saveActorAndServerAndModelIfNotExist ( | 95 | function saveActorAndServerAndModelIfNotExist ( |
66 | result: FetchRemoteActorResult, | 96 | result: FetchRemoteActorResult, |
67 | ownerActor?: ActorModel, | 97 | ownerActor?: ActorModel, |
@@ -90,6 +120,14 @@ function saveActorAndServerAndModelIfNotExist ( | |||
90 | // Save our new account in database | 120 | // Save our new account in database |
91 | actor.set('serverId', server.id) | 121 | actor.set('serverId', server.id) |
92 | 122 | ||
123 | // Avatar? | ||
124 | if (result.avatarName) { | ||
125 | const avatar = await AvatarModel.create({ | ||
126 | filename: result.avatarName | ||
127 | }, { transaction: t }) | ||
128 | actor.set('avatarId', avatar.id) | ||
129 | } | ||
130 | |||
93 | // Force the actor creation, sometimes Sequelize skips the save() when it thinks the instance already exists | 131 | // Force the actor creation, sometimes Sequelize skips the save() when it thinks the instance already exists |
94 | // (which could be false in a retried query) | 132 | // (which could be false in a retried query) |
95 | const actorCreated = await ActorModel.create(actor.toJSON(), { transaction: t }) | 133 | const actorCreated = await ActorModel.create(actor.toJSON(), { transaction: t }) |
@@ -112,6 +150,7 @@ type FetchRemoteActorResult = { | |||
112 | actor: ActorModel | 150 | actor: ActorModel |
113 | name: string | 151 | name: string |
114 | summary: string | 152 | summary: string |
153 | avatarName?: string | ||
115 | attributedTo: ActivityPubAttributedTo[] | 154 | attributedTo: ActivityPubAttributedTo[] |
116 | } | 155 | } |
117 | async function fetchRemoteActor (actorUrl: string): Promise<FetchRemoteActorResult> { | 156 | async function fetchRemoteActor (actorUrl: string): Promise<FetchRemoteActorResult> { |
@@ -151,43 +190,33 @@ async function fetchRemoteActor (actorUrl: string): Promise<FetchRemoteActorResu | |||
151 | followingUrl: actorJSON.following | 190 | followingUrl: actorJSON.following |
152 | }) | 191 | }) |
153 | 192 | ||
193 | // Fetch icon? | ||
194 | let avatarName: string = undefined | ||
195 | if ( | ||
196 | actorJSON.icon && actorJSON.icon.type === 'Image' && actorJSON.icon.mediaType === 'image/png' && | ||
197 | isActivityPubUrlValid(actorJSON.icon.url) | ||
198 | ) { | ||
199 | const extension = actorJSON.icon.mediaType === 'image/png' ? '.png' : '.jpg' | ||
200 | |||
201 | avatarName = uuidv4() + extension | ||
202 | const destPath = join(CONFIG.STORAGE.AVATARS_DIR, avatarName) | ||
203 | |||
204 | await doRequestAndSaveToFile({ | ||
205 | method: 'GET', | ||
206 | uri: actorJSON.icon.url | ||
207 | }, destPath) | ||
208 | } | ||
209 | |||
154 | const name = actorJSON.name || actorJSON.preferredUsername | 210 | const name = actorJSON.name || actorJSON.preferredUsername |
155 | return { | 211 | return { |
156 | actor, | 212 | actor, |
157 | name, | 213 | name, |
214 | avatarName, | ||
158 | summary: actorJSON.summary, | 215 | summary: actorJSON.summary, |
159 | attributedTo: actorJSON.attributedTo | 216 | attributedTo: actorJSON.attributedTo |
160 | } | 217 | } |
161 | } | 218 | } |
162 | 219 | ||
163 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string, uuid?: string) { | ||
164 | return new ActorModel({ | ||
165 | type, | ||
166 | url, | ||
167 | preferredUsername, | ||
168 | uuid, | ||
169 | publicKey: null, | ||
170 | privateKey: null, | ||
171 | followersCount: 0, | ||
172 | followingCount: 0, | ||
173 | inboxUrl: url + '/inbox', | ||
174 | outboxUrl: url + '/outbox', | ||
175 | sharedInboxUrl: CONFIG.WEBSERVER.URL + '/inbox', | ||
176 | followersUrl: url + '/followers', | ||
177 | followingUrl: url + '/following' | ||
178 | }) | ||
179 | } | ||
180 | |||
181 | export { | ||
182 | getOrCreateActorAndServerAndModel, | ||
183 | saveActorAndServerAndModelIfNotExist, | ||
184 | fetchRemoteActor, | ||
185 | buildActorInstance, | ||
186 | setAsyncActorKeys | ||
187 | } | ||
188 | |||
189 | // --------------------------------------------------------------------------- | ||
190 | |||
191 | async function fetchActorTotalItems (url: string) { | 220 | async function fetchActorTotalItems (url: string) { |
192 | const options = { | 221 | const options = { |
193 | uri: url, | 222 | uri: url, |
diff --git a/server/lib/activitypub/url.ts b/server/lib/activitypub/url.ts index 3d5f0523c..0d76922e0 100644 --- a/server/lib/activitypub/url.ts +++ b/server/lib/activitypub/url.ts | |||
@@ -18,7 +18,7 @@ function getVideoChannelActivityPubUrl (videoChannelUUID: string) { | |||
18 | } | 18 | } |
19 | 19 | ||
20 | function getAccountActivityPubUrl (accountName: string) { | 20 | function getAccountActivityPubUrl (accountName: string) { |
21 | return CONFIG.WEBSERVER.URL + '/account/' + accountName | 21 | return CONFIG.WEBSERVER.URL + '/accounts/' + accountName |
22 | } | 22 | } |
23 | 23 | ||
24 | function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseModel) { | 24 | function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseModel) { |