mirror of
https://github.com/sissbruecker/linkding.git
synced 2025-08-14 05:59:29 +02:00
Make Internet Archive integration opt-in (#250)
* Make web archive integration opt-in * Add toast message about web archive integration opt-in * Improve wording for web archive setting * Add toast admin * Fix toast clear button visited styles * Add test for redirect * Improve wording * Ensure redirects to same domain * Improve wording * Fix snapshot test Co-authored-by: Sascha Ißbrücker <sascha.issbruecker@gmail.com>
This commit is contained in:
@@ -29,7 +29,7 @@ def create_bookmark(bookmark: Bookmark, tag_string: str, current_user: User):
|
||||
_update_bookmark_tags(bookmark, tag_string, current_user)
|
||||
bookmark.save()
|
||||
# Create snapshot on web archive
|
||||
tasks.create_web_archive_snapshot(bookmark.id, False)
|
||||
tasks.create_web_archive_snapshot(current_user, bookmark, False)
|
||||
|
||||
return bookmark
|
||||
|
||||
@@ -47,7 +47,7 @@ def update_bookmark(bookmark: Bookmark, tag_string, current_user: User):
|
||||
bookmark.save()
|
||||
# Update web archive snapshot, if URL changed
|
||||
if has_url_changed:
|
||||
tasks.create_web_archive_snapshot(bookmark.id, True)
|
||||
tasks.create_web_archive_snapshot(current_user, bookmark, True)
|
||||
|
||||
return bookmark
|
||||
|
||||
|
@@ -40,7 +40,7 @@ def import_netscape_html(html: str, user: User):
|
||||
result.failed = result.failed + 1
|
||||
|
||||
# Create snapshots for newly imported bookmarks
|
||||
tasks.schedule_bookmarks_without_snapshots(user.id)
|
||||
tasks.schedule_bookmarks_without_snapshots(user)
|
||||
|
||||
return result
|
||||
|
||||
|
@@ -4,30 +4,29 @@ import waybackpy
|
||||
from background_task import background
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import User
|
||||
from waybackpy.exceptions import WaybackError
|
||||
|
||||
from bookmarks.models import Bookmark
|
||||
from bookmarks.models import Bookmark, UserProfile
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def when_background_tasks_enabled(fn):
|
||||
def wrapper(*args, **kwargs):
|
||||
if settings.LD_DISABLE_BACKGROUND_TASKS:
|
||||
return
|
||||
return fn(*args, **kwargs)
|
||||
def is_web_archive_integration_active(user: User) -> bool:
|
||||
background_tasks_enabled = not settings.LD_DISABLE_BACKGROUND_TASKS
|
||||
web_archive_integration_enabled = \
|
||||
user.profile.web_archive_integration == UserProfile.WEB_ARCHIVE_INTEGRATION_ENABLED
|
||||
|
||||
# Expose attributes from wrapped TaskProxy function
|
||||
attrs = vars(fn)
|
||||
for key, value in attrs.items():
|
||||
setattr(wrapper, key, value)
|
||||
|
||||
return wrapper
|
||||
return background_tasks_enabled and web_archive_integration_enabled
|
||||
|
||||
|
||||
def create_web_archive_snapshot(user: User, bookmark: Bookmark, force_update: bool):
|
||||
if is_web_archive_integration_active(user):
|
||||
_create_web_archive_snapshot_task(bookmark.id, force_update)
|
||||
|
||||
|
||||
@when_background_tasks_enabled
|
||||
@background()
|
||||
def create_web_archive_snapshot(bookmark_id: int, force_update: bool):
|
||||
def _create_web_archive_snapshot_task(bookmark_id: int, force_update: bool):
|
||||
try:
|
||||
bookmark = Bookmark.objects.get(id=bookmark_id)
|
||||
except Bookmark.DoesNotExist:
|
||||
@@ -52,11 +51,15 @@ def create_web_archive_snapshot(bookmark_id: int, force_update: bool):
|
||||
logger.debug(f'Successfully created web archive link for bookmark: {bookmark}...')
|
||||
|
||||
|
||||
@when_background_tasks_enabled
|
||||
def schedule_bookmarks_without_snapshots(user: User):
|
||||
if is_web_archive_integration_active(user):
|
||||
_schedule_bookmarks_without_snapshots_task(user.id)
|
||||
|
||||
|
||||
@background()
|
||||
def schedule_bookmarks_without_snapshots(user_id: int):
|
||||
def _schedule_bookmarks_without_snapshots_task(user_id: int):
|
||||
user = get_user_model().objects.get(id=user_id)
|
||||
bookmarks_without_snapshots = Bookmark.objects.filter(web_archive_snapshot_url__exact='', owner=user)
|
||||
|
||||
for bookmark in bookmarks_without_snapshots:
|
||||
create_web_archive_snapshot(bookmark.id, False)
|
||||
_create_web_archive_snapshot_task(bookmark.id, False)
|
||||
|
Reference in New Issue
Block a user