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

21 push notification #145

Merged
merged 23 commits into from Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 79 additions & 0 deletions app_backend/app/routers/notification.py
@@ -0,0 +1,79 @@
from fastapi import APIRouter, Response, status
from pydantic import BaseModel
from database import Database
from dotenv import load_dotenv
import os
from models.user import User

router = APIRouter(prefix="/notification")

load_dotenv()
db = Database(os.getenv("DB_USERNAME"), os.getenv("DB_PASSWORD"))
db.connect()

"""
add_device binds a device to a user

resp: Response
The response to send back to the user which contains the status code and body

device_code: str
The UUID of the friend to be added

returns Response
Response.body: dict
Provides any msgs/errs for the request
Response.status: int
The status code for the request
"""
@router.post("/add_device")
async def add_device(resp: Response, device_code: str):
user = User() # Todo: Get User from JWT
# Add the device to the user
devices = user.devices
if devices is None:
devices = [device_code]
elif device_code in devices: # Allows multiple add.
resp.body = {"msg": "Device already added"}
return resp
else:
devices.append(device_code)
# Update the user
db.db["users"].update_one({"_id": user.id}, {"$set": {"devices": devices}})

resp.status = status.HTTP_200_OK
resp.body = {"msg": "Device added successfully"}
return resp


"""
add_device binds a device to a user
DDVD233 marked this conversation as resolved.
Show resolved Hide resolved

resp: Response
The response to send back to the user which contains the status code and body

device_code: str
The UUID of the friend to be added

returns Response
Response.body: dict
Provides any msgs/errs for the request
Response.status: int
The status code for the request
"""
@router.post("/remove_device")
async def add_device(resp: Response, device_code: str):
user = User() # Todo: Get User from JWT
DDVD233 marked this conversation as resolved.
Show resolved Hide resolved
# Add the device to the user
devices = user.devices
if devices is None or device_code not in devices:
resp.body = {"msg": "Device not found"}
return resp
else:
devices.remove(device_code)
# Update the user
db.db["users"].update_one({"_id": user.id}, {"$set": {"devices": devices}})

resp.status = status.HTTP_200_OK
resp.body = {"msg": "Device removed successfully"}
return resp
15 changes: 1 addition & 14 deletions app_backend/app/routers/user.py
Expand Up @@ -2,24 +2,11 @@
from fastapi import APIRouter, status, Response
import re
from database import db
from models.user import User

router = APIRouter(prefix="/user")


"""
UserLogin model keeps track of the user login information
username: str
The username of the user
email: str
The email of the user
password: str
The password of the user
"""
class User(BaseModel):
name: str
email: str
password: str


"""
getuser implements the route /login/
Expand Down
Empty file.
41 changes: 41 additions & 0 deletions app_backend/app/utils/notify.py
@@ -0,0 +1,41 @@
from firebase_admin import messaging


def notify_one(token: str, title: str, body: str) -> str:
"""

:param token: The registration token of the device to which the message should be sent
:param title: Notification title
:param body: Notification body
:return:
Returns:
string: A message ID string that uniquely identifies the sent message.
"""
message = messaging.Message(
notification=messaging.Notification(
title=title,
body=body
),
token=token
)
return messaging.send(message)


def notify_multiple(tokens: list, title: str, body: str) -> str:
"""

:param tokens: The registration token of the device to which the message should be sent
:param title: Notification title
:param body: Notification body
:return:
Returns:
string: A message ID string that uniquely identifies the sent message.
"""
message = messaging.MulticastMessage(
tokens=tokens,
notification=messaging.Notification(
title=title,
body=body
)
)
return messaging.send(message)
Empty file.
37 changes: 37 additions & 0 deletions app_backend/models/user.py
@@ -0,0 +1,37 @@
from bson import ObjectId
from pydantic import BaseModel, Field
from typing import Optional, List


class PyObjectId(ObjectId):
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v):
if not ObjectId.is_valid(v):
raise ValueError("Invalid objectid")
return ObjectId(v)
@classmethod
def __modify_schema__(cls, field_schema):
field_schema.update(type="string")


"""
UserLogin model keeps track of the user login information
username: str
The username of the user
email: str
The email of the user
password: str
The password of the user
"""
class User(BaseModel):
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id")
name: str
email: str
password: str
devices: List[str] = []
friends: List[PyObjectId] = []
favorites: List[str] = []
is_sharing_location: bool = False
1 change: 1 addition & 0 deletions app_backend/requirements.txt
Expand Up @@ -31,3 +31,4 @@ typing-extensions==3.10.0.2
urllib3==1.26.7
uvicorn==0.15.0
wrapt==1.12.1
firebase_admin
DDVD233 marked this conversation as resolved.
Show resolved Hide resolved