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

♻️ Frontend: Notifications cleanup #4004

Merged
25 changes: 11 additions & 14 deletions api/specs/webserver/openapi-user.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ paths:
default:
$ref: "#/components/responses/DefaultErrorResponse"

/me/notifications/{nid}:
/me/notifications/{id}:
parameters:
- name: nid
- name: id
in: path
required: true
schema:
Expand All @@ -150,7 +150,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/NotificationUpdate'
$ref: "#/components/schemas/NotificationUpdate"
responses:
"204":
description: All good
Expand All @@ -177,30 +177,27 @@ components:
category:
type: string
enum:
- new_organization
- study_shared
- template_shared
- NEW_ORGANIZATION
- STUDY_SHARED
- TEMPLATE_SHARED
description: >-
notification type, the frontend will use this to decorate the notification
example: new_organization
actionable_path:
type: string
description: >-
the frontend will use this information to trigger an action when the user click on it
example:
organization/123
example: organization/123
title:
type: string
description: >-
the notification title to show in the frontend
example:
New Organization
example: New Organization
text:
type: string
description: >-
the notification text to show in the frontend
example:
You are now part of Dummy Organization!
example: You are now part of Dummy Organization!
date:
type: string
format: date-time
Expand All @@ -213,8 +210,8 @@ components:
- $ref: "#/components/schemas/NotificationIn"
- type: "object"
- required:
- id
- read
- id
- read
properties:
id:
type: string
Expand Down
4 changes: 2 additions & 2 deletions api/specs/webserver/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ paths:
/me/notifications:
$ref: "./openapi-user.yaml#/paths/~1me~1notifications"

/me/notifications/{nid}:
$ref: "./openapi-user.yaml#/paths/~1me~1notifications~1{nid}"
/me/notifications/{id}:
$ref: "./openapi-user.yaml#/paths/~1me~1notifications~1{id}"

# GROUP SETTINGS ------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ qx.Class.define("osparc.component.notification.Notification", {
},

