Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show more in NodeInfo #413

Merged
merged 3 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion catalog/templates/item_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ <h1>
{% if request.user.is_authenticated and not mark.shelf_type %}
<div id="item-primary-action" class="right mark">
<div class="item-action item-mark-buttons">
{% for k, v in shelf_types %}
{% for k, v in shelf_labels %}
{% if v %}
<button class="primary"
data-status="{{ k }}"
Expand Down
6 changes: 3 additions & 3 deletions catalog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Review,
ShelfMember,
ShelfType,
ShelfTypeNames,
get_shelf_labels_for_category,
q_piece_in_home_feed_of_user,
q_piece_visible_to_user,
)
Expand Down Expand Up @@ -92,7 +92,7 @@ def retrieve(request, item_path, item_uuid):
my_collections = []
collection_list = []
child_item_comments = []
shelf_types = [(n[1], n[2]) for n in iter(ShelfTypeNames) if n[0] == item.category]
shelf_labels = get_shelf_labels_for_category(item.category)
if request.user.is_authenticated:
visible = q_piece_visible_to_user(request.user)
mark = Mark(request.user.identity, item)
Expand Down Expand Up @@ -126,7 +126,7 @@ def retrieve(request, item_path, item_uuid):
"child_item_comments": child_item_comments,
"my_collections": my_collections,
"collection_list": collection_list,
"shelf_types": shelf_types,
"shelf_labels": shelf_labels,
},
)

Expand Down
19 changes: 8 additions & 11 deletions common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.shortcuts import redirect, render
from django.urls import reverse

from boofilsic import __version__
from users.models import User


Expand Down Expand Up @@ -41,32 +42,28 @@ def nodeinfo2(request):
# TODO filter local with SQL function in https://wiki.postgresql.org/wiki/Count_estimate
with connection.cursor() as cursor:
cursor.execute(
"SELECT n_live_tup FROM pg_stat_all_tables WHERE relname = 'journal_shelfmember';"
"SELECT n_live_tup FROM pg_stat_all_tables WHERE relname = 'journal_shelflogentry';"
)
row = cursor.fetchone()
if row:
usage["localPosts"] = row[0]
with connection.cursor() as cursor:
cursor.execute(
"SELECT n_live_tup FROM pg_stat_all_tables WHERE relname = 'journal_comment';"
)
row = cursor.fetchone()
if row:
usage["localComments"] = row[0]
return JsonResponse(
{
"version": "2.0",
"software": {
"name": "neodb",
"version": settings.NEODB_VERSION,
"version": __version__,
"repository": "https://github.com/neodb-social/neodb",
"homepage": "https://neodb.net/",
},
"protocols": ["activitypub", "neodb"],
"openRegistrations": False, # settings.SITE_INFO["open_registrations"],
"openRegistrations": not settings.INVITE_ONLY,
"services": {"outbound": [], "inbound": []},
"usage": usage,
"metadata": {"nodeName": settings.SITE_INFO["site_name"]},
"metadata": {
"nodeName": settings.SITE_INFO["site_name"],
"nodeRevision": settings.NEODB_VERSION,
},
}
)

