Skip to content

Commit

Permalink
Merge pull request #6 from vapdev/Paiva
Browse files Browse the repository at this point in the history
order tweets by likes and dislikes
  • Loading branch information
vapdev authored Mar 4, 2023
2 parents 36a52ac + 4289455 commit cabbaf7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apps/feed/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
path('api/add_tweek/', api_add_tweek, name='api_add_tweek'),
path('api/tweek/<int:tweek_id>/', api_get_tweek, name='api_get_tweek'),
path('api/tweek/<int:tweek_id>/replies/', api_get_reply_tweeks, name='get_reply_tweeks'),
]
]
13 changes: 10 additions & 3 deletions apps/feed/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.shortcuts import render
import json
import re
from django.db.models import Count
from django.http import JsonResponse
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
Expand Down Expand Up @@ -57,7 +58,7 @@ def api_add_tweek(request):

if tweek_type == 'retweek':
Tweek.objects.create(body=body, created_by=user, retweek_id=parent_id, image=image_url)

elif tweek_type == 'reply':
Tweek.objects.create(body=body, created_by=user, comment_from_id=parent_id, image=image_url)
else:
Expand All @@ -73,7 +74,7 @@ def api_add_tweek(request):
notification = Notification(created_by=request, to_user=user, notification_type='mention')
notifications.append(notification)
create_notification_bulk(notifications)

return JsonResponse({'success': True})


Expand Down Expand Up @@ -186,6 +187,12 @@ def api_get_tweeks(request):
for tweeker in request.user.twikkerprofile.follows.all():
userids.append(tweeker.user.id)
tweeks = Tweek.objects.filter(created_by__id__in=userids)
if request.GET.get('order_by') == '0':
tweeks = tweeks.order_by('-created_at')
elif request.GET.get('order_by') == '1':
tweeks = tweeks.order_by('-likes')
elif request.GET.get('order_by') == '2':
tweeks = tweeks.annotate(num_retweets=Count('retweek')).order_by('-num_retweets')
paginator = PageNumberPagination()
paginator.page_size = 25
results = paginator.paginate_queryset(tweeks, request)
Expand Down Expand Up @@ -220,4 +227,4 @@ def api_get_reply_tweeks(request, tweek_id):
def api_get_tweek(request, tweek_id):
tweek = Tweek.objects.get(id=tweek_id)
serializer = TweekSerializer(tweek, many=False, context={'request': request})
return JsonResponse({'success': True, 'tweek': serializer.data})
return JsonResponse({'success': True, 'tweek': serializer.data})
12 changes: 10 additions & 2 deletions frontend/src/pages/Feed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
class="flex flex-col w-full border-solid border-x-2 border-gray-100 dark:border-gray-700 max-[600px]:border-x-0"
>
<DefaultHeader :mainText="'Página Inicial'" />
<div class="mb-2">
<select v-model="selectedOption" @change="reloadTweeks()">
<option value="0">Mais recentes</option>
<option value="1">Mais Curtidos</option>
<option value="2">Mais Compartilhamentos</option>
</select>
</div>
<NewTextBox
v-bind:is-posting="isPosting"
@callSubmitTweek="submitTweek"
Expand All @@ -18,7 +25,7 @@
<LoadingSpinner v-if="isLoading" :size="8" class="mt-5" />
</div>
</div>

<div v-if="unseen_tweeks" @click="reloadTweeks()" class="cursor-pointer shadow-outline fixed top-20 left-1/2 transform -translate-x-1/2 bg-blue-500 rounded-xl text-white p-2 text-center w-664 h-10">
Ver novos tweeks!
</div>
Expand All @@ -33,6 +40,7 @@ import DefaultHeader from "../components/DefaultHeader.vue";
import { useUserStore } from "../store/UserStore";
const userStore = useUserStore();
const selectedOption = ref(0); // defina selectedOption como uma referência reativa
const props = defineProps({
unseenTweeks: Boolean
Expand Down Expand Up @@ -96,7 +104,7 @@ async function getTweeks(refresh = false) {
currentPage = 1;
lastTweekId = null; // reset last tweet ID to null
}
let url = `/api/get_tweeks/?page=${currentPage}`;
let url = `/api/get_tweeks/?page=${currentPage}&order_by=${selectedOption.value}`;
if (lastTweekId) {
url += `&last_tweek_id=${lastTweekId}`; // add last tweet ID to URL if it exists
}
Expand Down

0 comments on commit cabbaf7

Please sign in to comment.