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 = [link 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 = [link for link in tags if "mediaType" in link and link ["mediaType" ] == "video/mp4" ]
24+
25+ external_video_attributes = {
26+ "ap_id" : payload ["id" ],
27+ "video" : video_source_links [0 ]["href" ],
28+ "title" : payload ["name" ],
29+ "date_added" : payload ["published" ],
30+ "thumbnail" : [icon for icon in payload ["icon" ] if "thumbnails" in icon ["url" ]][0 ]["url" ],
31+ "duration" : int (payload ["duration" ].lstrip ("PT" ).rstrip ("S" )),
32+ "viewcount" : payload ["views" ],
33+ "source_instance" : source_instance ,
34+ }
35+
36+ if (
37+ "language" in payload
38+ and "identifier" in payload ["language" ]
39+ and (identifier := payload ["language" ]["identifier" ])
40+ and identifier in LANG_CHOICES
41+ ):
42+ external_video_attributes ["main_lang" ] = identifier
43+
44+ if "content" in payload and (content := payload ["content" ]):
45+ external_video_attributes ["description" ] = content
46+
47+ external_video , created = ExternalVideo .objects .update_or_create (
48+ ap_id = external_video_attributes ["ap_id" ],
49+ defaults = external_video_attributes ,
50+ )
51+
52+ if created :
53+ logger .info ("ActivityPub external video %s created from %s instance" , external_video , source_instance )
54+ else :
55+ logger .info ("ActivityPub external video %s updated from %s instance" , external_video , source_instance )
56+
57+ return external_video
1358
1459
1560def video_to_ap_video (video ):
@@ -125,7 +170,6 @@ def video_urls(video):
125170 magnets may become fully optional someday
126171 https://framacolibri.org/t/comments-and-suggestions-on-the-peertube-activitypub-implementation/21215/2
127172 """
128-
129173 return {
130174 "url" : (
131175 [
@@ -141,7 +185,11 @@ def video_urls(video):
141185 {
142186 "type" : "Link" ,
143187 "mediaType" : mp4 .encoding_format ,
144- "href" : ap_url (mp4 .source_file .url ),
188+ # "href": ap_url(mp4.source_file.url),
189+ "href" : ap_url (reverse (
190+ "video:video_mp4" ,
191+ kwargs = {"id" : video .id , "mp4_id" : mp4 .id },
192+ )),
145193 "height" : mp4 .height ,
146194 "width" : mp4 .width ,
147195 "size" : mp4 .source_file .size ,
@@ -370,16 +418,16 @@ def video_icon(video):
370418 # only image/jpeg is supported on peertube
371419 # https://github.com/Chocobozzz/PeerTube/blob/b824480af7054a5a49ddb1788c26c769c89ccc8a/server/core/helpers/custom-validators/activitypub/videos.ts#L192
372420 """
373- if not video .thumbnail :
374- return {}
421+ # if not video.thumbnail:
422+ # return {}
375423
376424 return {
377425 "icon" : [
378426 {
379427 "type" : "Image" ,
380- "url" : video .get_thumbnail_url (scheme = True ),
381- "width" : video .thumbnail .file .width ,
382- "height" : video .thumbnail .file .height ,
428+ "url" : video .get_thumbnail_url (scheme = True , is_activity_pub = True ),
429+ "width" : video .thumbnail .file .width if video . thumbnail else 640 ,
430+ "height" : video .thumbnail .file .height if video . thumbnail else 360 ,
383431 # TODO: use the real media type when peertub supports JPEG
384432 # "mediaType": video.thumbnail.file_type,
385433 "mediaType" : "image/jpeg" ,
0 commit comments