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

read/unread merge with main #49

Merged
merged 2 commits into from
May 21, 2021
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 client/src/services/socketio.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import socketIOClient from "socket.io-client";

// Controls reconnect attempts
const io = socketIOClient(process.env.REACT_APP_SERVER_URL, {
withCredentials: true,
reconnectionAttempts: 1,
Expand Down
1 change: 1 addition & 0 deletions server/inquire/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class UserCourse(EmbeddedMongoModel):
nickname = fields.CharField(blank=True)
color = fields.CharField(blank=True)
role = fields.CharField(required=True)
viewed = fields.DictField(default={})


'''
Expand Down
5 changes: 5 additions & 0 deletions server/inquire/resources/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from inquire.mongo import *
from inquire.socketio_app import io

import datetime

class Comments(Resource):
@permission_layer(require_joined_course=True)
Expand Down Expand Up @@ -42,6 +43,10 @@ def get(self, courseId=None, postId=None):
post = self.retrieve_post(postId)
if post is None:
return abort(400, errors=["Bad post id"])
# Marking that the current_user just viewed this post
if (course := current_user.get_course(courseId)) != None:
course.viewed[postId] = datetime.datetime.now()
current_user.save()

return [self.serialize(comment) for comment in Comment.objects.raw({'postId': postId})]

Expand Down
30 changes: 30 additions & 0 deletions server/inquire/resources/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

Last Modified Date: 03/12/2021
'''
import datetime
from flask import jsonify, request, current_app
from flask_restful import reqparse, Resource
from inquire.auth import current_user, permission_layer
Expand Down Expand Up @@ -99,6 +100,13 @@ def post(self, courseId=None):
return {"errors": str(exc)}

result = self.serialize(post)

# Marking that the creator of the post has viewed their own post
if (course := current_user.get_course(courseId)) != None:
course.viewed[result["_id"]] = datetime.datetime.now()
current_user.save()


if not result['isPrivate'] and current_app.config['include_socketio']:
current_app.socketio.emit('Post/create', result, room=courseId)
return result, 200
Expand Down Expand Up @@ -239,6 +247,11 @@ def get(self, courseId=None):

# Get the json for all the posts we want to display
result = [self.serialize(post) for post in query]
# Setting "read" property of each post based on UserCourse.viewed dict
if (course := current_user.get_course(courseId)) !=None:
viewed = course.viewed
for post in result:
self.set_read(post, viewed)

return result, 200

Expand Down Expand Up @@ -436,6 +449,23 @@ def serialize(self, post):
self.anonymize_poll_content(post["content"])
return result

def set_read(self, post, viewed):
"""Modifies a dictionary representing a post by setting a new key-value
pair of either "read": True or "read": False depending on the contents
of the viewed dict.

Args:
post (dict): Dictionary representing a post
viewed (dict): Dictionary representing the last time a user viewed the comments on a post.
Keys are post id strings, values are datetime objects. If post id is not a key in the dict,
the user has never viewed the post.
"""
post_id = post["_id"]
if post_id in viewed and viewed[post_id] > post['updatedDate']:
post['read'] = True
else:
post['read'] = False

def anonymize_poll_content(poll):
for field_name in poll["fields"]:
poll["fields"][field_name].pop("users")
Expand Down