Repository: internetarchive/openlibrary

Issue:
Blur book covers with content_warnings when safe-mode is enabled
<!-- What issue does this PR close? -->
Follow-Up to PR #7775 and Issue #7416 (part 2) to blur covers containing nsfw content.

<!-- What does this PR achieve? [feature|hotfix|fix|refactor] -->
This PR implements the blurring functionality of the opt-in safe-mode feature. Books tagged as nsfw/containing inappropriate content via a `content_warnings:*` subject will have their covers blurred if the user has enabled Safe Mode in 'Manage Privacy and Content Moderation Settings' on the Settings page. 

### Technical
<!-- What should be noted about the implementation? -->

When Safe Mode is enabled, an `sfw` cookie is set to 'yes' and a list of subjects is retrieved  when a search is entered. If a `content_warnings:*` subject is present for that book, a blurred book cover appears else the original book cover is displayed. This also works for the small book previews as shown in the screenshots. 

### Testing
<!-- Steps for reviewer to reproduce/verify what this PR does/fixes. -->

1. Go to `conf/openlibrary.yml` and change line 18 `coverstore_url: http://covers:7075` to `coverstore_url: covers.openlibrary.org` 
2. Remake css: Enter `docker compose run --rm home make css` in the terminal after 'docker compose up' 
3. Restart memcached in docker: `docker compose restart memcached` 
4. Enable safe mode (from Privacy and Content Moderation Settings) 
5. Search for a book (type the book title or any search term) and press Enter. Currently, any book with a cover or preview will be blurred for testing purposes, but this will be adjusted for only books with the `content-warnings:*` subject. 
7. On the search results page on the top right, just above the displayed book components, uncheck the 'Solr Editions Beta' checkbox. 
8. The book cover and/or preview should be blurred. 

### Screenshots
<!-- If this PR touches UI, please post evidence (screenshots) of it behaving correctly. -->

<img width="630" alt="bookcover_blur" src="https://user-images.githubusercontent.com/123398993/236555484-5554924a-6afe-4309-9537-90c125c685ab.png">

<img width="658" alt="preview_blur" src="https://user-images.githubusercontent.com/123398993/236555559-26dccce0-4791-40e5-b1a7-873795402271.png">


<!-- Attribution Disclaimer: By proposing this pull request, I affirm to have made a best-effort and exercised my discretion to make sure relevant sections of this code which substantially leverage code suggestions, code generation, or code snippets from sources (e.g. Stack Overflow, GitHub) have been annotated with basic attribution so reviewers & contributors may have confidence and access to the correct context to evaluate and use this code. -->



Discussion:

Resolving patch:
diff --git a/openlibrary/plugins/worksearch/code.py b/openlibrary/plugins/worksearch/code.py
--- a/openlibrary/plugins/worksearch/code.py
+++ b/openlibrary/plugins/worksearch/code.py
@@ -287,14 +287,15 @@ def do_search(
     :param sort: csv sort ordering
     :param spellcheck_count: Not really used; should probably drop
     """
+
+    if web.cookies(sfw="").sfw == 'yes':
+        fields = list(
+            WorkSearchScheme.default_fetched_fields | {'editions'} | {'subject_key'}
+        )
+    else:
+        fields = list(WorkSearchScheme.default_fetched_fields | {'editions'})
     return run_solr_query(
-        WorkSearchScheme(),
-        param,
-        rows,
-        page,
-        sort,
-        spellcheck_count,
-        fields=list(WorkSearchScheme.default_fetched_fields | {'editions'}),
+        WorkSearchScheme(), param, rows, page, sort, spellcheck_count, fields=fields
     )
 
 
@@ -443,6 +444,7 @@ def GET(self):
         for k in ('title', 'author', 'isbn', 'subject', 'place', 'person', 'publisher'):
             if k in i:
                 q_list.append(f'{k}:{fully_escape_query(i[k].strip())}')
+        safe_mode_cookie = web.cookies(sfw="")
         return render.work_search(
             i,
             ' '.join(q_list),
@@ -450,6 +452,7 @@ def GET(self):
             get_doc,
             fulltext_search,
             WorkSearchScheme.facet_fields,
+            safe_mode_cookie.sfw == 'yes',
         )
 
 


Test patch: