diff options
Diffstat (limited to 'server/lib/activitypub/process/misc.ts')
-rw-r--r-- | server/lib/activitypub/process/misc.ts | 79 |
1 files changed, 71 insertions, 8 deletions
diff --git a/server/lib/activitypub/process/misc.ts b/server/lib/activitypub/process/misc.ts index a9c6f913c..f65395c99 100644 --- a/server/lib/activitypub/process/misc.ts +++ b/server/lib/activitypub/process/misc.ts | |||
@@ -1,11 +1,15 @@ | |||
1 | import * as magnetUtil from 'magnet-uri' | 1 | import * as magnetUtil from 'magnet-uri' |
2 | import { VideoTorrentObject } from '../../../../shared' | 2 | import { VideoTorrentObject } from '../../../../shared' |
3 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' | ||
3 | import { VideoPrivacy } from '../../../../shared/models/videos' | 4 | import { VideoPrivacy } from '../../../../shared/models/videos' |
4 | import { doRequest } from '../../../helpers' | ||
5 | import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos' | 5 | import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos' |
6 | import { logger } from '../../../helpers/logger' | ||
7 | import { doRequest } from '../../../helpers/requests' | ||
6 | import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers' | 8 | import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers' |
9 | import { ActorModel } from '../../../models/activitypub/actor' | ||
7 | import { VideoModel } from '../../../models/video/video' | 10 | import { VideoModel } from '../../../models/video/video' |
8 | import { VideoChannelModel } from '../../../models/video/video-channel' | 11 | import { VideoChannelModel } from '../../../models/video/video-channel' |
12 | import { VideoCommentModel } from '../../../models/video/video-comment' | ||
9 | import { VideoShareModel } from '../../../models/video/video-share' | 13 | import { VideoShareModel } from '../../../models/video/video-share' |
10 | import { getOrCreateActorAndServerAndModel } from '../actor' | 14 | import { getOrCreateActorAndServerAndModel } from '../actor' |
11 | 15 | ||
@@ -97,14 +101,43 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObje | |||
97 | return attributes | 101 | return attributes |
98 | } | 102 | } |
99 | 103 | ||
100 | async function addVideoShares (instance: VideoModel, shares: string[]) { | 104 | async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) { |
101 | for (const share of shares) { | 105 | let originCommentId: number = null |
106 | let inReplyToCommentId: number = null | ||
107 | |||
108 | // If this is not a reply to the video (thread), create or get the parent comment | ||
109 | if (video.url !== comment.inReplyTo) { | ||
110 | const [ parent ] = await addVideoComment(video, comment.inReplyTo) | ||
111 | if (!parent) { | ||
112 | logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id) | ||
113 | return undefined | ||
114 | } | ||
115 | |||
116 | originCommentId = parent.originCommentId || parent.id | ||
117 | inReplyToCommentId = parent.id | ||
118 | } | ||
119 | |||
120 | return { | ||
121 | url: comment.url, | ||
122 | text: comment.content, | ||
123 | videoId: video.id, | ||
124 | accountId: actor.Account.id, | ||
125 | inReplyToCommentId, | ||
126 | originCommentId, | ||
127 | createdAt: new Date(comment.published), | ||
128 | updatedAt: new Date(comment.updated) | ||
129 | } | ||
130 | } | ||
131 | |||
132 | async function addVideoShares (instance: VideoModel, shareUrls: string[]) { | ||
133 | for (const shareUrl of shareUrls) { | ||
102 | // Fetch url | 134 | // Fetch url |
103 | const json = await doRequest({ | 135 | const { body } = await doRequest({ |
104 | uri: share, | 136 | uri: shareUrl, |
105 | json: true | 137 | json: true, |
138 | activityPub: true | ||
106 | }) | 139 | }) |
107 | const actorUrl = json['actor'] | 140 | const actorUrl = body.actor |
108 | if (!actorUrl) continue | 141 | if (!actorUrl) continue |
109 | 142 | ||
110 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | 143 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) |
@@ -121,10 +154,40 @@ async function addVideoShares (instance: VideoModel, shares: string[]) { | |||
121 | } | 154 | } |
122 | } | 155 | } |
123 | 156 | ||
157 | async function addVideoComments (instance: VideoModel, commentUrls: string[]) { | ||
158 | for (const commentUrl of commentUrls) { | ||
159 | await addVideoComment(instance, commentUrl) | ||
160 | } | ||
161 | } | ||
162 | |||
163 | async function addVideoComment (instance: VideoModel, commentUrl: string) { | ||
164 | // Fetch url | ||
165 | const { body } = await doRequest({ | ||
166 | uri: commentUrl, | ||
167 | json: true, | ||
168 | activityPub: true | ||
169 | }) | ||
170 | |||
171 | const actorUrl = body.attributedTo | ||
172 | if (!actorUrl) return [] | ||
173 | |||
174 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | ||
175 | const entry = await videoCommentActivityObjectToDBAttributes(instance, actor, body) | ||
176 | if (!entry) return [] | ||
177 | |||
178 | return VideoCommentModel.findOrCreate({ | ||
179 | where: { | ||
180 | url: body.id | ||
181 | }, | ||
182 | defaults: entry | ||
183 | }) | ||
184 | } | ||
185 | |||
124 | // --------------------------------------------------------------------------- | 186 | // --------------------------------------------------------------------------- |
125 | 187 | ||
126 | export { | 188 | export { |
127 | videoFileActivityUrlToDBAttributes, | 189 | videoFileActivityUrlToDBAttributes, |
128 | videoActivityObjectToDBAttributes, | 190 | videoActivityObjectToDBAttributes, |
129 | addVideoShares | 191 | addVideoShares, |
192 | addVideoComments | ||
130 | } | 193 | } |