update-last-seen-aware
All checks were successful
Deploy to core / deploy (push) Successful in 1m39s

This commit is contained in:
Untone 2024-02-22 13:20:14 +03:00
parent 4a1ee2ac80
commit 88a0d58751
2 changed files with 22 additions and 17 deletions

View File

@ -1,4 +1,5 @@
import json import json
import time
from typing import List from typing import List
from sqlalchemy import select, or_ from sqlalchemy import select, or_
@ -15,6 +16,7 @@ from resolvers.topic import topic_follow, topic_unfollow
from resolvers.stat import add_topic_stat_columns, get_topics_from_query, add_author_stat_columns from resolvers.stat import add_topic_stat_columns, get_topics_from_query, add_author_stat_columns
from services.auth import login_required from services.auth import login_required
from services.db import local_session from services.db import local_session
from services.follows import DEFAULT_FOLLOWS
from services.notify import notify_follower from services.notify import notify_follower
from services.schema import mutation, query from services.schema import mutation, query
from services.logger import root_logger as logger from services.logger import root_logger as logger
@ -84,6 +86,7 @@ async def unfollow(_, info, what, slug):
def query_follows(user_id: str): def query_follows(user_id: str):
logger.debug(f'query follows for {user_id} from database')
topics = [] topics = []
authors = [] authors = []
with local_session() as session: with local_session() as session:
@ -157,18 +160,18 @@ def query_follows(user_id: str):
async def get_follows_by_user_id(user_id: str): async def get_follows_by_user_id(user_id: str):
if user_id: if user_id:
redis_key = f'user:{user_id}:follows' author = await redis.execute('GET', f'user:{user_id}:author')
res = await redis.execute('GET', redis_key) follows = DEFAULT_FOLLOWS
day_old = int(time.time()) - author.get('last_seen', 0) > 24*60*60
if day_old:
follows = query_follows(user_id)
else:
logger.debug(f'getting follows for {user_id} from redis')
res = await redis.execute('GET', f'user:{user_id}:follows')
if isinstance(res, str): if isinstance(res, str):
follows = json.loads(res) follows = json.loads(res)
return follows return follows
logger.debug(f'getting follows for {user_id}')
follows = query_follows(user_id)
await redis.execute('SET', redis_key, json.dumps(follows))
return follows
def reactions_follow(author_id, shout_id, auto=False): def reactions_follow(author_id, shout_id, auto=False):
try: try:

View File

@ -9,6 +9,14 @@ from resolvers.stat import add_author_stat_columns, add_topic_stat_columns
from services.rediscache import redis from services.rediscache import redis
DEFAULT_FOLLOWS = {
'topics': [],
'authors': [],
'communities': [
{'slug': 'discours', 'name': 'Дискурс', 'id': 1, 'desc': ''}
],
}
async def update_author(author: Author, ttl = 25 * 60 * 60): async def update_author(author: Author, ttl = 25 * 60 * 60):
redis_key = f'user:{author.user}:author' redis_key = f'user:{author.user}:author'
await redis.execute('SETEX', redis_key, ttl, json.dumps(author.dict())) await redis.execute('SETEX', redis_key, ttl, json.dumps(author.dict()))
@ -56,13 +64,7 @@ async def update_follows_for_user(
if follows_str: if follows_str:
follows = json.loads(follows_str) follows = json.loads(follows_str)
else: else:
follows = { follows = DEFAULT_FOLLOWS
'topics': [],
'authors': [],
'communities': [
{'slug': 'discours', 'name': 'Дискурс', 'id': 1, 'desc': ''}
],
}
if is_insert: if is_insert:
follows[f'{entity_type}s'].append(entity) follows[f'{entity_type}s'].append(entity)
else: else: