diff options
author | Chocobozzz <me@florianbigard.com> | 2018-01-29 10:52:19 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-01-29 10:52:19 +0100 |
commit | 46531a0abdd5c860a1a8cdb4b636b9c55bfb115b (patch) | |
tree | 9cc1aaa565985c04b12bf985e9c6f21aebf42ff3 | |
parent | c88593f72f1097cc34a091b4cb4b6d6e214d1e9f (diff) | |
download | PeerTube-46531a0abdd5c860a1a8cdb4b636b9c55bfb115b.tar.gz PeerTube-46531a0abdd5c860a1a8cdb4b636b9c55bfb115b.tar.zst PeerTube-46531a0abdd5c860a1a8cdb4b636b9c55bfb115b.zip |
Add id to likes/dislikes/comments/shares collections
-rw-r--r-- | server/controllers/activitypub/client.ts | 56 | ||||
-rw-r--r-- | server/helpers/activitypub.ts | 3 | ||||
-rw-r--r-- | server/lib/activitypub/url.ts | 22 | ||||
-rw-r--r-- | server/models/video/video.ts | 65 |
4 files changed, 128 insertions, 18 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 7b60cc311..55bd85c48 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts | |||
@@ -36,10 +36,26 @@ activityPubClientRouter.get('/videos/watch/:id', | |||
36 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | 36 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), |
37 | executeIfActivityPub(asyncMiddleware(videoController)) | 37 | executeIfActivityPub(asyncMiddleware(videoController)) |
38 | ) | 38 | ) |
39 | activityPubClientRouter.get('/videos/watch/:id/announces', | ||
40 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | ||
41 | executeIfActivityPub(asyncMiddleware(videoAnnouncesController)) | ||
42 | ) | ||
39 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', | 43 | activityPubClientRouter.get('/videos/watch/:id/announces/:accountId', |
40 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), | 44 | executeIfActivityPub(asyncMiddleware(videosShareValidator)), |
41 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) | 45 | executeIfActivityPub(asyncMiddleware(videoAnnounceController)) |
42 | ) | 46 | ) |
47 | activityPubClientRouter.get('/videos/watch/:id/likes', | ||
48 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | ||
49 | executeIfActivityPub(asyncMiddleware(videoLikesController)) | ||
50 | ) | ||
51 | activityPubClientRouter.get('/videos/watch/:id/dislikes', | ||
52 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | ||
53 | executeIfActivityPub(asyncMiddleware(videoDislikesController)) | ||
54 | ) | ||
55 | activityPubClientRouter.get('/videos/watch/:id/comments', | ||
56 | executeIfActivityPub(asyncMiddleware(videosGetValidator)), | ||
57 | executeIfActivityPub(asyncMiddleware(videoCommentsController)) | ||
58 | ) | ||
43 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', | 59 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', |
44 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), | 60 | executeIfActivityPub(asyncMiddleware(videoCommentGetValidator)), |
45 | executeIfActivityPub(asyncMiddleware(videoCommentController)) | 61 | executeIfActivityPub(asyncMiddleware(videoCommentController)) |
@@ -105,6 +121,46 @@ async function videoAnnounceController (req: express.Request, res: express.Respo | |||
105 | return res.json(activityPubContextify(object)) | 121 | return res.json(activityPubContextify(object)) |
106 | } | 122 | } |
107 | 123 | ||
124 | async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
125 | const video: VideoModel = res.locals.video | ||
126 | |||
127 | // We need more attributes | ||
128 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | ||
129 | const object = videoAll.toAnnouncesActivityPubObject() | ||
130 | |||
131 | return res.json(activityPubContextify(object)) | ||
132 | } | ||
133 | |||
134 | async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
135 | const video: VideoModel = res.locals.video | ||
136 | |||
137 | // We need more attributes | ||
138 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | ||
139 | const { likesObject } = videoAll.toRatesActivityPubObjects() | ||
140 | |||
141 | return res.json(activityPubContextify(likesObject)) | ||
142 | } | ||
143 | |||
144 | async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
145 | const video: VideoModel = res.locals.video | ||
146 | |||
147 | // We need more attributes | ||
148 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | ||
149 | const { dislikesObject } = videoAll.toRatesActivityPubObjects() | ||
150 | |||
151 | return res.json(activityPubContextify(dislikesObject)) | ||
152 | } | ||
153 | |||
154 | async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
155 | const video: VideoModel = res.locals.video | ||
156 | |||
157 | // We need more attributes | ||
158 | const videoAll = await VideoModel.loadAndPopulateAll(video.id) | ||
159 | const commentsObject = videoAll.toCommentsActivityPubObject() | ||
160 | |||
161 | return res.json(activityPubContextify(commentsObject)) | ||
162 | } | ||
163 | |||
108 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { | 164 | async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { |
109 | const videoChannel: VideoChannelModel = res.locals.videoChannel | 165 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
110 | 166 | ||
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index 97115680c..aa5485850 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts | |||
@@ -25,8 +25,9 @@ function activityPubContextify <T> (data: T) { | |||
25 | }) | 25 | }) |
26 | } | 26 | } |
27 | 27 | ||
28 | function activityPubCollection (results: any[]) { | 28 | function activityPubCollection (url: string, results: any[]) { |
29 | return { | 29 | return { |
30 | id: url, | ||
30 | type: 'OrderedCollection', | 31 | type: 'OrderedCollection', |
31 | totalItems: results.length, | 32 | totalItems: results.length, |
32 | orderedItems: results | 33 | orderedItems: results |
diff --git a/server/lib/activitypub/url.ts b/server/lib/activitypub/url.ts index 5705afbd6..ba3bf688d 100644 --- a/server/lib/activitypub/url.ts +++ b/server/lib/activitypub/url.ts | |||
@@ -37,6 +37,22 @@ function getVideoDislikeActivityPubUrl (byActor: ActorModel, video: VideoModel) | |||
37 | return byActor.url + '/dislikes/' + video.id | 37 | return byActor.url + '/dislikes/' + video.id |
38 | } | 38 | } |
39 | 39 | ||
40 | function getVideoSharesActivityPubUrl (video: VideoModel) { | ||
41 | return video.url + '/announces' | ||
42 | } | ||
43 | |||
44 | function getVideoCommentsActivityPubUrl (video: VideoModel) { | ||
45 | return video.url + '/comments' | ||
46 | } | ||
47 | |||
48 | function getVideoLikesActivityPubUrl (video: VideoModel) { | ||
49 | return video.url + '/likes' | ||
50 | } | ||
51 | |||
52 | function getVideoDislikesActivityPubUrl (video: VideoModel) { | ||
53 | return video.url + '/dislikes' | ||
54 | } | ||
55 | |||
40 | function getActorFollowActivityPubUrl (actorFollow: ActorFollowModel) { | 56 | function getActorFollowActivityPubUrl (actorFollow: ActorFollowModel) { |
41 | const me = actorFollow.ActorFollower | 57 | const me = actorFollow.ActorFollower |
42 | const following = actorFollow.ActorFollowing | 58 | const following = actorFollow.ActorFollowing |
@@ -81,5 +97,9 @@ export { | |||
81 | getVideoLikeActivityPubUrl, | 97 | getVideoLikeActivityPubUrl, |
82 | getVideoDislikeActivityPubUrl, | 98 | getVideoDislikeActivityPubUrl, |
83 | getVideoCommentActivityPubUrl, | 99 | getVideoCommentActivityPubUrl, |
84 | getDeleteActivityPubUrl | 100 | getDeleteActivityPubUrl, |
101 | getVideoSharesActivityPubUrl, | ||
102 | getVideoCommentsActivityPubUrl, | ||
103 | getVideoLikesActivityPubUrl, | ||
104 | getVideoDislikesActivityPubUrl | ||
85 | } | 105 | } |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 8e1acdd44..4e572a16b 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -58,6 +58,12 @@ import { | |||
58 | VIDEO_LICENCES, | 58 | VIDEO_LICENCES, |
59 | VIDEO_PRIVACIES | 59 | VIDEO_PRIVACIES |
60 | } from '../../initializers' | 60 | } from '../../initializers' |
61 | import { | ||
62 | getVideoCommentsActivityPubUrl, | ||
63 | getVideoDislikesActivityPubUrl, | ||
64 | getVideoLikesActivityPubUrl, | ||
65 | getVideoSharesActivityPubUrl | ||
66 | } from '../../lib/activitypub' | ||
61 | import { sendDeleteVideo } from '../../lib/activitypub/send' | 67 | import { sendDeleteVideo } from '../../lib/activitypub/send' |
62 | import { AccountModel } from '../account/account' | 68 | import { AccountModel } from '../account/account' |
63 | import { AccountVideoRateModel } from '../account/account-video-rate' | 69 | import { AccountVideoRateModel } from '../account/account-video-rate' |
@@ -958,30 +964,19 @@ export class VideoModel extends Model<VideoModel> { | |||
958 | } | 964 | } |
959 | } | 965 | } |
960 | 966 | ||
961 | likesObject = activityPubCollection(likes) | 967 | const res = this.toRatesActivityPubObjects() |
962 | dislikesObject = activityPubCollection(dislikes) | 968 | likesObject = res.likesObject |
969 | dislikesObject = res.dislikesObject | ||
963 | } | 970 | } |
964 | 971 | ||
965 | let sharesObject | 972 | let sharesObject |
966 | if (Array.isArray(this.VideoShares)) { | 973 | if (Array.isArray(this.VideoShares)) { |
967 | const shares: string[] = [] | 974 | sharesObject = this.toAnnouncesActivityPubObject() |
968 | |||
969 | for (const videoShare of this.VideoShares) { | ||
970 | shares.push(videoShare.url) | ||
971 | } | ||
972 | |||
973 | sharesObject = activityPubCollection(shares) | ||
974 | } | 975 | } |
975 | 976 | ||
976 | let commentsObject | 977 | let commentsObject |
977 | if (Array.isArray(this.VideoComments)) { | 978 | if (Array.isArray(this.VideoComments)) { |
978 | const comments: string[] = [] | 979 | commentsObject = this.toCommentsActivityPubObject() |
979 | |||
980 | for (const videoComment of this.VideoComments) { | ||
981 | comments.push(videoComment.url) | ||
982 | } | ||
983 | |||
984 | commentsObject = activityPubCollection(comments) | ||
985 | } | 980 | } |
986 | 981 | ||
987 | const url = [] | 982 | const url = [] |
@@ -1058,6 +1053,44 @@ export class VideoModel extends Model<VideoModel> { | |||
1058 | } | 1053 | } |
1059 | } | 1054 | } |
1060 | 1055 | ||
1056 | toAnnouncesActivityPubObject () { | ||
1057 | const shares: string[] = [] | ||
1058 | |||
1059 | for (const videoShare of this.VideoShares) { | ||
1060 | shares.push(videoShare.url) | ||
1061 | } | ||
1062 | |||
1063 | return activityPubCollection(getVideoSharesActivityPubUrl(this), shares) | ||
1064 | } | ||
1065 | |||
1066 | toCommentsActivityPubObject () { | ||
1067 | const comments: string[] = [] | ||
1068 | |||
1069 | for (const videoComment of this.VideoComments) { | ||
1070 | comments.push(videoComment.url) | ||
1071 | } | ||
1072 | |||
1073 | return activityPubCollection(getVideoCommentsActivityPubUrl(this), comments) | ||
1074 | } | ||
1075 | |||
1076 | toRatesActivityPubObjects () { | ||
1077 | const likes: string[] = [] | ||
1078 | const dislikes: string[] = [] | ||
1079 | |||
1080 | for (const rate of this.AccountVideoRates) { | ||
1081 | if (rate.type === 'like') { | ||
1082 | likes.push(rate.Account.Actor.url) | ||
1083 | } else if (rate.type === 'dislike') { | ||
1084 | dislikes.push(rate.Account.Actor.url) | ||
1085 | } | ||
1086 | } | ||
1087 | |||
1088 | const likesObject = activityPubCollection(getVideoLikesActivityPubUrl(this), likes) | ||
1089 | const dislikesObject = activityPubCollection(getVideoDislikesActivityPubUrl(this), dislikes) | ||
1090 | |||
1091 | return { likesObject, dislikesObject } | ||
1092 | } | ||
1093 | |||
1061 | getTruncatedDescription () { | 1094 | getTruncatedDescription () { |
1062 | if (!this.description) return null | 1095 | if (!this.description) return null |
1063 | 1096 | ||