Expand Down
3 changes: 2 additions & 1 deletion journal/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
from .renderers import render_md
from .review import Review
from .shelf import (
SHELF_LABELS,
Shelf,
ShelfLogEntry,
ShelfManager,
ShelfMember,
ShelfType,
ShelfTypeNames,
get_shelf_labels_for_category,
)
from .tag import Tag, TagManager, TagMember
from .utils import (
Expand Down
8 changes: 6 additions & 2 deletions journal/models/shelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ShelfType(models.TextChoices):
# DISCARDED = ('discarded', '放弃')


ShelfTypeNames = [
SHELF_LABELS = [
[ItemCategory.Book, ShelfType.WISHLIST, _("想读")],
[ItemCategory.Book, ShelfType.PROGRESS, _("在读")],
[ItemCategory.Book, ShelfType.COMPLETE, _("读过")],
Expand All @@ -52,6 +52,10 @@ class ShelfType(models.TextChoices):
]


def get_shelf_labels_for_category(item_category: ItemCategory):
return [(n[1], n[2]) for n in SHELF_LABELS if n[0] == item_category]


class ShelfMember(ListMember):
parent = models.ForeignKey(
"Shelf", related_name="members", on_delete=models.CASCADE
Expand Down Expand Up @@ -268,7 +272,7 @@ def get_action_label(
cls, shelf_type: ShelfType | str, item_category: ItemCategory
) -> str:
st = str(shelf_type)
sts = [n[2] for n in ShelfTypeNames if n[0] == item_category and n[1] == st]
sts = [n[2] for n in SHELF_LABELS if n[0] == item_category and n[1] == st]
return sts[0] if sts else st

@classmethod
Expand Down
11 changes: 5 additions & 6 deletions journal/templates/_sidebar_user_mark_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
{% endfor %}
</select>
<select name="shelf" id="shelf" onchange="filter()">
<option {% if '/wishlist/' in request.path %}selected{% endif %}
value="wishlist">想看</option>
<option {% if '/progress/' in request.path %}selected{% endif %}
value="progress">在看</option>
<option {% if '/complete/' in request.path %}selected{% endif %}
value="complete">看过</option>
{% for typ, label in shelf_labels %}
{% if label %}
<option {% if typ in request.path %}selected{% endif %} value="{{ typ }}">{{ label }}</option>
{% endif %}
{% endfor %}
<option {% if '/reviews/' in request.path %}selected{% endif %}
value="reviews">评论</option>
</select>
Expand Down
4 changes: 2 additions & 2 deletions journal/templates/mark.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
<div class="add-to-list-modal__body">
<form method="post" action="{% url 'journal:mark' item.uuid %}">
{% csrf_token %}
{% if shelf_types %}
{% if shelf_labels %}
<div class="grid mark-line">
<div>
<fieldset>
{% for k, v in shelf_types %}
{% for k, v in shelf_labels %}
{% if v %}
<input type="radio"
name="status"
Expand Down
2 changes: 2 additions & 0 deletions journal/views/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def render_list(
page_number = int(request.GET.get("page", default=1))
members = paginator.get_page(page_number)
pagination = PageLinksGenerator(PAGE_SIZE, page_number, paginator.num_pages)
shelf_labels = get_shelf_labels_for_category(item_category) if item_category else []
return render(
request,
f"user_{type}_list.html",
Expand All @@ -103,6 +104,7 @@ def render_list(
"years": years,
"year": year,
"shelf": shelf_type,
"shelf_labels": shelf_labels,
"category": item_category,
},
)
Expand Down
15 changes: 10 additions & 5 deletions journal/views/mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
from mastodon.api import boost_toot_later
from takahe.utils import Takahe

from ..models import Comment, Mark, Piece, ShelfType, ShelfTypeNames, TagManager
from ..models import (
Comment,
Mark,
Piece,
ShelfType,
TagManager,
get_shelf_labels_for_category,
)
from .common import render_list, render_relogin, target_identity_required

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -98,9 +105,7 @@ def mark(request: AuthedHttpRequest, item_uuid):
mark = Mark(request.user.identity, item)
if request.method == "GET":
tags = request.user.identity.tag_manager.get_item_tags(item)
shelf_types = [
(n[1], n[2]) for n in iter(ShelfTypeNames) if n[0] == item.category
]
shelf_labels = get_shelf_labels_for_category(item.category)
shelf_type = request.GET.get("shelf_type", mark.shelf_type)
return render(
request,
Expand All @@ -110,7 +115,7 @@ def mark(request: AuthedHttpRequest, item_uuid):
"mark": mark,
"shelf_type": shelf_type,
"tags": ",".join(tags),
"shelf_types": shelf_types,
"shelf_labels": shelf_labels,
"date_today": timezone.localdate().isoformat(),
},
)
Expand Down
2 changes: 1 addition & 1 deletion neodb-takahe
22 changes: 12 additions & 10 deletions takahe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,16 +1065,17 @@ def create_local(
)
post.object_uri = post.urls.object_uri
post.url = post.absolute_object_uri()
post.mentions.set(mentions)
post.emojis.set(emojis)
if published and published < timezone.now():
post.published = published
if (
timezone.now() - published
> datetime.timedelta(days=settings.FANOUT_LIMIT_DAYS)
and _migration_mode
):
post.state = "fanned_out" # add post quietly if it's old
if _migration_mode:
post.state = "fanned_out"
else:
post.mentions.set(mentions)
post.emojis.set(emojis)
if published and published < timezone.now():
post.published = published
if timezone.now() - published > datetime.timedelta(
days=settings.FANOUT_LIMIT_DAYS
):
post.state = "fanned_out" # add post quietly if it's old
# if attachments:# FIXME
# post.attachments.set(attachments)
# if question: # FIXME
Expand All @@ -1087,6 +1088,7 @@ def create_local(
if reply_to:
reply_to.calculate_stats()
if post.state == "fanned_out" and not _migration_mode:
# add post to auther's timeline directly if it's old
post.add_to_timeline(author)
return post

Expand Down
10 changes: 10 additions & 0 deletions takahe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def get_node_name_for_domain(d: str):
if domain and domain.nodeinfo:
return domain.nodeinfo.get("metadata", {}).get("nodeName")

@staticmethod
def sync_password(u: "NeoUser"):
user = User.objects.filter(pk=u.pk).first()
if not user:
raise ValueError(f"Cannot find takahe user {u}")
elif user.password != u.password:
logger.info(f"Updating takahe user {u} password")
user.password = u.password
user.save()

@staticmethod
def init_identity_for_local_user(u: "NeoUser"):
"""
Expand Down
5 changes: 3 additions & 2 deletions takahe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
from loguru import logger

from .models import TakaheSession
from .utils import Takahe

_TAKAHE_SESSION_COOKIE_NAME = "sessionid"


@login_required
def auth_login(request: HttpRequest):
def auth_login(request):
"""Redirect to the login page if not yet, otherwise sync login info to takahe session"""

Takahe.sync_password(request.user)
# if SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies" in Takahe
session = SessionStore(session_key=request.COOKIES.get(_TAKAHE_SESSION_COOKIE_NAME))
session._session_cache = request.session._session # type: ignore
Expand Down