Merge pull request #100 from Discours/revert-99-feature/lint
Revert "Feature/lint"
This commit is contained in:
commit
17c29c7f4f
6
.flake8
6
.flake8
|
@ -1,6 +1,6 @@
|
||||||
[flake8]
|
[flake8]
|
||||||
ignore = E203
|
ignore = E203,W504,W191,W503
|
||||||
exclude = .git,__pycache__,orm/rbac.py
|
exclude = .git,__pycache__,orm/rbac.py
|
||||||
max-complexity = 15
|
max-complexity = 10
|
||||||
max-line-length = 100
|
max-line-length = 108
|
||||||
indent-string = ' '
|
indent-string = ' '
|
||||||
|
|
|
@ -6,7 +6,7 @@ exclude: |
|
||||||
)
|
)
|
||||||
|
|
||||||
default_language_version:
|
default_language_version:
|
||||||
python: python3.10
|
python: python3.8
|
||||||
|
|
||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
@ -17,23 +17,28 @@ repos:
|
||||||
- id: check-docstring-first
|
- id: check-docstring-first
|
||||||
- id: check-json
|
- id: check-json
|
||||||
- id: check-merge-conflict
|
- id: check-merge-conflict
|
||||||
|
- id: check-toml
|
||||||
- id: check-yaml
|
- id: check-yaml
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
|
|
||||||
# - repo: https://github.com/timothycrosley/isort
|
- repo: https://github.com/timothycrosley/isort
|
||||||
# rev: 5.12.0
|
rev: 5.5.3
|
||||||
# hooks:
|
hooks:
|
||||||
# - id: isort
|
- id: isort
|
||||||
|
|
||||||
- repo: https://github.com/ambv/black
|
- repo: https://github.com/ambv/black
|
||||||
rev: 23.9.1
|
rev: 20.8b1
|
||||||
hooks:
|
hooks:
|
||||||
- id: black
|
- id: black
|
||||||
args:
|
args:
|
||||||
- --line-length=100
|
- --line-length=100
|
||||||
|
- --skip-string-normalization
|
||||||
|
|
||||||
- repo: https://github.com/PyCQA/flake8
|
- repo: https://gitlab.com/pycqa/flake8
|
||||||
rev: 6.1.0
|
rev: 3.8.3
|
||||||
hooks:
|
hooks:
|
||||||
- id: flake8
|
- id: flake8
|
||||||
|
args:
|
||||||
|
- --max-line-length=100
|
||||||
|
- --disable=protected-access
|
||||||
|
|
|
@ -35,13 +35,6 @@ pip install -r requirements.txt
|
||||||
python3 server.py dev
|
python3 server.py dev
|
||||||
```
|
```
|
||||||
|
|
||||||
# pre-commit hook
|
|
||||||
|
|
||||||
```
|
|
||||||
pip install -r requirements-dev.txt
|
|
||||||
pre-commit install
|
|
||||||
```
|
|
||||||
|
|
||||||
# How to do an authorized request
|
# How to do an authorized request
|
||||||
|
|
||||||
Put the header 'Authorization' with token from signIn query or registerUser mutation.
|
Put the header 'Authorization' with token from signIn query or registerUser mutation.
|
||||||
|
@ -49,3 +42,4 @@ Put the header 'Authorization' with token from signIn query or registerUser muta
|
||||||
# How to debug Ackee
|
# How to debug Ackee
|
||||||
|
|
||||||
Set ACKEE_TOKEN var
|
Set ACKEE_TOKEN var
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,28 @@
|
||||||
|
import re
|
||||||
|
import nltk
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from nltk.corpus import stopwords
|
from nltk.corpus import stopwords
|
||||||
from pymystem3 import Mystem
|
from pymystem3 import Mystem
|
||||||
from string import punctuation
|
from string import punctuation
|
||||||
|
from transformers import BertTokenizer
|
||||||
import nltk
|
|
||||||
import re
|
|
||||||
|
|
||||||
nltk.download("stopwords")
|
nltk.download("stopwords")
|
||||||
|
|
||||||
|
|
||||||
def get_clear_text(text):
|
def get_clear_text(text):
|
||||||
soup = BeautifulSoup(text, "html.parser")
|
soup = BeautifulSoup(text, 'html.parser')
|
||||||
|
|
||||||
# extract the plain text from the HTML document without tags
|
# extract the plain text from the HTML document without tags
|
||||||
clear_text = ""
|
clear_text = ''
|
||||||
for tag in soup.find_all():
|
for tag in soup.find_all():
|
||||||
clear_text += tag.string or ""
|
clear_text += tag.string or ''
|
||||||
|
|
||||||
clear_text = re.sub(pattern="[\u202F\u00A0\n]+", repl=" ", string=clear_text)
|
clear_text = re.sub(pattern='[\u202F\u00A0\n]+', repl=' ', string=clear_text)
|
||||||
|
|
||||||
# only words
|
# only words
|
||||||
clear_text = re.sub(pattern="[^A-ZА-ЯЁ -]", repl="", string=clear_text, flags=re.IGNORECASE)
|
clear_text = re.sub(pattern='[^A-ZА-ЯЁ -]', repl='', string=clear_text, flags=re.IGNORECASE)
|
||||||
|
|
||||||
clear_text = re.sub(pattern=r"\s+", repl=" ", string=clear_text)
|
clear_text = re.sub(pattern='\s+', repl=' ', string=clear_text)
|
||||||
|
|
||||||
clear_text = clear_text.lower()
|
clear_text = clear_text.lower()
|
||||||
|
|
||||||
|
@ -30,11 +30,9 @@ def get_clear_text(text):
|
||||||
russian_stopwords = stopwords.words("russian")
|
russian_stopwords = stopwords.words("russian")
|
||||||
|
|
||||||
tokens = mystem.lemmatize(clear_text)
|
tokens = mystem.lemmatize(clear_text)
|
||||||
tokens = [
|
tokens = [token for token in tokens if token not in russian_stopwords \
|
||||||
token
|
and token != " " \
|
||||||
for token in tokens
|
and token.strip() not in punctuation]
|
||||||
if token not in russian_stopwords and token != " " and token.strip() not in punctuation
|
|
||||||
]
|
|
||||||
|
|
||||||
clear_text = " ".join(tokens)
|
clear_text = " ".join(tokens)
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
from alembic import context
|
|
||||||
from base.orm import Base
|
|
||||||
from logging.config import fileConfig
|
from logging.config import fileConfig
|
||||||
|
|
||||||
|
from sqlalchemy import engine_from_config
|
||||||
|
from sqlalchemy import pool
|
||||||
|
|
||||||
|
from alembic import context
|
||||||
|
|
||||||
from settings import DB_URL
|
from settings import DB_URL
|
||||||
from sqlalchemy import engine_from_config, pool
|
|
||||||
|
|
||||||
# this is the Alembic Config object, which provides
|
# this is the Alembic Config object, which provides
|
||||||
# access to the values within the .ini file in use.
|
# access to the values within the .ini file in use.
|
||||||
|
@ -16,6 +19,7 @@ config.set_section_option(config.config_ini_section, "DB_URL", DB_URL)
|
||||||
if config.config_file_name is not None:
|
if config.config_file_name is not None:
|
||||||
fileConfig(config.config_file_name)
|
fileConfig(config.config_file_name)
|
||||||
|
|
||||||
|
from base.orm import Base
|
||||||
target_metadata = [Base.metadata]
|
target_metadata = [Base.metadata]
|
||||||
|
|
||||||
# other values from the config, defined by the needs of env.py,
|
# other values from the config, defined by the needs of env.py,
|
||||||
|
@ -62,7 +66,9 @@ def run_migrations_online() -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
with connectable.connect() as connection:
|
with connectable.connect() as connection:
|
||||||
context.configure(connection=connection, target_metadata=target_metadata)
|
context.configure(
|
||||||
|
connection=connection, target_metadata=target_metadata
|
||||||
|
)
|
||||||
|
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
|
|
|
@ -7,12 +7,12 @@ Create Date: 2023-08-19 01:37:57.031933
|
||||||
"""
|
"""
|
||||||
from typing import Sequence, Union
|
from typing import Sequence, Union
|
||||||
|
|
||||||
# import sqlalchemy as sa
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
# from alembic import op
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision: str = "fe943b098418"
|
revision: str = 'fe943b098418'
|
||||||
down_revision: Union[str, None] = None
|
down_revision: Union[str, None] = None
|
||||||
branch_labels: Union[str, Sequence[str], None] = None
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
depends_on: Union[str, Sequence[str], None] = None
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
|
@ -1,70 +1,76 @@
|
||||||
from auth.credentials import AuthCredentials, AuthUser
|
|
||||||
from auth.tokenstorage import SessionToken
|
|
||||||
from base.exceptions import OperationNotAllowed
|
|
||||||
from base.orm import local_session
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
from graphql.type import GraphQLResolveInfo
|
from graphql.type import GraphQLResolveInfo
|
||||||
from orm.user import Role, User
|
from sqlalchemy.orm import joinedload, exc
|
||||||
from settings import SESSION_TOKEN_HEADER
|
|
||||||
from sqlalchemy.orm import exc, joinedload
|
|
||||||
from starlette.authentication import AuthenticationBackend
|
from starlette.authentication import AuthenticationBackend
|
||||||
from starlette.requests import HTTPConnection
|
from starlette.requests import HTTPConnection
|
||||||
from typing import Optional, Tuple
|
|
||||||
|
from auth.credentials import AuthCredentials, AuthUser
|
||||||
|
from base.orm import local_session
|
||||||
|
from orm.user import User, Role
|
||||||
|
|
||||||
|
from settings import SESSION_TOKEN_HEADER
|
||||||
|
from auth.tokenstorage import SessionToken
|
||||||
|
from base.exceptions import OperationNotAllowed
|
||||||
|
|
||||||
|
|
||||||
class JWTAuthenticate(AuthenticationBackend):
|
class JWTAuthenticate(AuthenticationBackend):
|
||||||
async def authenticate(
|
async def authenticate(
|
||||||
self, request: HTTPConnection
|
self, request: HTTPConnection
|
||||||
) -> Optional[Tuple[AuthCredentials, AuthUser]]:
|
) -> Optional[Tuple[AuthCredentials, AuthUser]]:
|
||||||
|
|
||||||
if SESSION_TOKEN_HEADER not in request.headers:
|
if SESSION_TOKEN_HEADER not in request.headers:
|
||||||
return AuthCredentials(scopes={}), AuthUser(user_id=None, username="")
|
return AuthCredentials(scopes={}), AuthUser(user_id=None, username='')
|
||||||
|
|
||||||
token = request.headers.get(SESSION_TOKEN_HEADER)
|
token = request.headers.get(SESSION_TOKEN_HEADER)
|
||||||
if not token:
|
if not token:
|
||||||
print("[auth.authenticate] no token in header %s" % SESSION_TOKEN_HEADER)
|
print("[auth.authenticate] no token in header %s" % SESSION_TOKEN_HEADER)
|
||||||
return AuthCredentials(scopes={}, error_message=str("no token")), AuthUser(
|
return AuthCredentials(scopes={}, error_message=str("no token")), AuthUser(
|
||||||
user_id=None, username=""
|
user_id=None, username=''
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(token.split(".")) > 1:
|
if len(token.split('.')) > 1:
|
||||||
payload = await SessionToken.verify(token)
|
payload = await SessionToken.verify(token)
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
try:
|
try:
|
||||||
user = (
|
user = (
|
||||||
session.query(User)
|
session.query(User).options(
|
||||||
.options(
|
|
||||||
joinedload(User.roles).options(joinedload(Role.permissions)),
|
joinedload(User.roles).options(joinedload(Role.permissions)),
|
||||||
joinedload(User.ratings),
|
joinedload(User.ratings)
|
||||||
)
|
).filter(
|
||||||
.filter(User.id == payload.user_id)
|
User.id == payload.user_id
|
||||||
.one()
|
).one()
|
||||||
)
|
)
|
||||||
|
|
||||||
scopes = {} # TODO: integrate await user.get_permission()
|
scopes = {} # TODO: integrate await user.get_permission()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
AuthCredentials(user_id=payload.user_id, scopes=scopes, logged_in=True),
|
AuthCredentials(
|
||||||
AuthUser(user_id=user.id, username=""),
|
user_id=payload.user_id,
|
||||||
|
scopes=scopes,
|
||||||
|
logged_in=True
|
||||||
|
),
|
||||||
|
AuthUser(user_id=user.id, username=''),
|
||||||
)
|
)
|
||||||
except exc.NoResultFound:
|
except exc.NoResultFound:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return AuthCredentials(scopes={}, error_message=str("Invalid token")), AuthUser(
|
return AuthCredentials(scopes={}, error_message=str('Invalid token')), AuthUser(user_id=None, username='')
|
||||||
user_id=None, username=""
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def login_required(func):
|
def login_required(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
async def wrap(parent, info: GraphQLResolveInfo, *args, **kwargs):
|
async def wrap(parent, info: GraphQLResolveInfo, *args, **kwargs):
|
||||||
# debug only
|
# print('[auth.authenticate] login required for %r with info %r' % (func, info)) # debug only
|
||||||
# print('[auth.authenticate] login required for %r with info %r' % (func, info))
|
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
# print(auth)
|
# print(auth)
|
||||||
if not auth or not auth.logged_in:
|
if not auth or not auth.logged_in:
|
||||||
# raise Unauthorized(auth.error_message or "Please login")
|
# raise Unauthorized(auth.error_message or "Please login")
|
||||||
return {"error": "Please login first"}
|
return {
|
||||||
|
"error": "Please login first"
|
||||||
|
}
|
||||||
return await func(parent, info, *args, **kwargs)
|
return await func(parent, info, *args, **kwargs)
|
||||||
|
|
||||||
return wrap
|
return wrap
|
||||||
|
@ -73,9 +79,7 @@ def login_required(func):
|
||||||
def permission_required(resource, operation, func):
|
def permission_required(resource, operation, func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
async def wrap(parent, info: GraphQLResolveInfo, *args, **kwargs):
|
async def wrap(parent, info: GraphQLResolveInfo, *args, **kwargs):
|
||||||
print(
|
print('[auth.authenticate] permission_required for %r with info %r' % (func, info)) # debug only
|
||||||
"[auth.authenticate] permission_required for %r with info %r" % (func, info)
|
|
||||||
) # debug only
|
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
if not auth.logged_in:
|
if not auth.logged_in:
|
||||||
raise OperationNotAllowed(auth.error_message or "Please login")
|
raise OperationNotAllowed(auth.error_message or "Please login")
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from pydantic import BaseModel
|
|
||||||
from typing import List, Optional, Text
|
from typing import List, Optional, Text
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
# from base.exceptions import Unauthorized
|
# from base.exceptions import Unauthorized
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,7 +23,9 @@ class AuthCredentials(BaseModel):
|
||||||
async def permissions(self) -> List[Permission]:
|
async def permissions(self) -> List[Permission]:
|
||||||
if self.user_id is None:
|
if self.user_id is None:
|
||||||
# raise Unauthorized("Please login first")
|
# raise Unauthorized("Please login first")
|
||||||
return {"error": "Please login first"}
|
return {
|
||||||
|
"error": "Please login first"
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
# TODO: implement permissions logix
|
# TODO: implement permissions logix
|
||||||
print(self.user_id)
|
print(self.user_id)
|
||||||
|
|
|
@ -1,17 +1,20 @@
|
||||||
from settings import MAILGUN_API_KEY, MAILGUN_DOMAIN
|
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
api_url = "https://api.mailgun.net/v3/%s/messages" % (MAILGUN_DOMAIN or "discours.io")
|
from settings import MAILGUN_API_KEY, MAILGUN_DOMAIN
|
||||||
noreply = "discours.io <noreply@%s>" % (MAILGUN_DOMAIN or "discours.io")
|
|
||||||
lang_subject = {"ru": "Подтверждение почты", "en": "Confirm email"}
|
api_url = "https://api.mailgun.net/v3/%s/messages" % (MAILGUN_DOMAIN or 'discours.io')
|
||||||
|
noreply = "discours.io <noreply@%s>" % (MAILGUN_DOMAIN or 'discours.io')
|
||||||
|
lang_subject = {
|
||||||
|
"ru": "Подтверждение почты",
|
||||||
|
"en": "Confirm email"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def send_auth_email(user, token, lang="ru", template="email_confirmation"):
|
async def send_auth_email(user, token, lang="ru", template="email_confirmation"):
|
||||||
try:
|
try:
|
||||||
to = "%s <%s>" % (user.name, user.email)
|
to = "%s <%s>" % (user.name, user.email)
|
||||||
if lang not in ["ru", "en"]:
|
if lang not in ['ru', 'en']:
|
||||||
lang = "ru"
|
lang = 'ru'
|
||||||
subject = lang_subject.get(lang, lang_subject["en"])
|
subject = lang_subject.get(lang, lang_subject["en"])
|
||||||
template = template + "_" + lang
|
template = template + "_" + lang
|
||||||
payload = {
|
payload = {
|
||||||
|
@ -19,12 +22,16 @@ async def send_auth_email(user, token, lang="ru", template="email_confirmation")
|
||||||
"to": to,
|
"to": to,
|
||||||
"subject": subject,
|
"subject": subject,
|
||||||
"template": template,
|
"template": template,
|
||||||
"h:X-Mailgun-Variables": '{ "token": "%s" }' % token,
|
"h:X-Mailgun-Variables": "{ \"token\": \"%s\" }" % token
|
||||||
}
|
}
|
||||||
print("[auth.email] payload: %r" % payload)
|
print('[auth.email] payload: %r' % payload)
|
||||||
# debug
|
# debug
|
||||||
# print('http://localhost:3000/?modal=auth&mode=confirm-email&token=%s' % token)
|
# print('http://localhost:3000/?modal=auth&mode=confirm-email&token=%s' % token)
|
||||||
response = requests.post(api_url, auth=("api", MAILGUN_API_KEY), data=payload)
|
response = requests.post(
|
||||||
|
api_url,
|
||||||
|
auth=("api", MAILGUN_API_KEY),
|
||||||
|
data=payload
|
||||||
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
from auth.jwtcodec import JWTCodec
|
|
||||||
from auth.tokenstorage import TokenStorage
|
|
||||||
|
|
||||||
# from base.exceptions import InvalidPassword, InvalidToken
|
|
||||||
from base.orm import local_session
|
|
||||||
from binascii import hexlify
|
from binascii import hexlify
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
|
|
||||||
from jwt import DecodeError, ExpiredSignatureError
|
from jwt import DecodeError, ExpiredSignatureError
|
||||||
from orm import User
|
|
||||||
from passlib.hash import bcrypt
|
from passlib.hash import bcrypt
|
||||||
from sqlalchemy import or_
|
from sqlalchemy import or_
|
||||||
|
|
||||||
|
from auth.jwtcodec import JWTCodec
|
||||||
|
from auth.tokenstorage import TokenStorage
|
||||||
|
# from base.exceptions import InvalidPassword, InvalidToken
|
||||||
|
from base.orm import local_session
|
||||||
|
from orm import User
|
||||||
from validations.auth import AuthInput
|
from validations.auth import AuthInput
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,7 +34,6 @@ class Password:
|
||||||
Verify that password hash is equal to specified hash. Hash format:
|
Verify that password hash is equal to specified hash. Hash format:
|
||||||
|
|
||||||
$2a$10$Ro0CUfOqk6cXEKf3dyaM7OhSCvnwM9s4wIX9JeLapehKK5YdLxKcm
|
$2a$10$Ro0CUfOqk6cXEKf3dyaM7OhSCvnwM9s4wIX9JeLapehKK5YdLxKcm
|
||||||
# noqa: W605
|
|
||||||
\__/\/ \____________________/\_____________________________/
|
\__/\/ \____________________/\_____________________________/
|
||||||
| | Salt Hash
|
| | Salt Hash
|
||||||
| Cost
|
| Cost
|
||||||
|
@ -57,10 +57,14 @@ class Identity:
|
||||||
user = User(**orm_user.dict())
|
user = User(**orm_user.dict())
|
||||||
if not user.password:
|
if not user.password:
|
||||||
# raise InvalidPassword("User password is empty")
|
# raise InvalidPassword("User password is empty")
|
||||||
return {"error": "User password is empty"}
|
return {
|
||||||
|
"error": "User password is empty"
|
||||||
|
}
|
||||||
if not Password.verify(password, user.password):
|
if not Password.verify(password, user.password):
|
||||||
# raise InvalidPassword("Wrong user password")
|
# raise InvalidPassword("Wrong user password")
|
||||||
return {"error": "Wrong user password"}
|
return {
|
||||||
|
"error": "Wrong user password"
|
||||||
|
}
|
||||||
return user
|
return user
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -83,22 +87,30 @@ class Identity:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def onetime(token: str) -> User:
|
async def onetime(token: str) -> User:
|
||||||
try:
|
try:
|
||||||
print("[auth.identity] using one time token")
|
print('[auth.identity] using one time token')
|
||||||
payload = JWTCodec.decode(token)
|
payload = JWTCodec.decode(token)
|
||||||
if not await TokenStorage.exist(f"{payload.user_id}-{payload.username}-{token}"):
|
if not await TokenStorage.exist(f"{payload.user_id}-{payload.username}-{token}"):
|
||||||
# raise InvalidToken("Login token has expired, please login again")
|
# raise InvalidToken("Login token has expired, please login again")
|
||||||
return {"error": "Token has expired"}
|
return {
|
||||||
|
"error": "Token has expired"
|
||||||
|
}
|
||||||
except ExpiredSignatureError:
|
except ExpiredSignatureError:
|
||||||
# raise InvalidToken("Login token has expired, please try again")
|
# raise InvalidToken("Login token has expired, please try again")
|
||||||
return {"error": "Token has expired"}
|
return {
|
||||||
|
"error": "Token has expired"
|
||||||
|
}
|
||||||
except DecodeError:
|
except DecodeError:
|
||||||
# raise InvalidToken("token format error") from e
|
# raise InvalidToken("token format error") from e
|
||||||
return {"error": "Token format error"}
|
return {
|
||||||
|
"error": "Token format error"
|
||||||
|
}
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
user = session.query(User).filter_by(id=payload.user_id).first()
|
user = session.query(User).filter_by(id=payload.user_id).first()
|
||||||
if not user:
|
if not user:
|
||||||
# raise Exception("user not exist")
|
# raise Exception("user not exist")
|
||||||
return {"error": "User does not exist"}
|
return {
|
||||||
|
"error": "User does not exist"
|
||||||
|
}
|
||||||
if not user.emailConfirmed:
|
if not user.emailConfirmed:
|
||||||
user.emailConfirmed = True
|
user.emailConfirmed = True
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
from base.exceptions import ExpiredToken, InvalidToken
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from settings import JWT_ALGORITHM, JWT_SECRET_KEY
|
|
||||||
from validations.auth import AuthInput, TokenPayload
|
|
||||||
|
|
||||||
import jwt
|
import jwt
|
||||||
|
from base.exceptions import ExpiredToken, InvalidToken
|
||||||
|
from validations.auth import TokenPayload, AuthInput
|
||||||
|
from settings import JWT_ALGORITHM, JWT_SECRET_KEY
|
||||||
|
|
||||||
|
|
||||||
class JWTCodec:
|
class JWTCodec:
|
||||||
|
@ -14,12 +13,12 @@ class JWTCodec:
|
||||||
"username": user.email or user.phone,
|
"username": user.email or user.phone,
|
||||||
"exp": exp,
|
"exp": exp,
|
||||||
"iat": datetime.now(tz=timezone.utc),
|
"iat": datetime.now(tz=timezone.utc),
|
||||||
"iss": "discours",
|
"iss": "discours"
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
return jwt.encode(payload, JWT_SECRET_KEY, JWT_ALGORITHM)
|
return jwt.encode(payload, JWT_SECRET_KEY, JWT_ALGORITHM)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("[auth.jwtcodec] JWT encode error %r" % e)
|
print('[auth.jwtcodec] JWT encode error %r' % e)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def decode(token: str, verify_exp: bool = True) -> TokenPayload:
|
def decode(token: str, verify_exp: bool = True) -> TokenPayload:
|
||||||
|
@ -34,18 +33,18 @@ class JWTCodec:
|
||||||
# "verify_signature": False
|
# "verify_signature": False
|
||||||
},
|
},
|
||||||
algorithms=[JWT_ALGORITHM],
|
algorithms=[JWT_ALGORITHM],
|
||||||
issuer="discours",
|
issuer="discours"
|
||||||
)
|
)
|
||||||
r = TokenPayload(**payload)
|
r = TokenPayload(**payload)
|
||||||
# print('[auth.jwtcodec] debug token %r' % r)
|
# print('[auth.jwtcodec] debug token %r' % r)
|
||||||
return r
|
return r
|
||||||
except jwt.InvalidIssuedAtError:
|
except jwt.InvalidIssuedAtError:
|
||||||
print("[auth.jwtcodec] invalid issued at: %r" % payload)
|
print('[auth.jwtcodec] invalid issued at: %r' % payload)
|
||||||
raise ExpiredToken("check token issued time")
|
raise ExpiredToken('check token issued time')
|
||||||
except jwt.ExpiredSignatureError:
|
except jwt.ExpiredSignatureError:
|
||||||
print("[auth.jwtcodec] expired signature %r" % payload)
|
print('[auth.jwtcodec] expired signature %r' % payload)
|
||||||
raise ExpiredToken("check token lifetime")
|
raise ExpiredToken('check token lifetime')
|
||||||
except jwt.InvalidTokenError:
|
except jwt.InvalidTokenError:
|
||||||
raise InvalidToken("token is not valid")
|
raise InvalidToken('token is not valid')
|
||||||
except jwt.InvalidSignatureError:
|
except jwt.InvalidSignatureError:
|
||||||
raise InvalidToken("token is not valid")
|
raise InvalidToken('token is not valid')
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
|
from authlib.integrations.starlette_client import OAuth
|
||||||
|
from starlette.responses import RedirectResponse
|
||||||
from auth.identity import Identity
|
from auth.identity import Identity
|
||||||
from auth.tokenstorage import TokenStorage
|
from auth.tokenstorage import TokenStorage
|
||||||
from authlib.integrations.starlette_client import OAuth
|
from settings import OAUTH_CLIENTS, FRONTEND_URL
|
||||||
from settings import FRONTEND_URL, OAUTH_CLIENTS
|
|
||||||
from starlette.responses import RedirectResponse
|
|
||||||
|
|
||||||
oauth = OAuth()
|
oauth = OAuth()
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
from auth.jwtcodec import JWTCodec
|
|
||||||
from base.redis import redis
|
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from settings import ONETIME_TOKEN_LIFE_SPAN, SESSION_TOKEN_LIFE_SPAN
|
|
||||||
|
from auth.jwtcodec import JWTCodec
|
||||||
from validations.auth import AuthInput
|
from validations.auth import AuthInput
|
||||||
|
from base.redis import redis
|
||||||
|
from settings import SESSION_TOKEN_LIFE_SPAN, ONETIME_TOKEN_LIFE_SPAN
|
||||||
|
|
||||||
|
|
||||||
async def save(token_key, life_span, auto_delete=True):
|
async def save(token_key, life_span, auto_delete=True):
|
||||||
|
@ -34,7 +35,7 @@ class SessionToken:
|
||||||
class TokenStorage:
|
class TokenStorage:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get(token_key):
|
async def get(token_key):
|
||||||
print("[tokenstorage.get] " + token_key)
|
print('[tokenstorage.get] ' + token_key)
|
||||||
# 2041-user@domain.zn-eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoyMDQxLCJ1c2VybmFtZSI6ImFudG9uLnJld2luK3Rlc3QtbG9hZGNoYXRAZ21haWwuY29tIiwiZXhwIjoxNjcxNzgwNjE2LCJpYXQiOjE2NjkxODg2MTYsImlzcyI6ImRpc2NvdXJzIn0.Nml4oV6iMjMmc6xwM7lTKEZJKBXvJFEIZ-Up1C1rITQ
|
# 2041-user@domain.zn-eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoyMDQxLCJ1c2VybmFtZSI6ImFudG9uLnJld2luK3Rlc3QtbG9hZGNoYXRAZ21haWwuY29tIiwiZXhwIjoxNjcxNzgwNjE2LCJpYXQiOjE2NjkxODg2MTYsImlzcyI6ImRpc2NvdXJzIn0.Nml4oV6iMjMmc6xwM7lTKEZJKBXvJFEIZ-Up1C1rITQ
|
||||||
return await redis.execute("GET", token_key)
|
return await redis.execute("GET", token_key)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from graphql.error import GraphQLError
|
from graphql.error import GraphQLError
|
||||||
|
|
||||||
# TODO: remove traceback from logs for defined exceptions
|
|
||||||
|
|
||||||
|
# TODO: remove traceback from logs for defined exceptions
|
||||||
|
|
||||||
class BaseHttpException(GraphQLError):
|
class BaseHttpException(GraphQLError):
|
||||||
code = 500
|
code = 500
|
||||||
|
|
14
base/orm.py
14
base/orm.py
|
@ -1,11 +1,15 @@
|
||||||
from settings import DB_URL
|
from typing import TypeVar, Any, Dict, Generic, Callable
|
||||||
from sqlalchemy import Column, create_engine, Integer
|
|
||||||
|
from sqlalchemy import create_engine, Column, Integer
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from sqlalchemy.sql.schema import Table
|
from sqlalchemy.sql.schema import Table
|
||||||
from typing import Any, Callable, Dict, Generic, TypeVar
|
|
||||||
|
|
||||||
engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20)
|
from settings import DB_URL
|
||||||
|
|
||||||
|
engine = create_engine(
|
||||||
|
DB_URL, echo=False, pool_size=10, max_overflow=20
|
||||||
|
)
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
@ -43,7 +47,7 @@ class Base(declarative_base()):
|
||||||
|
|
||||||
def update(self, input):
|
def update(self, input):
|
||||||
column_names = self.__table__.columns.keys()
|
column_names = self.__table__.columns.keys()
|
||||||
for name, value in input.items():
|
for (name, value) in input.items():
|
||||||
if name in column_names:
|
if name in column_names:
|
||||||
setattr(self, name, value)
|
setattr(self, name, value)
|
||||||
|
|
||||||
|
|
51
main.py
51
main.py
|
@ -1,29 +1,28 @@
|
||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
from importlib import import_module
|
||||||
|
from os.path import exists
|
||||||
from ariadne import load_schema_from_path, make_executable_schema
|
from ariadne import load_schema_from_path, make_executable_schema
|
||||||
from ariadne.asgi import GraphQL
|
from ariadne.asgi import GraphQL
|
||||||
from auth.authenticate import JWTAuthenticate
|
|
||||||
from auth.oauth import oauth_authorize, oauth_login
|
|
||||||
from base.redis import redis
|
|
||||||
from base.resolvers import resolvers
|
|
||||||
from importlib import import_module
|
|
||||||
from orm import init_tables
|
|
||||||
from os.path import exists
|
|
||||||
from resolvers.auth import confirm_email_handler
|
|
||||||
from resolvers.upload import upload_handler
|
|
||||||
from services.main import storages_init
|
|
||||||
from services.notifications.notification_service import notification_service
|
|
||||||
from services.notifications.sse import sse_subscribe_handler
|
|
||||||
from services.stat.viewed import ViewedStorage
|
|
||||||
|
|
||||||
# from services.zine.gittask import GitTask
|
|
||||||
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, SESSION_SECRET_KEY
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.middleware import Middleware
|
from starlette.middleware import Middleware
|
||||||
from starlette.middleware.authentication import AuthenticationMiddleware
|
from starlette.middleware.authentication import AuthenticationMiddleware
|
||||||
from starlette.middleware.sessions import SessionMiddleware
|
from starlette.middleware.sessions import SessionMiddleware
|
||||||
from starlette.routing import Route
|
from starlette.routing import Route
|
||||||
|
from orm import init_tables
|
||||||
|
|
||||||
import asyncio
|
from auth.authenticate import JWTAuthenticate
|
||||||
import os
|
from auth.oauth import oauth_login, oauth_authorize
|
||||||
|
from base.redis import redis
|
||||||
|
from base.resolvers import resolvers
|
||||||
|
from resolvers.auth import confirm_email_handler
|
||||||
|
from resolvers.upload import upload_handler
|
||||||
|
from services.main import storages_init
|
||||||
|
from services.notifications.notification_service import notification_service
|
||||||
|
from services.stat.viewed import ViewedStorage
|
||||||
|
# from services.zine.gittask import GitTask
|
||||||
|
from settings import DEV_SERVER_PID_FILE_NAME, SENTRY_DSN, SESSION_SECRET_KEY
|
||||||
|
from services.notifications.sse import sse_subscribe_handler
|
||||||
|
|
||||||
import_module("resolvers")
|
import_module("resolvers")
|
||||||
schema = make_executable_schema(load_schema_from_path("schema.graphql"), resolvers) # type: ignore
|
schema = make_executable_schema(load_schema_from_path("schema.graphql"), resolvers) # type: ignore
|
||||||
|
@ -47,10 +46,9 @@ async def start_up():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import sentry_sdk
|
import sentry_sdk
|
||||||
|
|
||||||
sentry_sdk.init(SENTRY_DSN)
|
sentry_sdk.init(SENTRY_DSN)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("[sentry] init error")
|
print('[sentry] init error')
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,7 +57,7 @@ async def dev_start_up():
|
||||||
await redis.connect()
|
await redis.connect()
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
with open(DEV_SERVER_PID_FILE_NAME, "w", encoding="utf-8") as f:
|
with open(DEV_SERVER_PID_FILE_NAME, 'w', encoding='utf-8') as f:
|
||||||
f.write(str(os.getpid()))
|
f.write(str(os.getpid()))
|
||||||
|
|
||||||
await start_up()
|
await start_up()
|
||||||
|
@ -74,7 +72,7 @@ routes = [
|
||||||
Route("/oauth/{provider}", endpoint=oauth_login),
|
Route("/oauth/{provider}", endpoint=oauth_login),
|
||||||
Route("/oauth-authorize", endpoint=oauth_authorize),
|
Route("/oauth-authorize", endpoint=oauth_authorize),
|
||||||
Route("/confirm/{token}", endpoint=confirm_email_handler),
|
Route("/confirm/{token}", endpoint=confirm_email_handler),
|
||||||
Route("/upload", endpoint=upload_handler, methods=["POST"]),
|
Route("/upload", endpoint=upload_handler, methods=['POST']),
|
||||||
Route("/subscribe/{user_id}", endpoint=sse_subscribe_handler),
|
Route("/subscribe/{user_id}", endpoint=sse_subscribe_handler),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -84,7 +82,9 @@ app = Starlette(
|
||||||
middleware=middleware,
|
middleware=middleware,
|
||||||
routes=routes,
|
routes=routes,
|
||||||
)
|
)
|
||||||
app.mount("/", GraphQL(schema))
|
app.mount("/", GraphQL(
|
||||||
|
schema
|
||||||
|
))
|
||||||
|
|
||||||
dev_app = Starlette(
|
dev_app = Starlette(
|
||||||
debug=True,
|
debug=True,
|
||||||
|
@ -93,4 +93,7 @@ dev_app = Starlette(
|
||||||
middleware=middleware,
|
middleware=middleware,
|
||||||
routes=routes,
|
routes=routes,
|
||||||
)
|
)
|
||||||
dev_app.mount("/", GraphQL(schema, debug=True))
|
dev_app.mount("/", GraphQL(
|
||||||
|
schema,
|
||||||
|
debug=True
|
||||||
|
))
|
||||||
|
|
|
@ -16,3 +16,4 @@ echo "Start migration"
|
||||||
python3 server.py migrate
|
python3 server.py migrate
|
||||||
if [ $? -ne 0 ]; then { echo "Migration failed, aborting." ; exit 1; } fi
|
if [ $? -ne 0 ]; then { echo "Migration failed, aborting." ; exit 1; } fi
|
||||||
echo 'Done!'
|
echo 'Done!'
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,24 @@
|
||||||
""" cmd managed migration """
|
""" cmd managed migration """
|
||||||
|
import asyncio
|
||||||
|
import gc
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
import bs4
|
||||||
|
|
||||||
from migration.export import export_mdx
|
from migration.export import export_mdx
|
||||||
from migration.tables.comments import migrate as migrateComment
|
from migration.tables.comments import migrate as migrateComment
|
||||||
from migration.tables.comments import migrate_2stage as migrateComment_2stage
|
from migration.tables.comments import migrate_2stage as migrateComment_2stage
|
||||||
from migration.tables.content_items import get_shout_slug
|
from migration.tables.content_items import get_shout_slug
|
||||||
from migration.tables.content_items import migrate as migrateShout
|
from migration.tables.content_items import migrate as migrateShout
|
||||||
|
from migration.tables.remarks import migrate as migrateRemark
|
||||||
# from migration.tables.remarks import migrate as migrateRemark
|
|
||||||
from migration.tables.topics import migrate as migrateTopic
|
from migration.tables.topics import migrate as migrateTopic
|
||||||
from migration.tables.users import migrate as migrateUser
|
from migration.tables.users import migrate as migrateUser, post_migrate as users_post_migrate
|
||||||
from migration.tables.users import migrate_2stage as migrateUser_2stage
|
from migration.tables.users import migrate_2stage as migrateUser_2stage
|
||||||
from migration.tables.users import post_migrate as users_post_migrate
|
|
||||||
from orm import init_tables
|
from orm import init_tables
|
||||||
from orm.reaction import Reaction
|
from orm.reaction import Reaction
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import bs4
|
|
||||||
import gc
|
|
||||||
import json
|
|
||||||
import sys
|
|
||||||
|
|
||||||
TODAY = datetime.strftime(datetime.now(tz=timezone.utc), "%Y%m%d")
|
TODAY = datetime.strftime(datetime.now(tz=timezone.utc), "%Y%m%d")
|
||||||
OLD_DATE = "2016-03-05 22:22:00.350000"
|
OLD_DATE = "2016-03-05 22:22:00.350000"
|
||||||
|
|
||||||
|
@ -64,8 +63,16 @@ async def topics_handle(storage):
|
||||||
del storage["topics"]["by_slug"][oldslug]
|
del storage["topics"]["by_slug"][oldslug]
|
||||||
storage["topics"]["by_oid"][oid] = storage["topics"]["by_slug"][newslug]
|
storage["topics"]["by_oid"][oid] = storage["topics"]["by_slug"][newslug]
|
||||||
print("[migration] " + str(counter) + " topics migrated")
|
print("[migration] " + str(counter) + " topics migrated")
|
||||||
print("[migration] " + str(len(storage["topics"]["by_oid"].values())) + " topics by oid")
|
print(
|
||||||
print("[migration] " + str(len(storage["topics"]["by_slug"].values())) + " topics by slug")
|
"[migration] "
|
||||||
|
+ str(len(storage["topics"]["by_oid"].values()))
|
||||||
|
+ " topics by oid"
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
"[migration] "
|
||||||
|
+ str(len(storage["topics"]["by_slug"].values()))
|
||||||
|
+ " topics by slug"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def shouts_handle(storage, args):
|
async def shouts_handle(storage, args):
|
||||||
|
@ -110,10 +117,9 @@ async def shouts_handle(storage, args):
|
||||||
|
|
||||||
# print main counter
|
# print main counter
|
||||||
counter += 1
|
counter += 1
|
||||||
print(
|
print('[migration] shouts_handle %d: %s @%s' % (
|
||||||
"[migration] shouts_handle %d: %s @%s"
|
(counter + 1), shout_dict["slug"], author["slug"]
|
||||||
% ((counter + 1), shout_dict["slug"], author["slug"])
|
))
|
||||||
)
|
|
||||||
|
|
||||||
b = bs4.BeautifulSoup(shout_dict["body"], "html.parser")
|
b = bs4.BeautifulSoup(shout_dict["body"], "html.parser")
|
||||||
texts = [shout_dict["title"].lower().replace(r"[^а-яА-Яa-zA-Z]", "")]
|
texts = [shout_dict["title"].lower().replace(r"[^а-яА-Яa-zA-Z]", "")]
|
||||||
|
@ -132,13 +138,13 @@ async def shouts_handle(storage, args):
|
||||||
print("[migration] " + str(anonymous_author) + " authored by @anonymous")
|
print("[migration] " + str(anonymous_author) + " authored by @anonymous")
|
||||||
|
|
||||||
|
|
||||||
# async def remarks_handle(storage):
|
async def remarks_handle(storage):
|
||||||
# print("[migration] comments")
|
print("[migration] comments")
|
||||||
# c = 0
|
c = 0
|
||||||
# for entry_remark in storage["remarks"]["data"]:
|
for entry_remark in storage["remarks"]["data"]:
|
||||||
# remark = await migrateRemark(entry_remark, storage)
|
remark = await migrateRemark(entry_remark, storage)
|
||||||
# c += 1
|
c += 1
|
||||||
# print("[migration] " + str(c) + " remarks migrated")
|
print("[migration] " + str(c) + " remarks migrated")
|
||||||
|
|
||||||
|
|
||||||
async def comments_handle(storage):
|
async def comments_handle(storage):
|
||||||
|
@ -149,9 +155,9 @@ async def comments_handle(storage):
|
||||||
for oldcomment in storage["reactions"]["data"]:
|
for oldcomment in storage["reactions"]["data"]:
|
||||||
if not oldcomment.get("deleted"):
|
if not oldcomment.get("deleted"):
|
||||||
reaction = await migrateComment(oldcomment, storage)
|
reaction = await migrateComment(oldcomment, storage)
|
||||||
if isinstance(reaction, str):
|
if type(reaction) == str:
|
||||||
missed_shouts[reaction] = oldcomment
|
missed_shouts[reaction] = oldcomment
|
||||||
elif isinstance(reaction, Reaction):
|
elif type(reaction) == Reaction:
|
||||||
reaction = reaction.dict()
|
reaction = reaction.dict()
|
||||||
rid = reaction["id"]
|
rid = reaction["id"]
|
||||||
oid = reaction["oid"]
|
oid = reaction["oid"]
|
||||||
|
@ -208,7 +214,9 @@ def data_load():
|
||||||
tags_data = json.loads(open("migration/data/tags.json").read())
|
tags_data = json.loads(open("migration/data/tags.json").read())
|
||||||
storage["topics"]["tags"] = tags_data
|
storage["topics"]["tags"] = tags_data
|
||||||
print("[migration.load] " + str(len(tags_data)) + " tags ")
|
print("[migration.load] " + str(len(tags_data)) + " tags ")
|
||||||
cats_data = json.loads(open("migration/data/content_item_categories.json").read())
|
cats_data = json.loads(
|
||||||
|
open("migration/data/content_item_categories.json").read()
|
||||||
|
)
|
||||||
storage["topics"]["cats"] = cats_data
|
storage["topics"]["cats"] = cats_data
|
||||||
print("[migration.load] " + str(len(cats_data)) + " cats ")
|
print("[migration.load] " + str(len(cats_data)) + " cats ")
|
||||||
comments_data = json.loads(open("migration/data/comments.json").read())
|
comments_data = json.loads(open("migration/data/comments.json").read())
|
||||||
|
@ -227,7 +235,11 @@ def data_load():
|
||||||
storage["users"]["by_oid"][x["_id"]] = x
|
storage["users"]["by_oid"][x["_id"]] = x
|
||||||
# storage['users']['by_slug'][x['slug']] = x
|
# storage['users']['by_slug'][x['slug']] = x
|
||||||
# no user.slug yet
|
# no user.slug yet
|
||||||
print("[migration.load] " + str(len(storage["users"]["by_oid"].keys())) + " users by oid")
|
print(
|
||||||
|
"[migration.load] "
|
||||||
|
+ str(len(storage["users"]["by_oid"].keys()))
|
||||||
|
+ " users by oid"
|
||||||
|
)
|
||||||
for x in tags_data:
|
for x in tags_data:
|
||||||
storage["topics"]["by_oid"][x["_id"]] = x
|
storage["topics"]["by_oid"][x["_id"]] = x
|
||||||
storage["topics"]["by_slug"][x["slug"]] = x
|
storage["topics"]["by_slug"][x["slug"]] = x
|
||||||
|
@ -235,7 +247,9 @@ def data_load():
|
||||||
storage["topics"]["by_oid"][x["_id"]] = x
|
storage["topics"]["by_oid"][x["_id"]] = x
|
||||||
storage["topics"]["by_slug"][x["slug"]] = x
|
storage["topics"]["by_slug"][x["slug"]] = x
|
||||||
print(
|
print(
|
||||||
"[migration.load] " + str(len(storage["topics"]["by_slug"].keys())) + " topics by slug"
|
"[migration.load] "
|
||||||
|
+ str(len(storage["topics"]["by_slug"].keys()))
|
||||||
|
+ " topics by slug"
|
||||||
)
|
)
|
||||||
for item in content_data:
|
for item in content_data:
|
||||||
slug = get_shout_slug(item)
|
slug = get_shout_slug(item)
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
from .utils import DateTimeEncoder
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
import bson
|
import bson
|
||||||
import gc
|
import gc
|
||||||
import json
|
from .utils import DateTimeEncoder
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
def json_tables():
|
def json_tables():
|
||||||
|
@ -15,10 +15,10 @@ def json_tables():
|
||||||
"email_subscriptions": [],
|
"email_subscriptions": [],
|
||||||
"users": [],
|
"users": [],
|
||||||
"comments": [],
|
"comments": [],
|
||||||
"remarks": [],
|
"remarks": []
|
||||||
}
|
}
|
||||||
for table in data.keys():
|
for table in data.keys():
|
||||||
print("[migration] bson2json for " + table)
|
print('[migration] bson2json for ' + table)
|
||||||
gc.collect()
|
gc.collect()
|
||||||
lc = []
|
lc = []
|
||||||
bs = open("dump/discours/" + table + ".bson", "rb").read()
|
bs = open("dump/discours/" + table + ".bson", "rb").read()
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
from .extract import extract_html, extract_media
|
import json
|
||||||
from .utils import DateTimeEncoder
|
import os
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
import frontmatter
|
import frontmatter
|
||||||
import json
|
|
||||||
import os
|
from .extract import extract_html, extract_media
|
||||||
|
from .utils import DateTimeEncoder
|
||||||
|
|
||||||
OLD_DATE = "2016-03-05 22:22:00.350000"
|
OLD_DATE = "2016-03-05 22:22:00.350000"
|
||||||
EXPORT_DEST = "../discoursio-web/data/"
|
EXPORT_DEST = "../discoursio-web/data/"
|
||||||
|
@ -70,29 +71,47 @@ def export_slug(slug, storage):
|
||||||
|
|
||||||
|
|
||||||
def export_email_subscriptions():
|
def export_email_subscriptions():
|
||||||
email_subscriptions_data = json.loads(open("migration/data/email_subscriptions.json").read())
|
email_subscriptions_data = json.loads(
|
||||||
|
open("migration/data/email_subscriptions.json").read()
|
||||||
|
)
|
||||||
for data in email_subscriptions_data:
|
for data in email_subscriptions_data:
|
||||||
# TODO: migrate to mailgun list manually
|
# TODO: migrate to mailgun list manually
|
||||||
# migrate_email_subscription(data)
|
# migrate_email_subscription(data)
|
||||||
pass
|
pass
|
||||||
print("[migration] " + str(len(email_subscriptions_data)) + " email subscriptions exported")
|
print(
|
||||||
|
"[migration] "
|
||||||
|
+ str(len(email_subscriptions_data))
|
||||||
|
+ " email subscriptions exported"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def export_shouts(storage):
|
def export_shouts(storage):
|
||||||
# update what was just migrated or load json again
|
# update what was just migrated or load json again
|
||||||
if len(storage["users"]["by_slugs"].keys()) == 0:
|
if len(storage["users"]["by_slugs"].keys()) == 0:
|
||||||
storage["users"]["by_slugs"] = json.loads(open(EXPORT_DEST + "authors.json").read())
|
storage["users"]["by_slugs"] = json.loads(
|
||||||
print("[migration] " + str(len(storage["users"]["by_slugs"].keys())) + " exported authors ")
|
open(EXPORT_DEST + "authors.json").read()
|
||||||
if len(storage["shouts"]["by_slugs"].keys()) == 0:
|
)
|
||||||
storage["shouts"]["by_slugs"] = json.loads(open(EXPORT_DEST + "articles.json").read())
|
|
||||||
print(
|
print(
|
||||||
"[migration] " + str(len(storage["shouts"]["by_slugs"].keys())) + " exported articles "
|
"[migration] "
|
||||||
|
+ str(len(storage["users"]["by_slugs"].keys()))
|
||||||
|
+ " exported authors "
|
||||||
|
)
|
||||||
|
if len(storage["shouts"]["by_slugs"].keys()) == 0:
|
||||||
|
storage["shouts"]["by_slugs"] = json.loads(
|
||||||
|
open(EXPORT_DEST + "articles.json").read()
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
"[migration] "
|
||||||
|
+ str(len(storage["shouts"]["by_slugs"].keys()))
|
||||||
|
+ " exported articles "
|
||||||
)
|
)
|
||||||
for slug in storage["shouts"]["by_slugs"].keys():
|
for slug in storage["shouts"]["by_slugs"].keys():
|
||||||
export_slug(slug, storage)
|
export_slug(slug, storage)
|
||||||
|
|
||||||
|
|
||||||
def export_json(export_articles={}, export_authors={}, export_topics={}, export_comments={}):
|
def export_json(
|
||||||
|
export_articles={}, export_authors={}, export_topics={}, export_comments={}
|
||||||
|
):
|
||||||
open(EXPORT_DEST + "authors.json", "w").write(
|
open(EXPORT_DEST + "authors.json", "w").write(
|
||||||
json.dumps(
|
json.dumps(
|
||||||
export_authors,
|
export_authors,
|
||||||
|
@ -133,4 +152,8 @@ def export_json(export_articles={}, export_authors={}, export_topics={}, export_
|
||||||
ensure_ascii=False,
|
ensure_ascii=False,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
print("[migration] " + str(len(export_comments.items())) + " exported articles with comments")
|
print(
|
||||||
|
"[migration] "
|
||||||
|
+ str(len(export_comments.items()))
|
||||||
|
+ " exported articles with comments"
|
||||||
|
)
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
from bs4 import BeautifulSoup
|
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import uuid
|
||||||
|
|
||||||
# import uuid
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
|
||||||
TOOLTIP_REGEX = r"(\/\/\/(.+)\/\/\/)"
|
TOOLTIP_REGEX = r"(\/\/\/(.+)\/\/\/)"
|
||||||
|
@ -28,40 +27,37 @@ def replace_tooltips(body):
|
||||||
return newbody
|
return newbody
|
||||||
|
|
||||||
|
|
||||||
# def extract_footnotes(body, shout_dict):
|
|
||||||
# parts = body.split("&&&")
|
def extract_footnotes(body, shout_dict):
|
||||||
# lll = len(parts)
|
parts = body.split("&&&")
|
||||||
# newparts = list(parts)
|
lll = len(parts)
|
||||||
# placed = False
|
newparts = list(parts)
|
||||||
# if lll & 1:
|
placed = False
|
||||||
# if lll > 1:
|
if lll & 1:
|
||||||
# i = 1
|
if lll > 1:
|
||||||
# print("[extract] found %d footnotes in body" % (lll - 1))
|
i = 1
|
||||||
# for part in parts[1:]:
|
print("[extract] found %d footnotes in body" % (lll - 1))
|
||||||
# if i & 1:
|
for part in parts[1:]:
|
||||||
# placed = True
|
if i & 1:
|
||||||
# if 'a class="footnote-url" href=' in part:
|
placed = True
|
||||||
# print("[extract] footnote: " + part)
|
if 'a class="footnote-url" href=' in part:
|
||||||
# fn = 'a class="footnote-url" href="'
|
print("[extract] footnote: " + part)
|
||||||
# # exxtracted_link = part.split(fn, 1)[1].split('"', 1)[0]
|
fn = 'a class="footnote-url" href="'
|
||||||
# extracted_body = part.split(fn, 1)[1].split(">", 1)[1].split("</a>", 1)[0]
|
exxtracted_link = part.split(fn, 1)[1].split('"', 1)[0]
|
||||||
# print("[extract] footnote link: " + extracted_link)
|
extracted_body = part.split(fn, 1)[1].split('>', 1)[1].split('</a>', 1)[0]
|
||||||
# with local_session() as session:
|
print("[extract] footnote link: " + extracted_link)
|
||||||
# Reaction.create(
|
with local_session() as session:
|
||||||
# {
|
Reaction.create({
|
||||||
# "shout": shout_dict["id"],
|
"shout": shout_dict['id'],
|
||||||
# "kind": ReactionKind.FOOTNOTE,
|
"kind": ReactionKind.FOOTNOTE,
|
||||||
# "body": extracted_body,
|
"body": extracted_body,
|
||||||
# "range": str(body.index(fn + link) - len("<"))
|
"range": str(body.index(fn + link) - len('<')) + ':' + str(body.index(extracted_body) + len('</a>'))
|
||||||
# + ":"
|
})
|
||||||
# + str(body.index(extracted_body) + len("</a>")),
|
newparts[i] = "<a href='#'>ℹ️</a>"
|
||||||
# }
|
else:
|
||||||
# )
|
newparts[i] = part
|
||||||
# newparts[i] = "<a href='#'>ℹ️</a>"
|
i += 1
|
||||||
# else:
|
return ("".join(newparts), placed)
|
||||||
# newparts[i] = part
|
|
||||||
# i += 1
|
|
||||||
# return ("".join(newparts), placed)
|
|
||||||
|
|
||||||
|
|
||||||
def place_tooltips(body):
|
def place_tooltips(body):
|
||||||
|
@ -80,7 +76,9 @@ def place_tooltips(body):
|
||||||
print("[extract] footnote: " + part)
|
print("[extract] footnote: " + part)
|
||||||
fn = 'a class="footnote-url" href="'
|
fn = 'a class="footnote-url" href="'
|
||||||
link = part.split(fn, 1)[1].split('"', 1)[0]
|
link = part.split(fn, 1)[1].split('"', 1)[0]
|
||||||
extracted_part = part.split(fn, 1)[0] + " " + part.split("/", 1)[-1]
|
extracted_part = (
|
||||||
|
part.split(fn, 1)[0] + " " + part.split("/", 1)[-1]
|
||||||
|
)
|
||||||
newparts[i] = (
|
newparts[i] = (
|
||||||
"<Tooltip"
|
"<Tooltip"
|
||||||
+ (' link="' + link + '" ' if link else "")
|
+ (' link="' + link + '" ' if link else "")
|
||||||
|
@ -98,9 +96,7 @@ def place_tooltips(body):
|
||||||
return ("".join(newparts), placed)
|
return ("".join(newparts), placed)
|
||||||
|
|
||||||
|
|
||||||
IMG_REGEX = (
|
IMG_REGEX = r"\!\[(.*?)\]\((data\:image\/(png|jpeg|jpg);base64\,((?:[A-Za-z\d+\/]{4})*(?:[A-Za-z\d+\/]{3}="
|
||||||
r"\!\[(.*?)\]\((data\:image\/(png|jpeg|jpg);base64\,((?:[A-Za-z\d+\/]{4})*(?:[A-Za-z\d+\/]{3}="
|
|
||||||
)
|
|
||||||
IMG_REGEX += r"|[A-Za-z\d+\/]{2}==)))\)"
|
IMG_REGEX += r"|[A-Za-z\d+\/]{2}==)))\)"
|
||||||
|
|
||||||
parentDir = "/".join(os.getcwd().split("/")[:-1])
|
parentDir = "/".join(os.getcwd().split("/")[:-1])
|
||||||
|
@ -163,7 +159,11 @@ def extract_imageparts(bodyparts, prefix):
|
||||||
try:
|
try:
|
||||||
content = base64.b64decode(b64encoded + "==")
|
content = base64.b64decode(b64encoded + "==")
|
||||||
open(public + link, "wb").write(content)
|
open(public + link, "wb").write(content)
|
||||||
print("[extract] " + str(len(content)) + " image bytes been written")
|
print(
|
||||||
|
"[extract] "
|
||||||
|
+ str(len(content))
|
||||||
|
+ " image bytes been written"
|
||||||
|
)
|
||||||
cache[b64encoded] = name
|
cache[b64encoded] = name
|
||||||
except Exception:
|
except Exception:
|
||||||
raise Exception
|
raise Exception
|
||||||
|
@ -172,11 +172,18 @@ def extract_imageparts(bodyparts, prefix):
|
||||||
print("[extract] cached link " + cache[b64encoded])
|
print("[extract] cached link " + cache[b64encoded])
|
||||||
name = cache[b64encoded]
|
name = cache[b64encoded]
|
||||||
link = cdn + "/upload/image-" + name + "." + ext
|
link = cdn + "/upload/image-" + name + "." + ext
|
||||||
newparts[i] = current[: -len(mime)] + current[-len(mime) :] + link + next[-b64end:]
|
newparts[i] = (
|
||||||
|
current[: -len(mime)]
|
||||||
|
+ current[-len(mime) :]
|
||||||
|
+ link
|
||||||
|
+ next[-b64end:]
|
||||||
|
)
|
||||||
newparts[i + 1] = next[:-b64end]
|
newparts[i + 1] = next[:-b64end]
|
||||||
break
|
break
|
||||||
return (
|
return (
|
||||||
extract_imageparts(newparts[i] + newparts[i + 1] + b64.join(bodyparts[(i + 2) :]), prefix)
|
extract_imageparts(
|
||||||
|
newparts[i] + newparts[i + 1] + b64.join(bodyparts[(i + 2) :]), prefix
|
||||||
|
)
|
||||||
if len(bodyparts) > (i + 1)
|
if len(bodyparts) > (i + 1)
|
||||||
else "".join(newparts)
|
else "".join(newparts)
|
||||||
)
|
)
|
||||||
|
@ -230,6 +237,7 @@ di = "data:image"
|
||||||
|
|
||||||
|
|
||||||
def extract_md_images(body, prefix):
|
def extract_md_images(body, prefix):
|
||||||
|
newbody = ""
|
||||||
body = (
|
body = (
|
||||||
body.replace("\n! [](" + di, "\n 
|
body.replace("\n! [](" + di, "\n 
|
||||||
.replace("\n[](" + di, "\n
|
.replace("\n[](" + di, "\n
|
||||||
|
@ -237,10 +245,10 @@ def extract_md_images(body, prefix):
|
||||||
)
|
)
|
||||||
parts = body.split(di)
|
parts = body.split(di)
|
||||||
if len(parts) > 1:
|
if len(parts) > 1:
|
||||||
new_body = extract_dataimages(parts, prefix)
|
newbody = extract_dataimages(parts, prefix)
|
||||||
else:
|
else:
|
||||||
new_body = body
|
newbody = body
|
||||||
return new_body
|
return newbody
|
||||||
|
|
||||||
|
|
||||||
def cleanup_md(body):
|
def cleanup_md(body):
|
||||||
|
@ -263,28 +271,29 @@ def cleanup_md(body):
|
||||||
return newbody
|
return newbody
|
||||||
|
|
||||||
|
|
||||||
# def extract_md(body, shout_dict=None):
|
def extract_md(body, shout_dict = None):
|
||||||
# newbody = body
|
newbody = body
|
||||||
# if newbody:
|
if newbody:
|
||||||
# newbody = cleanup_md(newbody)
|
newbody = cleanup_md(newbody)
|
||||||
# if not newbody:
|
if not newbody:
|
||||||
# raise Exception("cleanup error")
|
raise Exception("cleanup error")
|
||||||
#
|
|
||||||
# if shout_dict:
|
if shout_dict:
|
||||||
# uid = shout_dict["id"] or uuid.uuid4()
|
|
||||||
# newbody = extract_md_images(newbody, uid)
|
uid = shout_dict['id'] or uuid.uuid4()
|
||||||
# if not newbody:
|
newbody = extract_md_images(newbody, uid)
|
||||||
# raise Exception("extract_images error")
|
if not newbody:
|
||||||
#
|
raise Exception("extract_images error")
|
||||||
# newbody, placed = extract_footnotes(body, shout_dict)
|
|
||||||
# if not newbody:
|
newbody, placed = extract_footnotes(body, shout_dict)
|
||||||
# raise Exception("extract_footnotes error")
|
if not newbody:
|
||||||
#
|
raise Exception("extract_footnotes error")
|
||||||
# return newbody
|
|
||||||
|
return newbody
|
||||||
|
|
||||||
|
|
||||||
def extract_media(entry):
|
def extract_media(entry):
|
||||||
"""normalized media extraction method"""
|
''' normalized media extraction method '''
|
||||||
# media [ { title pic url body } ]}
|
# media [ { title pic url body } ]}
|
||||||
kind = entry.get("type")
|
kind = entry.get("type")
|
||||||
if not kind:
|
if not kind:
|
||||||
|
@ -314,7 +323,12 @@ def extract_media(entry):
|
||||||
url = "https://vimeo.com/" + m["vimeoId"]
|
url = "https://vimeo.com/" + m["vimeoId"]
|
||||||
# body
|
# body
|
||||||
body = m.get("body") or m.get("literatureBody") or ""
|
body = m.get("body") or m.get("literatureBody") or ""
|
||||||
media.append({"url": url, "pic": pic, "title": title, "body": body})
|
media.append({
|
||||||
|
"url": url,
|
||||||
|
"pic": pic,
|
||||||
|
"title": title,
|
||||||
|
"body": body
|
||||||
|
})
|
||||||
return media
|
return media
|
||||||
|
|
||||||
|
|
||||||
|
@ -384,7 +398,9 @@ def cleanup_html(body: str) -> str:
|
||||||
r"<h4>\s*</h4>",
|
r"<h4>\s*</h4>",
|
||||||
r"<div>\s*</div>",
|
r"<div>\s*</div>",
|
||||||
]
|
]
|
||||||
regex_replace = {r"<br>\s*</p>": "</p>"}
|
regex_replace = {
|
||||||
|
r"<br>\s*</p>": "</p>"
|
||||||
|
}
|
||||||
changed = True
|
changed = True
|
||||||
while changed:
|
while changed:
|
||||||
# we need several iterations to clean nested tags this way
|
# we need several iterations to clean nested tags this way
|
||||||
|
@ -398,15 +414,16 @@ def cleanup_html(body: str) -> str:
|
||||||
changed = True
|
changed = True
|
||||||
return new_body
|
return new_body
|
||||||
|
|
||||||
|
def extract_html(entry, shout_id = None, cleanup=False):
|
||||||
def extract_html(entry, cleanup=False):
|
body_orig = (entry.get("body") or "").replace('\(', '(').replace('\)', ')')
|
||||||
body_orig = (entry.get("body") or "").replace(r"\(", "(").replace(r"\)", ")")
|
|
||||||
if cleanup:
|
if cleanup:
|
||||||
# we do that before bs parsing to catch the invalid html
|
# we do that before bs parsing to catch the invalid html
|
||||||
body_clean = cleanup_html(body_orig)
|
body_clean = cleanup_html(body_orig)
|
||||||
if body_clean != body_orig:
|
if body_clean != body_orig:
|
||||||
print(f"[migration] html cleaned for slug {entry.get('slug', None)}")
|
print(f"[migration] html cleaned for slug {entry.get('slug', None)}")
|
||||||
body_orig = body_clean
|
body_orig = body_clean
|
||||||
|
if shout_id:
|
||||||
|
extract_footnotes(body_orig, shout_id)
|
||||||
body_html = str(BeautifulSoup(body_orig, features="html.parser"))
|
body_html = str(BeautifulSoup(body_orig, features="html.parser"))
|
||||||
if cleanup:
|
if cleanup:
|
||||||
# we do that after bs parsing because it can add dummy tags
|
# we do that after bs parsing because it can add dummy tags
|
||||||
|
|
|
@ -1,5 +1,13 @@
|
||||||
"""html2text: Turn HTML into equivalent Markdown-structured text."""
|
"""html2text: Turn HTML into equivalent Markdown-structured text."""
|
||||||
|
|
||||||
|
import html.entities
|
||||||
|
import html.parser
|
||||||
|
import re
|
||||||
|
import string
|
||||||
|
import urllib.parse as urlparse
|
||||||
|
from textwrap import wrap
|
||||||
|
from typing import Dict, List, Optional, Tuple, Union
|
||||||
|
|
||||||
from . import config
|
from . import config
|
||||||
from .elements import AnchorElement, ListElement
|
from .elements import AnchorElement, ListElement
|
||||||
from .typing import OutCallback
|
from .typing import OutCallback
|
||||||
|
@ -18,14 +26,6 @@ from .utils import (
|
||||||
skipwrap,
|
skipwrap,
|
||||||
unifiable_n,
|
unifiable_n,
|
||||||
)
|
)
|
||||||
from textwrap import wrap
|
|
||||||
from typing import Dict, List, Optional, Tuple, Union
|
|
||||||
|
|
||||||
import html.entities
|
|
||||||
import html.parser
|
|
||||||
import re
|
|
||||||
import string
|
|
||||||
import urllib.parse as urlparse
|
|
||||||
|
|
||||||
__version__ = (2020, 1, 16)
|
__version__ = (2020, 1, 16)
|
||||||
|
|
||||||
|
@ -119,7 +119,9 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
self.lastWasList = False
|
self.lastWasList = False
|
||||||
self.style = 0
|
self.style = 0
|
||||||
self.style_def = {} # type: Dict[str, Dict[str, str]]
|
self.style_def = {} # type: Dict[str, Dict[str, str]]
|
||||||
self.tag_stack = [] # type: List[Tuple[str, Dict[str, Optional[str]], Dict[str, str]]]
|
self.tag_stack = (
|
||||||
|
[]
|
||||||
|
) # type: List[Tuple[str, Dict[str, Optional[str]], Dict[str, str]]]
|
||||||
self.emphasis = 0
|
self.emphasis = 0
|
||||||
self.drop_white_space = 0
|
self.drop_white_space = 0
|
||||||
self.inheader = False
|
self.inheader = False
|
||||||
|
@ -298,7 +300,9 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
if strikethrough:
|
if strikethrough:
|
||||||
self.quiet -= 1
|
self.quiet -= 1
|
||||||
|
|
||||||
def handle_tag(self, tag: str, attrs: Dict[str, Optional[str]], start: bool) -> None:
|
def handle_tag(
|
||||||
|
self, tag: str, attrs: Dict[str, Optional[str]], start: bool
|
||||||
|
) -> None:
|
||||||
self.current_tag = tag
|
self.current_tag = tag
|
||||||
|
|
||||||
if self.tag_callback is not None:
|
if self.tag_callback is not None:
|
||||||
|
@ -329,7 +333,9 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
tag_style = element_style(attrs, self.style_def, parent_style)
|
tag_style = element_style(attrs, self.style_def, parent_style)
|
||||||
self.tag_stack.append((tag, attrs, tag_style))
|
self.tag_stack.append((tag, attrs, tag_style))
|
||||||
else:
|
else:
|
||||||
dummy, attrs, tag_style = self.tag_stack.pop() if self.tag_stack else (None, {}, {})
|
dummy, attrs, tag_style = (
|
||||||
|
self.tag_stack.pop() if self.tag_stack else (None, {}, {})
|
||||||
|
)
|
||||||
if self.tag_stack:
|
if self.tag_stack:
|
||||||
parent_style = self.tag_stack[-1][2]
|
parent_style = self.tag_stack[-1][2]
|
||||||
|
|
||||||
|
@ -379,7 +385,11 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
):
|
):
|
||||||
self.o("`") # NOTE: same as <code>
|
self.o("`") # NOTE: same as <code>
|
||||||
self.span_highlight = True
|
self.span_highlight = True
|
||||||
elif self.current_class == "lead" and not self.inheader and not self.span_highlight:
|
elif (
|
||||||
|
self.current_class == "lead"
|
||||||
|
and not self.inheader
|
||||||
|
and not self.span_highlight
|
||||||
|
):
|
||||||
# self.o("==") # NOTE: CriticMarkup {==
|
# self.o("==") # NOTE: CriticMarkup {==
|
||||||
self.span_lead = True
|
self.span_lead = True
|
||||||
else:
|
else:
|
||||||
|
@ -469,7 +479,11 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
and not self.span_lead
|
and not self.span_lead
|
||||||
and not self.span_highlight
|
and not self.span_highlight
|
||||||
):
|
):
|
||||||
if start and self.preceding_data and self.preceding_data[-1] == self.strong_mark[0]:
|
if (
|
||||||
|
start
|
||||||
|
and self.preceding_data
|
||||||
|
and self.preceding_data[-1] == self.strong_mark[0]
|
||||||
|
):
|
||||||
strong = " " + self.strong_mark
|
strong = " " + self.strong_mark
|
||||||
self.preceding_data += " "
|
self.preceding_data += " "
|
||||||
else:
|
else:
|
||||||
|
@ -534,8 +548,13 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
"href" in attrs
|
"href" in attrs
|
||||||
and not attrs["href"].startswith("#_ftn")
|
and not attrs["href"].startswith("#_ftn")
|
||||||
and attrs["href"] is not None
|
and attrs["href"] is not None
|
||||||
and not (self.skip_internal_links and attrs["href"].startswith("#"))
|
and not (
|
||||||
and not (self.ignore_mailto_links and attrs["href"].startswith("mailto:"))
|
self.skip_internal_links and attrs["href"].startswith("#")
|
||||||
|
)
|
||||||
|
and not (
|
||||||
|
self.ignore_mailto_links
|
||||||
|
and attrs["href"].startswith("mailto:")
|
||||||
|
)
|
||||||
):
|
):
|
||||||
self.astack.append(attrs)
|
self.astack.append(attrs)
|
||||||
self.maybe_automatic_link = attrs["href"]
|
self.maybe_automatic_link = attrs["href"]
|
||||||
|
@ -619,7 +638,9 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
self.o("![" + escape_md(alt) + "]")
|
self.o("![" + escape_md(alt) + "]")
|
||||||
if self.inline_links:
|
if self.inline_links:
|
||||||
href = attrs.get("href") or ""
|
href = attrs.get("href") or ""
|
||||||
self.o("(" + escape_md(urlparse.urljoin(self.baseurl, href)) + ")")
|
self.o(
|
||||||
|
"(" + escape_md(urlparse.urljoin(self.baseurl, href)) + ")"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
i = self.previousIndex(attrs)
|
i = self.previousIndex(attrs)
|
||||||
if i is not None:
|
if i is not None:
|
||||||
|
@ -675,7 +696,9 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
# WARNING: does not line up <ol><li>s > 9 correctly.
|
# WARNING: does not line up <ol><li>s > 9 correctly.
|
||||||
parent_list = None
|
parent_list = None
|
||||||
for list in self.list:
|
for list in self.list:
|
||||||
self.o(" " if parent_list == "ol" and list.name == "ul" else " ")
|
self.o(
|
||||||
|
" " if parent_list == "ol" and list.name == "ul" else " "
|
||||||
|
)
|
||||||
parent_list = list.name
|
parent_list = list.name
|
||||||
|
|
||||||
if li.name == "ul":
|
if li.name == "ul":
|
||||||
|
@ -764,7 +787,9 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
self.pbr()
|
self.pbr()
|
||||||
self.br_toggle = " "
|
self.br_toggle = " "
|
||||||
|
|
||||||
def o(self, data: str, puredata: bool = False, force: Union[bool, str] = False) -> None:
|
def o(
|
||||||
|
self, data: str, puredata: bool = False, force: Union[bool, str] = False
|
||||||
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Deal with indentation and whitespace
|
Deal with indentation and whitespace
|
||||||
"""
|
"""
|
||||||
|
@ -839,7 +864,9 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
self.out(" ")
|
self.out(" ")
|
||||||
self.space = False
|
self.space = False
|
||||||
|
|
||||||
if self.a and ((self.p_p == 2 and self.links_each_paragraph) or force == "end"):
|
if self.a and (
|
||||||
|
(self.p_p == 2 and self.links_each_paragraph) or force == "end"
|
||||||
|
):
|
||||||
if force == "end":
|
if force == "end":
|
||||||
self.out("\n")
|
self.out("\n")
|
||||||
|
|
||||||
|
@ -898,7 +925,11 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
|
|
||||||
if self.maybe_automatic_link is not None:
|
if self.maybe_automatic_link is not None:
|
||||||
href = self.maybe_automatic_link
|
href = self.maybe_automatic_link
|
||||||
if href == data and self.absolute_url_matcher.match(href) and self.use_automatic_links:
|
if (
|
||||||
|
href == data
|
||||||
|
and self.absolute_url_matcher.match(href)
|
||||||
|
and self.use_automatic_links
|
||||||
|
):
|
||||||
self.o("<" + data + ">")
|
self.o("<" + data + ">")
|
||||||
self.empty_link = False
|
self.empty_link = False
|
||||||
return
|
return
|
||||||
|
@ -969,7 +1000,9 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
self.inline_links = False
|
self.inline_links = False
|
||||||
for para in text.split("\n"):
|
for para in text.split("\n"):
|
||||||
if len(para) > 0:
|
if len(para) > 0:
|
||||||
if not skipwrap(para, self.wrap_links, self.wrap_list_items, self.wrap_tables):
|
if not skipwrap(
|
||||||
|
para, self.wrap_links, self.wrap_list_items, self.wrap_tables
|
||||||
|
):
|
||||||
indent = ""
|
indent = ""
|
||||||
if para.startswith(" " + self.ul_item_mark):
|
if para.startswith(" " + self.ul_item_mark):
|
||||||
# list item continuation: add a double indent to the
|
# list item continuation: add a double indent to the
|
||||||
|
@ -1010,7 +1043,9 @@ class HTML2Text(html.parser.HTMLParser):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def html2text(html: str, baseurl: str = "", bodywidth: Optional[int] = config.BODY_WIDTH) -> str:
|
def html2text(
|
||||||
|
html: str, baseurl: str = "", bodywidth: Optional[int] = config.BODY_WIDTH
|
||||||
|
) -> str:
|
||||||
h = html.strip() or ""
|
h = html.strip() or ""
|
||||||
if h:
|
if h:
|
||||||
h = HTML2Text(baseurl=baseurl, bodywidth=bodywidth)
|
h = HTML2Text(baseurl=baseurl, bodywidth=bodywidth)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
from . import __version__, config, HTML2Text
|
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from . import HTML2Text, __version__, config
|
||||||
|
|
||||||
|
|
||||||
# noinspection DuplicatedCode
|
# noinspection DuplicatedCode
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
@ -117,7 +117,10 @@ def main() -> None:
|
||||||
dest="images_with_size",
|
dest="images_with_size",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
default=config.IMAGES_WITH_SIZE,
|
default=config.IMAGES_WITH_SIZE,
|
||||||
help=("Write image tags with height and width attrs as raw html to retain " "dimensions"),
|
help=(
|
||||||
|
"Write image tags with height and width attrs as raw html to retain "
|
||||||
|
"dimensions"
|
||||||
|
),
|
||||||
)
|
)
|
||||||
p.add_argument(
|
p.add_argument(
|
||||||
"-g",
|
"-g",
|
||||||
|
@ -257,7 +260,9 @@ def main() -> None:
|
||||||
default=config.CLOSE_QUOTE,
|
default=config.CLOSE_QUOTE,
|
||||||
help="The character used to close quotes",
|
help="The character used to close quotes",
|
||||||
)
|
)
|
||||||
p.add_argument("--version", action="version", version=".".join(map(str, __version__)))
|
p.add_argument(
|
||||||
|
"--version", action="version", version=".".join(map(str, __version__))
|
||||||
|
)
|
||||||
p.add_argument("filename", nargs="?")
|
p.add_argument("filename", nargs="?")
|
||||||
p.add_argument("encoding", nargs="?", default="utf-8")
|
p.add_argument("encoding", nargs="?", default="utf-8")
|
||||||
args = p.parse_args()
|
args = p.parse_args()
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
from . import config
|
import html.entities
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
import html.entities
|
from . import config
|
||||||
|
|
||||||
unifiable_n = {
|
unifiable_n = {
|
||||||
html.entities.name2codepoint[k]: v for k, v in config.UNIFIABLE.items() if k != "nbsp"
|
html.entities.name2codepoint[k]: v
|
||||||
|
for k, v in config.UNIFIABLE.items()
|
||||||
|
if k != "nbsp"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -154,7 +156,9 @@ def list_numbering_start(attrs: Dict[str, Optional[str]]) -> int:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def skipwrap(para: str, wrap_links: bool, wrap_list_items: bool, wrap_tables: bool) -> bool:
|
def skipwrap(
|
||||||
|
para: str, wrap_links: bool, wrap_list_items: bool, wrap_tables: bool
|
||||||
|
) -> bool:
|
||||||
# If it appears to contain a link
|
# If it appears to contain a link
|
||||||
# don't wrap
|
# don't wrap
|
||||||
if not wrap_links and config.RE_LINK.search(para):
|
if not wrap_links and config.RE_LINK.search(para):
|
||||||
|
@ -232,7 +236,9 @@ def reformat_table(lines: List[str], right_margin: int) -> List[str]:
|
||||||
max_width += [len(x) + right_margin for x in cols[-(num_cols - max_cols) :]]
|
max_width += [len(x) + right_margin for x in cols[-(num_cols - max_cols) :]]
|
||||||
max_cols = num_cols
|
max_cols = num_cols
|
||||||
|
|
||||||
max_width = [max(len(x) + right_margin, old_len) for x, old_len in zip(cols, max_width)]
|
max_width = [
|
||||||
|
max(len(x) + right_margin, old_len) for x, old_len in zip(cols, max_width)
|
||||||
|
]
|
||||||
|
|
||||||
# reformat
|
# reformat
|
||||||
new_lines = []
|
new_lines = []
|
||||||
|
@ -241,13 +247,15 @@ def reformat_table(lines: List[str], right_margin: int) -> List[str]:
|
||||||
if set(line.strip()) == set("-|"):
|
if set(line.strip()) == set("-|"):
|
||||||
filler = "-"
|
filler = "-"
|
||||||
new_cols = [
|
new_cols = [
|
||||||
x.rstrip() + (filler * (M - len(x.rstrip()))) for x, M in zip(cols, max_width)
|
x.rstrip() + (filler * (M - len(x.rstrip())))
|
||||||
|
for x, M in zip(cols, max_width)
|
||||||
]
|
]
|
||||||
new_lines.append("|-" + "|".join(new_cols) + "|")
|
new_lines.append("|-" + "|".join(new_cols) + "|")
|
||||||
else:
|
else:
|
||||||
filler = " "
|
filler = " "
|
||||||
new_cols = [
|
new_cols = [
|
||||||
x.rstrip() + (filler * (M - len(x.rstrip()))) for x, M in zip(cols, max_width)
|
x.rstrip() + (filler * (M - len(x.rstrip())))
|
||||||
|
for x, M in zip(cols, max_width)
|
||||||
]
|
]
|
||||||
new_lines.append("| " + "|".join(new_cols) + "|")
|
new_lines.append("| " + "|".join(new_cols) + "|")
|
||||||
return new_lines
|
return new_lines
|
||||||
|
|
|
@ -1,50 +1,65 @@
|
||||||
from base.orm import local_session
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
from dateutil.parser import parse as date_parse
|
from dateutil.parser import parse as date_parse
|
||||||
|
|
||||||
|
from base.orm import local_session
|
||||||
from migration.html2text import html2text
|
from migration.html2text import html2text
|
||||||
from orm.reaction import Reaction, ReactionKind
|
from orm.reaction import Reaction, ReactionKind
|
||||||
from orm.shout import Shout, ShoutReactionsFollower
|
from orm.shout import ShoutReactionsFollower
|
||||||
from orm.topic import TopicFollower
|
from orm.topic import TopicFollower
|
||||||
from orm.user import User
|
from orm.user import User
|
||||||
|
from orm.shout import Shout
|
||||||
|
|
||||||
ts = datetime.now(tz=timezone.utc)
|
ts = datetime.now(tz=timezone.utc)
|
||||||
|
|
||||||
|
|
||||||
def auto_followers(session, topics, reaction_dict):
|
def auto_followers(session, topics, reaction_dict):
|
||||||
# creating shout's reactions following for reaction author
|
# creating shout's reactions following for reaction author
|
||||||
following1 = (
|
following1 = session.query(
|
||||||
session.query(ShoutReactionsFollower)
|
ShoutReactionsFollower
|
||||||
.where(ShoutReactionsFollower.follower == reaction_dict["createdBy"])
|
).where(
|
||||||
.filter(ShoutReactionsFollower.shout == reaction_dict["shout"])
|
ShoutReactionsFollower.follower == reaction_dict["createdBy"]
|
||||||
.first()
|
).filter(
|
||||||
)
|
ShoutReactionsFollower.shout == reaction_dict["shout"]
|
||||||
|
).first()
|
||||||
if not following1:
|
if not following1:
|
||||||
following1 = ShoutReactionsFollower.create(
|
following1 = ShoutReactionsFollower.create(
|
||||||
follower=reaction_dict["createdBy"], shout=reaction_dict["shout"], auto=True
|
follower=reaction_dict["createdBy"],
|
||||||
|
shout=reaction_dict["shout"],
|
||||||
|
auto=True
|
||||||
)
|
)
|
||||||
session.add(following1)
|
session.add(following1)
|
||||||
# creating topics followings for reaction author
|
# creating topics followings for reaction author
|
||||||
for t in topics:
|
for t in topics:
|
||||||
tf = (
|
tf = session.query(
|
||||||
session.query(TopicFollower)
|
TopicFollower
|
||||||
.where(TopicFollower.follower == reaction_dict["createdBy"])
|
).where(
|
||||||
.filter(TopicFollower.topic == t["id"])
|
TopicFollower.follower == reaction_dict["createdBy"]
|
||||||
.first()
|
).filter(
|
||||||
)
|
TopicFollower.topic == t['id']
|
||||||
|
).first()
|
||||||
if not tf:
|
if not tf:
|
||||||
topic_following = TopicFollower.create(
|
topic_following = TopicFollower.create(
|
||||||
follower=reaction_dict["createdBy"], topic=t["id"], auto=True
|
follower=reaction_dict["createdBy"],
|
||||||
|
topic=t['id'],
|
||||||
|
auto=True
|
||||||
)
|
)
|
||||||
session.add(topic_following)
|
session.add(topic_following)
|
||||||
|
|
||||||
|
|
||||||
def migrate_ratings(session, entry, reaction_dict):
|
def migrate_ratings(session, entry, reaction_dict):
|
||||||
for comment_rating_old in entry.get("ratings", []):
|
for comment_rating_old in entry.get("ratings", []):
|
||||||
rater = session.query(User).filter(User.oid == comment_rating_old["createdBy"]).first()
|
rater = (
|
||||||
|
session.query(User)
|
||||||
|
.filter(User.oid == comment_rating_old["createdBy"])
|
||||||
|
.first()
|
||||||
|
)
|
||||||
re_reaction_dict = {
|
re_reaction_dict = {
|
||||||
"shout": reaction_dict["shout"],
|
"shout": reaction_dict["shout"],
|
||||||
"replyTo": reaction_dict["id"],
|
"replyTo": reaction_dict["id"],
|
||||||
"kind": ReactionKind.LIKE if comment_rating_old["value"] > 0 else ReactionKind.DISLIKE,
|
"kind": ReactionKind.LIKE
|
||||||
|
if comment_rating_old["value"] > 0
|
||||||
|
else ReactionKind.DISLIKE,
|
||||||
"createdBy": rater.id if rater else 1,
|
"createdBy": rater.id if rater else 1,
|
||||||
}
|
}
|
||||||
cts = comment_rating_old.get("createdAt")
|
cts = comment_rating_old.get("createdAt")
|
||||||
|
@ -53,15 +68,18 @@ def migrate_ratings(session, entry, reaction_dict):
|
||||||
try:
|
try:
|
||||||
# creating reaction from old rating
|
# creating reaction from old rating
|
||||||
rr = Reaction.create(**re_reaction_dict)
|
rr = Reaction.create(**re_reaction_dict)
|
||||||
following2 = (
|
following2 = session.query(
|
||||||
session.query(ShoutReactionsFollower)
|
ShoutReactionsFollower
|
||||||
.where(ShoutReactionsFollower.follower == re_reaction_dict["createdBy"])
|
).where(
|
||||||
.filter(ShoutReactionsFollower.shout == rr.shout)
|
ShoutReactionsFollower.follower == re_reaction_dict['createdBy']
|
||||||
.first()
|
).filter(
|
||||||
)
|
ShoutReactionsFollower.shout == rr.shout
|
||||||
|
).first()
|
||||||
if not following2:
|
if not following2:
|
||||||
following2 = ShoutReactionsFollower.create(
|
following2 = ShoutReactionsFollower.create(
|
||||||
follower=re_reaction_dict["createdBy"], shout=rr.shout, auto=True
|
follower=re_reaction_dict['createdBy'],
|
||||||
|
shout=rr.shout,
|
||||||
|
auto=True
|
||||||
)
|
)
|
||||||
session.add(following2)
|
session.add(following2)
|
||||||
session.add(rr)
|
session.add(rr)
|
||||||
|
@ -132,7 +150,9 @@ async def migrate(entry, storage):
|
||||||
else:
|
else:
|
||||||
stage = "author and old id found"
|
stage = "author and old id found"
|
||||||
try:
|
try:
|
||||||
shout = session.query(Shout).where(Shout.slug == old_shout["slug"]).one()
|
shout = session.query(
|
||||||
|
Shout
|
||||||
|
).where(Shout.slug == old_shout["slug"]).one()
|
||||||
if shout:
|
if shout:
|
||||||
reaction_dict["shout"] = shout.id
|
reaction_dict["shout"] = shout.id
|
||||||
reaction_dict["createdBy"] = author.id if author else 1
|
reaction_dict["createdBy"] = author.id if author else 1
|
||||||
|
@ -158,9 +178,9 @@ async def migrate(entry, storage):
|
||||||
|
|
||||||
|
|
||||||
def migrate_2stage(old_comment, idmap):
|
def migrate_2stage(old_comment, idmap):
|
||||||
if old_comment.get("body"):
|
if old_comment.get('body'):
|
||||||
new_id = idmap.get(old_comment.get("oid"))
|
new_id = idmap.get(old_comment.get('oid'))
|
||||||
new_id = idmap.get(old_comment.get("_id"))
|
new_id = idmap.get(old_comment.get('_id'))
|
||||||
if new_id:
|
if new_id:
|
||||||
new_replyto_id = None
|
new_replyto_id = None
|
||||||
old_replyto_id = old_comment.get("replyTo")
|
old_replyto_id = old_comment.get("replyTo")
|
||||||
|
@ -170,20 +190,17 @@ def migrate_2stage(old_comment, idmap):
|
||||||
comment = session.query(Reaction).where(Reaction.id == new_id).first()
|
comment = session.query(Reaction).where(Reaction.id == new_id).first()
|
||||||
try:
|
try:
|
||||||
if new_replyto_id:
|
if new_replyto_id:
|
||||||
new_reply = (
|
new_reply = session.query(Reaction).where(Reaction.id == new_replyto_id).first()
|
||||||
session.query(Reaction).where(Reaction.id == new_replyto_id).first()
|
|
||||||
)
|
|
||||||
if not new_reply:
|
if not new_reply:
|
||||||
print(new_replyto_id)
|
print(new_replyto_id)
|
||||||
raise Exception("cannot find reply by id!")
|
raise Exception("cannot find reply by id!")
|
||||||
comment.replyTo = new_reply.id
|
comment.replyTo = new_reply.id
|
||||||
session.add(comment)
|
session.add(comment)
|
||||||
srf = (
|
srf = session.query(ShoutReactionsFollower).where(
|
||||||
session.query(ShoutReactionsFollower)
|
ShoutReactionsFollower.shout == comment.shout
|
||||||
.where(ShoutReactionsFollower.shout == comment.shout)
|
).filter(
|
||||||
.filter(ShoutReactionsFollower.follower == comment.createdBy)
|
ShoutReactionsFollower.follower == comment.createdBy
|
||||||
.first()
|
).first()
|
||||||
)
|
|
||||||
if not srf:
|
if not srf:
|
||||||
srf = ShoutReactionsFollower.create(
|
srf = ShoutReactionsFollower.create(
|
||||||
shout=comment.shout, follower=comment.createdBy, auto=True
|
shout=comment.shout, follower=comment.createdBy, auto=True
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
from base.orm import local_session
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
import json
|
||||||
from dateutil.parser import parse as date_parse
|
from dateutil.parser import parse as date_parse
|
||||||
from migration.extract import extract_html, extract_media
|
|
||||||
from orm.reaction import Reaction, ReactionKind
|
|
||||||
from orm.shout import Shout, ShoutReactionsFollower, ShoutTopic
|
|
||||||
from orm.topic import Topic, TopicFollower
|
|
||||||
from orm.user import User
|
|
||||||
from services.stat.viewed import ViewedStorage
|
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from transliterate import translit
|
from transliterate import translit
|
||||||
|
from base.orm import local_session
|
||||||
import json
|
from migration.extract import extract_html, extract_media
|
||||||
|
from orm.reaction import Reaction, ReactionKind
|
||||||
|
from orm.shout import Shout, ShoutTopic, ShoutReactionsFollower
|
||||||
|
from orm.user import User
|
||||||
|
from orm.topic import TopicFollower, Topic
|
||||||
|
from services.stat.viewed import ViewedStorage
|
||||||
import re
|
import re
|
||||||
|
|
||||||
OLD_DATE = "2016-03-05 22:22:00.350000"
|
OLD_DATE = "2016-03-05 22:22:00.350000"
|
||||||
|
@ -34,7 +33,7 @@ def get_shout_slug(entry):
|
||||||
slug = friend.get("slug", "")
|
slug = friend.get("slug", "")
|
||||||
if slug:
|
if slug:
|
||||||
break
|
break
|
||||||
slug = re.sub("[^0-9a-zA-Z]+", "-", slug)
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
|
||||||
return slug
|
return slug
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,27 +41,27 @@ def create_author_from_app(app):
|
||||||
user = None
|
user = None
|
||||||
userdata = None
|
userdata = None
|
||||||
# check if email is used
|
# check if email is used
|
||||||
if app["email"]:
|
if app['email']:
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
user = session.query(User).where(User.email == app["email"]).first()
|
user = session.query(User).where(User.email == app['email']).first()
|
||||||
if not user:
|
if not user:
|
||||||
# print('[migration] app %r' % app)
|
# print('[migration] app %r' % app)
|
||||||
name = app.get("name")
|
name = app.get('name')
|
||||||
if name:
|
if name:
|
||||||
slug = translit(name, "ru", reversed=True).lower()
|
slug = translit(name, "ru", reversed=True).lower()
|
||||||
slug = re.sub("[^0-9a-zA-Z]+", "-", slug)
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
|
||||||
print("[migration] created slug %s" % slug)
|
print('[migration] created slug %s' % slug)
|
||||||
# check if slug is used
|
# check if slug is used
|
||||||
if slug:
|
if slug:
|
||||||
user = session.query(User).where(User.slug == slug).first()
|
user = session.query(User).where(User.slug == slug).first()
|
||||||
|
|
||||||
# get slug from email
|
# get slug from email
|
||||||
if user:
|
if user:
|
||||||
slug = app["email"].split("@")[0]
|
slug = app['email'].split('@')[0]
|
||||||
user = session.query(User).where(User.slug == slug).first()
|
user = session.query(User).where(User.slug == slug).first()
|
||||||
# one more try
|
# one more try
|
||||||
if user:
|
if user:
|
||||||
slug += "-author"
|
slug += '-author'
|
||||||
user = session.query(User).where(User.slug == slug).first()
|
user = session.query(User).where(User.slug == slug).first()
|
||||||
|
|
||||||
# create user with application data
|
# create user with application data
|
||||||
|
@ -80,7 +79,7 @@ def create_author_from_app(app):
|
||||||
user = User.create(**userdata)
|
user = User.create(**userdata)
|
||||||
session.add(user)
|
session.add(user)
|
||||||
session.commit()
|
session.commit()
|
||||||
userdata["id"] = user.id
|
userdata['id'] = user.id
|
||||||
|
|
||||||
userdata = user.dict()
|
userdata = user.dict()
|
||||||
return userdata
|
return userdata
|
||||||
|
@ -92,12 +91,11 @@ async def create_shout(shout_dict):
|
||||||
s = Shout.create(**shout_dict)
|
s = Shout.create(**shout_dict)
|
||||||
author = s.authors[0]
|
author = s.authors[0]
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
srf = (
|
srf = session.query(ShoutReactionsFollower).where(
|
||||||
session.query(ShoutReactionsFollower)
|
ShoutReactionsFollower.shout == s.id
|
||||||
.where(ShoutReactionsFollower.shout == s.id)
|
).filter(
|
||||||
.filter(ShoutReactionsFollower.follower == author.id)
|
ShoutReactionsFollower.follower == author.id
|
||||||
.first()
|
).first()
|
||||||
)
|
|
||||||
if not srf:
|
if not srf:
|
||||||
srf = ShoutReactionsFollower.create(shout=s.id, follower=author.id, auto=True)
|
srf = ShoutReactionsFollower.create(shout=s.id, follower=author.id, auto=True)
|
||||||
session.add(srf)
|
session.add(srf)
|
||||||
|
@ -118,14 +116,14 @@ async def get_user(entry, storage):
|
||||||
elif user_oid:
|
elif user_oid:
|
||||||
userdata = storage["users"]["by_oid"].get(user_oid)
|
userdata = storage["users"]["by_oid"].get(user_oid)
|
||||||
if not userdata:
|
if not userdata:
|
||||||
print("no userdata by oid, anonymous")
|
print('no userdata by oid, anonymous')
|
||||||
userdata = anondict
|
userdata = anondict
|
||||||
print(app)
|
print(app)
|
||||||
# cleanup slug
|
# cleanup slug
|
||||||
if userdata:
|
if userdata:
|
||||||
slug = userdata.get("slug", "")
|
slug = userdata.get("slug", "")
|
||||||
if slug:
|
if slug:
|
||||||
slug = re.sub("[^0-9a-zA-Z]+", "-", slug)
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
|
||||||
userdata["slug"] = slug
|
userdata["slug"] = slug
|
||||||
else:
|
else:
|
||||||
userdata = anondict
|
userdata = anondict
|
||||||
|
@ -139,14 +137,11 @@ async def migrate(entry, storage):
|
||||||
r = {
|
r = {
|
||||||
"layout": type2layout[entry["type"]],
|
"layout": type2layout[entry["type"]],
|
||||||
"title": entry["title"],
|
"title": entry["title"],
|
||||||
"authors": [
|
"authors": [author, ],
|
||||||
author,
|
|
||||||
],
|
|
||||||
"slug": get_shout_slug(entry),
|
"slug": get_shout_slug(entry),
|
||||||
"cover": (
|
"cover": (
|
||||||
"https://images.discours.io/unsafe/" + entry["thumborId"]
|
"https://images.discours.io/unsafe/" +
|
||||||
if entry.get("thumborId")
|
entry["thumborId"] if entry.get("thumborId") else entry.get("image", {}).get("url")
|
||||||
else entry.get("image", {}).get("url")
|
|
||||||
),
|
),
|
||||||
"visibility": "public" if entry.get("published") else "community",
|
"visibility": "public" if entry.get("published") else "community",
|
||||||
"publishedAt": date_parse(entry.get("publishedAt")) if entry.get("published") else None,
|
"publishedAt": date_parse(entry.get("publishedAt")) if entry.get("published") else None,
|
||||||
|
@ -155,11 +150,11 @@ async def migrate(entry, storage):
|
||||||
"updatedAt": date_parse(entry["updatedAt"]) if "updatedAt" in entry else ts,
|
"updatedAt": date_parse(entry["updatedAt"]) if "updatedAt" in entry else ts,
|
||||||
"createdBy": author.id,
|
"createdBy": author.id,
|
||||||
"topics": await add_topics_follower(entry, storage, author),
|
"topics": await add_topics_follower(entry, storage, author),
|
||||||
"body": extract_html(entry, cleanup=True),
|
"body": extract_html(entry, cleanup=True)
|
||||||
}
|
}
|
||||||
|
|
||||||
# main topic patch
|
# main topic patch
|
||||||
r["mainTopic"] = r["topics"][0]
|
r['mainTopic'] = r['topics'][0]
|
||||||
|
|
||||||
# published author auto-confirm
|
# published author auto-confirm
|
||||||
if entry.get("published"):
|
if entry.get("published"):
|
||||||
|
@ -182,16 +177,14 @@ async def migrate(entry, storage):
|
||||||
shout_dict["oid"] = entry.get("_id", "")
|
shout_dict["oid"] = entry.get("_id", "")
|
||||||
shout = await create_shout(shout_dict)
|
shout = await create_shout(shout_dict)
|
||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
print("[migration] create_shout integrity error", e)
|
print('[migration] create_shout integrity error', e)
|
||||||
shout = await resolve_create_shout(shout_dict)
|
shout = await resolve_create_shout(shout_dict)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(e)
|
raise Exception(e)
|
||||||
|
|
||||||
# udpate data
|
# udpate data
|
||||||
shout_dict = shout.dict()
|
shout_dict = shout.dict()
|
||||||
shout_dict["authors"] = [
|
shout_dict["authors"] = [author.dict(), ]
|
||||||
author.dict(),
|
|
||||||
]
|
|
||||||
|
|
||||||
# shout topics aftermath
|
# shout topics aftermath
|
||||||
shout_dict["topics"] = await topics_aftermath(r, storage)
|
shout_dict["topics"] = await topics_aftermath(r, storage)
|
||||||
|
@ -200,9 +193,7 @@ async def migrate(entry, storage):
|
||||||
await content_ratings_to_reactions(entry, shout_dict["slug"])
|
await content_ratings_to_reactions(entry, shout_dict["slug"])
|
||||||
|
|
||||||
# shout views
|
# shout views
|
||||||
await ViewedStorage.increment(
|
await ViewedStorage.increment(shout_dict["slug"], amount=entry.get("views", 1), viewer='old-discours')
|
||||||
shout_dict["slug"], amount=entry.get("views", 1), viewer="old-discours"
|
|
||||||
)
|
|
||||||
# del shout_dict['ratings']
|
# del shout_dict['ratings']
|
||||||
|
|
||||||
storage["shouts"]["by_oid"][entry["_id"]] = shout_dict
|
storage["shouts"]["by_oid"][entry["_id"]] = shout_dict
|
||||||
|
@ -214,9 +205,7 @@ async def add_topics_follower(entry, storage, user):
|
||||||
topics = set([])
|
topics = set([])
|
||||||
category = entry.get("category")
|
category = entry.get("category")
|
||||||
topics_by_oid = storage["topics"]["by_oid"]
|
topics_by_oid = storage["topics"]["by_oid"]
|
||||||
oids = [
|
oids = [category, ] + entry.get("tags", [])
|
||||||
category,
|
|
||||||
] + entry.get("tags", [])
|
|
||||||
for toid in oids:
|
for toid in oids:
|
||||||
tslug = topics_by_oid.get(toid, {}).get("slug")
|
tslug = topics_by_oid.get(toid, {}).get("slug")
|
||||||
if tslug:
|
if tslug:
|
||||||
|
@ -228,18 +217,23 @@ async def add_topics_follower(entry, storage, user):
|
||||||
try:
|
try:
|
||||||
tpc = session.query(Topic).where(Topic.slug == tpcslug).first()
|
tpc = session.query(Topic).where(Topic.slug == tpcslug).first()
|
||||||
if tpc:
|
if tpc:
|
||||||
tf = (
|
tf = session.query(
|
||||||
session.query(TopicFollower)
|
TopicFollower
|
||||||
.where(TopicFollower.follower == user.id)
|
).where(
|
||||||
.filter(TopicFollower.topic == tpc.id)
|
TopicFollower.follower == user.id
|
||||||
.first()
|
).filter(
|
||||||
)
|
TopicFollower.topic == tpc.id
|
||||||
|
).first()
|
||||||
if not tf:
|
if not tf:
|
||||||
tf = TopicFollower.create(topic=tpc.id, follower=user.id, auto=True)
|
tf = TopicFollower.create(
|
||||||
|
topic=tpc.id,
|
||||||
|
follower=user.id,
|
||||||
|
auto=True
|
||||||
|
)
|
||||||
session.add(tf)
|
session.add(tf)
|
||||||
session.commit()
|
session.commit()
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
print("[migration.shout] hidden by topic " + tpc.slug)
|
print('[migration.shout] hidden by topic ' + tpc.slug)
|
||||||
# main topic
|
# main topic
|
||||||
maintopic = storage["replacements"].get(topics_by_oid.get(category, {}).get("slug"))
|
maintopic = storage["replacements"].get(topics_by_oid.get(category, {}).get("slug"))
|
||||||
if maintopic in ttt:
|
if maintopic in ttt:
|
||||||
|
@ -260,7 +254,7 @@ async def process_user(userdata, storage, oid):
|
||||||
if not user:
|
if not user:
|
||||||
try:
|
try:
|
||||||
slug = userdata["slug"].lower().strip()
|
slug = userdata["slug"].lower().strip()
|
||||||
slug = re.sub("[^0-9a-zA-Z]+", "-", slug)
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
|
||||||
userdata["slug"] = slug
|
userdata["slug"] = slug
|
||||||
user = User.create(**userdata)
|
user = User.create(**userdata)
|
||||||
session.add(user)
|
session.add(user)
|
||||||
|
@ -288,9 +282,9 @@ async def resolve_create_shout(shout_dict):
|
||||||
s = session.query(Shout).filter(Shout.slug == shout_dict["slug"]).first()
|
s = session.query(Shout).filter(Shout.slug == shout_dict["slug"]).first()
|
||||||
bump = False
|
bump = False
|
||||||
if s:
|
if s:
|
||||||
if s.createdAt != shout_dict["createdAt"]:
|
if s.createdAt != shout_dict['createdAt']:
|
||||||
# create new with different slug
|
# create new with different slug
|
||||||
shout_dict["slug"] += "-" + shout_dict["layout"]
|
shout_dict["slug"] += '-' + shout_dict["layout"]
|
||||||
try:
|
try:
|
||||||
await create_shout(shout_dict)
|
await create_shout(shout_dict)
|
||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
|
@ -301,7 +295,10 @@ async def resolve_create_shout(shout_dict):
|
||||||
for key in shout_dict:
|
for key in shout_dict:
|
||||||
if key in s.__dict__:
|
if key in s.__dict__:
|
||||||
if s.__dict__[key] != shout_dict[key]:
|
if s.__dict__[key] != shout_dict[key]:
|
||||||
print("[migration] shout already exists, but differs in %s" % key)
|
print(
|
||||||
|
"[migration] shout already exists, but differs in %s"
|
||||||
|
% key
|
||||||
|
)
|
||||||
bump = True
|
bump = True
|
||||||
else:
|
else:
|
||||||
print("[migration] shout already exists, but lacks %s" % key)
|
print("[migration] shout already exists, but lacks %s" % key)
|
||||||
|
@ -347,7 +344,9 @@ async def topics_aftermath(entry, storage):
|
||||||
)
|
)
|
||||||
if not shout_topic_new:
|
if not shout_topic_new:
|
||||||
try:
|
try:
|
||||||
ShoutTopic.create(**{"shout": shout.id, "topic": new_topic.id})
|
ShoutTopic.create(
|
||||||
|
**{"shout": shout.id, "topic": new_topic.id}
|
||||||
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
print("[migration] shout topic error: " + newslug)
|
print("[migration] shout topic error: " + newslug)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
@ -364,7 +363,9 @@ async def content_ratings_to_reactions(entry, slug):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
for content_rating in entry.get("ratings", []):
|
for content_rating in entry.get("ratings", []):
|
||||||
rater = (
|
rater = (
|
||||||
session.query(User).filter(User.oid == content_rating["createdBy"]).first()
|
session.query(User)
|
||||||
|
.filter(User.oid == content_rating["createdBy"])
|
||||||
|
.first()
|
||||||
) or User.default_user
|
) or User.default_user
|
||||||
shout = session.query(Shout).where(Shout.slug == slug).first()
|
shout = session.query(Shout).where(Shout.slug == slug).first()
|
||||||
cts = content_rating.get("createdAt")
|
cts = content_rating.get("createdAt")
|
||||||
|
@ -374,7 +375,7 @@ async def content_ratings_to_reactions(entry, slug):
|
||||||
if content_rating["value"] > 0
|
if content_rating["value"] > 0
|
||||||
else ReactionKind.DISLIKE,
|
else ReactionKind.DISLIKE,
|
||||||
"createdBy": rater.id,
|
"createdBy": rater.id,
|
||||||
"shout": shout.id,
|
"shout": shout.id
|
||||||
}
|
}
|
||||||
reaction = (
|
reaction = (
|
||||||
session.query(Reaction)
|
session.query(Reaction)
|
||||||
|
|
|
@ -5,26 +5,34 @@ from orm.reaction import Reaction, ReactionKind
|
||||||
|
|
||||||
|
|
||||||
def migrate(entry, storage):
|
def migrate(entry, storage):
|
||||||
post_oid = entry["contentItem"]
|
post_oid = entry['contentItem']
|
||||||
print(post_oid)
|
print(post_oid)
|
||||||
shout_dict = storage["shouts"]["by_oid"].get(post_oid)
|
shout_dict = storage['shouts']['by_oid'].get(post_oid)
|
||||||
if shout_dict:
|
if shout_dict:
|
||||||
print(shout_dict["body"])
|
print(shout_dict['body'])
|
||||||
remark = {
|
remark = {
|
||||||
"shout": shout_dict["id"],
|
"shout": shout_dict['id'],
|
||||||
"body": extract_md(html2text(entry["body"]), shout_dict),
|
"body": extract_md(
|
||||||
"kind": ReactionKind.REMARK,
|
html2text(entry['body']),
|
||||||
|
shout_dict
|
||||||
|
),
|
||||||
|
"kind": ReactionKind.REMARK
|
||||||
}
|
}
|
||||||
|
|
||||||
if entry.get("textBefore"):
|
if entry.get('textBefore'):
|
||||||
remark["range"] = (
|
remark['range'] = str(
|
||||||
str(shout_dict["body"].index(entry["textBefore"] or ""))
|
shout_dict['body']
|
||||||
+ ":"
|
.index(
|
||||||
+ str(
|
entry['textBefore'] or ''
|
||||||
shout_dict["body"].index(entry["textAfter"] or "")
|
)
|
||||||
+ len(entry["textAfter"] or "")
|
) + ':' + str(
|
||||||
|
shout_dict['body']
|
||||||
|
.index(
|
||||||
|
entry['textAfter'] or ''
|
||||||
|
) + len(
|
||||||
|
entry['textAfter'] or ''
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
rmrk = Reaction.create(**remark)
|
rmrk = Reaction.create(**remark)
|
||||||
|
|
|
@ -10,7 +10,7 @@ def migrate(entry):
|
||||||
"slug": entry["slug"],
|
"slug": entry["slug"],
|
||||||
"oid": entry["_id"],
|
"oid": entry["_id"],
|
||||||
"title": entry["title"].replace(" ", " "),
|
"title": entry["title"].replace(" ", " "),
|
||||||
"body": extract_md(html2text(body_orig)),
|
"body": extract_md(html2text(body_orig))
|
||||||
}
|
}
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
from base.orm import local_session
|
import re
|
||||||
|
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from dateutil.parser import parse
|
from dateutil.parser import parse
|
||||||
from orm.user import AuthorFollower, User, UserRating
|
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
import re
|
from base.orm import local_session
|
||||||
|
from orm.user import AuthorFollower, User, UserRating
|
||||||
|
|
||||||
|
|
||||||
def migrate(entry):
|
def migrate(entry):
|
||||||
|
@ -22,7 +23,7 @@ def migrate(entry):
|
||||||
"muted": False, # amnesty
|
"muted": False, # amnesty
|
||||||
"links": [],
|
"links": [],
|
||||||
"name": "anonymous",
|
"name": "anonymous",
|
||||||
"password": entry["services"]["password"].get("bcrypt"),
|
"password": entry["services"]["password"].get("bcrypt")
|
||||||
}
|
}
|
||||||
|
|
||||||
if "updatedAt" in entry:
|
if "updatedAt" in entry:
|
||||||
|
@ -32,13 +33,9 @@ def migrate(entry):
|
||||||
if entry.get("profile"):
|
if entry.get("profile"):
|
||||||
# slug
|
# slug
|
||||||
slug = entry["profile"].get("path").lower()
|
slug = entry["profile"].get("path").lower()
|
||||||
slug = re.sub("[^0-9a-zA-Z]+", "-", slug).strip()
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug).strip()
|
||||||
user_dict["slug"] = slug
|
user_dict["slug"] = slug
|
||||||
bio = (
|
bio = (entry.get("profile", {"bio": ""}).get("bio") or "").replace('\(', '(').replace('\)', ')')
|
||||||
(entry.get("profile", {"bio": ""}).get("bio") or "")
|
|
||||||
.replace(r"\(", "(")
|
|
||||||
.replace(r"\)", ")")
|
|
||||||
)
|
|
||||||
bio_text = BeautifulSoup(bio, features="lxml").text
|
bio_text = BeautifulSoup(bio, features="lxml").text
|
||||||
|
|
||||||
if len(bio_text) > 120:
|
if len(bio_text) > 120:
|
||||||
|
@ -49,7 +46,8 @@ def migrate(entry):
|
||||||
# userpic
|
# userpic
|
||||||
try:
|
try:
|
||||||
user_dict["userpic"] = (
|
user_dict["userpic"] = (
|
||||||
"https://images.discours.io/unsafe/" + entry["profile"]["thumborId"]
|
"https://images.discours.io/unsafe/"
|
||||||
|
+ entry["profile"]["thumborId"]
|
||||||
)
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
try:
|
try:
|
||||||
|
@ -64,7 +62,11 @@ def migrate(entry):
|
||||||
name = (name + " " + ln) if ln else name
|
name = (name + " " + ln) if ln else name
|
||||||
if not name:
|
if not name:
|
||||||
name = slug if slug else "anonymous"
|
name = slug if slug else "anonymous"
|
||||||
name = entry["profile"]["path"].lower().strip().replace(" ", "-") if len(name) < 2 else name
|
name = (
|
||||||
|
entry["profile"]["path"].lower().strip().replace(" ", "-")
|
||||||
|
if len(name) < 2
|
||||||
|
else name
|
||||||
|
)
|
||||||
user_dict["name"] = name
|
user_dict["name"] = name
|
||||||
|
|
||||||
# links
|
# links
|
||||||
|
@ -93,7 +95,9 @@ def migrate(entry):
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
print("[migration] cannot create user " + user_dict["slug"])
|
print("[migration] cannot create user " + user_dict["slug"])
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
old_user = session.query(User).filter(User.slug == user_dict["slug"]).first()
|
old_user = (
|
||||||
|
session.query(User).filter(User.slug == user_dict["slug"]).first()
|
||||||
|
)
|
||||||
old_user.oid = oid
|
old_user.oid = oid
|
||||||
old_user.password = user_dict["password"]
|
old_user.password = user_dict["password"]
|
||||||
session.commit()
|
session.commit()
|
||||||
|
@ -110,7 +114,7 @@ def post_migrate():
|
||||||
"slug": "old-discours",
|
"slug": "old-discours",
|
||||||
"username": "old-discours",
|
"username": "old-discours",
|
||||||
"email": "old@discours.io",
|
"email": "old@discours.io",
|
||||||
"name": "Просмотры на старой версии сайта",
|
"name": "Просмотры на старой версии сайта"
|
||||||
}
|
}
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
|
@ -143,8 +147,12 @@ def migrate_2stage(entry, id_map):
|
||||||
}
|
}
|
||||||
|
|
||||||
user_rating = UserRating.create(**user_rating_dict)
|
user_rating = UserRating.create(**user_rating_dict)
|
||||||
if user_rating_dict["value"] > 0:
|
if user_rating_dict['value'] > 0:
|
||||||
af = AuthorFollower.create(author=user.id, follower=rater.id, auto=True)
|
af = AuthorFollower.create(
|
||||||
|
author=user.id,
|
||||||
|
follower=rater.id,
|
||||||
|
auto=True
|
||||||
|
)
|
||||||
session.add(af)
|
session.add(af)
|
||||||
session.add(user_rating)
|
session.add(user_rating)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from base.orm import Base, engine
|
from base.orm import Base, engine
|
||||||
from orm.community import Community
|
from orm.community import Community
|
||||||
from orm.notification import Notification
|
from orm.notification import Notification
|
||||||
from orm.rbac import Operation, Permission, Resource, Role
|
from orm.rbac import Operation, Resource, Permission, Role
|
||||||
from orm.reaction import Reaction
|
from orm.reaction import Reaction
|
||||||
from orm.shout import Shout
|
from orm.shout import Shout
|
||||||
from orm.topic import Topic, TopicFollower
|
from orm.topic import Topic, TopicFollower
|
||||||
|
@ -32,5 +32,5 @@ __all__ = [
|
||||||
"Notification",
|
"Notification",
|
||||||
"Reaction",
|
"Reaction",
|
||||||
"UserRating",
|
"UserRating",
|
||||||
"init_tables",
|
"init_tables"
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
from base.orm import Base
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from sqlalchemy import Column, DateTime, ForeignKey, String
|
from sqlalchemy import Column, DateTime, ForeignKey, String
|
||||||
|
|
||||||
|
from base.orm import Base
|
||||||
|
|
||||||
|
|
||||||
class ShoutCollection(Base):
|
class ShoutCollection(Base):
|
||||||
__tablename__ = "shout_collection"
|
__tablename__ = "shout_collection"
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from base.orm import Base, local_session
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from sqlalchemy import Column, DateTime, ForeignKey, String
|
|
||||||
|
from sqlalchemy import Column, String, ForeignKey, DateTime
|
||||||
|
from base.orm import Base, local_session
|
||||||
|
|
||||||
|
|
||||||
class CommunityFollower(Base):
|
class CommunityFollower(Base):
|
||||||
|
@ -9,7 +10,9 @@ class CommunityFollower(Base):
|
||||||
id = None # type: ignore
|
id = None # type: ignore
|
||||||
follower = Column(ForeignKey("user.id"), primary_key=True)
|
follower = Column(ForeignKey("user.id"), primary_key=True)
|
||||||
community = Column(ForeignKey("community.id"), primary_key=True)
|
community = Column(ForeignKey("community.id"), primary_key=True)
|
||||||
joinedAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
|
joinedAt = Column(
|
||||||
|
DateTime, nullable=False, default=datetime.now, comment="Created at"
|
||||||
|
)
|
||||||
# role = Column(ForeignKey(Role.id), nullable=False, comment="Role for member")
|
# role = Column(ForeignKey(Role.id), nullable=False, comment="Role for member")
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,15 +23,19 @@ class Community(Base):
|
||||||
slug = Column(String, nullable=False, unique=True, comment="Slug")
|
slug = Column(String, nullable=False, unique=True, comment="Slug")
|
||||||
desc = Column(String, nullable=False, default="")
|
desc = Column(String, nullable=False, default="")
|
||||||
pic = Column(String, nullable=False, default="")
|
pic = Column(String, nullable=False, default="")
|
||||||
createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
|
createdAt = Column(
|
||||||
|
DateTime, nullable=False, default=datetime.now, comment="Created at"
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def init_table():
|
def init_table():
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
d = session.query(Community).filter(Community.slug == "discours").first()
|
d = (
|
||||||
|
session.query(Community).filter(Community.slug == "discours").first()
|
||||||
|
)
|
||||||
if not d:
|
if not d:
|
||||||
d = Community.create(name="Дискурс", slug="discours")
|
d = Community.create(name="Дискурс", slug="discours")
|
||||||
session.add(d)
|
session.add(d)
|
||||||
session.commit()
|
session.commit()
|
||||||
Community.default_community = d
|
Community.default_community = d
|
||||||
print("[orm] default community id: %s" % d.id)
|
print('[orm] default community id: %s' % d.id)
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
from base.orm import Base
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum as Enumeration
|
from sqlalchemy import Column, Enum, ForeignKey, DateTime, Boolean, Integer
|
||||||
from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer
|
|
||||||
from sqlalchemy.dialects.postgresql import JSONB
|
from sqlalchemy.dialects.postgresql import JSONB
|
||||||
|
|
||||||
|
from base.orm import Base
|
||||||
|
from enum import Enum as Enumeration
|
||||||
|
|
||||||
|
|
||||||
class NotificationType(Enumeration):
|
class NotificationType(Enumeration):
|
||||||
NEW_COMMENT = 1
|
NEW_COMMENT = 1
|
||||||
|
|
48
orm/rbac.py
48
orm/rbac.py
|
@ -1,8 +1,9 @@
|
||||||
from base.orm import Base, local_session, REGISTRY
|
import warnings
|
||||||
from sqlalchemy import Column, ForeignKey, String, TypeDecorator, UniqueConstraint
|
|
||||||
|
from sqlalchemy import String, Column, ForeignKey, UniqueConstraint, TypeDecorator
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
import warnings
|
from base.orm import Base, REGISTRY, engine, local_session
|
||||||
|
|
||||||
# Role Based Access Control #
|
# Role Based Access Control #
|
||||||
|
|
||||||
|
@ -120,23 +121,16 @@ class Operation(Base):
|
||||||
|
|
||||||
class Resource(Base):
|
class Resource(Base):
|
||||||
__tablename__ = "resource"
|
__tablename__ = "resource"
|
||||||
resourceClass = Column(String, nullable=False, unique=True, comment="Resource class")
|
resourceClass = Column(
|
||||||
|
String, nullable=False, unique=True, comment="Resource class"
|
||||||
|
)
|
||||||
name = Column(String, nullable=False, unique=True, comment="Resource name")
|
name = Column(String, nullable=False, unique=True, comment="Resource name")
|
||||||
# TODO: community = Column(ForeignKey())
|
# TODO: community = Column(ForeignKey())
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def init_table():
|
def init_table():
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
for res in [
|
for res in ["shout", "topic", "reaction", "chat", "message", "invite", "community", "user"]:
|
||||||
"shout",
|
|
||||||
"topic",
|
|
||||||
"reaction",
|
|
||||||
"chat",
|
|
||||||
"message",
|
|
||||||
"invite",
|
|
||||||
"community",
|
|
||||||
"user",
|
|
||||||
]:
|
|
||||||
r = session.query(Resource).filter(Resource.name == res).first()
|
r = session.query(Resource).filter(Resource.name == res).first()
|
||||||
if not r:
|
if not r:
|
||||||
r = Resource.create(name=res, resourceClass=res)
|
r = Resource.create(name=res, resourceClass=res)
|
||||||
|
@ -151,7 +145,9 @@ class Permission(Base):
|
||||||
{"extend_existing": True},
|
{"extend_existing": True},
|
||||||
)
|
)
|
||||||
|
|
||||||
role = Column(ForeignKey("role.id", ondelete="CASCADE"), nullable=False, comment="Role")
|
role = Column(
|
||||||
|
ForeignKey("role.id", ondelete="CASCADE"), nullable=False, comment="Role"
|
||||||
|
)
|
||||||
operation = Column(
|
operation = Column(
|
||||||
ForeignKey("operation.id", ondelete="CASCADE"),
|
ForeignKey("operation.id", ondelete="CASCADE"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
|
@ -164,14 +160,14 @@ class Permission(Base):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
# ops = [
|
ops = [
|
||||||
# Permission(role=1, operation=1, resource=1),
|
Permission(role=1, operation=1, resource=1),
|
||||||
# Permission(role=1, operation=2, resource=1),
|
Permission(role=1, operation=2, resource=1),
|
||||||
# Permission(role=1, operation=3, resource=1),
|
Permission(role=1, operation=3, resource=1),
|
||||||
# Permission(role=1, operation=4, resource=1),
|
Permission(role=1, operation=4, resource=1),
|
||||||
# Permission(role=2, operation=4, resource=1),
|
Permission(role=2, operation=4, resource=1),
|
||||||
# ]
|
]
|
||||||
# global_session.add_all(ops)
|
global_session.add_all(ops)
|
||||||
# global_session.commit()
|
global_session.commit()
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
from base.orm import Base
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from enum import Enum as Enumeration
|
from enum import Enum as Enumeration
|
||||||
|
|
||||||
from sqlalchemy import Column, DateTime, Enum, ForeignKey, String
|
from sqlalchemy import Column, DateTime, Enum, ForeignKey, String
|
||||||
|
|
||||||
|
from base.orm import Base
|
||||||
|
|
||||||
|
|
||||||
class ReactionKind(Enumeration):
|
class ReactionKind(Enumeration):
|
||||||
AGREE = 1 # +1
|
AGREE = 1 # +1
|
||||||
|
@ -25,14 +27,18 @@ class ReactionKind(Enumeration):
|
||||||
class Reaction(Base):
|
class Reaction(Base):
|
||||||
__tablename__ = "reaction"
|
__tablename__ = "reaction"
|
||||||
body = Column(String, nullable=True, comment="Reaction Body")
|
body = Column(String, nullable=True, comment="Reaction Body")
|
||||||
createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
|
createdAt = Column(
|
||||||
|
DateTime, nullable=False, default=datetime.now, comment="Created at"
|
||||||
|
)
|
||||||
createdBy = Column(ForeignKey("user.id"), nullable=False, index=True, comment="Sender")
|
createdBy = Column(ForeignKey("user.id"), nullable=False, index=True, comment="Sender")
|
||||||
updatedAt = Column(DateTime, nullable=True, comment="Updated at")
|
updatedAt = Column(DateTime, nullable=True, comment="Updated at")
|
||||||
updatedBy = Column(ForeignKey("user.id"), nullable=True, index=True, comment="Last Editor")
|
updatedBy = Column(ForeignKey("user.id"), nullable=True, index=True, comment="Last Editor")
|
||||||
deletedAt = Column(DateTime, nullable=True, comment="Deleted at")
|
deletedAt = Column(DateTime, nullable=True, comment="Deleted at")
|
||||||
deletedBy = Column(ForeignKey("user.id"), nullable=True, index=True, comment="Deleted by")
|
deletedBy = Column(ForeignKey("user.id"), nullable=True, index=True, comment="Deleted by")
|
||||||
shout = Column(ForeignKey("shout.id"), nullable=False, index=True)
|
shout = Column(ForeignKey("shout.id"), nullable=False, index=True)
|
||||||
replyTo = Column(ForeignKey("reaction.id"), nullable=True, comment="Reply to reaction ID")
|
replyTo = Column(
|
||||||
|
ForeignKey("reaction.id"), nullable=True, comment="Reply to reaction ID"
|
||||||
|
)
|
||||||
range = Column(String, nullable=True, comment="Range in format <start index>:<end>")
|
range = Column(String, nullable=True, comment="Range in format <start index>:<end>")
|
||||||
kind = Column(Enum(ReactionKind), nullable=False, comment="Reaction kind")
|
kind = Column(Enum(ReactionKind), nullable=False, comment="Reaction kind")
|
||||||
oid = Column(String, nullable=True, comment="Old ID")
|
oid = Column(String, nullable=True, comment="Old ID")
|
||||||
|
|
16
orm/shout.py
16
orm/shout.py
|
@ -1,10 +1,12 @@
|
||||||
from base.orm import Base, local_session
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, JSON
|
||||||
|
from sqlalchemy.orm import column_property, relationship
|
||||||
|
|
||||||
|
from base.orm import Base, local_session
|
||||||
from orm.reaction import Reaction
|
from orm.reaction import Reaction
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
from orm.user import User
|
from orm.user import User
|
||||||
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, JSON, String
|
|
||||||
from sqlalchemy.orm import column_property, relationship
|
|
||||||
|
|
||||||
|
|
||||||
class ShoutTopic(Base):
|
class ShoutTopic(Base):
|
||||||
|
@ -22,7 +24,9 @@ class ShoutReactionsFollower(Base):
|
||||||
follower = Column(ForeignKey("user.id"), primary_key=True, index=True)
|
follower = Column(ForeignKey("user.id"), primary_key=True, index=True)
|
||||||
shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
|
shout = Column(ForeignKey("shout.id"), primary_key=True, index=True)
|
||||||
auto = Column(Boolean, nullable=False, default=False)
|
auto = Column(Boolean, nullable=False, default=False)
|
||||||
createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
|
createdAt = Column(
|
||||||
|
DateTime, nullable=False, default=datetime.now, comment="Created at"
|
||||||
|
)
|
||||||
deletedAt = Column(DateTime, nullable=True)
|
deletedAt = Column(DateTime, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,7 +72,7 @@ class Shout(Base):
|
||||||
|
|
||||||
# TODO: these field should be used or modified
|
# TODO: these field should be used or modified
|
||||||
community = Column(ForeignKey("community.id"), default=1)
|
community = Column(ForeignKey("community.id"), default=1)
|
||||||
lang = Column(String, nullable=False, default="ru", comment="Language")
|
lang = Column(String, nullable=False, default='ru', comment="Language")
|
||||||
mainTopic = Column(ForeignKey("topic.slug"), nullable=True)
|
mainTopic = Column(ForeignKey("topic.slug"), nullable=True)
|
||||||
visibility = Column(String, nullable=True) # owner authors community public
|
visibility = Column(String, nullable=True) # owner authors community public
|
||||||
versionOf = Column(ForeignKey("shout.id"), nullable=True)
|
versionOf = Column(ForeignKey("shout.id"), nullable=True)
|
||||||
|
@ -83,7 +87,7 @@ class Shout(Base):
|
||||||
"slug": "genesis-block",
|
"slug": "genesis-block",
|
||||||
"body": "",
|
"body": "",
|
||||||
"title": "Ничего",
|
"title": "Ничего",
|
||||||
"lang": "ru",
|
"lang": "ru"
|
||||||
}
|
}
|
||||||
s = Shout.create(**entry)
|
s = Shout.create(**entry)
|
||||||
session.add(s)
|
session.add(s)
|
||||||
|
|
12
orm/topic.py
12
orm/topic.py
|
@ -1,7 +1,9 @@
|
||||||
from base.orm import Base
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, String
|
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, String
|
||||||
|
|
||||||
|
from base.orm import Base
|
||||||
|
|
||||||
|
|
||||||
class TopicFollower(Base):
|
class TopicFollower(Base):
|
||||||
__tablename__ = "topic_followers"
|
__tablename__ = "topic_followers"
|
||||||
|
@ -9,7 +11,9 @@ class TopicFollower(Base):
|
||||||
id = None # type: ignore
|
id = None # type: ignore
|
||||||
follower = Column(ForeignKey("user.id"), primary_key=True, index=True)
|
follower = Column(ForeignKey("user.id"), primary_key=True, index=True)
|
||||||
topic = Column(ForeignKey("topic.id"), primary_key=True, index=True)
|
topic = Column(ForeignKey("topic.id"), primary_key=True, index=True)
|
||||||
createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
|
createdAt = Column(
|
||||||
|
DateTime, nullable=False, default=datetime.now, comment="Created at"
|
||||||
|
)
|
||||||
auto = Column(Boolean, nullable=False, default=False)
|
auto = Column(Boolean, nullable=False, default=False)
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,5 +24,7 @@ class Topic(Base):
|
||||||
title = Column(String, nullable=False, comment="Title")
|
title = Column(String, nullable=False, comment="Title")
|
||||||
body = Column(String, nullable=True, comment="Body")
|
body = Column(String, nullable=True, comment="Body")
|
||||||
pic = Column(String, nullable=True, comment="Picture")
|
pic = Column(String, nullable=True, comment="Picture")
|
||||||
community = Column(ForeignKey("community.id"), default=1, comment="Community")
|
community = Column(
|
||||||
|
ForeignKey("community.id"), default=1, comment="Community"
|
||||||
|
)
|
||||||
oid = Column(String, nullable=True, comment="Old ID")
|
oid = Column(String, nullable=True, comment="Old ID")
|
||||||
|
|
20
orm/user.py
20
orm/user.py
|
@ -1,10 +1,10 @@
|
||||||
from base.orm import Base, local_session
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from orm.rbac import Role
|
|
||||||
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer
|
|
||||||
from sqlalchemy import JSON as JSONType
|
from sqlalchemy import JSON as JSONType
|
||||||
from sqlalchemy import String
|
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
from base.orm import Base, local_session
|
||||||
|
from orm.rbac import Role
|
||||||
|
|
||||||
|
|
||||||
class UserRating(Base):
|
class UserRating(Base):
|
||||||
|
@ -34,7 +34,9 @@ class AuthorFollower(Base):
|
||||||
id = None # type: ignore
|
id = None # type: ignore
|
||||||
follower = Column(ForeignKey("user.id"), primary_key=True, index=True)
|
follower = Column(ForeignKey("user.id"), primary_key=True, index=True)
|
||||||
author = Column(ForeignKey("user.id"), primary_key=True, index=True)
|
author = Column(ForeignKey("user.id"), primary_key=True, index=True)
|
||||||
createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
|
createdAt = Column(
|
||||||
|
DateTime, nullable=False, default=datetime.now, comment="Created at"
|
||||||
|
)
|
||||||
auto = Column(Boolean, nullable=False, default=False)
|
auto = Column(Boolean, nullable=False, default=False)
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,8 +54,12 @@ class User(Base):
|
||||||
slug = Column(String, unique=True, comment="User's slug")
|
slug = Column(String, unique=True, comment="User's slug")
|
||||||
muted = Column(Boolean, default=False)
|
muted = Column(Boolean, default=False)
|
||||||
emailConfirmed = Column(Boolean, default=False)
|
emailConfirmed = Column(Boolean, default=False)
|
||||||
createdAt = Column(DateTime, nullable=False, default=datetime.now, comment="Created at")
|
createdAt = Column(
|
||||||
lastSeen = Column(DateTime, nullable=False, default=datetime.now, comment="Was online at")
|
DateTime, nullable=False, default=datetime.now, comment="Created at"
|
||||||
|
)
|
||||||
|
lastSeen = Column(
|
||||||
|
DateTime, nullable=False, default=datetime.now, comment="Was online at"
|
||||||
|
)
|
||||||
deletedAt = Column(DateTime, nullable=True, comment="Deleted at")
|
deletedAt = Column(DateTime, nullable=True, comment="Deleted at")
|
||||||
links = Column(JSONType, nullable=True, comment="Links")
|
links = Column(JSONType, nullable=True, comment="Links")
|
||||||
oauth = Column(String, nullable=True)
|
oauth = Column(String, nullable=True)
|
||||||
|
|
|
@ -2,5 +2,3 @@ isort
|
||||||
brunette
|
brunette
|
||||||
flake8
|
flake8
|
||||||
mypy
|
mypy
|
||||||
pre-commit
|
|
||||||
black
|
|
||||||
|
|
|
@ -18,12 +18,15 @@ transliterate~=1.10.2
|
||||||
requests~=2.28.1
|
requests~=2.28.1
|
||||||
bcrypt>=4.0.0
|
bcrypt>=4.0.0
|
||||||
bson~=0.5.10
|
bson~=0.5.10
|
||||||
|
flake8
|
||||||
DateTime~=4.7
|
DateTime~=4.7
|
||||||
asyncio~=3.4.3
|
asyncio~=3.4.3
|
||||||
python-dateutil~=2.8.2
|
python-dateutil~=2.8.2
|
||||||
beautifulsoup4~=4.11.1
|
beautifulsoup4~=4.11.1
|
||||||
lxml
|
lxml
|
||||||
sentry-sdk>=1.14.0
|
sentry-sdk>=1.14.0
|
||||||
|
# sse_starlette
|
||||||
|
graphql-ws
|
||||||
nltk~=3.8.1
|
nltk~=3.8.1
|
||||||
pymystem3~=0.2.0
|
pymystem3~=0.2.0
|
||||||
transformers~=4.28.1
|
transformers~=4.28.1
|
||||||
|
|
|
@ -53,3 +53,4 @@ echo "Start migration"
|
||||||
python3 server.py migrate
|
python3 server.py migrate
|
||||||
if [ $? -ne 0 ]; then { echo "Migration failed, aborting." ; exit 1; } fi
|
if [ $? -ne 0 ]; then { echo "Migration failed, aborting." ; exit 1; } fi
|
||||||
echo 'Done!'
|
echo 'Done!'
|
||||||
|
|
||||||
|
|
67
resolvers/__init__.py
Normal file
67
resolvers/__init__.py
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
from resolvers.auth import (
|
||||||
|
login,
|
||||||
|
sign_out,
|
||||||
|
is_email_used,
|
||||||
|
register_by_email,
|
||||||
|
confirm_email,
|
||||||
|
auth_send_link,
|
||||||
|
get_current_user,
|
||||||
|
)
|
||||||
|
|
||||||
|
from resolvers.create.migrate import markdown_body
|
||||||
|
from resolvers.create.editor import create_shout, delete_shout, update_shout
|
||||||
|
|
||||||
|
from resolvers.zine.profile import (
|
||||||
|
load_authors_by,
|
||||||
|
rate_user,
|
||||||
|
update_profile,
|
||||||
|
get_authors_all
|
||||||
|
)
|
||||||
|
|
||||||
|
from resolvers.zine.reactions import (
|
||||||
|
create_reaction,
|
||||||
|
delete_reaction,
|
||||||
|
update_reaction,
|
||||||
|
reactions_unfollow,
|
||||||
|
reactions_follow,
|
||||||
|
load_reactions_by
|
||||||
|
)
|
||||||
|
from resolvers.zine.topics import (
|
||||||
|
topic_follow,
|
||||||
|
topic_unfollow,
|
||||||
|
topics_by_author,
|
||||||
|
topics_by_community,
|
||||||
|
topics_all,
|
||||||
|
get_topic
|
||||||
|
)
|
||||||
|
|
||||||
|
from resolvers.zine.following import (
|
||||||
|
follow,
|
||||||
|
unfollow
|
||||||
|
)
|
||||||
|
|
||||||
|
from resolvers.zine.load import (
|
||||||
|
load_shout,
|
||||||
|
load_shouts_by
|
||||||
|
)
|
||||||
|
|
||||||
|
from resolvers.inbox.chats import (
|
||||||
|
create_chat,
|
||||||
|
delete_chat,
|
||||||
|
update_chat
|
||||||
|
|
||||||
|
)
|
||||||
|
from resolvers.inbox.messages import (
|
||||||
|
create_message,
|
||||||
|
delete_message,
|
||||||
|
update_message,
|
||||||
|
mark_as_read
|
||||||
|
)
|
||||||
|
from resolvers.inbox.load import (
|
||||||
|
load_chats,
|
||||||
|
load_messages_by,
|
||||||
|
load_recipients
|
||||||
|
)
|
||||||
|
from resolvers.inbox.search import search_recipients
|
||||||
|
|
||||||
|
from resolvers.notifications import load_notifications
|
|
@ -1,29 +1,24 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from urllib.parse import quote_plus
|
||||||
|
|
||||||
|
from graphql.type import GraphQLResolveInfo
|
||||||
|
from starlette.responses import RedirectResponse
|
||||||
|
from transliterate import translit
|
||||||
|
import re
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from auth.email import send_auth_email
|
from auth.email import send_auth_email
|
||||||
from auth.identity import Identity, Password
|
from auth.identity import Identity, Password
|
||||||
from auth.jwtcodec import JWTCodec
|
from auth.jwtcodec import JWTCodec
|
||||||
from auth.tokenstorage import TokenStorage
|
from auth.tokenstorage import TokenStorage
|
||||||
from base.exceptions import (
|
from base.exceptions import (BaseHttpException, InvalidPassword, InvalidToken,
|
||||||
BaseHttpException,
|
ObjectNotExist, Unauthorized)
|
||||||
InvalidPassword,
|
|
||||||
InvalidToken,
|
|
||||||
ObjectNotExist,
|
|
||||||
Unauthorized,
|
|
||||||
)
|
|
||||||
from base.orm import local_session
|
from base.orm import local_session
|
||||||
from base.resolvers import mutation, query
|
from base.resolvers import mutation, query
|
||||||
from datetime import datetime, timezone
|
|
||||||
from graphql.type import GraphQLResolveInfo
|
|
||||||
from orm import Role, User
|
from orm import Role, User
|
||||||
from settings import FRONTEND_URL, SESSION_TOKEN_HEADER
|
from settings import SESSION_TOKEN_HEADER, FRONTEND_URL
|
||||||
from starlette.responses import RedirectResponse
|
|
||||||
from transliterate import translit
|
|
||||||
from urllib.parse import quote_plus
|
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("getSession")
|
@mutation.field("getSession")
|
||||||
|
@ -37,14 +32,17 @@ async def get_current_user(_, info):
|
||||||
user.lastSeen = datetime.now(tz=timezone.utc)
|
user.lastSeen = datetime.now(tz=timezone.utc)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return {"token": token, "user": user}
|
return {
|
||||||
|
"token": token,
|
||||||
|
"user": user
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("confirmEmail")
|
@mutation.field("confirmEmail")
|
||||||
async def confirm_email(_, info, token):
|
async def confirm_email(_, info, token):
|
||||||
"""confirm owning email address"""
|
"""confirm owning email address"""
|
||||||
try:
|
try:
|
||||||
print("[resolvers.auth] confirm email by token")
|
print('[resolvers.auth] confirm email by token')
|
||||||
payload = JWTCodec.decode(token)
|
payload = JWTCodec.decode(token)
|
||||||
user_id = payload.user_id
|
user_id = payload.user_id
|
||||||
await TokenStorage.get(f"{user_id}-{payload.username}-{token}")
|
await TokenStorage.get(f"{user_id}-{payload.username}-{token}")
|
||||||
|
@ -55,7 +53,10 @@ async def confirm_email(_, info, token):
|
||||||
user.lastSeen = datetime.now(tz=timezone.utc)
|
user.lastSeen = datetime.now(tz=timezone.utc)
|
||||||
session.add(user)
|
session.add(user)
|
||||||
session.commit()
|
session.commit()
|
||||||
return {"token": session_token, "user": user}
|
return {
|
||||||
|
"token": session_token,
|
||||||
|
"user": user
|
||||||
|
}
|
||||||
except InvalidToken as e:
|
except InvalidToken as e:
|
||||||
raise InvalidToken(e.message)
|
raise InvalidToken(e.message)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -67,9 +68,9 @@ async def confirm_email_handler(request):
|
||||||
token = request.path_params["token"] # one time
|
token = request.path_params["token"] # one time
|
||||||
request.session["token"] = token
|
request.session["token"] = token
|
||||||
res = await confirm_email(None, {}, token)
|
res = await confirm_email(None, {}, token)
|
||||||
print("[resolvers.auth] confirm_email request: %r" % request)
|
print('[resolvers.auth] confirm_email request: %r' % request)
|
||||||
if "error" in res:
|
if "error" in res:
|
||||||
raise BaseHttpException(res["error"])
|
raise BaseHttpException(res['error'])
|
||||||
else:
|
else:
|
||||||
response = RedirectResponse(url=FRONTEND_URL)
|
response = RedirectResponse(url=FRONTEND_URL)
|
||||||
response.set_cookie("token", res["token"]) # session token
|
response.set_cookie("token", res["token"]) # session token
|
||||||
|
@ -86,22 +87,22 @@ def create_user(user_dict):
|
||||||
|
|
||||||
|
|
||||||
def generate_unique_slug(src):
|
def generate_unique_slug(src):
|
||||||
print("[resolvers.auth] generating slug from: " + src)
|
print('[resolvers.auth] generating slug from: ' + src)
|
||||||
slug = translit(src, "ru", reversed=True).replace(".", "-").lower()
|
slug = translit(src, "ru", reversed=True).replace(".", "-").lower()
|
||||||
slug = re.sub("[^0-9a-zA-Z]+", "-", slug)
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
|
||||||
if slug != src:
|
if slug != src:
|
||||||
print("[resolvers.auth] translited name: " + slug)
|
print('[resolvers.auth] translited name: ' + slug)
|
||||||
c = 1
|
c = 1
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
user = session.query(User).where(User.slug == slug).first()
|
user = session.query(User).where(User.slug == slug).first()
|
||||||
while user:
|
while user:
|
||||||
user = session.query(User).where(User.slug == slug).first()
|
user = session.query(User).where(User.slug == slug).first()
|
||||||
slug = slug + "-" + str(c)
|
slug = slug + '-' + str(c)
|
||||||
c += 1
|
c += 1
|
||||||
if not user:
|
if not user:
|
||||||
unique_slug = slug
|
unique_slug = slug
|
||||||
print("[resolvers.auth] " + unique_slug)
|
print('[resolvers.auth] ' + unique_slug)
|
||||||
return quote_plus(unique_slug.replace("'", "")).replace("+", "-")
|
return quote_plus(unique_slug.replace('\'', '')).replace('+', '-')
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("registerUser")
|
@mutation.field("registerUser")
|
||||||
|
@ -116,12 +117,12 @@ async def register_by_email(_, _info, email: str, password: str = "", name: str
|
||||||
slug = generate_unique_slug(name)
|
slug = generate_unique_slug(name)
|
||||||
user = session.query(User).where(User.slug == slug).first()
|
user = session.query(User).where(User.slug == slug).first()
|
||||||
if user:
|
if user:
|
||||||
slug = generate_unique_slug(email.split("@")[0])
|
slug = generate_unique_slug(email.split('@')[0])
|
||||||
user_dict = {
|
user_dict = {
|
||||||
"email": email,
|
"email": email,
|
||||||
"username": email, # will be used to store phone number or some messenger network id
|
"username": email, # will be used to store phone number or some messenger network id
|
||||||
"name": name,
|
"name": name,
|
||||||
"slug": slug,
|
"slug": slug
|
||||||
}
|
}
|
||||||
if password:
|
if password:
|
||||||
user_dict["password"] = Password.encode(password)
|
user_dict["password"] = Password.encode(password)
|
||||||
|
@ -171,7 +172,10 @@ async def login(_, info, email: str, password: str = "", lang: str = "ru"):
|
||||||
user = Identity.password(orm_user, password)
|
user = Identity.password(orm_user, password)
|
||||||
session_token = await TokenStorage.create_session(user)
|
session_token = await TokenStorage.create_session(user)
|
||||||
print(f"[auth] user {email} authorized")
|
print(f"[auth] user {email} authorized")
|
||||||
return {"token": session_token, "user": user}
|
return {
|
||||||
|
"token": session_token,
|
||||||
|
"user": user
|
||||||
|
}
|
||||||
except InvalidPassword:
|
except InvalidPassword:
|
||||||
print(f"[auth] {email}: invalid password")
|
print(f"[auth] {email}: invalid password")
|
||||||
raise InvalidPassword("invalid password") # contains webserver status
|
raise InvalidPassword("invalid password") # contains webserver status
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
from sqlalchemy import and_
|
||||||
|
from sqlalchemy.orm import joinedload
|
||||||
|
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from base.orm import local_session
|
from base.orm import local_session
|
||||||
from base.resolvers import mutation
|
from base.resolvers import mutation
|
||||||
from datetime import datetime, timezone
|
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
from resolvers.zine.reactions import reactions_follow, reactions_unfollow
|
from resolvers.zine.reactions import reactions_follow, reactions_unfollow
|
||||||
from sqlalchemy import and_
|
|
||||||
from sqlalchemy.orm import joinedload
|
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("createShout")
|
@mutation.field("createShout")
|
||||||
|
@ -16,23 +18,21 @@ async def create_shout(_, info, inp):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
topics = session.query(Topic).filter(Topic.slug.in_(inp.get("topics", []))).all()
|
topics = session.query(Topic).filter(Topic.slug.in_(inp.get('topics', []))).all()
|
||||||
|
|
||||||
new_shout = Shout.create(
|
new_shout = Shout.create(**{
|
||||||
**{
|
"title": inp.get("title"),
|
||||||
"title": inp.get("title"),
|
"subtitle": inp.get('subtitle'),
|
||||||
"subtitle": inp.get("subtitle"),
|
"lead": inp.get('lead'),
|
||||||
"lead": inp.get("lead"),
|
"description": inp.get('description'),
|
||||||
"description": inp.get("description"),
|
"body": inp.get("body", ''),
|
||||||
"body": inp.get("body", ""),
|
"layout": inp.get("layout"),
|
||||||
"layout": inp.get("layout"),
|
"authors": inp.get("authors", []),
|
||||||
"authors": inp.get("authors", []),
|
"slug": inp.get("slug"),
|
||||||
"slug": inp.get("slug"),
|
"mainTopic": inp.get("mainTopic"),
|
||||||
"mainTopic": inp.get("mainTopic"),
|
"visibility": "owner",
|
||||||
"visibility": "owner",
|
"createdBy": auth.user_id
|
||||||
"createdBy": auth.user_id,
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
for topic in topics:
|
for topic in topics:
|
||||||
t = ShoutTopic.create(topic=topic.id, shout=new_shout.id)
|
t = ShoutTopic.create(topic=topic.id, shout=new_shout.id)
|
||||||
|
@ -64,15 +64,10 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shout = (
|
shout = session.query(Shout).options(
|
||||||
session.query(Shout)
|
joinedload(Shout.authors),
|
||||||
.options(
|
joinedload(Shout.topics),
|
||||||
joinedload(Shout.authors),
|
).filter(Shout.id == shout_id).first()
|
||||||
joinedload(Shout.topics),
|
|
||||||
)
|
|
||||||
.filter(Shout.id == shout_id)
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
|
|
||||||
if not shout:
|
if not shout:
|
||||||
return {"error": "shout not found"}
|
return {"error": "shout not found"}
|
||||||
|
@ -99,36 +94,24 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
for new_topic_to_link in new_topics_to_link:
|
for new_topic_to_link in new_topics_to_link:
|
||||||
created_unlinked_topic = ShoutTopic.create(
|
created_unlinked_topic = ShoutTopic.create(shout=shout.id, topic=new_topic_to_link.id)
|
||||||
shout=shout.id, topic=new_topic_to_link.id
|
|
||||||
)
|
|
||||||
session.add(created_unlinked_topic)
|
session.add(created_unlinked_topic)
|
||||||
|
|
||||||
existing_topics_input = [
|
existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get("id", 0) > 0]
|
||||||
topic_input for topic_input in topics_input if topic_input.get("id", 0) > 0
|
existing_topic_to_link_ids = [existing_topic_input["id"] for existing_topic_input in existing_topics_input
|
||||||
]
|
if existing_topic_input["id"] not in [topic.id for topic in shout.topics]]
|
||||||
existing_topic_to_link_ids = [
|
|
||||||
existing_topic_input["id"]
|
|
||||||
for existing_topic_input in existing_topics_input
|
|
||||||
if existing_topic_input["id"] not in [topic.id for topic in shout.topics]
|
|
||||||
]
|
|
||||||
|
|
||||||
for existing_topic_to_link_id in existing_topic_to_link_ids:
|
for existing_topic_to_link_id in existing_topic_to_link_ids:
|
||||||
created_unlinked_topic = ShoutTopic.create(
|
created_unlinked_topic = ShoutTopic.create(shout=shout.id, topic=existing_topic_to_link_id)
|
||||||
shout=shout.id, topic=existing_topic_to_link_id
|
|
||||||
)
|
|
||||||
session.add(created_unlinked_topic)
|
session.add(created_unlinked_topic)
|
||||||
|
|
||||||
topic_to_unlink_ids = [
|
topic_to_unlink_ids = [topic.id for topic in shout.topics
|
||||||
topic.id
|
if topic.id not in [topic_input["id"] for topic_input in existing_topics_input]]
|
||||||
for topic in shout.topics
|
|
||||||
if topic.id not in [topic_input["id"] for topic_input in existing_topics_input]
|
|
||||||
]
|
|
||||||
|
|
||||||
shout_topics_to_remove = session.query(ShoutTopic).filter(
|
shout_topics_to_remove = session.query(ShoutTopic).filter(
|
||||||
and_(
|
and_(
|
||||||
ShoutTopic.shout == shout.id,
|
ShoutTopic.shout == shout.id,
|
||||||
ShoutTopic.topic.in_(topic_to_unlink_ids),
|
ShoutTopic.topic.in_(topic_to_unlink_ids)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -137,13 +120,13 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
|
||||||
|
|
||||||
shout_input["mainTopic"] = shout_input["mainTopic"]["slug"]
|
shout_input["mainTopic"] = shout_input["mainTopic"]["slug"]
|
||||||
|
|
||||||
if shout_input["mainTopic"] == "":
|
if shout_input["mainTopic"] == '':
|
||||||
del shout_input["mainTopic"]
|
del shout_input["mainTopic"]
|
||||||
|
|
||||||
shout.update(shout_input)
|
shout.update(shout_input)
|
||||||
updated = True
|
updated = True
|
||||||
|
|
||||||
if publish and shout.visibility == "owner":
|
if publish and shout.visibility == 'owner':
|
||||||
shout.visibility = "community"
|
shout.visibility = "community"
|
||||||
shout.publishedAt = datetime.now(tz=timezone.utc)
|
shout.publishedAt = datetime.now(tz=timezone.utc)
|
||||||
updated = True
|
updated = True
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
# from base.resolvers import query
|
|
||||||
# from migration.extract import extract_md
|
from base.resolvers import query
|
||||||
# from resolvers.auth import login_required
|
from resolvers.auth import login_required
|
||||||
#
|
from migration.extract import extract_md
|
||||||
#
|
|
||||||
# @login_required
|
|
||||||
# @query.field("markdownBody")
|
@login_required
|
||||||
# def markdown_body(_, info, body: str):
|
@query.field("markdownBody")
|
||||||
# body = extract_md(body)
|
def markdown_body(_, info, body: str):
|
||||||
# return body
|
body = extract_md(body)
|
||||||
|
return body
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
|
import json
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from base.redis import redis
|
from base.redis import redis
|
||||||
from base.resolvers import mutation
|
from base.resolvers import mutation
|
||||||
from datetime import datetime, timezone
|
|
||||||
from validations.inbox import Chat
|
from validations.inbox import Chat
|
||||||
|
|
||||||
import json
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("updateChat")
|
@mutation.field("updateChat")
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -24,24 +24,27 @@ async def update_chat(_, info, chat_new: Chat):
|
||||||
chat_id = chat_new["id"]
|
chat_id = chat_new["id"]
|
||||||
chat = await redis.execute("GET", f"chats/{chat_id}")
|
chat = await redis.execute("GET", f"chats/{chat_id}")
|
||||||
if not chat:
|
if not chat:
|
||||||
return {"error": "chat not exist"}
|
return {
|
||||||
|
"error": "chat not exist"
|
||||||
|
}
|
||||||
chat = dict(json.loads(chat))
|
chat = dict(json.loads(chat))
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
if auth.user_id in chat["admins"]:
|
if auth.user_id in chat["admins"]:
|
||||||
chat.update(
|
chat.update({
|
||||||
{
|
"title": chat_new.get("title", chat["title"]),
|
||||||
"title": chat_new.get("title", chat["title"]),
|
"description": chat_new.get("description", chat["description"]),
|
||||||
"description": chat_new.get("description", chat["description"]),
|
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
||||||
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
"admins": chat_new.get("admins", chat.get("admins") or []),
|
||||||
"admins": chat_new.get("admins", chat.get("admins") or []),
|
"users": chat_new.get("users", chat["users"])
|
||||||
"users": chat_new.get("users", chat["users"]),
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
await redis.execute("SET", f"chats/{chat.id}", json.dumps(chat))
|
await redis.execute("SET", f"chats/{chat.id}", json.dumps(chat))
|
||||||
await redis.execute("COMMIT")
|
await redis.execute("COMMIT")
|
||||||
|
|
||||||
return {"error": None, "chat": chat}
|
return {
|
||||||
|
"error": None,
|
||||||
|
"chat": chat
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("createChat")
|
@mutation.field("createChat")
|
||||||
|
@ -49,7 +52,7 @@ async def update_chat(_, info, chat_new: Chat):
|
||||||
async def create_chat(_, info, title="", members=[]):
|
async def create_chat(_, info, title="", members=[]):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
chat = {}
|
chat = {}
|
||||||
print("create_chat members: %r" % members)
|
print('create_chat members: %r' % members)
|
||||||
if auth.user_id not in members:
|
if auth.user_id not in members:
|
||||||
members.append(int(auth.user_id))
|
members.append(int(auth.user_id))
|
||||||
|
|
||||||
|
@ -71,12 +74,15 @@ async def create_chat(_, info, title="", members=[]):
|
||||||
chat = await redis.execute("GET", f"chats/{c.decode('utf-8')}")
|
chat = await redis.execute("GET", f"chats/{c.decode('utf-8')}")
|
||||||
if chat:
|
if chat:
|
||||||
chat = json.loads(chat)
|
chat = json.loads(chat)
|
||||||
if chat["title"] == "":
|
if chat['title'] == "":
|
||||||
print("[inbox] createChat found old chat")
|
print('[inbox] createChat found old chat')
|
||||||
print(chat)
|
print(chat)
|
||||||
break
|
break
|
||||||
if chat:
|
if chat:
|
||||||
return {"chat": chat, "error": "existed"}
|
return {
|
||||||
|
"chat": chat,
|
||||||
|
"error": "existed"
|
||||||
|
}
|
||||||
|
|
||||||
chat_id = str(uuid.uuid4())
|
chat_id = str(uuid.uuid4())
|
||||||
chat = {
|
chat = {
|
||||||
|
@ -86,7 +92,7 @@ async def create_chat(_, info, title="", members=[]):
|
||||||
"createdBy": auth.user_id,
|
"createdBy": auth.user_id,
|
||||||
"createdAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
"createdAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
||||||
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
"updatedAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
||||||
"admins": members if (len(members) == 2 and title == "") else [],
|
"admins": members if (len(members) == 2 and title == "") else []
|
||||||
}
|
}
|
||||||
|
|
||||||
for m in members:
|
for m in members:
|
||||||
|
@ -94,7 +100,10 @@ async def create_chat(_, info, title="", members=[]):
|
||||||
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
await redis.execute("SET", f"chats/{chat_id}", json.dumps(chat))
|
||||||
await redis.execute("SET", f"chats/{chat_id}/next_message_id", str(0))
|
await redis.execute("SET", f"chats/{chat_id}/next_message_id", str(0))
|
||||||
await redis.execute("COMMIT")
|
await redis.execute("COMMIT")
|
||||||
return {"error": None, "chat": chat}
|
return {
|
||||||
|
"error": None,
|
||||||
|
"chat": chat
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("deleteChat")
|
@mutation.field("deleteChat")
|
||||||
|
@ -105,9 +114,11 @@ async def delete_chat(_, info, chat_id: str):
|
||||||
chat = await redis.execute("GET", f"/chats/{chat_id}")
|
chat = await redis.execute("GET", f"/chats/{chat_id}")
|
||||||
if chat:
|
if chat:
|
||||||
chat = dict(json.loads(chat))
|
chat = dict(json.loads(chat))
|
||||||
if auth.user_id in chat["admins"]:
|
if auth.user_id in chat['admins']:
|
||||||
await redis.execute("DEL", f"chats/{chat_id}")
|
await redis.execute("DEL", f"chats/{chat_id}")
|
||||||
await redis.execute("SREM", "chats_by_user/" + str(auth.user_id), chat_id)
|
await redis.execute("SREM", "chats_by_user/" + str(auth.user_id), chat_id)
|
||||||
await redis.execute("COMMIT")
|
await redis.execute("COMMIT")
|
||||||
else:
|
else:
|
||||||
return {"error": "chat not exist"}
|
return {
|
||||||
|
"error": "chat not exist"
|
||||||
|
}
|
||||||
|
|
|
@ -1,26 +1,28 @@
|
||||||
from .unread import get_unread_counter
|
import json
|
||||||
|
# from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from base.orm import local_session
|
|
||||||
from base.redis import redis
|
from base.redis import redis
|
||||||
|
from base.orm import local_session
|
||||||
from base.resolvers import query
|
from base.resolvers import query
|
||||||
from orm.user import User
|
from orm.user import User
|
||||||
from resolvers.zine.profile import followed_authors
|
from resolvers.zine.profile import followed_authors
|
||||||
|
from .unread import get_unread_counter
|
||||||
import json
|
|
||||||
|
|
||||||
# from datetime import datetime, timedelta, timezone
|
|
||||||
|
|
||||||
|
|
||||||
async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=[]):
|
async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=[]):
|
||||||
"""load :limit messages for :chat_id with :offset"""
|
''' load :limit messages for :chat_id with :offset '''
|
||||||
messages = []
|
messages = []
|
||||||
message_ids = []
|
message_ids = []
|
||||||
if ids:
|
if ids:
|
||||||
message_ids += ids
|
message_ids += ids
|
||||||
try:
|
try:
|
||||||
if limit:
|
if limit:
|
||||||
mids = await redis.lrange(f"chats/{chat_id}/message_ids", offset, offset + limit)
|
mids = await redis.lrange(f"chats/{chat_id}/message_ids",
|
||||||
|
offset,
|
||||||
|
offset + limit
|
||||||
|
)
|
||||||
mids = [mid.decode("utf-8") for mid in mids]
|
mids = [mid.decode("utf-8") for mid in mids]
|
||||||
message_ids += mids
|
message_ids += mids
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -28,10 +30,10 @@ async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=[]):
|
||||||
if message_ids:
|
if message_ids:
|
||||||
message_keys = [f"chats/{chat_id}/messages/{mid}" for mid in message_ids]
|
message_keys = [f"chats/{chat_id}/messages/{mid}" for mid in message_ids]
|
||||||
messages = await redis.mget(*message_keys)
|
messages = await redis.mget(*message_keys)
|
||||||
messages = [json.loads(msg.decode("utf-8")) for msg in messages]
|
messages = [json.loads(msg.decode('utf-8')) for msg in messages]
|
||||||
replies = []
|
replies = []
|
||||||
for m in messages:
|
for m in messages:
|
||||||
rt = m.get("replyTo")
|
rt = m.get('replyTo')
|
||||||
if rt:
|
if rt:
|
||||||
rt = int(rt)
|
rt = int(rt)
|
||||||
if rt not in message_ids:
|
if rt not in message_ids:
|
||||||
|
@ -44,14 +46,14 @@ async def load_messages(chat_id: str, limit: int = 5, offset: int = 0, ids=[]):
|
||||||
@query.field("loadChats")
|
@query.field("loadChats")
|
||||||
@login_required
|
@login_required
|
||||||
async def load_chats(_, info, limit: int = 50, offset: int = 0):
|
async def load_chats(_, info, limit: int = 50, offset: int = 0):
|
||||||
"""load :limit chats of current user with :offset"""
|
""" load :limit chats of current user with :offset """
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
|
|
||||||
cids = await redis.execute("SMEMBERS", "chats_by_user/" + str(auth.user_id))
|
cids = await redis.execute("SMEMBERS", "chats_by_user/" + str(auth.user_id))
|
||||||
if cids:
|
if cids:
|
||||||
cids = list(cids)[offset : offset + limit]
|
cids = list(cids)[offset:offset + limit]
|
||||||
if not cids:
|
if not cids:
|
||||||
print("[inbox.load] no chats were found")
|
print('[inbox.load] no chats were found')
|
||||||
cids = []
|
cids = []
|
||||||
onliners = await redis.execute("SMEMBERS", "users-online")
|
onliners = await redis.execute("SMEMBERS", "users-online")
|
||||||
if not onliners:
|
if not onliners:
|
||||||
|
@ -62,53 +64,62 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0):
|
||||||
c = await redis.execute("GET", "chats/" + cid)
|
c = await redis.execute("GET", "chats/" + cid)
|
||||||
if c:
|
if c:
|
||||||
c = dict(json.loads(c))
|
c = dict(json.loads(c))
|
||||||
c["messages"] = await load_messages(cid, 5, 0)
|
c['messages'] = await load_messages(cid, 5, 0)
|
||||||
c["unread"] = await get_unread_counter(cid, auth.user_id)
|
c['unread'] = await get_unread_counter(cid, auth.user_id)
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
c["members"] = []
|
c['members'] = []
|
||||||
for uid in c["users"]:
|
for uid in c["users"]:
|
||||||
a = session.query(User).where(User.id == uid).first()
|
a = session.query(User).where(User.id == uid).first()
|
||||||
if a:
|
if a:
|
||||||
c["members"].append(
|
c['members'].append({
|
||||||
{
|
"id": a.id,
|
||||||
"id": a.id,
|
"slug": a.slug,
|
||||||
"slug": a.slug,
|
"userpic": a.userpic,
|
||||||
"userpic": a.userpic,
|
"name": a.name,
|
||||||
"name": a.name,
|
"lastSeen": a.lastSeen,
|
||||||
"lastSeen": a.lastSeen,
|
"online": a.id in onliners
|
||||||
"online": a.id in onliners,
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
chats.append(c)
|
chats.append(c)
|
||||||
return {"chats": chats, "error": None}
|
return {
|
||||||
|
"chats": chats,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@query.field("loadMessagesBy")
|
@query.field("loadMessagesBy")
|
||||||
@login_required
|
@login_required
|
||||||
async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
|
async def load_messages_by(_, info, by, limit: int = 10, offset: int = 0):
|
||||||
"""load :limit messages of :chat_id with :offset"""
|
''' load :limit messages of :chat_id with :offset '''
|
||||||
|
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
userchats = await redis.execute("SMEMBERS", "chats_by_user/" + str(auth.user_id))
|
userchats = await redis.execute("SMEMBERS", "chats_by_user/" + str(auth.user_id))
|
||||||
userchats = [c.decode("utf-8") for c in userchats]
|
userchats = [c.decode('utf-8') for c in userchats]
|
||||||
# print('[inbox] userchats: %r' % userchats)
|
# print('[inbox] userchats: %r' % userchats)
|
||||||
if userchats:
|
if userchats:
|
||||||
# print('[inbox] loading messages by...')
|
# print('[inbox] loading messages by...')
|
||||||
messages = []
|
messages = []
|
||||||
by_chat = by.get("chat")
|
by_chat = by.get('chat')
|
||||||
if by_chat in userchats:
|
if by_chat in userchats:
|
||||||
chat = await redis.execute("GET", f"chats/{by_chat}")
|
chat = await redis.execute("GET", f"chats/{by_chat}")
|
||||||
# print(chat)
|
# print(chat)
|
||||||
if not chat:
|
if not chat:
|
||||||
return {"messages": [], "error": "chat not exist"}
|
return {
|
||||||
|
"messages": [],
|
||||||
|
"error": "chat not exist"
|
||||||
|
}
|
||||||
# everyone's messages in filtered chat
|
# everyone's messages in filtered chat
|
||||||
messages = await load_messages(by_chat, limit, offset)
|
messages = await load_messages(by_chat, limit, offset)
|
||||||
return {
|
return {
|
||||||
"messages": sorted(list(messages), key=lambda m: m["createdAt"]),
|
"messages": sorted(
|
||||||
"error": None,
|
list(messages),
|
||||||
|
key=lambda m: m['createdAt']
|
||||||
|
),
|
||||||
|
"error": None
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
return {"error": "Cannot access messages of this chat"}
|
return {
|
||||||
|
"error": "Cannot access messages of this chat"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@query.field("loadRecipients")
|
@query.field("loadRecipients")
|
||||||
|
@ -127,14 +138,15 @@ async def load_recipients(_, info, limit=50, offset=0):
|
||||||
chat_users += session.query(User).where(User.emailConfirmed).limit(limit).offset(offset)
|
chat_users += session.query(User).where(User.emailConfirmed).limit(limit).offset(offset)
|
||||||
members = []
|
members = []
|
||||||
for a in chat_users:
|
for a in chat_users:
|
||||||
members.append(
|
members.append({
|
||||||
{
|
"id": a.id,
|
||||||
"id": a.id,
|
"slug": a.slug,
|
||||||
"slug": a.slug,
|
"userpic": a.userpic,
|
||||||
"userpic": a.userpic,
|
"name": a.name,
|
||||||
"name": a.name,
|
"lastSeen": a.lastSeen,
|
||||||
"lastSeen": a.lastSeen,
|
"online": a.id in onliners
|
||||||
"online": a.id in onliners,
|
})
|
||||||
}
|
return {
|
||||||
)
|
"members": members,
|
||||||
return {"members": members, "error": None}
|
"error": None
|
||||||
|
}
|
||||||
|
|
|
@ -1,36 +1,41 @@
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
from typing import Any
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from graphql.type import GraphQLResolveInfo
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from base.redis import redis
|
from base.redis import redis
|
||||||
from base.resolvers import mutation
|
from base.resolvers import mutation
|
||||||
from datetime import datetime, timezone
|
from services.following import FollowingManager, FollowingResult, Following
|
||||||
from services.following import FollowingManager, FollowingResult
|
from validations.inbox import Message
|
||||||
|
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("createMessage")
|
@mutation.field("createMessage")
|
||||||
@login_required
|
@login_required
|
||||||
async def create_message(_, info, chat: str, body: str, replyTo=None):
|
async def create_message(_, info, chat: str, body: str, replyTo=None):
|
||||||
"""create message with :body for :chat_id replying to :replyTo optionally"""
|
""" create message with :body for :chat_id replying to :replyTo optionally """
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
|
|
||||||
chat = await redis.execute("GET", f"chats/{chat}")
|
chat = await redis.execute("GET", f"chats/{chat}")
|
||||||
if not chat:
|
if not chat:
|
||||||
return {"error": "chat is not exist"}
|
return {
|
||||||
|
"error": "chat is not exist"
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
chat = dict(json.loads(chat))
|
chat = dict(json.loads(chat))
|
||||||
message_id = await redis.execute("GET", f"chats/{chat['id']}/next_message_id")
|
message_id = await redis.execute("GET", f"chats/{chat['id']}/next_message_id")
|
||||||
message_id = int(message_id)
|
message_id = int(message_id)
|
||||||
new_message = {
|
new_message = {
|
||||||
"chatId": chat["id"],
|
"chatId": chat['id'],
|
||||||
"id": message_id,
|
"id": message_id,
|
||||||
"author": auth.user_id,
|
"author": auth.user_id,
|
||||||
"body": body,
|
"body": body,
|
||||||
"createdAt": int(datetime.now(tz=timezone.utc).timestamp()),
|
"createdAt": int(datetime.now(tz=timezone.utc).timestamp())
|
||||||
}
|
}
|
||||||
if replyTo:
|
if replyTo:
|
||||||
new_message["replyTo"] = replyTo
|
new_message['replyTo'] = replyTo
|
||||||
chat["updatedAt"] = new_message["createdAt"]
|
chat['updatedAt'] = new_message['createdAt']
|
||||||
await redis.execute("SET", f"chats/{chat['id']}", json.dumps(chat))
|
await redis.execute("SET", f"chats/{chat['id']}", json.dumps(chat))
|
||||||
print(f"[inbox] creating message {new_message}")
|
print(f"[inbox] creating message {new_message}")
|
||||||
await redis.execute(
|
await redis.execute(
|
||||||
|
@ -41,12 +46,17 @@ async def create_message(_, info, chat: str, body: str, replyTo=None):
|
||||||
|
|
||||||
users = chat["users"]
|
users = chat["users"]
|
||||||
for user_slug in users:
|
for user_slug in users:
|
||||||
await redis.execute("LPUSH", f"chats/{chat['id']}/unread/{user_slug}", str(message_id))
|
await redis.execute(
|
||||||
|
"LPUSH", f"chats/{chat['id']}/unread/{user_slug}", str(message_id)
|
||||||
|
)
|
||||||
|
|
||||||
result = FollowingResult("NEW", "chat", new_message)
|
result = FollowingResult("NEW", 'chat', new_message)
|
||||||
await FollowingManager.push("chat", result)
|
await FollowingManager.push('chat', result)
|
||||||
|
|
||||||
return {"message": new_message, "error": None}
|
return {
|
||||||
|
"message": new_message,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("updateMessage")
|
@mutation.field("updateMessage")
|
||||||
|
@ -71,10 +81,13 @@ async def update_message(_, info, chat_id: str, message_id: int, body: str):
|
||||||
|
|
||||||
await redis.execute("SET", f"chats/{chat_id}/messages/{message_id}", json.dumps(message))
|
await redis.execute("SET", f"chats/{chat_id}/messages/{message_id}", json.dumps(message))
|
||||||
|
|
||||||
result = FollowingResult("UPDATED", "chat", message)
|
result = FollowingResult("UPDATED", 'chat', message)
|
||||||
await FollowingManager.push("chat", result)
|
await FollowingManager.push('chat', result)
|
||||||
|
|
||||||
return {"message": message, "error": None}
|
return {
|
||||||
|
"message": message,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("deleteMessage")
|
@mutation.field("deleteMessage")
|
||||||
|
@ -101,7 +114,7 @@ async def delete_message(_, info, chat_id: str, message_id: int):
|
||||||
for user_id in users:
|
for user_id in users:
|
||||||
await redis.execute("LREM", f"chats/{chat_id}/unread/{user_id}", 0, str(message_id))
|
await redis.execute("LREM", f"chats/{chat_id}/unread/{user_id}", 0, str(message_id))
|
||||||
|
|
||||||
result = FollowingResult("DELETED", "chat", message)
|
result = FollowingResult("DELETED", 'chat', message)
|
||||||
await FollowingManager.push(result)
|
await FollowingManager.push(result)
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
@ -124,4 +137,6 @@ async def mark_as_read(_, info, chat_id: str, messages: [int]):
|
||||||
for message_id in messages:
|
for message_id in messages:
|
||||||
await redis.execute("LREM", f"chats/{chat_id}/unread/{auth.user_id}", 0, str(message_id))
|
await redis.execute("LREM", f"chats/{chat_id}/unread/{auth.user_id}", 0, str(message_id))
|
||||||
|
|
||||||
return {"error": None}
|
return {
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
|
import json
|
||||||
|
from datetime import datetime, timezone, timedelta
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from base.orm import local_session
|
|
||||||
from base.redis import redis
|
from base.redis import redis
|
||||||
from base.resolvers import query
|
from base.resolvers import query
|
||||||
from datetime import datetime, timedelta, timezone
|
from base.orm import local_session
|
||||||
from orm.user import AuthorFollower, User
|
from orm.user import AuthorFollower, User
|
||||||
from resolvers.inbox.load import load_messages
|
from resolvers.inbox.load import load_messages
|
||||||
|
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
@query.field("searchRecipients")
|
@query.field("searchRecipients")
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -18,7 +17,7 @@ async def search_recipients(_, info, query: str, limit: int = 50, offset: int =
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
talk_before = await redis.execute("GET", f"/chats_by_user/{auth.user_id}")
|
talk_before = await redis.execute("GET", f"/chats_by_user/{auth.user_id}")
|
||||||
if talk_before:
|
if talk_before:
|
||||||
talk_before = list(json.loads(talk_before))[offset : offset + limit]
|
talk_before = list(json.loads(talk_before))[offset:offset + limit]
|
||||||
for chat_id in talk_before:
|
for chat_id in talk_before:
|
||||||
members = await redis.execute("GET", f"/chats/{chat_id}/users")
|
members = await redis.execute("GET", f"/chats/{chat_id}/users")
|
||||||
if members:
|
if members:
|
||||||
|
@ -32,24 +31,23 @@ async def search_recipients(_, info, query: str, limit: int = 50, offset: int =
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
# followings
|
# followings
|
||||||
result += (
|
result += session.query(AuthorFollower.author).join(
|
||||||
session.query(AuthorFollower.author)
|
User, User.id == AuthorFollower.follower
|
||||||
.join(User, User.id == AuthorFollower.follower)
|
).where(
|
||||||
.where(User.slug.startswith(query))
|
User.slug.startswith(query)
|
||||||
.offset(offset + len(result))
|
).offset(offset + len(result)).limit(more_amount)
|
||||||
.limit(more_amount)
|
|
||||||
)
|
|
||||||
|
|
||||||
more_amount = limit
|
more_amount = limit
|
||||||
# followers
|
# followers
|
||||||
result += (
|
result += session.query(AuthorFollower.follower).join(
|
||||||
session.query(AuthorFollower.follower)
|
User, User.id == AuthorFollower.author
|
||||||
.join(User, User.id == AuthorFollower.author)
|
).where(
|
||||||
.where(User.slug.startswith(query))
|
User.slug.startswith(query)
|
||||||
.offset(offset + len(result))
|
).offset(offset + len(result)).limit(offset + len(result) + limit)
|
||||||
.limit(offset + len(result) + limit)
|
return {
|
||||||
)
|
"members": list(result),
|
||||||
return {"members": list(result), "error": None}
|
"error": None
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@query.field("searchMessages")
|
@query.field("searchMessages")
|
||||||
|
@ -59,22 +57,22 @@ async def search_user_chats(by, messages, user_id: int, limit, offset):
|
||||||
cids.union(set(await redis.execute("SMEMBERS", "chats_by_user/" + str(user_id))))
|
cids.union(set(await redis.execute("SMEMBERS", "chats_by_user/" + str(user_id))))
|
||||||
messages = []
|
messages = []
|
||||||
|
|
||||||
by_author = by.get("author")
|
by_author = by.get('author')
|
||||||
if by_author:
|
if by_author:
|
||||||
# all author's messages
|
# all author's messages
|
||||||
cids.union(set(await redis.execute("SMEMBERS", f"chats_by_user/{by_author}")))
|
cids.union(set(await redis.execute("SMEMBERS", f"chats_by_user/{by_author}")))
|
||||||
# author's messages in filtered chat
|
# author's messages in filtered chat
|
||||||
messages.union(set(filter(lambda m: m["author"] == by_author, list(messages))))
|
messages.union(set(filter(lambda m: m["author"] == by_author, list(messages))))
|
||||||
for c in cids:
|
for c in cids:
|
||||||
c = c.decode("utf-8")
|
c = c.decode('utf-8')
|
||||||
messages = await load_messages(c, limit, offset)
|
messages = await load_messages(c, limit, offset)
|
||||||
|
|
||||||
body_like = by.get("body")
|
body_like = by.get('body')
|
||||||
if body_like:
|
if body_like:
|
||||||
# search in all messages in all user's chats
|
# search in all messages in all user's chats
|
||||||
for c in cids:
|
for c in cids:
|
||||||
# FIXME: use redis scan here
|
# FIXME: use redis scan here
|
||||||
c = c.decode("utf-8")
|
c = c.decode('utf-8')
|
||||||
mmm = await load_messages(c, limit, offset)
|
mmm = await load_messages(c, limit, offset)
|
||||||
for m in mmm:
|
for m in mmm:
|
||||||
if body_like in m["body"]:
|
if body_like in m["body"]:
|
||||||
|
@ -85,12 +83,13 @@ async def search_user_chats(by, messages, user_id: int, limit, offset):
|
||||||
|
|
||||||
days = by.get("days")
|
days = by.get("days")
|
||||||
if days:
|
if days:
|
||||||
messages.extend(
|
messages.extend(filter(
|
||||||
filter(
|
list(messages),
|
||||||
list(messages),
|
key=lambda m: (
|
||||||
key=lambda m: (
|
datetime.now(tz=timezone.utc) - int(m["createdAt"]) < timedelta(days=by["days"])
|
||||||
datetime.now(tz=timezone.utc) - int(m["createdAt"]) < timedelta(days=by["days"])
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
)
|
))
|
||||||
return {"messages": messages, "error": None}
|
return {
|
||||||
|
"messages": messages,
|
||||||
|
"error": None
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
from auth.authenticate import login_required
|
from sqlalchemy import select, desc, and_, update
|
||||||
|
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
|
from base.resolvers import query, mutation
|
||||||
|
from auth.authenticate import login_required
|
||||||
from base.orm import local_session
|
from base.orm import local_session
|
||||||
from base.resolvers import mutation, query
|
|
||||||
from orm import Notification
|
from orm import Notification
|
||||||
from sqlalchemy import and_, desc, select, update
|
|
||||||
|
|
||||||
|
|
||||||
@query.field("loadNotifications")
|
@query.field("loadNotifications")
|
||||||
|
@ -15,26 +16,25 @@ async def load_notifications(_, info, params=None):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
user_id = auth.user_id
|
user_id = auth.user_id
|
||||||
|
|
||||||
limit = params.get("limit", 50)
|
limit = params.get('limit', 50)
|
||||||
offset = params.get("offset", 0)
|
offset = params.get('offset', 0)
|
||||||
|
|
||||||
q = (
|
q = select(Notification).where(
|
||||||
select(Notification)
|
Notification.user == user_id
|
||||||
.where(Notification.user == user_id)
|
).order_by(desc(Notification.createdAt)).limit(limit).offset(offset)
|
||||||
.order_by(desc(Notification.createdAt))
|
|
||||||
.limit(limit)
|
|
||||||
.offset(offset)
|
|
||||||
)
|
|
||||||
|
|
||||||
notifications = []
|
notifications = []
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
total_count = session.query(Notification).where(Notification.user == user_id).count()
|
total_count = session.query(Notification).where(
|
||||||
|
Notification.user == user_id
|
||||||
|
).count()
|
||||||
|
|
||||||
total_unread_count = (
|
total_unread_count = session.query(Notification).where(
|
||||||
session.query(Notification)
|
and_(
|
||||||
.where(and_(Notification.user == user_id, Notification.seen == False)) # noqa: E712
|
Notification.user == user_id,
|
||||||
.count()
|
Notification.seen == False
|
||||||
)
|
)
|
||||||
|
).count()
|
||||||
|
|
||||||
for [notification] in session.execute(q):
|
for [notification] in session.execute(q):
|
||||||
notification.type = notification.type.name
|
notification.type = notification.type.name
|
||||||
|
@ -43,7 +43,7 @@ async def load_notifications(_, info, params=None):
|
||||||
return {
|
return {
|
||||||
"notifications": notifications,
|
"notifications": notifications,
|
||||||
"totalCount": total_count,
|
"totalCount": total_count,
|
||||||
"totalUnreadCount": total_unread_count,
|
"totalUnreadCount": total_unread_count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,11 +54,9 @@ async def mark_notification_as_read(_, info, notification_id: int):
|
||||||
user_id = auth.user_id
|
user_id = auth.user_id
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
notification = (
|
notification = session.query(Notification).where(
|
||||||
session.query(Notification)
|
and_(Notification.id == notification_id, Notification.user == user_id)
|
||||||
.where(and_(Notification.id == notification_id, Notification.user == user_id))
|
).one()
|
||||||
.one()
|
|
||||||
)
|
|
||||||
notification.seen = True
|
notification.seen = True
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
@ -71,11 +69,12 @@ async def mark_all_notifications_as_read(_, info):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
user_id = auth.user_id
|
user_id = auth.user_id
|
||||||
|
|
||||||
statement = (
|
statement = update(Notification).where(
|
||||||
update(Notification)
|
and_(
|
||||||
.where(and_(Notification.user == user_id, Notification.seen == False)) # noqa: E712
|
Notification.user == user_id,
|
||||||
.values(seen=True)
|
Notification.seen == False
|
||||||
)
|
)
|
||||||
|
).values(seen=True)
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,37 +1,34 @@
|
||||||
from botocore.exceptions import BotoCoreError, ClientError
|
|
||||||
from starlette.responses import JSONResponse
|
|
||||||
|
|
||||||
import boto3
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
import uuid
|
import uuid
|
||||||
|
import boto3
|
||||||
|
from botocore.exceptions import BotoCoreError, ClientError
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
|
||||||
STORJ_ACCESS_KEY = os.environ.get("STORJ_ACCESS_KEY")
|
STORJ_ACCESS_KEY = os.environ.get('STORJ_ACCESS_KEY')
|
||||||
STORJ_SECRET_KEY = os.environ.get("STORJ_SECRET_KEY")
|
STORJ_SECRET_KEY = os.environ.get('STORJ_SECRET_KEY')
|
||||||
STORJ_END_POINT = os.environ.get("STORJ_END_POINT")
|
STORJ_END_POINT = os.environ.get('STORJ_END_POINT')
|
||||||
STORJ_BUCKET_NAME = os.environ.get("STORJ_BUCKET_NAME")
|
STORJ_BUCKET_NAME = os.environ.get('STORJ_BUCKET_NAME')
|
||||||
CDN_DOMAIN = os.environ.get("CDN_DOMAIN")
|
CDN_DOMAIN = os.environ.get('CDN_DOMAIN')
|
||||||
|
|
||||||
|
|
||||||
async def upload_handler(request):
|
async def upload_handler(request):
|
||||||
form = await request.form()
|
form = await request.form()
|
||||||
file = form.get("file")
|
file = form.get('file')
|
||||||
|
|
||||||
if file is None:
|
if file is None:
|
||||||
return JSONResponse({"error": "No file uploaded"}, status_code=400)
|
return JSONResponse({'error': 'No file uploaded'}, status_code=400)
|
||||||
|
|
||||||
file_name, file_extension = os.path.splitext(file.filename)
|
file_name, file_extension = os.path.splitext(file.filename)
|
||||||
|
|
||||||
key = "files/" + str(uuid.uuid4()) + file_extension
|
key = 'files/' + str(uuid.uuid4()) + file_extension
|
||||||
|
|
||||||
# Create an S3 client with Storj configuration
|
# Create an S3 client with Storj configuration
|
||||||
s3 = boto3.client(
|
s3 = boto3.client('s3',
|
||||||
"s3",
|
aws_access_key_id=STORJ_ACCESS_KEY,
|
||||||
aws_access_key_id=STORJ_ACCESS_KEY,
|
aws_secret_access_key=STORJ_SECRET_KEY,
|
||||||
aws_secret_access_key=STORJ_SECRET_KEY,
|
endpoint_url=STORJ_END_POINT)
|
||||||
endpoint_url=STORJ_END_POINT,
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Save the uploaded file to a temporary file
|
# Save the uploaded file to a temporary file
|
||||||
|
@ -42,13 +39,18 @@ async def upload_handler(request):
|
||||||
Filename=tmp_file.name,
|
Filename=tmp_file.name,
|
||||||
Bucket=STORJ_BUCKET_NAME,
|
Bucket=STORJ_BUCKET_NAME,
|
||||||
Key=key,
|
Key=key,
|
||||||
ExtraArgs={"ContentType": file.content_type},
|
ExtraArgs={
|
||||||
|
"ContentType": file.content_type
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
url = "https://" + CDN_DOMAIN + "/" + key
|
url = 'https://' + CDN_DOMAIN + '/' + key
|
||||||
|
|
||||||
return JSONResponse({"url": url, "originalFilename": file.filename})
|
return JSONResponse({'url': url, 'originalFilename': file.filename})
|
||||||
|
|
||||||
except (BotoCoreError, ClientError) as e:
|
except (BotoCoreError, ClientError) as e:
|
||||||
print(e)
|
print(e)
|
||||||
return JSONResponse({"error": "Failed to upload file"}, status_code=500)
|
return JSONResponse({'error': 'Failed to upload file'}, status_code=500)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
|
import asyncio
|
||||||
|
from base.orm import local_session
|
||||||
|
from base.resolvers import mutation
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from base.resolvers import mutation
|
|
||||||
|
|
||||||
# from resolvers.community import community_follow, community_unfollow
|
# from resolvers.community import community_follow, community_unfollow
|
||||||
|
from orm.user import AuthorFollower
|
||||||
|
from orm.topic import TopicFollower
|
||||||
|
from orm.shout import ShoutReactionsFollower
|
||||||
from resolvers.zine.profile import author_follow, author_unfollow
|
from resolvers.zine.profile import author_follow, author_unfollow
|
||||||
from resolvers.zine.reactions import reactions_follow, reactions_unfollow
|
from resolvers.zine.reactions import reactions_follow, reactions_unfollow
|
||||||
from resolvers.zine.topics import topic_follow, topic_unfollow
|
from resolvers.zine.topics import topic_follow, topic_unfollow
|
||||||
from services.following import FollowingManager, FollowingResult
|
from services.following import Following, FollowingManager, FollowingResult
|
||||||
|
from graphql.type import GraphQLResolveInfo
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("follow")
|
@mutation.field("follow")
|
||||||
|
@ -17,20 +22,20 @@ async def follow(_, info, what, slug):
|
||||||
try:
|
try:
|
||||||
if what == "AUTHOR":
|
if what == "AUTHOR":
|
||||||
if author_follow(auth.user_id, slug):
|
if author_follow(auth.user_id, slug):
|
||||||
result = FollowingResult("NEW", "author", slug)
|
result = FollowingResult("NEW", 'author', slug)
|
||||||
await FollowingManager.push("author", result)
|
await FollowingManager.push('author', result)
|
||||||
elif what == "TOPIC":
|
elif what == "TOPIC":
|
||||||
if topic_follow(auth.user_id, slug):
|
if topic_follow(auth.user_id, slug):
|
||||||
result = FollowingResult("NEW", "topic", slug)
|
result = FollowingResult("NEW", 'topic', slug)
|
||||||
await FollowingManager.push("topic", result)
|
await FollowingManager.push('topic', result)
|
||||||
elif what == "COMMUNITY":
|
elif what == "COMMUNITY":
|
||||||
if False: # TODO: use community_follow(auth.user_id, slug):
|
if False: # TODO: use community_follow(auth.user_id, slug):
|
||||||
result = FollowingResult("NEW", "community", slug)
|
result = FollowingResult("NEW", 'community', slug)
|
||||||
await FollowingManager.push("community", result)
|
await FollowingManager.push('community', result)
|
||||||
elif what == "REACTIONS":
|
elif what == "REACTIONS":
|
||||||
if reactions_follow(auth.user_id, slug):
|
if reactions_follow(auth.user_id, slug):
|
||||||
result = FollowingResult("NEW", "shout", slug)
|
result = FollowingResult("NEW", 'shout', slug)
|
||||||
await FollowingManager.push("shout", result)
|
await FollowingManager.push('shout', result)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(Exception(e))
|
print(Exception(e))
|
||||||
return {"error": str(e)}
|
return {"error": str(e)}
|
||||||
|
@ -46,20 +51,20 @@ async def unfollow(_, info, what, slug):
|
||||||
try:
|
try:
|
||||||
if what == "AUTHOR":
|
if what == "AUTHOR":
|
||||||
if author_unfollow(auth.user_id, slug):
|
if author_unfollow(auth.user_id, slug):
|
||||||
result = FollowingResult("DELETED", "author", slug)
|
result = FollowingResult("DELETED", 'author', slug)
|
||||||
await FollowingManager.push("author", result)
|
await FollowingManager.push('author', result)
|
||||||
elif what == "TOPIC":
|
elif what == "TOPIC":
|
||||||
if topic_unfollow(auth.user_id, slug):
|
if topic_unfollow(auth.user_id, slug):
|
||||||
result = FollowingResult("DELETED", "topic", slug)
|
result = FollowingResult("DELETED", 'topic', slug)
|
||||||
await FollowingManager.push("topic", result)
|
await FollowingManager.push('topic', result)
|
||||||
elif what == "COMMUNITY":
|
elif what == "COMMUNITY":
|
||||||
if False: # TODO: use community_unfollow(auth.user_id, slug):
|
if False: # TODO: use community_unfollow(auth.user_id, slug):
|
||||||
result = FollowingResult("DELETED", "community", slug)
|
result = FollowingResult("DELETED", 'community', slug)
|
||||||
await FollowingManager.push("community", result)
|
await FollowingManager.push('community', result)
|
||||||
elif what == "REACTIONS":
|
elif what == "REACTIONS":
|
||||||
if reactions_unfollow(auth.user_id, slug):
|
if reactions_unfollow(auth.user_id, slug):
|
||||||
result = FollowingResult("DELETED", "shout", slug)
|
result = FollowingResult("DELETED", 'shout', slug)
|
||||||
await FollowingManager.push("shout", result)
|
await FollowingManager.push('shout', result)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"error": str(e)}
|
return {"error": str(e)}
|
||||||
|
|
||||||
|
|
|
@ -1,47 +1,49 @@
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
|
from sqlalchemy.orm import joinedload, aliased
|
||||||
|
from sqlalchemy.sql.expression import desc, asc, select, func, case, and_, text, nulls_last
|
||||||
|
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from base.exceptions import ObjectNotExist
|
from base.exceptions import ObjectNotExist, OperationNotAllowed
|
||||||
from base.orm import local_session
|
from base.orm import local_session
|
||||||
from base.resolvers import query
|
from base.resolvers import query
|
||||||
from datetime import datetime, timedelta, timezone
|
|
||||||
from orm import TopicFollower
|
from orm import TopicFollower
|
||||||
from orm.reaction import Reaction, ReactionKind
|
from orm.reaction import Reaction, ReactionKind
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||||
from orm.user import AuthorFollower
|
from orm.user import AuthorFollower
|
||||||
from sqlalchemy.orm import aliased, joinedload
|
|
||||||
from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select
|
|
||||||
|
|
||||||
|
|
||||||
def add_stat_columns(q):
|
def add_stat_columns(q):
|
||||||
aliased_reaction = aliased(Reaction)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
|
||||||
q = q.outerjoin(aliased_reaction).add_columns(
|
q = q.outerjoin(aliased_reaction).add_columns(
|
||||||
func.sum(aliased_reaction.id).label("reacted_stat"),
|
func.sum(
|
||||||
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT, 1), else_=0)).label(
|
aliased_reaction.id
|
||||||
"commented_stat"
|
).label('reacted_stat'),
|
||||||
),
|
|
||||||
func.sum(
|
func.sum(
|
||||||
case(
|
case(
|
||||||
# do not count comments' reactions
|
(aliased_reaction.kind == ReactionKind.COMMENT, 1),
|
||||||
(aliased_reaction.replyTo.is_not(None), 0),
|
else_=0
|
||||||
(aliased_reaction.kind == ReactionKind.AGREE, 1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.DISAGREE, -1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.PROOF, 1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.DISPROOF, -1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.ACCEPT, 1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.REJECT, -1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.LIKE, 1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.DISLIKE, -1),
|
|
||||||
else_=0,
|
|
||||||
)
|
)
|
||||||
).label("rating_stat"),
|
).label('commented_stat'),
|
||||||
func.max(
|
func.sum(case(
|
||||||
case(
|
# do not count comments' reactions
|
||||||
(aliased_reaction.kind != ReactionKind.COMMENT, None),
|
(aliased_reaction.replyTo.is_not(None), 0),
|
||||||
else_=aliased_reaction.createdAt,
|
(aliased_reaction.kind == ReactionKind.AGREE, 1),
|
||||||
)
|
(aliased_reaction.kind == ReactionKind.DISAGREE, -1),
|
||||||
).label("last_comment"),
|
(aliased_reaction.kind == ReactionKind.PROOF, 1),
|
||||||
)
|
(aliased_reaction.kind == ReactionKind.DISPROOF, -1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.ACCEPT, 1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.REJECT, -1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.LIKE, 1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.DISLIKE, -1),
|
||||||
|
else_=0)
|
||||||
|
).label('rating_stat'),
|
||||||
|
func.max(case(
|
||||||
|
(aliased_reaction.kind != ReactionKind.COMMENT, None),
|
||||||
|
else_=aliased_reaction.createdAt
|
||||||
|
)).label('last_comment'))
|
||||||
|
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
@ -58,7 +60,7 @@ def apply_filters(q, filters, user_id=None):
|
||||||
|
|
||||||
if filters.get("layout"):
|
if filters.get("layout"):
|
||||||
q = q.filter(Shout.layout == filters.get("layout"))
|
q = q.filter(Shout.layout == filters.get("layout"))
|
||||||
if filters.get("excludeLayout"):
|
if filters.get('excludeLayout'):
|
||||||
q = q.filter(Shout.layout != filters.get("excludeLayout"))
|
q = q.filter(Shout.layout != filters.get("excludeLayout"))
|
||||||
if filters.get("author"):
|
if filters.get("author"):
|
||||||
q = q.filter(Shout.authors.any(slug=filters.get("author")))
|
q = q.filter(Shout.authors.any(slug=filters.get("author")))
|
||||||
|
@ -85,27 +87,27 @@ async def load_shout(_, info, slug=None, shout_id=None):
|
||||||
q = add_stat_columns(q)
|
q = add_stat_columns(q)
|
||||||
|
|
||||||
if slug is not None:
|
if slug is not None:
|
||||||
q = q.filter(Shout.slug == slug)
|
q = q.filter(
|
||||||
|
Shout.slug == slug
|
||||||
|
)
|
||||||
|
|
||||||
if shout_id is not None:
|
if shout_id is not None:
|
||||||
q = q.filter(Shout.id == shout_id)
|
q = q.filter(
|
||||||
|
Shout.id == shout_id
|
||||||
|
)
|
||||||
|
|
||||||
q = q.filter(Shout.deletedAt.is_(None)).group_by(Shout.id)
|
q = q.filter(
|
||||||
|
Shout.deletedAt.is_(None)
|
||||||
|
).group_by(Shout.id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
[
|
[shout, reacted_stat, commented_stat, rating_stat, last_comment] = session.execute(q).first()
|
||||||
shout,
|
|
||||||
reacted_stat,
|
|
||||||
commented_stat,
|
|
||||||
rating_stat,
|
|
||||||
last_comment,
|
|
||||||
] = session.execute(q).first()
|
|
||||||
|
|
||||||
shout.stat = {
|
shout.stat = {
|
||||||
"viewed": shout.views,
|
"viewed": shout.views,
|
||||||
"reacted": reacted_stat,
|
"reacted": reacted_stat,
|
||||||
"commented": commented_stat,
|
"commented": commented_stat,
|
||||||
"rating": rating_stat,
|
"rating": rating_stat
|
||||||
}
|
}
|
||||||
|
|
||||||
for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
|
for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug):
|
||||||
|
@ -140,13 +142,14 @@ async def load_shouts_by(_, info, options):
|
||||||
:return: Shout[]
|
:return: Shout[]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
q = (
|
q = select(Shout).options(
|
||||||
select(Shout)
|
joinedload(Shout.authors),
|
||||||
.options(
|
joinedload(Shout.topics),
|
||||||
joinedload(Shout.authors),
|
).where(
|
||||||
joinedload(Shout.topics),
|
and_(
|
||||||
|
Shout.deletedAt.is_(None),
|
||||||
|
Shout.layout.is_not(None)
|
||||||
)
|
)
|
||||||
.where(and_(Shout.deletedAt.is_(None), Shout.layout.is_not(None)))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
q = add_stat_columns(q)
|
q = add_stat_columns(q)
|
||||||
|
@ -156,7 +159,7 @@ async def load_shouts_by(_, info, options):
|
||||||
|
|
||||||
order_by = options.get("order_by", Shout.publishedAt)
|
order_by = options.get("order_by", Shout.publishedAt)
|
||||||
|
|
||||||
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
|
||||||
offset = options.get("offset", 0)
|
offset = options.get("offset", 0)
|
||||||
limit = options.get("limit", 10)
|
limit = options.get("limit", 10)
|
||||||
|
|
||||||
|
@ -166,19 +169,13 @@ async def load_shouts_by(_, info, options):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shouts_map = {}
|
shouts_map = {}
|
||||||
|
|
||||||
for [
|
for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(q).unique():
|
||||||
shout,
|
|
||||||
reacted_stat,
|
|
||||||
commented_stat,
|
|
||||||
rating_stat,
|
|
||||||
last_comment,
|
|
||||||
] in session.execute(q).unique():
|
|
||||||
shouts.append(shout)
|
shouts.append(shout)
|
||||||
shout.stat = {
|
shout.stat = {
|
||||||
"viewed": shout.views,
|
"viewed": shout.views,
|
||||||
"reacted": reacted_stat,
|
"reacted": reacted_stat,
|
||||||
"commented": commented_stat,
|
"commented": commented_stat,
|
||||||
"rating": rating_stat,
|
"rating": rating_stat
|
||||||
}
|
}
|
||||||
shouts_map[shout.id] = shout
|
shouts_map[shout.id] = shout
|
||||||
|
|
||||||
|
@ -191,13 +188,11 @@ async def get_drafts(_, info):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
user_id = auth.user_id
|
user_id = auth.user_id
|
||||||
|
|
||||||
q = (
|
q = select(Shout).options(
|
||||||
select(Shout)
|
joinedload(Shout.authors),
|
||||||
.options(
|
joinedload(Shout.topics),
|
||||||
joinedload(Shout.authors),
|
).where(
|
||||||
joinedload(Shout.topics),
|
and_(Shout.deletedAt.is_(None), Shout.createdBy == user_id)
|
||||||
)
|
|
||||||
.where(and_(Shout.deletedAt.is_(None), Shout.createdBy == user_id))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
q = q.group_by(Shout.id)
|
q = q.group_by(Shout.id)
|
||||||
|
@ -216,26 +211,24 @@ async def get_my_feed(_, info, options):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
user_id = auth.user_id
|
user_id = auth.user_id
|
||||||
|
|
||||||
subquery = (
|
subquery = select(Shout.id).join(
|
||||||
select(Shout.id)
|
ShoutAuthor
|
||||||
.join(ShoutAuthor)
|
).join(
|
||||||
.join(AuthorFollower, AuthorFollower.follower == user_id)
|
AuthorFollower, AuthorFollower.follower == user_id
|
||||||
.join(ShoutTopic)
|
).join(
|
||||||
.join(TopicFollower, TopicFollower.follower == user_id)
|
ShoutTopic
|
||||||
|
).join(
|
||||||
|
TopicFollower, TopicFollower.follower == user_id
|
||||||
)
|
)
|
||||||
|
|
||||||
q = (
|
q = select(Shout).options(
|
||||||
select(Shout)
|
joinedload(Shout.authors),
|
||||||
.options(
|
joinedload(Shout.topics),
|
||||||
joinedload(Shout.authors),
|
).where(
|
||||||
joinedload(Shout.topics),
|
and_(
|
||||||
)
|
Shout.publishedAt.is_not(None),
|
||||||
.where(
|
Shout.deletedAt.is_(None),
|
||||||
and_(
|
Shout.id.in_(subquery)
|
||||||
Shout.publishedAt.is_not(None),
|
|
||||||
Shout.deletedAt.is_(None),
|
|
||||||
Shout.id.in_(subquery),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -244,7 +237,7 @@ async def get_my_feed(_, info, options):
|
||||||
|
|
||||||
order_by = options.get("order_by", Shout.publishedAt)
|
order_by = options.get("order_by", Shout.publishedAt)
|
||||||
|
|
||||||
query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by)
|
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
|
||||||
offset = options.get("offset", 0)
|
offset = options.get("offset", 0)
|
||||||
limit = options.get("limit", 10)
|
limit = options.get("limit", 10)
|
||||||
|
|
||||||
|
@ -253,19 +246,13 @@ async def get_my_feed(_, info, options):
|
||||||
shouts = []
|
shouts = []
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shouts_map = {}
|
shouts_map = {}
|
||||||
for [
|
for [shout, reacted_stat, commented_stat, rating_stat, last_comment] in session.execute(q).unique():
|
||||||
shout,
|
|
||||||
reacted_stat,
|
|
||||||
commented_stat,
|
|
||||||
rating_stat,
|
|
||||||
last_comment,
|
|
||||||
] in session.execute(q).unique():
|
|
||||||
shouts.append(shout)
|
shouts.append(shout)
|
||||||
shout.stat = {
|
shout.stat = {
|
||||||
"viewed": shout.views,
|
"viewed": shout.views,
|
||||||
"reacted": reacted_stat,
|
"reacted": reacted_stat,
|
||||||
"commented": commented_stat,
|
"commented": commented_stat,
|
||||||
"rating": rating_stat,
|
"rating": rating_stat
|
||||||
}
|
}
|
||||||
shouts_map[shout.id] = shout
|
shouts_map[shout.id] = shout
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
|
from typing import List
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from sqlalchemy import and_, func, distinct, select, literal
|
||||||
|
from sqlalchemy.orm import aliased, joinedload
|
||||||
|
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from base.orm import local_session
|
from base.orm import local_session
|
||||||
from base.resolvers import mutation, query
|
from base.resolvers import mutation, query
|
||||||
from datetime import datetime, timedelta, timezone
|
|
||||||
from orm.reaction import Reaction, ReactionKind
|
from orm.reaction import Reaction, ReactionKind
|
||||||
from orm.shout import ShoutAuthor, ShoutTopic
|
from orm.shout import ShoutAuthor, ShoutTopic
|
||||||
from orm.topic import Topic, TopicFollower
|
from orm.topic import Topic, TopicFollower
|
||||||
from orm.user import AuthorFollower, Role, User, UserRating, UserRole
|
from orm.user import AuthorFollower, Role, User, UserRating, UserRole
|
||||||
from resolvers.zine.topics import followed_by_user
|
from resolvers.zine.topics import followed_by_user
|
||||||
from sqlalchemy import and_, distinct, func, literal, select
|
|
||||||
from sqlalchemy.orm import aliased, joinedload
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
|
|
||||||
def add_author_stat_columns(q):
|
def add_author_stat_columns(q):
|
||||||
|
@ -20,24 +21,24 @@ def add_author_stat_columns(q):
|
||||||
# user_rating_aliased = aliased(UserRating)
|
# user_rating_aliased = aliased(UserRating)
|
||||||
|
|
||||||
q = q.outerjoin(shout_author_aliased).add_columns(
|
q = q.outerjoin(shout_author_aliased).add_columns(
|
||||||
func.count(distinct(shout_author_aliased.shout)).label("shouts_stat")
|
func.count(distinct(shout_author_aliased.shout)).label('shouts_stat')
|
||||||
)
|
)
|
||||||
q = q.outerjoin(author_followers, author_followers.author == User.id).add_columns(
|
q = q.outerjoin(author_followers, author_followers.author == User.id).add_columns(
|
||||||
func.count(distinct(author_followers.follower)).label("followers_stat")
|
func.count(distinct(author_followers.follower)).label('followers_stat')
|
||||||
)
|
)
|
||||||
|
|
||||||
q = q.outerjoin(author_following, author_following.follower == User.id).add_columns(
|
q = q.outerjoin(author_following, author_following.follower == User.id).add_columns(
|
||||||
func.count(distinct(author_following.author)).label("followings_stat")
|
func.count(distinct(author_following.author)).label('followings_stat')
|
||||||
)
|
)
|
||||||
|
|
||||||
q = q.add_columns(literal(0).label("rating_stat"))
|
q = q.add_columns(literal(0).label('rating_stat'))
|
||||||
# FIXME
|
# FIXME
|
||||||
# q = q.outerjoin(user_rating_aliased, user_rating_aliased.user == User.id).add_columns(
|
# q = q.outerjoin(user_rating_aliased, user_rating_aliased.user == User.id).add_columns(
|
||||||
# # TODO: check
|
# # TODO: check
|
||||||
# func.sum(user_rating_aliased.value).label('rating_stat')
|
# func.sum(user_rating_aliased.value).label('rating_stat')
|
||||||
# )
|
# )
|
||||||
|
|
||||||
q = q.add_columns(literal(0).label("commented_stat"))
|
q = q.add_columns(literal(0).label('commented_stat'))
|
||||||
# q = q.outerjoin(Reaction, and_(Reaction.createdBy == User.id, Reaction.body.is_not(None))).add_columns(
|
# q = q.outerjoin(Reaction, and_(Reaction.createdBy == User.id, Reaction.body.is_not(None))).add_columns(
|
||||||
# func.count(distinct(Reaction.id)).label('commented_stat')
|
# func.count(distinct(Reaction.id)).label('commented_stat')
|
||||||
# )
|
# )
|
||||||
|
@ -48,19 +49,13 @@ def add_author_stat_columns(q):
|
||||||
|
|
||||||
|
|
||||||
def add_stat(author, stat_columns):
|
def add_stat(author, stat_columns):
|
||||||
[
|
[shouts_stat, followers_stat, followings_stat, rating_stat, commented_stat] = stat_columns
|
||||||
shouts_stat,
|
|
||||||
followers_stat,
|
|
||||||
followings_stat,
|
|
||||||
rating_stat,
|
|
||||||
commented_stat,
|
|
||||||
] = stat_columns
|
|
||||||
author.stat = {
|
author.stat = {
|
||||||
"shouts": shouts_stat,
|
"shouts": shouts_stat,
|
||||||
"followers": followers_stat,
|
"followers": followers_stat,
|
||||||
"followings": followings_stat,
|
"followings": followings_stat,
|
||||||
"rating": rating_stat,
|
"rating": rating_stat,
|
||||||
"commented": commented_stat,
|
"commented": commented_stat
|
||||||
}
|
}
|
||||||
|
|
||||||
return author
|
return author
|
||||||
|
@ -124,10 +119,10 @@ async def user_followers(_, _info, slug) -> List[User]:
|
||||||
q = add_author_stat_columns(q)
|
q = add_author_stat_columns(q)
|
||||||
|
|
||||||
aliased_user = aliased(User)
|
aliased_user = aliased(User)
|
||||||
q = (
|
q = q.join(AuthorFollower, AuthorFollower.follower == User.id).join(
|
||||||
q.join(AuthorFollower, AuthorFollower.follower == User.id)
|
aliased_user, aliased_user.id == AuthorFollower.author
|
||||||
.join(aliased_user, aliased_user.id == AuthorFollower.author)
|
).where(
|
||||||
.where(aliased_user.slug == slug)
|
aliased_user.slug == slug
|
||||||
)
|
)
|
||||||
|
|
||||||
return get_authors_from_query(q)
|
return get_authors_from_query(q)
|
||||||
|
@ -155,10 +150,15 @@ async def update_profile(_, info, profile):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
user = session.query(User).filter(User.id == user_id).one()
|
user = session.query(User).filter(User.id == user_id).one()
|
||||||
if not user:
|
if not user:
|
||||||
return {"error": "canoot find user"}
|
return {
|
||||||
|
"error": "canoot find user"
|
||||||
|
}
|
||||||
user.update(profile)
|
user.update(profile)
|
||||||
session.commit()
|
session.commit()
|
||||||
return {"error": None, "author": user}
|
return {
|
||||||
|
"error": None,
|
||||||
|
"author": user
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("rateUser")
|
@mutation.field("rateUser")
|
||||||
|
@ -200,10 +200,13 @@ def author_follow(user_id, slug):
|
||||||
def author_unfollow(user_id, slug):
|
def author_unfollow(user_id, slug):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
flw = (
|
flw = (
|
||||||
session.query(AuthorFollower)
|
session.query(
|
||||||
.join(User, User.id == AuthorFollower.author)
|
AuthorFollower
|
||||||
.filter(and_(AuthorFollower.follower == user_id, User.slug == slug))
|
).join(User, User.id == AuthorFollower.author).filter(
|
||||||
.first()
|
and_(
|
||||||
|
AuthorFollower.follower == user_id, User.slug == slug
|
||||||
|
)
|
||||||
|
).first()
|
||||||
)
|
)
|
||||||
if flw:
|
if flw:
|
||||||
session.delete(flw)
|
session.delete(flw)
|
||||||
|
@ -229,16 +232,12 @@ async def get_author(_, _info, slug):
|
||||||
[author] = get_authors_from_query(q)
|
[author] = get_authors_from_query(q)
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
comments_count = (
|
comments_count = session.query(Reaction).where(
|
||||||
session.query(Reaction)
|
and_(
|
||||||
.where(
|
Reaction.createdBy == author.id,
|
||||||
and_(
|
Reaction.kind == ReactionKind.COMMENT
|
||||||
Reaction.createdBy == author.id,
|
|
||||||
Reaction.kind == ReactionKind.COMMENT,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.count()
|
).count()
|
||||||
)
|
|
||||||
author.stat["commented"] = comments_count
|
author.stat["commented"] = comments_count
|
||||||
|
|
||||||
return author
|
return author
|
||||||
|
@ -261,7 +260,9 @@ async def load_authors_by(_, info, by, limit, offset):
|
||||||
days_before = datetime.now(tz=timezone.utc) - timedelta(days=by["createdAt"])
|
days_before = datetime.now(tz=timezone.utc) - timedelta(days=by["createdAt"])
|
||||||
q = q.filter(User.createdAt > days_before)
|
q = q.filter(User.createdAt > days_before)
|
||||||
|
|
||||||
q = q.order_by(by.get("order", User.createdAt)).limit(limit).offset(offset)
|
q = q.order_by(
|
||||||
|
by.get("order", User.createdAt)
|
||||||
|
).limit(limit).offset(offset)
|
||||||
|
|
||||||
return get_authors_from_query(q)
|
return get_authors_from_query(q)
|
||||||
|
|
||||||
|
@ -272,13 +273,13 @@ async def load_my_subscriptions(_, info):
|
||||||
auth = info.context["request"].auth
|
auth = info.context["request"].auth
|
||||||
user_id = auth.user_id
|
user_id = auth.user_id
|
||||||
|
|
||||||
authors_query = (
|
authors_query = select(User).join(AuthorFollower, AuthorFollower.author == User.id).where(
|
||||||
select(User)
|
AuthorFollower.follower == user_id
|
||||||
.join(AuthorFollower, AuthorFollower.author == User.id)
|
|
||||||
.where(AuthorFollower.follower == user_id)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
topics_query = select(Topic).join(TopicFollower).where(TopicFollower.follower == user_id)
|
topics_query = select(Topic).join(TopicFollower).where(
|
||||||
|
TopicFollower.follower == user_id
|
||||||
|
)
|
||||||
|
|
||||||
topics = []
|
topics = []
|
||||||
authors = []
|
authors = []
|
||||||
|
@ -290,4 +291,7 @@ async def load_my_subscriptions(_, info):
|
||||||
for [topic] in session.execute(topics_query):
|
for [topic] in session.execute(topics_query):
|
||||||
topics.append(topic)
|
topics.append(topic)
|
||||||
|
|
||||||
return {"topics": topics, "authors": authors}
|
return {
|
||||||
|
"topics": topics,
|
||||||
|
"authors": authors
|
||||||
|
}
|
||||||
|
|
|
@ -1,37 +1,42 @@
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from sqlalchemy import and_, asc, desc, select, text, func, case
|
||||||
|
from sqlalchemy.orm import aliased
|
||||||
|
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from auth.credentials import AuthCredentials
|
from auth.credentials import AuthCredentials
|
||||||
from base.exceptions import OperationNotAllowed
|
from base.exceptions import OperationNotAllowed
|
||||||
from base.orm import local_session
|
from base.orm import local_session
|
||||||
from base.resolvers import mutation, query
|
from base.resolvers import mutation, query
|
||||||
from datetime import datetime, timedelta, timezone
|
|
||||||
from orm.reaction import Reaction, ReactionKind
|
from orm.reaction import Reaction, ReactionKind
|
||||||
from orm.shout import Shout, ShoutReactionsFollower
|
from orm.shout import Shout, ShoutReactionsFollower
|
||||||
from orm.user import User
|
from orm.user import User
|
||||||
from services.notifications.notification_service import notification_service
|
from services.notifications.notification_service import notification_service
|
||||||
from sqlalchemy import and_, asc, case, desc, func, select, text
|
|
||||||
from sqlalchemy.orm import aliased
|
|
||||||
|
|
||||||
|
|
||||||
def add_reaction_stat_columns(q):
|
def add_reaction_stat_columns(q):
|
||||||
aliased_reaction = aliased(Reaction)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
|
||||||
q = q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.replyTo).add_columns(
|
q = q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.replyTo).add_columns(
|
||||||
func.sum(aliased_reaction.id).label("reacted_stat"),
|
func.sum(
|
||||||
func.sum(case((aliased_reaction.body.is_not(None), 1), else_=0)).label("commented_stat"),
|
aliased_reaction.id
|
||||||
|
).label('reacted_stat'),
|
||||||
func.sum(
|
func.sum(
|
||||||
case(
|
case(
|
||||||
(aliased_reaction.kind == ReactionKind.AGREE, 1),
|
(aliased_reaction.body.is_not(None), 1),
|
||||||
(aliased_reaction.kind == ReactionKind.DISAGREE, -1),
|
else_=0
|
||||||
(aliased_reaction.kind == ReactionKind.PROOF, 1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.DISPROOF, -1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.ACCEPT, 1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.REJECT, -1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.LIKE, 1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.DISLIKE, -1),
|
|
||||||
else_=0,
|
|
||||||
)
|
)
|
||||||
).label("rating_stat"),
|
).label('commented_stat'),
|
||||||
)
|
func.sum(case(
|
||||||
|
(aliased_reaction.kind == ReactionKind.AGREE, 1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.DISAGREE, -1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.PROOF, 1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.DISPROOF, -1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.ACCEPT, 1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.REJECT, -1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.LIKE, 1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.DISLIKE, -1),
|
||||||
|
else_=0)
|
||||||
|
).label('rating_stat'))
|
||||||
|
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
@ -42,19 +47,17 @@ def reactions_follow(user_id, shout_id: int, auto=False):
|
||||||
shout = session.query(Shout).where(Shout.id == shout_id).one()
|
shout = session.query(Shout).where(Shout.id == shout_id).one()
|
||||||
|
|
||||||
following = (
|
following = (
|
||||||
session.query(ShoutReactionsFollower)
|
session.query(ShoutReactionsFollower).where(and_(
|
||||||
.where(
|
ShoutReactionsFollower.follower == user_id,
|
||||||
and_(
|
ShoutReactionsFollower.shout == shout.id,
|
||||||
ShoutReactionsFollower.follower == user_id,
|
)).first()
|
||||||
ShoutReactionsFollower.shout == shout.id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.first()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if not following:
|
if not following:
|
||||||
following = ShoutReactionsFollower.create(
|
following = ShoutReactionsFollower.create(
|
||||||
follower=user_id, shout=shout.id, auto=auto
|
follower=user_id,
|
||||||
|
shout=shout.id,
|
||||||
|
auto=auto
|
||||||
)
|
)
|
||||||
session.add(following)
|
session.add(following)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
@ -69,14 +72,10 @@ def reactions_unfollow(user_id: int, shout_id: int):
|
||||||
shout = session.query(Shout).where(Shout.id == shout_id).one()
|
shout = session.query(Shout).where(Shout.id == shout_id).one()
|
||||||
|
|
||||||
following = (
|
following = (
|
||||||
session.query(ShoutReactionsFollower)
|
session.query(ShoutReactionsFollower).where(and_(
|
||||||
.where(
|
ShoutReactionsFollower.follower == user_id,
|
||||||
and_(
|
ShoutReactionsFollower.shout == shout.id
|
||||||
ShoutReactionsFollower.follower == user_id,
|
)).first()
|
||||||
ShoutReactionsFollower.shout == shout.id,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.first()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if following:
|
if following:
|
||||||
|
@ -89,31 +88,30 @@ def reactions_unfollow(user_id: int, shout_id: int):
|
||||||
|
|
||||||
|
|
||||||
def is_published_author(session, user_id):
|
def is_published_author(session, user_id):
|
||||||
"""checks if user has at least one publication"""
|
''' checks if user has at least one publication '''
|
||||||
return (
|
return session.query(
|
||||||
session.query(Shout)
|
Shout
|
||||||
.where(Shout.authors.contains(user_id))
|
).where(
|
||||||
.filter(and_(Shout.publishedAt.is_not(None), Shout.deletedAt.is_(None)))
|
Shout.authors.contains(user_id)
|
||||||
.count()
|
).filter(
|
||||||
> 0
|
and_(
|
||||||
)
|
Shout.publishedAt.is_not(None),
|
||||||
|
Shout.deletedAt.is_(None)
|
||||||
|
)
|
||||||
|
).count() > 0
|
||||||
|
|
||||||
|
|
||||||
def check_to_publish(session, user_id, reaction):
|
def check_to_publish(session, user_id, reaction):
|
||||||
"""set shout to public if publicated approvers amount > 4"""
|
''' set shout to public if publicated approvers amount > 4 '''
|
||||||
if not reaction.replyTo and reaction.kind in [
|
if not reaction.replyTo and reaction.kind in [
|
||||||
ReactionKind.ACCEPT,
|
ReactionKind.ACCEPT,
|
||||||
ReactionKind.LIKE,
|
ReactionKind.LIKE,
|
||||||
ReactionKind.PROOF,
|
ReactionKind.PROOF
|
||||||
]:
|
]:
|
||||||
if is_published_author(user_id):
|
if is_published_author(user_id):
|
||||||
# now count how many approvers are voted already
|
# now count how many approvers are voted already
|
||||||
approvers_reactions = (
|
approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
|
||||||
session.query(Reaction).where(Reaction.shout == reaction.shout).all()
|
approvers = [user_id, ]
|
||||||
)
|
|
||||||
approvers = [
|
|
||||||
user_id,
|
|
||||||
]
|
|
||||||
for ar in approvers_reactions:
|
for ar in approvers_reactions:
|
||||||
a = ar.createdBy
|
a = ar.createdBy
|
||||||
if is_published_author(session, a):
|
if is_published_author(session, a):
|
||||||
|
@ -124,11 +122,11 @@ def check_to_publish(session, user_id, reaction):
|
||||||
|
|
||||||
|
|
||||||
def check_to_hide(session, user_id, reaction):
|
def check_to_hide(session, user_id, reaction):
|
||||||
"""hides any shout if 20% of reactions are negative"""
|
''' hides any shout if 20% of reactions are negative '''
|
||||||
if not reaction.replyTo and reaction.kind in [
|
if not reaction.replyTo and reaction.kind in [
|
||||||
ReactionKind.REJECT,
|
ReactionKind.REJECT,
|
||||||
ReactionKind.DISLIKE,
|
ReactionKind.DISLIKE,
|
||||||
ReactionKind.DISPROOF,
|
ReactionKind.DISPROOF
|
||||||
]:
|
]:
|
||||||
# if is_published_author(user):
|
# if is_published_author(user):
|
||||||
approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
|
approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
|
||||||
|
@ -137,7 +135,7 @@ def check_to_hide(session, user_id, reaction):
|
||||||
if r.kind in [
|
if r.kind in [
|
||||||
ReactionKind.REJECT,
|
ReactionKind.REJECT,
|
||||||
ReactionKind.DISLIKE,
|
ReactionKind.DISLIKE,
|
||||||
ReactionKind.DISPROOF,
|
ReactionKind.DISPROOF
|
||||||
]:
|
]:
|
||||||
rejects += 1
|
rejects += 1
|
||||||
if len(approvers_reactions) / rejects < 5:
|
if len(approvers_reactions) / rejects < 5:
|
||||||
|
@ -148,14 +146,14 @@ def check_to_hide(session, user_id, reaction):
|
||||||
def set_published(session, shout_id):
|
def set_published(session, shout_id):
|
||||||
s = session.query(Shout).where(Shout.id == shout_id).first()
|
s = session.query(Shout).where(Shout.id == shout_id).first()
|
||||||
s.publishedAt = datetime.now(tz=timezone.utc)
|
s.publishedAt = datetime.now(tz=timezone.utc)
|
||||||
s.visibility = text("public")
|
s.visibility = text('public')
|
||||||
session.add(s)
|
session.add(s)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
def set_hidden(session, shout_id):
|
def set_hidden(session, shout_id):
|
||||||
s = session.query(Shout).where(Shout.id == shout_id).first()
|
s = session.query(Shout).where(Shout.id == shout_id).first()
|
||||||
s.visibility = text("community")
|
s.visibility = text('community')
|
||||||
session.add(s)
|
session.add(s)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
@ -164,46 +162,37 @@ def set_hidden(session, shout_id):
|
||||||
@login_required
|
@login_required
|
||||||
async def create_reaction(_, info, reaction):
|
async def create_reaction(_, info, reaction):
|
||||||
auth: AuthCredentials = info.context["request"].auth
|
auth: AuthCredentials = info.context["request"].auth
|
||||||
reaction["createdBy"] = auth.user_id
|
reaction['createdBy'] = auth.user_id
|
||||||
rdict = {}
|
rdict = {}
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shout = session.query(Shout).where(Shout.id == reaction["shout"]).one()
|
shout = session.query(Shout).where(Shout.id == reaction["shout"]).one()
|
||||||
author = session.query(User).where(User.id == auth.user_id).one()
|
author = session.query(User).where(User.id == auth.user_id).one()
|
||||||
|
|
||||||
if reaction["kind"] in [ReactionKind.DISLIKE.name, ReactionKind.LIKE.name]:
|
if reaction["kind"] in [
|
||||||
existing_reaction = (
|
ReactionKind.DISLIKE.name,
|
||||||
session.query(Reaction)
|
ReactionKind.LIKE.name
|
||||||
.where(
|
]:
|
||||||
and_(
|
existing_reaction = session.query(Reaction).where(
|
||||||
Reaction.shout == reaction["shout"],
|
and_(
|
||||||
Reaction.createdBy == auth.user_id,
|
Reaction.shout == reaction["shout"],
|
||||||
Reaction.kind == reaction["kind"],
|
Reaction.createdBy == auth.user_id,
|
||||||
Reaction.replyTo == reaction.get("replyTo"),
|
Reaction.kind == reaction["kind"],
|
||||||
)
|
Reaction.replyTo == reaction.get("replyTo")
|
||||||
)
|
)
|
||||||
.first()
|
).first()
|
||||||
)
|
|
||||||
|
|
||||||
if existing_reaction is not None:
|
if existing_reaction is not None:
|
||||||
raise OperationNotAllowed("You can't vote twice")
|
raise OperationNotAllowed("You can't vote twice")
|
||||||
|
|
||||||
opposite_reaction_kind = (
|
opposite_reaction_kind = ReactionKind.DISLIKE if reaction["kind"] == ReactionKind.LIKE.name else ReactionKind.LIKE
|
||||||
ReactionKind.DISLIKE
|
opposite_reaction = session.query(Reaction).where(
|
||||||
if reaction["kind"] == ReactionKind.LIKE.name
|
|
||||||
else ReactionKind.LIKE
|
|
||||||
)
|
|
||||||
opposite_reaction = (
|
|
||||||
session.query(Reaction)
|
|
||||||
.where(
|
|
||||||
and_(
|
and_(
|
||||||
Reaction.shout == reaction["shout"],
|
Reaction.shout == reaction["shout"],
|
||||||
Reaction.createdBy == auth.user_id,
|
Reaction.createdBy == auth.user_id,
|
||||||
Reaction.kind == opposite_reaction_kind,
|
Reaction.kind == opposite_reaction_kind,
|
||||||
Reaction.replyTo == reaction.get("replyTo"),
|
Reaction.replyTo == reaction.get("replyTo")
|
||||||
)
|
)
|
||||||
)
|
).first()
|
||||||
.first()
|
|
||||||
)
|
|
||||||
|
|
||||||
if opposite_reaction is not None:
|
if opposite_reaction is not None:
|
||||||
session.delete(opposite_reaction)
|
session.delete(opposite_reaction)
|
||||||
|
@ -232,8 +221,8 @@ async def create_reaction(_, info, reaction):
|
||||||
await notification_service.handle_new_reaction(r.id)
|
await notification_service.handle_new_reaction(r.id)
|
||||||
|
|
||||||
rdict = r.dict()
|
rdict = r.dict()
|
||||||
rdict["shout"] = shout.dict()
|
rdict['shout'] = shout.dict()
|
||||||
rdict["createdBy"] = author.dict()
|
rdict['createdBy'] = author.dict()
|
||||||
|
|
||||||
# self-regulation mechanics
|
# self-regulation mechanics
|
||||||
if check_to_hide(session, auth.user_id, r):
|
if check_to_hide(session, auth.user_id, r):
|
||||||
|
@ -246,7 +235,11 @@ async def create_reaction(_, info, reaction):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[resolvers.reactions] error on reactions autofollowing: {e}")
|
print(f"[resolvers.reactions] error on reactions autofollowing: {e}")
|
||||||
|
|
||||||
rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0}
|
rdict['stat'] = {
|
||||||
|
"commented": 0,
|
||||||
|
"reacted": 0,
|
||||||
|
"rating": 0
|
||||||
|
}
|
||||||
return {"reaction": rdict}
|
return {"reaction": rdict}
|
||||||
|
|
||||||
|
|
||||||
|
@ -279,7 +272,7 @@ async def update_reaction(_, info, id, reaction={}):
|
||||||
r.stat = {
|
r.stat = {
|
||||||
"commented": commented_stat,
|
"commented": commented_stat,
|
||||||
"reacted": reacted_stat,
|
"reacted": reacted_stat,
|
||||||
"rating": rating_stat,
|
"rating": rating_stat
|
||||||
}
|
}
|
||||||
|
|
||||||
return {"reaction": r}
|
return {"reaction": r}
|
||||||
|
@ -297,12 +290,17 @@ async def delete_reaction(_, info, id):
|
||||||
if r.createdBy != auth.user_id:
|
if r.createdBy != auth.user_id:
|
||||||
return {"error": "access denied"}
|
return {"error": "access denied"}
|
||||||
|
|
||||||
if r.kind in [ReactionKind.LIKE, ReactionKind.DISLIKE]:
|
if r.kind in [
|
||||||
|
ReactionKind.LIKE,
|
||||||
|
ReactionKind.DISLIKE
|
||||||
|
]:
|
||||||
session.delete(r)
|
session.delete(r)
|
||||||
else:
|
else:
|
||||||
r.deletedAt = datetime.now(tz=timezone.utc)
|
r.deletedAt = datetime.now(tz=timezone.utc)
|
||||||
session.commit()
|
session.commit()
|
||||||
return {"reaction": r}
|
return {
|
||||||
|
"reaction": r
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@query.field("loadReactionsBy")
|
@query.field("loadReactionsBy")
|
||||||
|
@ -323,10 +321,12 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0):
|
||||||
:return: Reaction[]
|
:return: Reaction[]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
q = (
|
q = select(
|
||||||
select(Reaction, User, Shout)
|
Reaction, User, Shout
|
||||||
.join(User, Reaction.createdBy == User.id)
|
).join(
|
||||||
.join(Shout, Reaction.shout == Shout.id)
|
User, Reaction.createdBy == User.id
|
||||||
|
).join(
|
||||||
|
Shout, Reaction.shout == Shout.id
|
||||||
)
|
)
|
||||||
|
|
||||||
if by.get("shout"):
|
if by.get("shout"):
|
||||||
|
@ -344,7 +344,7 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0):
|
||||||
if by.get("comment"):
|
if by.get("comment"):
|
||||||
q = q.filter(func.length(Reaction.body) > 0)
|
q = q.filter(func.length(Reaction.body) > 0)
|
||||||
|
|
||||||
if len(by.get("search", "")) > 2:
|
if len(by.get('search', '')) > 2:
|
||||||
q = q.filter(Reaction.body.ilike(f'%{by["body"]}%'))
|
q = q.filter(Reaction.body.ilike(f'%{by["body"]}%'))
|
||||||
|
|
||||||
if by.get("days"):
|
if by.get("days"):
|
||||||
|
@ -352,9 +352,13 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0):
|
||||||
q = q.filter(Reaction.createdAt > after)
|
q = q.filter(Reaction.createdAt > after)
|
||||||
|
|
||||||
order_way = asc if by.get("sort", "").startswith("-") else desc
|
order_way = asc if by.get("sort", "").startswith("-") else desc
|
||||||
order_field = by.get("sort", "").replace("-", "") or Reaction.createdAt
|
order_field = by.get("sort", "").replace('-', '') or Reaction.createdAt
|
||||||
|
|
||||||
q = q.group_by(Reaction.id, User.id, Shout.id).order_by(order_way(order_field))
|
q = q.group_by(
|
||||||
|
Reaction.id, User.id, Shout.id
|
||||||
|
).order_by(
|
||||||
|
order_way(order_field)
|
||||||
|
)
|
||||||
|
|
||||||
q = add_reaction_stat_columns(q)
|
q = add_reaction_stat_columns(q)
|
||||||
|
|
||||||
|
@ -363,20 +367,13 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0):
|
||||||
reactions = []
|
reactions = []
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
for [
|
for [reaction, user, shout, reacted_stat, commented_stat, rating_stat] in session.execute(q):
|
||||||
reaction,
|
|
||||||
user,
|
|
||||||
shout,
|
|
||||||
reacted_stat,
|
|
||||||
commented_stat,
|
|
||||||
rating_stat,
|
|
||||||
] in session.execute(q):
|
|
||||||
reaction.createdBy = user
|
reaction.createdBy = user
|
||||||
reaction.shout = shout
|
reaction.shout = shout
|
||||||
reaction.stat = {
|
reaction.stat = {
|
||||||
"rating": rating_stat,
|
"rating": rating_stat,
|
||||||
"commented": commented_stat,
|
"commented": commented_stat,
|
||||||
"reacted": reacted_stat,
|
"reacted": reacted_stat
|
||||||
}
|
}
|
||||||
|
|
||||||
reaction.kind = reaction.kind.name
|
reaction.kind = reaction.kind.name
|
||||||
|
|
|
@ -1,24 +1,24 @@
|
||||||
|
from sqlalchemy import and_, select, distinct, func
|
||||||
|
from sqlalchemy.orm import aliased
|
||||||
|
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
from base.orm import local_session
|
from base.orm import local_session
|
||||||
from base.resolvers import mutation, query
|
from base.resolvers import mutation, query
|
||||||
from orm import User
|
from orm.shout import ShoutTopic, ShoutAuthor
|
||||||
from orm.shout import ShoutAuthor, ShoutTopic
|
|
||||||
from orm.topic import Topic, TopicFollower
|
from orm.topic import Topic, TopicFollower
|
||||||
from sqlalchemy import and_, distinct, func, select
|
from orm import User
|
||||||
from sqlalchemy.orm import aliased
|
|
||||||
|
|
||||||
|
|
||||||
def add_topic_stat_columns(q):
|
def add_topic_stat_columns(q):
|
||||||
aliased_shout_author = aliased(ShoutAuthor)
|
aliased_shout_author = aliased(ShoutAuthor)
|
||||||
aliased_topic_follower = aliased(TopicFollower)
|
aliased_topic_follower = aliased(TopicFollower)
|
||||||
|
|
||||||
q = (
|
q = q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic).add_columns(
|
||||||
q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic)
|
func.count(distinct(ShoutTopic.shout)).label('shouts_stat')
|
||||||
.add_columns(func.count(distinct(ShoutTopic.shout)).label("shouts_stat"))
|
).outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout).add_columns(
|
||||||
.outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout)
|
func.count(distinct(aliased_shout_author.user)).label('authors_stat')
|
||||||
.add_columns(func.count(distinct(aliased_shout_author.user)).label("authors_stat"))
|
).outerjoin(aliased_topic_follower).add_columns(
|
||||||
.outerjoin(aliased_topic_follower)
|
func.count(distinct(aliased_topic_follower.follower)).label('followers_stat')
|
||||||
.add_columns(func.count(distinct(aliased_topic_follower.follower)).label("followers_stat"))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
q = q.group_by(Topic.id)
|
q = q.group_by(Topic.id)
|
||||||
|
@ -31,7 +31,7 @@ def add_stat(topic, stat_columns):
|
||||||
topic.stat = {
|
topic.stat = {
|
||||||
"shouts": shouts_stat,
|
"shouts": shouts_stat,
|
||||||
"authors": authors_stat,
|
"authors": authors_stat,
|
||||||
"followers": followers_stat,
|
"followers": followers_stat
|
||||||
}
|
}
|
||||||
|
|
||||||
return topic
|
return topic
|
||||||
|
@ -133,10 +133,12 @@ def topic_unfollow(user_id, slug):
|
||||||
try:
|
try:
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
sub = (
|
sub = (
|
||||||
session.query(TopicFollower)
|
session.query(TopicFollower).join(Topic).filter(
|
||||||
.join(Topic)
|
and_(
|
||||||
.filter(and_(TopicFollower.follower == user_id, Topic.slug == slug))
|
TopicFollower.follower == user_id,
|
||||||
.first()
|
Topic.slug == slug
|
||||||
|
)
|
||||||
|
).first()
|
||||||
)
|
)
|
||||||
if sub:
|
if sub:
|
||||||
session.delete(sub)
|
session.delete(sub)
|
||||||
|
|
78
server.py
78
server.py
|
@ -1,45 +1,56 @@
|
||||||
from settings import DEV_SERVER_PID_FILE_NAME, PORT
|
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
|
from settings import PORT, DEV_SERVER_PID_FILE_NAME
|
||||||
|
|
||||||
|
|
||||||
def exception_handler(exception_type, exception, traceback, debug_hook=sys.excepthook):
|
def exception_handler(exception_type, exception, traceback, debug_hook=sys.excepthook):
|
||||||
print("%s: %s" % (exception_type.__name__, exception))
|
print("%s: %s" % (exception_type.__name__, exception))
|
||||||
|
|
||||||
|
|
||||||
log_settings = {
|
log_settings = {
|
||||||
"version": 1,
|
'version': 1,
|
||||||
"disable_existing_loggers": True,
|
'disable_existing_loggers': True,
|
||||||
"formatters": {
|
'formatters': {
|
||||||
"default": {
|
'default': {
|
||||||
"()": "uvicorn.logging.DefaultFormatter",
|
'()': 'uvicorn.logging.DefaultFormatter',
|
||||||
"fmt": "%(levelprefix)s %(message)s",
|
'fmt': '%(levelprefix)s %(message)s',
|
||||||
"use_colors": None,
|
'use_colors': None
|
||||||
},
|
|
||||||
"access": {
|
|
||||||
"()": "uvicorn.logging.AccessFormatter",
|
|
||||||
"fmt": '%(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s',
|
|
||||||
},
|
},
|
||||||
|
'access': {
|
||||||
|
'()': 'uvicorn.logging.AccessFormatter',
|
||||||
|
'fmt': '%(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s'
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"handlers": {
|
'handlers': {
|
||||||
"default": {
|
'default': {
|
||||||
"formatter": "default",
|
'formatter': 'default',
|
||||||
"class": "logging.StreamHandler",
|
'class': 'logging.StreamHandler',
|
||||||
"stream": "ext://sys.stderr",
|
'stream': 'ext://sys.stderr'
|
||||||
},
|
},
|
||||||
"access": {
|
'access': {
|
||||||
"formatter": "access",
|
'formatter': 'access',
|
||||||
"class": "logging.StreamHandler",
|
'class': 'logging.StreamHandler',
|
||||||
"stream": "ext://sys.stdout",
|
'stream': 'ext://sys.stdout'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'loggers': {
|
||||||
|
'uvicorn': {
|
||||||
|
'handlers': ['default'],
|
||||||
|
'level': 'INFO'
|
||||||
},
|
},
|
||||||
},
|
'uvicorn.error': {
|
||||||
"loggers": {
|
'level': 'INFO',
|
||||||
"uvicorn": {"handlers": ["default"], "level": "INFO"},
|
'handlers': ['default'],
|
||||||
"uvicorn.error": {"level": "INFO", "handlers": ["default"], "propagate": True},
|
'propagate': True
|
||||||
"uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False},
|
},
|
||||||
},
|
'uvicorn.access': {
|
||||||
|
'handlers': ['access'],
|
||||||
|
'level': 'INFO',
|
||||||
|
'propagate': False
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
local_headers = [
|
local_headers = [
|
||||||
|
@ -47,8 +58,7 @@ local_headers = [
|
||||||
("Access-Control-Allow-Origin", "https://localhost:3000"),
|
("Access-Control-Allow-Origin", "https://localhost:3000"),
|
||||||
(
|
(
|
||||||
"Access-Control-Allow-Headers",
|
"Access-Control-Allow-Headers",
|
||||||
"DNT,User-Agent,X-Requested-With,If-Modified-Since,"
|
"DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization",
|
||||||
+ "Cache-Control,Content-Type,Range,Authorization",
|
|
||||||
),
|
),
|
||||||
("Access-Control-Expose-Headers", "Content-Length,Content-Range"),
|
("Access-Control-Expose-Headers", "Content-Length,Content-Range"),
|
||||||
("Access-Control-Allow-Credentials", "true"),
|
("Access-Control-Allow-Credentials", "true"),
|
||||||
|
@ -76,17 +86,15 @@ if __name__ == "__main__":
|
||||||
# log_config=log_settings,
|
# log_config=log_settings,
|
||||||
log_level=None,
|
log_level=None,
|
||||||
access_log=True,
|
access_log=True,
|
||||||
reload=want_reload,
|
reload=want_reload
|
||||||
) # , ssl_keyfile="discours.key", ssl_certfile="discours.crt")
|
) # , ssl_keyfile="discours.key", ssl_certfile="discours.crt")
|
||||||
elif x == "migrate":
|
elif x == "migrate":
|
||||||
from migration import process
|
from migration import process
|
||||||
|
|
||||||
print("MODE: MIGRATE")
|
print("MODE: MIGRATE")
|
||||||
|
|
||||||
process()
|
process()
|
||||||
elif x == "bson":
|
elif x == "bson":
|
||||||
from migration.bson2json import json_tables
|
from migration.bson2json import json_tables
|
||||||
|
|
||||||
print("MODE: BSON")
|
print("MODE: BSON")
|
||||||
|
|
||||||
json_tables()
|
json_tables()
|
||||||
|
@ -97,5 +105,5 @@ if __name__ == "__main__":
|
||||||
host="0.0.0.0",
|
host="0.0.0.0",
|
||||||
port=PORT,
|
port=PORT,
|
||||||
proxy_headers=True,
|
proxy_headers=True,
|
||||||
server_header=True,
|
server_header=True
|
||||||
)
|
)
|
||||||
|
|
|
@ -18,7 +18,12 @@ class Following:
|
||||||
|
|
||||||
class FollowingManager:
|
class FollowingManager:
|
||||||
lock = asyncio.Lock()
|
lock = asyncio.Lock()
|
||||||
data = {"author": [], "topic": [], "shout": [], "chat": []}
|
data = {
|
||||||
|
'author': [],
|
||||||
|
'topic': [],
|
||||||
|
'shout': [],
|
||||||
|
'chat': []
|
||||||
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def register(kind, uid):
|
async def register(kind, uid):
|
||||||
|
@ -34,13 +39,13 @@ class FollowingManager:
|
||||||
async def push(kind, payload):
|
async def push(kind, payload):
|
||||||
try:
|
try:
|
||||||
async with FollowingManager.lock:
|
async with FollowingManager.lock:
|
||||||
if kind == "chat":
|
if kind == 'chat':
|
||||||
for chat in FollowingManager["chat"]:
|
for chat in FollowingManager['chat']:
|
||||||
if payload.message["chatId"] == chat.uid:
|
if payload.message["chatId"] == chat.uid:
|
||||||
chat.queue.put_nowait(payload)
|
chat.queue.put_nowait(payload)
|
||||||
else:
|
else:
|
||||||
for entity in FollowingManager[kind]:
|
for entity in FollowingManager[kind]:
|
||||||
if payload.shout["createdBy"] == entity.uid:
|
if payload.shout['createdBy'] == entity.uid:
|
||||||
entity.queue.put_nowait(payload)
|
entity.queue.put_nowait(payload)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(Exception(e))
|
print(Exception(e))
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
from base.orm import local_session
|
|
||||||
from services.search import SearchService
|
from services.search import SearchService
|
||||||
from services.stat.viewed import ViewedStorage
|
from services.stat.viewed import ViewedStorage
|
||||||
|
from base.orm import local_session
|
||||||
|
|
||||||
|
|
||||||
async def storages_init():
|
async def storages_init():
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
print("[main] initialize SearchService")
|
print('[main] initialize SearchService')
|
||||||
await SearchService.init(session)
|
await SearchService.init(session)
|
||||||
print("[main] SearchService initialized")
|
print('[main] SearchService initialized')
|
||||||
print("[main] initialize storages")
|
print('[main] initialize storages')
|
||||||
await ViewedStorage.init()
|
await ViewedStorage.init()
|
||||||
print("[main] storages initialized")
|
print('[main] storages initialized')
|
||||||
|
|
|
@ -1,17 +1,21 @@
|
||||||
from base.orm import local_session
|
import asyncio
|
||||||
|
import json
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from orm import Notification, Reaction, Shout, User
|
|
||||||
|
from sqlalchemy import and_
|
||||||
|
|
||||||
|
from base.orm import local_session
|
||||||
|
from orm import Reaction, Shout, Notification, User
|
||||||
from orm.notification import NotificationType
|
from orm.notification import NotificationType
|
||||||
from orm.reaction import ReactionKind
|
from orm.reaction import ReactionKind
|
||||||
from services.notifications.sse import connection_manager
|
from services.notifications.sse import connection_manager
|
||||||
from sqlalchemy import and_
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
def shout_to_shout_data(shout):
|
def shout_to_shout_data(shout):
|
||||||
return {"title": shout.title, "slug": shout.slug}
|
return {
|
||||||
|
"title": shout.title,
|
||||||
|
"slug": shout.slug
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def user_to_user_data(user):
|
def user_to_user_data(user):
|
||||||
|
@ -19,14 +23,14 @@ def user_to_user_data(user):
|
||||||
"id": user.id,
|
"id": user.id,
|
||||||
"name": user.name,
|
"name": user.name,
|
||||||
"slug": user.slug,
|
"slug": user.slug,
|
||||||
"userpic": user.userpic,
|
"userpic": user.userpic
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def update_prev_notification(notification, user, reaction):
|
def update_prev_notification(notification, user, reaction):
|
||||||
notification_data = json.loads(notification.data)
|
notification_data = json.loads(notification.data)
|
||||||
|
|
||||||
notification_data["users"] = [u for u in notification_data["users"] if u["id"] != user.id]
|
notification_data["users"] = [u for u in notification_data["users"] if u['id'] != user.id]
|
||||||
notification_data["users"].append(user_to_user_data(user))
|
notification_data["users"].append(user_to_user_data(user))
|
||||||
|
|
||||||
if notification_data["reactionIds"] is None:
|
if notification_data["reactionIds"] is None:
|
||||||
|
@ -53,45 +57,34 @@ class NewReactionNotificator:
|
||||||
if reaction.kind == ReactionKind.COMMENT:
|
if reaction.kind == ReactionKind.COMMENT:
|
||||||
parent_reaction = None
|
parent_reaction = None
|
||||||
if reaction.replyTo:
|
if reaction.replyTo:
|
||||||
parent_reaction = (
|
parent_reaction = session.query(Reaction).where(Reaction.id == reaction.replyTo).one()
|
||||||
session.query(Reaction).where(Reaction.id == reaction.replyTo).one()
|
|
||||||
)
|
|
||||||
if parent_reaction.createdBy != reaction.createdBy:
|
if parent_reaction.createdBy != reaction.createdBy:
|
||||||
prev_new_reply_notification = (
|
prev_new_reply_notification = session.query(Notification).where(
|
||||||
session.query(Notification)
|
and_(
|
||||||
.where(
|
Notification.user == shout.createdBy,
|
||||||
and_(
|
Notification.type == NotificationType.NEW_REPLY,
|
||||||
Notification.user == shout.createdBy,
|
Notification.shout == shout.id,
|
||||||
Notification.type == NotificationType.NEW_REPLY,
|
Notification.reaction == parent_reaction.id,
|
||||||
Notification.shout == shout.id,
|
Notification.seen == False
|
||||||
Notification.reaction == parent_reaction.id,
|
|
||||||
Notification.seen == False, # noqa: E712
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.first()
|
).first()
|
||||||
)
|
|
||||||
|
|
||||||
if prev_new_reply_notification:
|
if prev_new_reply_notification:
|
||||||
update_prev_notification(prev_new_reply_notification, user, reaction)
|
update_prev_notification(prev_new_reply_notification, user, reaction)
|
||||||
else:
|
else:
|
||||||
reply_notification_data = json.dumps(
|
reply_notification_data = json.dumps({
|
||||||
{
|
"shout": shout_to_shout_data(shout),
|
||||||
"shout": shout_to_shout_data(shout),
|
"users": [user_to_user_data(user)],
|
||||||
"users": [user_to_user_data(user)],
|
"reactionIds": [reaction.id]
|
||||||
"reactionIds": [reaction.id],
|
}, ensure_ascii=False)
|
||||||
},
|
|
||||||
ensure_ascii=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
reply_notification = Notification.create(
|
reply_notification = Notification.create(**{
|
||||||
**{
|
"user": parent_reaction.createdBy,
|
||||||
"user": parent_reaction.createdBy,
|
"type": NotificationType.NEW_REPLY,
|
||||||
"type": NotificationType.NEW_REPLY,
|
"shout": shout.id,
|
||||||
"shout": shout.id,
|
"reaction": parent_reaction.id,
|
||||||
"reaction": parent_reaction.id,
|
"data": reply_notification_data
|
||||||
"data": reply_notification_data,
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
session.add(reply_notification)
|
session.add(reply_notification)
|
||||||
|
|
||||||
|
@ -100,39 +93,30 @@ class NewReactionNotificator:
|
||||||
if reaction.createdBy != shout.createdBy and (
|
if reaction.createdBy != shout.createdBy and (
|
||||||
parent_reaction is None or parent_reaction.createdBy != shout.createdBy
|
parent_reaction is None or parent_reaction.createdBy != shout.createdBy
|
||||||
):
|
):
|
||||||
prev_new_comment_notification = (
|
prev_new_comment_notification = session.query(Notification).where(
|
||||||
session.query(Notification)
|
and_(
|
||||||
.where(
|
Notification.user == shout.createdBy,
|
||||||
and_(
|
Notification.type == NotificationType.NEW_COMMENT,
|
||||||
Notification.user == shout.createdBy,
|
Notification.shout == shout.id,
|
||||||
Notification.type == NotificationType.NEW_COMMENT,
|
Notification.seen == False
|
||||||
Notification.shout == shout.id,
|
|
||||||
Notification.seen == False, # noqa: E712
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.first()
|
).first()
|
||||||
)
|
|
||||||
|
|
||||||
if prev_new_comment_notification:
|
if prev_new_comment_notification:
|
||||||
update_prev_notification(prev_new_comment_notification, user, reaction)
|
update_prev_notification(prev_new_comment_notification, user, reaction)
|
||||||
else:
|
else:
|
||||||
notification_data_string = json.dumps(
|
notification_data_string = json.dumps({
|
||||||
{
|
"shout": shout_to_shout_data(shout),
|
||||||
"shout": shout_to_shout_data(shout),
|
"users": [user_to_user_data(user)],
|
||||||
"users": [user_to_user_data(user)],
|
"reactionIds": [reaction.id]
|
||||||
"reactionIds": [reaction.id],
|
}, ensure_ascii=False)
|
||||||
},
|
|
||||||
ensure_ascii=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
author_notification = Notification.create(
|
author_notification = Notification.create(**{
|
||||||
**{
|
"user": shout.createdBy,
|
||||||
"user": shout.createdBy,
|
"type": NotificationType.NEW_COMMENT,
|
||||||
"type": NotificationType.NEW_COMMENT,
|
"shout": shout.id,
|
||||||
"shout": shout.id,
|
"data": notification_data_string
|
||||||
"data": notification_data_string,
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
session.add(author_notification)
|
session.add(author_notification)
|
||||||
|
|
||||||
|
@ -158,7 +142,7 @@ class NotificationService:
|
||||||
try:
|
try:
|
||||||
await notificator.run()
|
await notificator.run()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[NotificationService.worker] error: {str(e)}")
|
print(f'[NotificationService.worker] error: {str(e)}')
|
||||||
|
|
||||||
|
|
||||||
notification_service = NotificationService()
|
notification_service = NotificationService()
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
|
import json
|
||||||
|
|
||||||
from sse_starlette.sse import EventSourceResponse
|
from sse_starlette.sse import EventSourceResponse
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
class ConnectionManager:
|
class ConnectionManager:
|
||||||
|
@ -28,7 +28,9 @@ class ConnectionManager:
|
||||||
return
|
return
|
||||||
|
|
||||||
for connection in self.connections_by_user_id[user_id]:
|
for connection in self.connections_by_user_id[user_id]:
|
||||||
data = {"type": "newNotifications"}
|
data = {
|
||||||
|
"type": "newNotifications"
|
||||||
|
}
|
||||||
data_string = json.dumps(data, ensure_ascii=False)
|
data_string = json.dumps(data, ensure_ascii=False)
|
||||||
await connection.put(data_string)
|
await connection.put(data_string)
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
from base.redis import redis
|
from base.redis import redis
|
||||||
from orm.shout import Shout
|
from orm.shout import Shout
|
||||||
from resolvers.zine.load import load_shouts_by
|
from resolvers.zine.load import load_shouts_by
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
class SearchService:
|
class SearchService:
|
||||||
lock = asyncio.Lock()
|
lock = asyncio.Lock()
|
||||||
|
@ -13,7 +12,7 @@ class SearchService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def init(session):
|
async def init(session):
|
||||||
async with SearchService.lock:
|
async with SearchService.lock:
|
||||||
print("[search.service] did nothing")
|
print('[search.service] did nothing')
|
||||||
SearchService.cache = {}
|
SearchService.cache = {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -25,7 +24,7 @@ class SearchService:
|
||||||
"title": text,
|
"title": text,
|
||||||
"body": text,
|
"body": text,
|
||||||
"limit": limit,
|
"limit": limit,
|
||||||
"offset": offset,
|
"offset": offset
|
||||||
}
|
}
|
||||||
payload = await load_shouts_by(None, None, options)
|
payload = await load_shouts_by(None, None, options)
|
||||||
await redis.execute("SET", text, json.dumps(payload))
|
await redis.execute("SET", text, json.dumps(payload))
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
from base.orm import local_session
|
import asyncio
|
||||||
from datetime import datetime, timedelta, timezone
|
import time
|
||||||
from gql import Client, gql
|
from datetime import timedelta, timezone, datetime
|
||||||
from gql.transport.aiohttp import AIOHTTPTransport
|
|
||||||
from orm import Topic
|
|
||||||
from orm.shout import Shout, ShoutTopic
|
|
||||||
from os import environ, path
|
from os import environ, path
|
||||||
from ssl import create_default_context
|
from ssl import create_default_context
|
||||||
|
|
||||||
import asyncio
|
from gql import Client, gql
|
||||||
import time
|
from gql.transport.aiohttp import AIOHTTPTransport
|
||||||
|
from sqlalchemy import func
|
||||||
|
|
||||||
load_facts = gql(
|
from base.orm import local_session
|
||||||
"""
|
from orm import User, Topic
|
||||||
|
from orm.shout import ShoutTopic, Shout
|
||||||
|
|
||||||
|
load_facts = gql("""
|
||||||
query getDomains {
|
query getDomains {
|
||||||
domains {
|
domains {
|
||||||
id
|
id
|
||||||
|
@ -24,11 +25,9 @@ query getDomains {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"""
|
""")
|
||||||
)
|
|
||||||
|
|
||||||
load_pages = gql(
|
load_pages = gql("""
|
||||||
"""
|
|
||||||
query getDomains {
|
query getDomains {
|
||||||
domains {
|
domains {
|
||||||
title
|
title
|
||||||
|
@ -42,9 +41,8 @@ query getDomains {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"""
|
""")
|
||||||
)
|
schema_str = open(path.dirname(__file__) + '/ackee.graphql').read()
|
||||||
schema_str = open(path.dirname(__file__) + "/ackee.graphql").read()
|
|
||||||
token = environ.get("ACKEE_TOKEN", "")
|
token = environ.get("ACKEE_TOKEN", "")
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,8 +52,8 @@ def create_client(headers=None, schema=None):
|
||||||
transport=AIOHTTPTransport(
|
transport=AIOHTTPTransport(
|
||||||
url="https://ackee.discours.io/api",
|
url="https://ackee.discours.io/api",
|
||||||
ssl=create_default_context(),
|
ssl=create_default_context(),
|
||||||
headers=headers,
|
headers=headers
|
||||||
),
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,13 +71,13 @@ class ViewedStorage:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def init():
|
async def init():
|
||||||
"""graphql client connection using permanent token"""
|
""" graphql client connection using permanent token """
|
||||||
self = ViewedStorage
|
self = ViewedStorage
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
if token:
|
if token:
|
||||||
self.client = create_client(
|
self.client = create_client({
|
||||||
{"Authorization": "Bearer %s" % str(token)}, schema=schema_str
|
"Authorization": "Bearer %s" % str(token)
|
||||||
)
|
}, schema=schema_str)
|
||||||
print("[stat.viewed] * authorized permanentely by ackee.discours.io: %s" % token)
|
print("[stat.viewed] * authorized permanentely by ackee.discours.io: %s" % token)
|
||||||
else:
|
else:
|
||||||
print("[stat.viewed] * please set ACKEE_TOKEN")
|
print("[stat.viewed] * please set ACKEE_TOKEN")
|
||||||
|
@ -87,7 +85,7 @@ class ViewedStorage:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def update_pages():
|
async def update_pages():
|
||||||
"""query all the pages from ackee sorted by views count"""
|
""" query all the pages from ackee sorted by views count """
|
||||||
print("[stat.viewed] ⎧ updating ackee pages data ---")
|
print("[stat.viewed] ⎧ updating ackee pages data ---")
|
||||||
start = time.time()
|
start = time.time()
|
||||||
self = ViewedStorage
|
self = ViewedStorage
|
||||||
|
@ -98,7 +96,7 @@ class ViewedStorage:
|
||||||
try:
|
try:
|
||||||
for page in self.pages:
|
for page in self.pages:
|
||||||
p = page["value"].split("?")[0]
|
p = page["value"].split("?")[0]
|
||||||
slug = p.split("discours.io/")[-1]
|
slug = p.split('discours.io/')[-1]
|
||||||
shouts[slug] = page["count"]
|
shouts[slug] = page["count"]
|
||||||
for slug in shouts.keys():
|
for slug in shouts.keys():
|
||||||
await ViewedStorage.increment(slug, shouts[slug])
|
await ViewedStorage.increment(slug, shouts[slug])
|
||||||
|
@ -120,7 +118,7 @@ class ViewedStorage:
|
||||||
# unused yet
|
# unused yet
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get_shout(shout_slug):
|
async def get_shout(shout_slug):
|
||||||
"""getting shout views metric by slug"""
|
""" getting shout views metric by slug """
|
||||||
self = ViewedStorage
|
self = ViewedStorage
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
shout_views = self.by_shouts.get(shout_slug)
|
shout_views = self.by_shouts.get(shout_slug)
|
||||||
|
@ -138,7 +136,7 @@ class ViewedStorage:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get_topic(topic_slug):
|
async def get_topic(topic_slug):
|
||||||
"""getting topic views value summed"""
|
""" getting topic views value summed """
|
||||||
self = ViewedStorage
|
self = ViewedStorage
|
||||||
topic_views = 0
|
topic_views = 0
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
|
@ -148,28 +146,24 @@ class ViewedStorage:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_topics(session, shout_slug):
|
def update_topics(session, shout_slug):
|
||||||
"""updates topics counters by shout slug"""
|
""" updates topics counters by shout slug """
|
||||||
self = ViewedStorage
|
self = ViewedStorage
|
||||||
for [shout_topic, topic] in (
|
for [shout_topic, topic] in session.query(ShoutTopic, Topic).join(Topic).join(Shout).where(
|
||||||
session.query(ShoutTopic, Topic)
|
Shout.slug == shout_slug
|
||||||
.join(Topic)
|
).all():
|
||||||
.join(Shout)
|
|
||||||
.where(Shout.slug == shout_slug)
|
|
||||||
.all()
|
|
||||||
):
|
|
||||||
if not self.by_topics.get(topic.slug):
|
if not self.by_topics.get(topic.slug):
|
||||||
self.by_topics[topic.slug] = {}
|
self.by_topics[topic.slug] = {}
|
||||||
self.by_topics[topic.slug][shout_slug] = self.by_shouts[shout_slug]
|
self.by_topics[topic.slug][shout_slug] = self.by_shouts[shout_slug]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def increment(shout_slug, amount=1, viewer="ackee"):
|
async def increment(shout_slug, amount=1, viewer='ackee'):
|
||||||
"""the only way to change views counter"""
|
""" the only way to change views counter """
|
||||||
self = ViewedStorage
|
self = ViewedStorage
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
# TODO optimize, currenty we execute 1 DB transaction per shout
|
# TODO optimize, currenty we execute 1 DB transaction per shout
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shout = session.query(Shout).where(Shout.slug == shout_slug).one()
|
shout = session.query(Shout).where(Shout.slug == shout_slug).one()
|
||||||
if viewer == "old-discours":
|
if viewer == 'old-discours':
|
||||||
# this is needed for old db migration
|
# this is needed for old db migration
|
||||||
if shout.viewsOld == amount:
|
if shout.viewsOld == amount:
|
||||||
print(f"viewsOld amount: {amount}")
|
print(f"viewsOld amount: {amount}")
|
||||||
|
@ -191,7 +185,7 @@ class ViewedStorage:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def worker():
|
async def worker():
|
||||||
"""async task worker"""
|
""" async task worker """
|
||||||
failed = 0
|
failed = 0
|
||||||
self = ViewedStorage
|
self = ViewedStorage
|
||||||
if self.disabled:
|
if self.disabled:
|
||||||
|
@ -211,10 +205,9 @@ class ViewedStorage:
|
||||||
if failed == 0:
|
if failed == 0:
|
||||||
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
|
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
|
||||||
t = format(when.astimezone().isoformat())
|
t = format(when.astimezone().isoformat())
|
||||||
print(
|
print("[stat.viewed] ⎩ next update: %s" % (
|
||||||
"[stat.viewed] ⎩ next update: %s"
|
t.split("T")[0] + " " + t.split("T")[1].split(".")[0]
|
||||||
% (t.split("T")[0] + " " + t.split("T")[1].split(".")[0])
|
))
|
||||||
)
|
|
||||||
await asyncio.sleep(self.period)
|
await asyncio.sleep(self.period)
|
||||||
else:
|
else:
|
||||||
await asyncio.sleep(10)
|
await asyncio.sleep(10)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
from pathlib import Path
|
|
||||||
from settings import SHOUTS_REPO
|
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from settings import SHOUTS_REPO
|
||||||
|
|
||||||
|
|
||||||
class GitTask:
|
class GitTask:
|
||||||
|
|
|
@ -3,9 +3,8 @@ from os import environ
|
||||||
PORT = 8080
|
PORT = 8080
|
||||||
|
|
||||||
DB_URL = (
|
DB_URL = (
|
||||||
environ.get("DATABASE_URL")
|
environ.get("DATABASE_URL") or environ.get("DB_URL") or
|
||||||
or environ.get("DB_URL")
|
"postgresql://postgres@localhost:5432/discoursio"
|
||||||
or "postgresql://postgres@localhost:5432/discoursio"
|
|
||||||
)
|
)
|
||||||
JWT_ALGORITHM = "HS256"
|
JWT_ALGORITHM = "HS256"
|
||||||
JWT_SECRET_KEY = environ.get("JWT_SECRET_KEY") or "8f1bd7696ffb482d8486dfbc6e7d16dd-secret-key"
|
JWT_SECRET_KEY = environ.get("JWT_SECRET_KEY") or "8f1bd7696ffb482d8486dfbc6e7d16dd-secret-key"
|
||||||
|
@ -31,4 +30,4 @@ SENTRY_DSN = environ.get("SENTRY_DSN")
|
||||||
SESSION_SECRET_KEY = environ.get("SESSION_SECRET_KEY") or "!secret"
|
SESSION_SECRET_KEY = environ.get("SESSION_SECRET_KEY") or "!secret"
|
||||||
|
|
||||||
# for local development
|
# for local development
|
||||||
DEV_SERVER_PID_FILE_NAME = "dev-server.pid"
|
DEV_SERVER_PID_FILE_NAME = 'dev-server.pid'
|
||||||
|
|
7
setup.cfg
Normal file → Executable file
7
setup.cfg
Normal file → Executable file
|
@ -9,16 +9,15 @@ force_alphabetical_sort = false
|
||||||
|
|
||||||
[tool:brunette]
|
[tool:brunette]
|
||||||
# https://github.com/odwyersoftware/brunette
|
# https://github.com/odwyersoftware/brunette
|
||||||
line-length = 100
|
line-length = 120
|
||||||
single-quotes = false
|
single-quotes = false
|
||||||
|
|
||||||
[flake8]
|
[flake8]
|
||||||
# https://github.com/PyCQA/flake8
|
# https://github.com/PyCQA/flake8
|
||||||
exclude = .git,__pycache__,.mypy_cache,.vercel
|
exclude = .git,__pycache__,.mypy_cache,.vercel
|
||||||
max-line-length = 100
|
max-line-length = 120
|
||||||
max-complexity = 10
|
max-complexity = 15
|
||||||
select = B,C,E,F,W,T4,B9
|
select = B,C,E,F,W,T4,B9
|
||||||
# FIXME
|
|
||||||
# E203: Whitespace before ':'
|
# E203: Whitespace before ':'
|
||||||
# E266: Too many leading '#' for block comment
|
# E266: Too many leading '#' for block comment
|
||||||
# E501: Line too long (82 > 79 characters)
|
# E501: Line too long (82 > 79 characters)
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
[isort]
|
|
||||||
# https://github.com/PyCQA/isort
|
|
||||||
line_length = 120
|
|
||||||
multi_line_output = 3
|
|
||||||
include_trailing_comma = true
|
|
||||||
force_grid_wrap = 0
|
|
||||||
use_parentheses = true
|
|
||||||
force_alphabetical_sort = false
|
|
||||||
|
|
||||||
[tool:brunette]
|
|
||||||
# https://github.com/odwyersoftware/brunette
|
|
||||||
line-length = 120
|
|
||||||
single-quotes = false
|
|
||||||
|
|
||||||
[flake8]
|
|
||||||
# https://github.com/PyCQA/flake8
|
|
||||||
exclude = .git,__pycache__,.mypy_cache,.vercel
|
|
||||||
max-line-length = 120
|
|
||||||
max-complexity = 15
|
|
||||||
select = B,C,E,F,W,T4,B9
|
|
||||||
# E203: Whitespace before ':'
|
|
||||||
# E266: Too many leading '#' for block comment
|
|
||||||
# E501: Line too long (82 > 79 characters)
|
|
||||||
# E722: Do not use bare except, specify exception instead
|
|
||||||
# W503: Line break occurred before a binary operator
|
|
||||||
# F403: 'from module import *' used; unable to detect undefined names
|
|
||||||
# C901: Function is too complex
|
|
||||||
ignore = E203,E266,E501,E722,W503,F403,C901
|
|
||||||
|
|
||||||
[mypy]
|
|
||||||
# https://github.com/python/mypy
|
|
||||||
ignore_missing_imports = true
|
|
||||||
warn_return_any = false
|
|
||||||
warn_unused_configs = true
|
|
||||||
disallow_untyped_calls = true
|
|
||||||
disallow_untyped_defs = true
|
|
||||||
disallow_incomplete_defs = true
|
|
||||||
[mypy-api.*]
|
|
||||||
ignore_errors = true
|
|
|
@ -1,5 +1,5 @@
|
||||||
from pydantic import BaseModel
|
|
||||||
from typing import Optional, Text
|
from typing import Optional, Text
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class AuthInput(BaseModel):
|
class AuthInput(BaseModel):
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
from typing import Optional, Text, List
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from typing import List, Optional, Text
|
|
||||||
|
|
||||||
|
|
||||||
class Message(BaseModel):
|
class Message(BaseModel):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user