Skip to content

Commit

Permalink
Implement user discoverability
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Nov 26, 2022
1 parent 5fe5f04 commit 1cf1f2e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 13 deletions.
4 changes: 3 additions & 1 deletion activities/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Migration(migrations.Migration):
(
"author",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
on_delete=django.db.models.deletion.CASCADE,
related_name="posts",
to="users.identity",
),
Expand Down Expand Up @@ -257,6 +257,8 @@ class Migration(migrations.Migration):
models.CharField(
choices=[
("post", "Post"),
("post_edited", "Post Edited"),
("post_deleted", "Post Deleted"),
("interaction", "Interaction"),
("undo_interaction", "Undo Interaction"),
],
Expand Down
5 changes: 4 additions & 1 deletion core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class LoggedOutHomepage(TemplateView):

def get_context_data(self):
return {
"identities": Identity.objects.filter(local=True).order_by("-created")[:20],
"identities": Identity.objects.filter(
local=True,
discoverable=True,
).order_by("-created")[:20],
}


Expand Down
1 change: 1 addition & 0 deletions templates/identity/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h1>Create New Identity</h1>
{% include "forms/_field.html" with field=form.username %}
{% include "forms/_field.html" with field=form.domain %}
{% include "forms/_field.html" with field=form.name %}
{% include "forms/_field.html" with field=form.discoverable %}
</fieldset>
<div class="buttons">
<button>Create</button>
Expand Down
1 change: 1 addition & 0 deletions templates/settings/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<legend>Details</legend>
{% include "forms/_field.html" with field=form.name %}
{% include "forms/_field.html" with field=form.summary %}
{% include "forms/_field.html" with field=form.discoverable %}
</fieldset>
<fieldset>
<legend>Images</legend>
Expand Down
18 changes: 18 additions & 0 deletions users/migrations/0002_identity_discoverable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.3 on 2022-11-26 01:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("users", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="identity",
name="discoverable",
field=models.BooleanField(default=True),
),
]
6 changes: 5 additions & 1 deletion users/models/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Identity(StatorModel):
name = models.CharField(max_length=500, blank=True, null=True)
summary = models.TextField(blank=True, null=True)
manually_approves_followers = models.BooleanField(blank=True, null=True)
discoverable = models.BooleanField(default=True)

profile_uri = models.CharField(max_length=500, blank=True, null=True)
inbox_uri = models.CharField(max_length=500, blank=True, null=True)
Expand Down Expand Up @@ -240,7 +241,7 @@ def to_ap(self):
},
"published": self.created.strftime("%Y-%m-%dT%H:%M:%SZ"),
"url": self.absolute_profile_uri(),
"discoverable": True,
"http://joinmastodon.org/ns#discoverable": self.discoverable,
}
if self.name:
response["name"] = self.name
Expand Down Expand Up @@ -373,6 +374,9 @@ async def fetch_actor(self) -> bool:
self.public_key_id = document.get("publicKey", {}).get("id")
self.icon_uri = document.get("icon", {}).get("url")
self.image_uri = document.get("image", {}).get("url")
self.discoverable = document.get(
"http://joinmastodon.org/ns#discoverable", True
)
# Now go do webfinger with that info to see if we can get a canonical domain
actor_url_parts = urlparse(self.actor_uri)
get_domain = sync_to_async(Domain.get_remote_domain)
Expand Down
9 changes: 9 additions & 0 deletions users/views/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ class form_class(forms.Form):
name = forms.CharField(
help_text="The display name other users see. You can change this easily."
)
discoverable = forms.BooleanField(
help_text="If this user is visible on the frontpage and in user directories.",
initial=True,
widget=forms.Select(
choices=[(True, "Discoverable"), (False, "Not Discoverable")]
),
required=False,
)

def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -219,6 +227,7 @@ def form_valid(self, form):
domain_id=domain,
name=form.cleaned_data["name"],
local=True,
discoverable=form.cleaned_data["discoverable"],
)
new_identity.users.add(self.request.user)
new_identity.generate_keypair()
Expand Down
31 changes: 21 additions & 10 deletions users/views/settings/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,22 @@ class form_class(forms.Form):
image = forms.ImageField(
required=False, help_text="Shown at the top of your profile"
)
discoverable = forms.BooleanField(
help_text="If this user is visible on the frontpage and in user directories.",
widget=forms.Select(
choices=[(True, "Discoverable"), (False, "Not Discoverable")]
),
required=False,
)

def get_initial(self):
identity = self.request.identity
return {
"name": self.request.identity.name,
"summary": self.request.identity.summary,
"icon": self.request.identity.icon and self.request.identity.icon.url,
"image": self.request.identity.image and self.request.identity.image.url,
"name": identity.name,
"summary": identity.summary,
"icon": identity.icon and identity.icon.url,
"image": identity.image and identity.image.url,
"discoverable": identity.discoverable,
}

def resize_image(self, image: File, *, size: tuple[int, int]) -> File:
Expand All @@ -50,21 +59,23 @@ def resize_image(self, image: File, *, size: tuple[int, int]) -> File:
return File(new_image_bytes)

def form_valid(self, form):
# Update identity name and summary
self.request.identity.name = form.cleaned_data["name"]
self.request.identity.summary = form.cleaned_data["summary"]
# Update basic info
identity = self.request.identity
identity.name = form.cleaned_data["name"]
identity.summary = form.cleaned_data["summary"]
identity.discoverable = form.cleaned_data["discoverable"]
# Resize images
icon = form.cleaned_data.get("icon")
image = form.cleaned_data.get("image")
if isinstance(icon, File):
self.request.identity.icon.save(
identity.icon.save(
icon.name,
self.resize_image(icon, size=(400, 400)),
)
if isinstance(image, File):
self.request.identity.image.save(
identity.image.save(
image.name,
self.resize_image(image, size=(1500, 500)),
)
self.request.identity.save()
identity.save()
return redirect(".")

0 comments on commit 1cf1f2e

Please sign in to comment.