55from pod .activitypub .constants import AP_LICENSE_MAPPING
66from pod .activitypub .models import ExternalVideo
77from pod .activitypub .utils import ap_url , make_magnet_url , stable_uuid
8+ from pod .video .models import LANG_CHOICES
89
10+ import logging
11+ logger = logging .getLogger (__name__ )
912
10- def ap_video_to_external_video (payload ):
13+
14+ def ap_video_to_external_video (payload , source_instance ):
1115 """Create an ExternalVideo object from an AP Video payload."""
12- return ExternalVideo .objects .create ()
16+
17+ video_source_links = [{"type" : link ["mediaType" ], "src" : link ["href" ], "size" : link ["size" ], "width" : link ["width" ], "height" : link ["height" ]} for link in payload ["url" ] if "mediaType" in link and link ["mediaType" ] == "video/mp4" ]
18+ if not video_source_links :
19+ tags = []
20+ for link in payload ["url" ]:
21+ if "tag" in link :
22+ tags .extend (link ["tag" ])
23+ video_source_links = [
24+ {"type" : link ["mediaType" ], "src" : link ["href" ], "size" : link ["size" ], "width" : link ["width" ], "height" : link ["height" ]} for link in tags if "mediaType" in link and link ["mediaType" ] == "video/mp4"
25+ ]
26+
27+ external_video_attributes = {
28+ "ap_id" : payload ["id" ],
29+ "video" : video_source_links [0 ]["src" ],
30+ "videos" : video_source_links ,
31+ "title" : payload ["name" ],
32+ "date_added" : payload ["published" ],
33+ "thumbnail" : [icon for icon in payload ["icon" ] if "thumbnails" in icon ["url" ]][0 ]["url" ],
34+ "duration" : int (payload ["duration" ].lstrip ("PT" ).rstrip ("S" )),
35+ "viewcount" : payload ["views" ],
36+ "source_instance" : source_instance ,
37+ }
38+
39+ if (
40+ "language" in payload
41+ and "identifier" in payload ["language" ]
42+ and (identifier := payload ["language" ]["identifier" ])
43+ and identifier in LANG_CHOICES
44+ ):
45+ external_video_attributes ["main_lang" ] = identifier
46+
47+ if "content" in payload and (content := payload ["content" ]):
48+ external_video_attributes ["description" ] = content
49+
50+ external_video , created = ExternalVideo .objects .update_or_create (
51+ ap_id = external_video_attributes ["ap_id" ],
52+ defaults = external_video_attributes ,
53+ )
54+
55+ if created :
56+ logger .info ("ActivityPub external video %s created from %s instance" , external_video , source_instance )
57+ else :
58+ logger .info ("ActivityPub external video %s updated from %s instance" , external_video , source_instance )
59+
60+ return external_video
1361
1462
1563def video_to_ap_video (video ):
@@ -125,7 +173,6 @@ def video_urls(video):
125173 magnets may become fully optional someday
126174 https://framacolibri.org/t/comments-and-suggestions-on-the-peertube-activitypub-implementation/21215/2
127175 """
128-
129176 return {
130177 "url" : (
131178 [
@@ -141,7 +188,11 @@ def video_urls(video):
141188 {
142189 "type" : "Link" ,
143190 "mediaType" : mp4 .encoding_format ,
144- "href" : ap_url (mp4 .source_file .url ),
191+ # "href": ap_url(mp4.source_file.url),
192+ "href" : ap_url (reverse (
193+ "video:video_mp4" ,
194+ kwargs = {"id" : video .id , "mp4_id" : mp4 .id },
195+ )),
145196 "height" : mp4 .height ,
146197 "width" : mp4 .width ,
147198 "size" : mp4 .source_file .size ,
@@ -370,16 +421,16 @@ def video_icon(video):
370421 # only image/jpeg is supported on peertube
371422 # https://github.com/Chocobozzz/PeerTube/blob/b824480af7054a5a49ddb1788c26c769c89ccc8a/server/core/helpers/custom-validators/activitypub/videos.ts#L192
372423 """
373- if not video .thumbnail :
374- return {}
424+ # if not video.thumbnail:
425+ # return {}
375426
376427 return {
377428 "icon" : [
378429 {
379430 "type" : "Image" ,
380- "url" : video .get_thumbnail_url (scheme = True ),
381- "width" : video .thumbnail .file .width ,
382- "height" : video .thumbnail .file .height ,
431+ "url" : video .get_thumbnail_url (scheme = True , is_activity_pub = True ),
432+ "width" : video .thumbnail .file .width if video . thumbnail else 640 ,
433+ "height" : video .thumbnail .file .height if video . thumbnail else 360 ,
383434 # TODO: use the real media type when peertub supports JPEG
384435 # "mediaType": video.thumbnail.file_type,
385436 "mediaType" : "image/jpeg" ,
0 commit comments