aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/misc.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-28 11:16:08 +0100
committerChocobozzz <me@florianbigard.com>2017-12-28 11:16:08 +0100
commitda854ddd502cd70685ef779c673b9e63757b8aa0 (patch)
tree21501d170cceaa044a5f23449cbd2eb47fd6415d /server/lib/activitypub/process/misc.ts
parentf40bbe3146553ef45515ee6b6d93ce6028f045ca (diff)
downloadPeerTube-da854ddd502cd70685ef779c673b9e63757b8aa0.tar.gz
PeerTube-da854ddd502cd70685ef779c673b9e63757b8aa0.tar.zst
PeerTube-da854ddd502cd70685ef779c673b9e63757b8aa0.zip
Propagate old comment on new follow
Diffstat (limited to 'server/lib/activitypub/process/misc.ts')
-rw-r--r--server/lib/activitypub/process/misc.ts79
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 @@
1import * as magnetUtil from 'magnet-uri' 1import * as magnetUtil from 'magnet-uri'
2import { VideoTorrentObject } from '../../../../shared' 2import { VideoTorrentObject } from '../../../../shared'
3import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
3import { VideoPrivacy } from '../../../../shared/models/videos' 4import { VideoPrivacy } from '../../../../shared/models/videos'
4import { doRequest } from '../../../helpers'
5import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos' 5import { isVideoFileInfoHashValid } from '../../../helpers/custom-validators/videos'
6import { logger } from '../../../helpers/logger'
7import { doRequest } from '../../../helpers/requests'
6import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers' 8import { ACTIVITY_PUB, VIDEO_MIMETYPE_EXT } from '../../../initializers'
9import { ActorModel } from '../../../models/activitypub/actor'
7import { VideoModel } from '../../../models/video/video' 10import { VideoModel } from '../../../models/video/video'
8import { VideoChannelModel } from '../../../models/video/video-channel' 11import { VideoChannelModel } from '../../../models/video/video-channel'
12import { VideoCommentModel } from '../../../models/video/video-comment'
9import { VideoShareModel } from '../../../models/video/video-share' 13import { VideoShareModel } from '../../../models/video/video-share'
10import { getOrCreateActorAndServerAndModel } from '../actor' 14import { getOrCreateActorAndServerAndModel } from '../actor'
11 15
@@ -97,14 +101,43 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObje
97 return attributes 101 return attributes
98} 102}
99 103
100async function addVideoShares (instance: VideoModel, shares: string[]) { 104async 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
132async 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
157async function addVideoComments (instance: VideoModel, commentUrls: string[]) {
158 for (const commentUrl of commentUrls) {
159 await addVideoComment(instance, commentUrl)
160 }
161}
162
163async 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
126export { 188export {
127 videoFileActivityUrlToDBAttributes, 189 videoFileActivityUrlToDBAttributes,
128 videoActivityObjectToDBAttributes, 190 videoActivityObjectToDBAttributes,
129 addVideoShares 191 addVideoShares,
192 addVideoComments
130} 193}