From c0aea222e0f469bff8a19d3f3e25c23eb8c09821 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 25 Apr 2024 14:54:04 -0700 Subject: [PATCH] Revert "Cache videos" This reverts commit 26d2627a1c49f62565de53e7ddf015162bb77e62. --- damus.xcodeproj/project.pbxproj | 8 -- damus/ContentView.swift | 1 - damus/Views/Video/DamusVideoPlayer.swift | 2 +- .../Video/DamusVideoPlayerViewModel.swift | 6 - damusTests/VideoCacheTests.swift | 112 ------------------ 5 files changed, 1 insertion(+), 128 deletions(-) delete mode 100644 damusTests/VideoCacheTests.swift diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj index cb96aa93..b9b130ac 100644 --- a/damus.xcodeproj/project.pbxproj +++ b/damus.xcodeproj/project.pbxproj @@ -488,7 +488,6 @@ D76556D62B1E6C08001B0CCC /* DamusPurpleWelcomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D76556D52B1E6C08001B0CCC /* DamusPurpleWelcomeView.swift */; }; D76874F32AE3632B00FB0F68 /* ProfileZapLinkView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D76874F22AE3632B00FB0F68 /* ProfileZapLinkView.swift */; }; D77BFA0B2AE3051200621634 /* ProfileActionSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D77BFA0A2AE3051200621634 /* ProfileActionSheetView.swift */; }; - D7831AF82BBE11E2005DA780 /* VideoCacheTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7831AF72BBE11E2005DA780 /* VideoCacheTests.swift */; }; D783A63F2AD4E53D00658DDA /* SuggestedHashtagsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D783A63E2AD4E53D00658DDA /* SuggestedHashtagsView.swift */; }; D78525252A7B2EA4002FA637 /* NoteContentViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D78525242A7B2EA4002FA637 /* NoteContentViewTests.swift */; }; D7870BC12AC4750B0080BA88 /* MentionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7870BC02AC4750B0080BA88 /* MentionView.swift */; }; @@ -519,7 +518,6 @@ D7ADD3DE2B53854300F104C4 /* DamusPurpleURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7ADD3DD2B53854300F104C4 /* DamusPurpleURL.swift */; }; D7ADD3E02B538D4200F104C4 /* DamusPurpleURLSheetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7ADD3DF2B538D4200F104C4 /* DamusPurpleURLSheetView.swift */; }; D7ADD3E22B538E3500F104C4 /* DamusPurpleVerifyNpubView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7ADD3E12B538E3500F104C4 /* DamusPurpleVerifyNpubView.swift */; }; - D7C28E3B2BBB4D0000EE459F /* VideoCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C28E3A2BBB4D0000EE459F /* VideoCache.swift */; }; D7C6787E2B2D34CC00BCEAFB /* NIP98AuthenticatedRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C6787D2B2D34CC00BCEAFB /* NIP98AuthenticatedRequest.swift */; }; D7CB5D3B2B112FBB00AD4105 /* NotificationFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D70A3B162B02DCE5008BD568 /* NotificationFormatter.swift */; }; D7CB5D3C2B1130C600AD4105 /* LocalNotification.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDA128B29EB19C40006FA5A /* LocalNotification.swift */; }; @@ -1403,7 +1401,6 @@ D76556D52B1E6C08001B0CCC /* DamusPurpleWelcomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DamusPurpleWelcomeView.swift; sourceTree = ""; }; D76874F22AE3632B00FB0F68 /* ProfileZapLinkView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileZapLinkView.swift; sourceTree = ""; }; D77BFA0A2AE3051200621634 /* ProfileActionSheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileActionSheetView.swift; sourceTree = ""; }; - D7831AF72BBE11E2005DA780 /* VideoCacheTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoCacheTests.swift; sourceTree = ""; }; D783A63E2AD4E53D00658DDA /* SuggestedHashtagsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuggestedHashtagsView.swift; sourceTree = ""; }; D78525242A7B2EA4002FA637 /* NoteContentViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteContentViewTests.swift; sourceTree = ""; }; D7870BC02AC4750B0080BA88 /* MentionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MentionView.swift; sourceTree = ""; }; @@ -1419,7 +1416,6 @@ D7ADD3DD2B53854300F104C4 /* DamusPurpleURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DamusPurpleURL.swift; sourceTree = ""; }; D7ADD3DF2B538D4200F104C4 /* DamusPurpleURLSheetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DamusPurpleURLSheetView.swift; sourceTree = ""; }; D7ADD3E12B538E3500F104C4 /* DamusPurpleVerifyNpubView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DamusPurpleVerifyNpubView.swift; sourceTree = ""; }; - D7C28E3A2BBB4D0000EE459F /* VideoCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoCache.swift; sourceTree = ""; }; D7C6787D2B2D34CC00BCEAFB /* NIP98AuthenticatedRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NIP98AuthenticatedRequest.swift; sourceTree = ""; }; D7CB5D3D2B116DAD00AD4105 /* NotificationsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsManager.swift; sourceTree = ""; }; D7CB5D442B116FE800AD4105 /* Contacts+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Contacts+.swift"; sourceTree = ""; }; @@ -1655,7 +1651,6 @@ D74AAFC12B153395006CF0F4 /* HeadlessDamusState.swift */, B5C60C1F2B530D5100C5ECA7 /* MuteItem.swift */, B533694D2B66D791008A805E /* MutelistManager.swift */, - D7C28E3A2BBB4D0000EE459F /* VideoCache.swift */, ); path = Models; sourceTree = ""; @@ -2556,7 +2551,6 @@ E06336A92B75832100A88E6B /* ImageMetadataTest.swift */, D7CBD1D52B8D509800BFD889 /* DamusPurpleImpendingExpirationTests.swift */, D72927AC2BAB515C00F93E90 /* RelayURLTests.swift */, - D7831AF72BBE11E2005DA780 /* VideoCacheTests.swift */, 4C2D34402BDAF1B300F9FB44 /* NIP10Tests.swift */, ); path = damusTests; @@ -3293,7 +3287,6 @@ 4C1253522A76C6130004F4B8 /* ComposeNotify.swift in Sources */, 4C7D09662A0AE62100943473 /* AlbyButton.swift in Sources */, D7100C582B76FC8400C59298 /* MarketingContentView.swift in Sources */, - D7C28E3B2BBB4D0000EE459F /* VideoCache.swift in Sources */, 4CAAD8AD298851D000060CEA /* AccountDeletion.swift in Sources */, 4CFF8F6329CC9AD7008DB934 /* ImageContextMenuModifier.swift in Sources */, 4C54AA0A29A55429003E4487 /* EventGroup.swift in Sources */, @@ -3551,7 +3544,6 @@ 75AD872B2AA23A460085EF2C /* Block+Tests.swift in Sources */, E0E024112B7C19C20075735D /* TranslationTests.swift in Sources */, F944F56E29EA9CCC0067B3BF /* DamusParseContentTests.swift in Sources */, - D7831AF82BBE11E2005DA780 /* VideoCacheTests.swift in Sources */, 3A5E47C72A4A76C800C0D090 /* TrieTests.swift in Sources */, B501062D2B363036003874F5 /* AuthIntegrationTests.swift in Sources */, 4CB883AE2976FA9300DC99E7 /* FormatTests.swift in Sources */, diff --git a/damus/ContentView.swift b/damus/ContentView.swift index d8411447..e8efd7d2 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -516,7 +516,6 @@ struct ContentView: View { print("txn: 📙 DAMUS BACKGROUNDED") Task { @MainActor in damus_state.ndb.close() - VideoCache.standard?.periodic_purge() } break case .inactive: diff --git a/damus/Views/Video/DamusVideoPlayer.swift b/damus/Views/Video/DamusVideoPlayer.swift index 70f7d530..6beaa924 100644 --- a/damus/Views/Video/DamusVideoPlayer.swift +++ b/damus/Views/Video/DamusVideoPlayer.swift @@ -33,7 +33,7 @@ struct DamusVideoPlayer: View { else { mute = nil } - _model = StateObject(wrappedValue: DamusVideoPlayerViewModel.cached_video_model(url: url, video_size: video_size, controller: controller, mute: mute)) + _model = StateObject(wrappedValue: DamusVideoPlayerViewModel(url: url, video_size: video_size, controller: controller, mute: mute)) self.visibility_tracking_method = visibility_tracking_method self.style = style } diff --git a/damus/Views/Video/DamusVideoPlayerViewModel.swift b/damus/Views/Video/DamusVideoPlayerViewModel.swift index 9c5188bb..524c0b72 100644 --- a/damus/Views/Video/DamusVideoPlayerViewModel.swift +++ b/damus/Views/Video/DamusVideoPlayerViewModel.swift @@ -57,12 +57,6 @@ final class DamusVideoPlayerViewModel: ObservableObject { } } - static func cached_video_model(url: URL, video_size: Binding, controller: VideoController, mute: Bool? = nil) -> Self { - let maybe_cached_url = (try? VideoCache.standard?.maybe_cached_url_for(video_url: url)) ?? url - Log.info("Loading video with URL: %s",for: .render, maybe_cached_url.absoluteString) - return Self.init(url: maybe_cached_url, video_size: video_size, controller: controller, mute: mute) - } - init(url: URL, video_size: Binding, controller: VideoController, mute: Bool? = nil) { self.url = url player_item = AVPlayerItem(url: url) diff --git a/damusTests/VideoCacheTests.swift b/damusTests/VideoCacheTests.swift deleted file mode 100644 index 47128506..00000000 --- a/damusTests/VideoCacheTests.swift +++ /dev/null @@ -1,112 +0,0 @@ -// -// VideoCacheTests.swift -// damusTests -// -// Created by Daniel D’Aquino on 2024-04-03. -// - -import Foundation -import XCTest -@testable import damus - -// TODO: Reduce test dependency on external factors such as external URLs. -let TEST_VIDEO_URL = "http://cdn.jb55.com/s/zaps-build.mp4" -let LONG_TEST_EXPIRY_TIME: TimeInterval = 60 * 60 * 24 // A long expiry time for a video (in seconds). -let SHORT_TEST_EXPIRY_TIME: TimeInterval = 15 // A short expiry time for a video (in seconds). Must be as short as possible but large enough to allow some test operations to occur -let CACHE_SAVE_TIME_TIMEOUT: TimeInterval = 8 // How long the test will wait for the cache to save a file (in seconds) -let EXPIRY_TIME_MARGIN: TimeInterval = 3 // The extra time we will wait after expected expiry, to avoid test timing issues. (in seconds) - -final class VideoCacheTests: XCTestCase { - - func testCachedURLForExistingVideo() throws { - // Create a temporary directory for the cache - let test_cache_directory = FileManager.default.temporaryDirectory.appendingPathComponent("test_video_cache") - - // Create a test video file - let original_video_url = URL(string: TEST_VIDEO_URL)! - FileManager.default.createFile(atPath: original_video_url.path, contents: Data(), attributes: nil) - - // Create a VideoCache instance with the temporary cache directory - let test_expiry_time: TimeInterval = 10 - let video_cache = try VideoCache(cache_url: test_cache_directory, expiry_time: test_expiry_time)! - - // Call the maybe_cached_url_for method with the test video URL - let expected_cache_url = video_cache.url_to_cached_url(url: original_video_url) - let maybe_cached_url = try video_cache.maybe_cached_url_for(video_url: original_video_url) - - // Assert that the returned URL is the same as the original - XCTAssertEqual(maybe_cached_url, original_video_url, "Returned URL should be the same as the original video URL on the first time we download it") - - // Check that next time we get this video, we get the cached URL. - let cached_url_expectation = XCTestExpectation(description: "On second time we get a video, the cached URL should be returned") - let start_time = Date() - while Date().timeIntervalSince(start_time) < CACHE_SAVE_TIME_TIMEOUT { - let maybe_cached_url = try video_cache.maybe_cached_url_for(video_url: original_video_url) - if maybe_cached_url == expected_cache_url { - cached_url_expectation.fulfill() - break - } - sleep(1) - } - wait(for: [cached_url_expectation], timeout: CACHE_SAVE_TIME_TIMEOUT) - - // Now wait for the remaining time until the expiry time + a margin - let remaining_time = test_expiry_time + EXPIRY_TIME_MARGIN - Date().timeIntervalSince(start_time) - - // Wait for the expiry time to pass - sleep(UInt32(max(remaining_time, 0))) - - // Call the periodic_purge method to purge expired video items - video_cache.periodic_purge() - - // Call the maybe_cached_url_for method again - let maybe_cached_url_after_expiry = try video_cache.maybe_cached_url_for(video_url: original_video_url) - - // Assert that the returned URL is the same as the original video URL, since the cache should have expired. - XCTAssertEqual(maybe_cached_url_after_expiry, original_video_url, "Video cache should expire after expiry time") - - // Clean up the temporary files and directory - try FileManager.default.removeItem(at: test_cache_directory) - } - - func testClearCache() throws { - // Create a temporary directory for the cache - let test_cache_directory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("test_video_cache") - try FileManager.default.createDirectory(at: test_cache_directory, withIntermediateDirectories: true, attributes: nil) - - // Create a test video file - let original_video_url = URL(string: TEST_VIDEO_URL)! - FileManager.default.createFile(atPath: original_video_url.path, contents: Data(), attributes: nil) - - // Create a VideoCache instance with the temporary cache directory and a longer expiry time - let expiry_time: TimeInterval = LONG_TEST_EXPIRY_TIME - let video_cache = try VideoCache(cache_url: test_cache_directory, expiry_time: expiry_time)! - - // Request the cached URL for the test video to create the cached file - let expected_cache_url = video_cache.url_to_cached_url(url: original_video_url) - let _ = try video_cache.maybe_cached_url_for(video_url: original_video_url) - - // Check that next time we get this video, we get the cached URL. - let cached_url_expectation = XCTestExpectation(description: "On second time we get a video, the cached URL should be returned") - let start_time = Date() - while Date().timeIntervalSince(start_time) < CACHE_SAVE_TIME_TIMEOUT { - let maybe_cached_url = try video_cache.maybe_cached_url_for(video_url: original_video_url) - if maybe_cached_url == expected_cache_url { - cached_url_expectation.fulfill() - break - } - sleep(1) - } - wait(for: [cached_url_expectation], timeout: CACHE_SAVE_TIME_TIMEOUT) - - // Call the periodic_purge method - DamusCacheManager.shared.clear_cache(damus_state: test_damus_state, completion: { - // Assert that fetching the cached URL after clearing cache will - let maybe_cached_url_after_purge = try? video_cache.maybe_cached_url_for(video_url: original_video_url) - XCTAssertEqual(maybe_cached_url_after_purge, original_video_url) - - // Clean up the temporary directory - try? FileManager.default.removeItem(at: test_cache_directory) - }) - } -}