diff --git a/create_crt.sh b/create_crt.sh old mode 100644 new mode 100755 index d36eec87..9a5264bb --- a/create_crt.sh +++ b/create_crt.sh @@ -7,4 +7,4 @@ openssl req -newkey rsa:4096 \ -nodes \ -out discours.crt \ -keyout discours.key \ - -subj "/C=RU/ST=Moscow/L=Moscow/O=Discours/OU=Site/CN=test-api.discours.io" + -subj "/C=RU/ST=Moscow/L=Moscow/O=Discours/OU=Site/CN=build.discours.io" diff --git a/migration/tables/content_item_categories.py b/migration/tables/content_item_categories.py index c2965f5e..08fcef85 100644 --- a/migration/tables/content_item_categories.py +++ b/migration/tables/content_item_categories.py @@ -15,8 +15,8 @@ def migrate(entry): ''' topic_dict = { 'slug': entry['slug'], - 'createdBy': entry['createdBy'], - 'createdAt': date_parse(entry['createdAt']), + # 'createdBy': entry['createdBy'], + # 'createdAt': date_parse(entry['createdAt']), 'title': entry['title'].lower(), 'parents': [], 'children': [], diff --git a/migration/tables/tags.py b/migration/tables/tags.py index 9a35301d..afe0c09b 100644 --- a/migration/tables/tags.py +++ b/migration/tables/tags.py @@ -21,8 +21,8 @@ def migrate(entry): ts = datetime.fromtimestamp(entry['createdAt']/1000) topic_dict = { 'slug': entry['slug'], - 'createdBy': entry['createdBy'], - 'createdAt': ts, + # 'createdBy': entry['createdBy'], + # 'createdAt': ts, 'title': entry['title'].lower(), 'parents': [], 'children': [] diff --git a/orm/__init__.py b/orm/__init__.py index 815d7c9a..8bdfdfc5 100644 --- a/orm/__init__.py +++ b/orm/__init__.py @@ -2,14 +2,14 @@ from orm.rbac import Operation, Resource, Permission, Role from orm.community import Community from orm.user import User, UserRating from orm.message import Message -from orm.topic import Topic +from orm.topic import Topic, TopicSubscription from orm.notification import Notification from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutRating, ShoutViewByDay,\ ShoutRatingStorage, ShoutViewStorage from orm.base import Base, engine, local_session from orm.comment import Comment, CommentRating -__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "Notification", "ShoutRating", "Comment", "CommentRating", "UserRating"] +__all__ = ["User", "Role", "Operation", "Permission", "Message", "Shout", "Topic", "TopicSubscription", "Notification", "ShoutRating", "Comment", "CommentRating", "UserRating"] Base.metadata.create_all(engine) Operation.init_table() diff --git a/orm/topic.py b/orm/topic.py index a437e305..f10a1195 100644 --- a/orm/topic.py +++ b/orm/topic.py @@ -11,13 +11,18 @@ Connection = Table('topic_connections', UniqueConstraint('parent', 'child', name='unique_usage') ) +class TopicSubscription(Base): + __tablename__ = "topic_subscrptions" + + id = None + topic = Column(ForeignKey('topic.slug'), primary_key = True) + user = Column(ForeignKey('user.id'), primary_key = True) + createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at") class Topic(Base): __tablename__ = 'topic' slug: str = Column(String, unique = True, nullable = False) - createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at") - createdBy: str = Column(ForeignKey("user.id"), nullable=False, comment="Author") title: str = Column(String, nullable=False, comment="Title") body: str = Column(String, nullable=True, comment="Body") pic: str = Column(String, nullable=True, comment="Picture") @@ -26,4 +31,5 @@ class Topic(Base): parents = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.parent, secondaryjoin=slug==Connection.c.child, viewonly=True) # list of Topics where the current node is the "parent" children = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.child, secondaryjoin=slug==Connection.c.parent) + community = Column(ForeignKey("community.slug"), nullable=True, comment="Community") diff --git a/resolvers/__init__.py b/resolvers/__init__.py index c6508c86..e0cc0b8b 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -2,6 +2,7 @@ from resolvers.auth import login, sign_out, is_email_free, register, confirm from resolvers.inbox import create_message, delete_message, update_message, get_messages from resolvers.zine import create_shout, get_shout_by_slug from resolvers.profile import get_user_by_slug, get_current_user +from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, topics_by_community, topics_by_slugs __all__ = [ "login", @@ -17,5 +18,10 @@ __all__ = [ "create_shout", "get_current_user", "get_user_by_slug", - "get_shout_by_slug" + "get_shout_by_slug", + "topics_by_slugs", + "topics_by_community", + "topics_by_author", + "topic_subscribe", + "topic_unsubscribe" ] diff --git a/resolvers/topics.py b/resolvers/topics.py new file mode 100644 index 00000000..05fda722 --- /dev/null +++ b/resolvers/topics.py @@ -0,0 +1,45 @@ +from orm import Topic, TopicSubscription +from orm.base import local_session +from resolvers.base import mutation, query, subscription +from auth.authenticate import login_required +import asyncio + +@query.field("topicsBySlugs") +async def topics_by_slugs(_, info, slugs): + topics = [] + with local_session() as session: + topics = session.query(Topic).filter(Topic.slug in slugs) + return topics + +@query.field("topicsByCommunity") +async def topics_by_community(_, info, community): + topics = [] + with local_session() as session: + topics = session.query(Topic).filter(Topic.community == community) + return topics + +@query.field("topicsByAuthor") +async def topics_by_author(_, info, author): + topics = [] + with local_session() as session: + topics = session.query(Topic).filter(Topic.community == community) + return topics + +@mutation.field("topicSubscribe") +@login_required +async def topic_subscribe(_, info, slug): + auth = info.context["request"].auth + user_id = auth.user_id + sub = TopicSubscription.create({ user: user_id, topic: slug }) + return {} # type Result + +@mutation.field("topicUnsubscribe") +@login_required +async def topic_unsubscribe(_, info, slug): + auth = info.context["request"].auth + user_id = auth.user_id + sub = session.query(TopicSubscription).filter(TopicSubscription.user == user_id and TopicSubscription.topic == slug).first() + with local_session() as session: + session.delete(sub) + return {} # type Result + return { "error": "session error" } diff --git a/schema.graphql b/schema.graphql index 65f4c73d..abb521cd 100644 --- a/schema.graphql +++ b/schema.graphql @@ -75,6 +75,10 @@ type Mutation { # rateUser(value: Int!): Result! # updateOnlineStatus: Result! updateProfile(profile: ProfileInput!): Result! + + # topics + topicSubscribe(slug: String!): Result! + topicUnsubscribe(slug: String!): Result! } ################################### Query @@ -107,6 +111,11 @@ type Query { favoritesShouts(limit: Int): [Shout]! topAuthors(limit: Int): [User]! + # topics + topicsBySlugs(slugs: [String]!): [Topic]! + topicsByCommunity(community: String!): [Topic]! + topicsByAuthor(author: String!): [Topic]! + # getOnlineUsers: [User!]! } @@ -120,6 +129,7 @@ type Subscription { onlineUpdated: [User!]! shoutUpdated: Shout! userUpdated: User! + topicUpdated: Shout! } ############################################ Entities @@ -253,8 +263,6 @@ type Topic { title: String body: String pic: String - createdBy: Int! # User - createdAt: DateTime! parents: [String] # NOTE: topic can have parent topics children: [String] # and children cat_id: String diff --git a/server.py b/server.py index 8ff87e58..3c96e7ff 100644 --- a/server.py +++ b/server.py @@ -5,7 +5,8 @@ import sys if __name__ == '__main__': dev_mode = len(sys.argv) > 1 and sys.argv[1] == "dev" - if dev_mode : + if dev_mode: + print("DEV MODE") uvicorn.run("main:app", host="0.0.0.0", port=8080, ssl_keyfile="discours.key", ssl_certfile="discours.crt", reload=True) else : uvicorn.run("main:app", host="0.0.0.0", port=PORT)