category: {
check: ["new_organization", "study_shared", "template_shared"],
check: ["NEW_ORGANIZATION", "STUDY_SHARED", "TEMPLATE_SHARED"],
init: null,
nullable: false,
event: "changeCategory"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ qx.Class.define("osparc.component.notification.NotificationUI", {
converter: value => {
let source = "";
switch (value) {
case "new_organization":
case "NEW_ORGANIZATION":
source = "@FontAwesome5Solid/users/14";
break;
case "study_shared":
case "STUDY_SHARED":
source = "@FontAwesome5Solid/file/14";
break;
case "template_shared":
case "TEMPLATE_SHARED":
source = "@FontAwesome5Solid/copy/14";
break;
}
Expand Down Expand Up @@ -187,8 +187,8 @@ qx.Class.define("osparc.component.notification.NotificationUI", {
orgsWindow.openOrganizationDetails(parseInt(orgId));
break;
}
case "template_shared":
case "study_shared": {
case "TEMPLATE_SHARED":
case "STUDY_SHARED": {
const items = actionablePath.split("/");
const studyId = items.pop();
const params = {
Expand All @@ -200,7 +200,7 @@ qx.Class.define("osparc.component.notification.NotificationUI", {
.then(studyData => {
if (studyData) {
const studyDataCopy = osparc.data.model.Study.deepCloneStudyObject(studyData);
studyDataCopy["resourceType"] = notification.getCategory() === "study_shared" ? "study" : "template";
studyDataCopy["resourceType"] = notification.getCategory() === "STUDY_SHARED" ? "study" : "template";
const moreOpts = new osparc.dashboard.ResourceMoreOptions(studyData);
const title = this.tr("Options");
osparc.ui.window.Window.popUpInWindow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ qx.Class.define("osparc.component.notification.Notifications", {
__newOrganizationObj: function(userId, orgId) {
return {
"user_id": userId.toString(),
"category": "new_organization",
"category": "NEW_ORGANIZATION",
"actionable_path": "organization/"+orgId,
"title": "New organization",
"text": "You're now member of a new Organization",
Expand All @@ -39,7 +39,7 @@ qx.Class.define("osparc.component.notification.Notifications", {
__newStudyObj: function(userId, studyId) {
return {
"user_id": userId.toString(),
"category": "study_shared",
"category": "STUDY_SHARED",
"actionable_path": "study/"+studyId,
"title": "Study shared",
"text": "A study was shared with you",
Expand All @@ -50,7 +50,7 @@ qx.Class.define("osparc.component.notification.Notifications", {
__newTemplateObj: function(userId, templateId) {
return {
"user_id": userId.toString(),
"category": "template_shared",
"category": "TEMPLATE_SHARED",
"actionable_path": "template/"+templateId,
"title": "Template shared",
"text": "A template was shared with you",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ qx.Class.define("osparc.component.share.CollaboratorsStudy", {
})
.finally(() => cb());

// push 'study_shared'/'template_shared' notification
// push 'STUDY_SHARED'/'TEMPLATE_SHARED' notification
osparc.store.Store.getInstance().getPotentialCollaborators()
.then(potentialCollaborators => {
gids.forEach(gid => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ qx.Class.define("osparc.desktop.organizations.MembersList", {
osparc.store.Store.getInstance().reset("organizationMembers");
this.__reloadOrgMembers();

// push 'new_organization' notification
// push 'NEW_ORGANIZATION' notification
const params2 = {
url: {
"gid": orgId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1281,9 +1281,9 @@ paths:
category:
type: string
enum:
- new_organization
- study_shared
- template_shared
- NEW_ORGANIZATION
- STUDY_SHARED
- TEMPLATE_SHARED
description: 'notification type, the frontend will use this to decorate the notification'
example: new_organization
actionable_path:
Expand All @@ -1307,9 +1307,9 @@ paths:
description: Notification registered
default:
$ref: '#/components/responses/DefaultErrorResponse'
'/me/notifications/{nid}':
'/me/notifications/{id}':
parameters:
- name: nid
- name: id
in: path
required: true
schema:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from copy import deepcopy
from datetime import datetime
from enum import auto
from typing import Any, Final
from uuid import uuid4

from models_library.users import UserID
from models_library.utils.enums import StrAutoEnum
from pydantic import BaseModel, NonNegativeInt, validator

MAX_NOTIFICATIONS_FOR_USER: Final[NonNegativeInt] = 10


def get_notification_key(user_id: UserID) -> str:
return f"user_id={user_id}"


class NotificationCategory(StrAutoEnum):
NEW_ORGANIZATION = auto()
STUDY_SHARED = auto()
TEMPLATE_SHARED = auto()


class UserNotification(BaseModel):
id: str
GitHK marked this conversation as resolved.
Show resolved Hide resolved
user_id: UserID
category: NotificationCategory
actionable_path: str
title: str
text: str
date: datetime
read: bool

def update_from(self, data: dict[str, Any]) -> None:
for k, v in data.items():
self.__setattr__(k, v)

@validator("category", pre=True)
@classmethod
def category_to_upper(cls, value: str) -> str:
return value.upper()

@classmethod
def create_from_request_data(
cls, request_data: dict[str, Any]
) -> "UserNotification":
params = deepcopy(request_data)
params["id"] = f"{uuid4()}"
params["read"] = False
return cls.parse_obj(params)

class Config:
schema_extra = {
"examples": [
{
"id": "123",
GitHK marked this conversation as resolved.
Show resolved Hide resolved
"user_id": "1",
"category": "new_organization",
GitHK marked this conversation as resolved.
Show resolved Hide resolved
"actionable_path": "organization/40",
"title": "New organization",
"text": "You're now member of a new Organization",
"date": "2023-02-23T16:23:13.122Z",
"read": True,
},
{
"id": "456",
"user_id": "1",
"category": "STUDY_SHARED",
"actionable_path": "study/27edd65c-b360-11ed-93d7-02420a000014",
"title": "Study shared",
"text": "A study was shared with you",
"date": "2023-02-23T16:25:13.122Z",
"read": False,
},
{
"id": "789",
"user_id": "1",
"category": "TEMPLATE_SHARED",
"actionable_path": "template/f60477b6-a07e-11ed-8d29-02420a00002d",
"title": "Template shared",
"text": "A template was shared with you",
"date": "2023-02-23T16:28:13.122Z",
"read": False,
},
]
}
Loading