2024-02-27 12:40:53 +00:00
|
|
|
import json
|
2024-02-25 13:43:04 +00:00
|
|
|
|
2024-05-03 11:12:57 +00:00
|
|
|
from orm.topic import TopicFollower
|
2024-05-06 21:06:31 +00:00
|
|
|
from services.db import local_session
|
2024-03-06 18:57:04 +00:00
|
|
|
from services.encoders import CustomJSONEncoder
|
2024-04-08 07:38:58 +00:00
|
|
|
from services.rediscache import redis
|
2024-02-25 13:43:04 +00:00
|
|
|
|
2024-02-27 12:40:53 +00:00
|
|
|
DEFAULT_FOLLOWS = {
|
2024-04-17 15:32:23 +00:00
|
|
|
"topics": [],
|
|
|
|
"authors": [],
|
|
|
|
"communities": [{"id": 1, "name": "Дискурс", "slug": "discours", "pic": ""}],
|
2024-02-27 12:40:53 +00:00
|
|
|
}
|
2024-02-25 13:43:04 +00:00
|
|
|
|
|
|
|
|
2024-04-09 08:17:32 +00:00
|
|
|
async def cache_author(author: dict):
|
2024-05-05 18:38:59 +00:00
|
|
|
author_id = author.get("id")
|
2024-03-06 19:00:37 +00:00
|
|
|
payload = json.dumps(author, cls=CustomJSONEncoder)
|
2024-04-17 15:32:23 +00:00
|
|
|
await redis.execute("SET", f'user:{author.get("user")}', payload)
|
2024-05-06 07:25:09 +00:00
|
|
|
await redis.execute("SET", f"author:{author_id}", payload)
|
2024-03-12 12:50:57 +00:00
|
|
|
|
2024-04-08 18:33:47 +00:00
|
|
|
# update stat all field for followers' caches in <authors> list
|
2024-05-06 07:25:09 +00:00
|
|
|
followers_str = await redis.execute("GET", f"author:{author_id}:followers")
|
2024-04-08 18:33:47 +00:00
|
|
|
followers = []
|
2024-04-17 17:30:05 +00:00
|
|
|
if isinstance(followers_str, str):
|
2024-04-08 18:33:47 +00:00
|
|
|
followers = json.loads(followers_str)
|
|
|
|
if isinstance(followers, list):
|
|
|
|
for follower in followers:
|
|
|
|
follower_follows_authors = []
|
2024-05-06 07:25:09 +00:00
|
|
|
follower_follows_authors_str = await redis.execute(
|
|
|
|
"GET", f"author:{author_id}:follows-authors"
|
|
|
|
)
|
2024-04-18 09:34:04 +00:00
|
|
|
if isinstance(follower_follows_authors_str, str):
|
2024-04-08 18:33:47 +00:00
|
|
|
follower_follows_authors = json.loads(follower_follows_authors_str)
|
|
|
|
c = 0
|
|
|
|
for old_author in follower_follows_authors:
|
2024-04-17 15:32:23 +00:00
|
|
|
if int(old_author.get("id")) == int(author.get("id", 0)):
|
2024-04-08 18:33:47 +00:00
|
|
|
follower_follows_authors[c] = author
|
|
|
|
break # exit the loop since we found and updated the author
|
|
|
|
c += 1
|
|
|
|
else:
|
|
|
|
# author not found in the list, so add the new author with the updated stat field
|
|
|
|
follower_follows_authors.append(author)
|
|
|
|
|
|
|
|
# update stat field for all authors' caches in <followers> list
|
2024-05-06 07:25:09 +00:00
|
|
|
follows_str = await redis.execute("GET", f"author:{author_id}:follows-authors")
|
2024-04-08 18:33:47 +00:00
|
|
|
follows_authors = []
|
2024-04-18 09:34:04 +00:00
|
|
|
if isinstance(follows_str, str):
|
2024-04-08 18:33:47 +00:00
|
|
|
follows_authors = json.loads(follows_str)
|
|
|
|
if isinstance(follows_authors, list):
|
|
|
|
for followed_author in follows_authors:
|
|
|
|
followed_author_followers = []
|
2024-05-06 07:25:09 +00:00
|
|
|
followed_author_followers_str = await redis.execute(
|
|
|
|
"GET", f"author:{author_id}:followers"
|
|
|
|
)
|
2024-04-18 09:34:04 +00:00
|
|
|
if isinstance(followed_author_followers_str, str):
|
2024-04-08 18:33:47 +00:00
|
|
|
followed_author_followers = json.loads(followed_author_followers_str)
|
|
|
|
c = 0
|
|
|
|
for old_follower in followed_author_followers:
|
2024-05-05 18:38:59 +00:00
|
|
|
old_follower_id = int(old_follower.get("id"))
|
|
|
|
if old_follower_id == author_id:
|
2024-04-08 18:33:47 +00:00
|
|
|
followed_author_followers[c] = author
|
|
|
|
break # exit the loop since we found and updated the author
|
|
|
|
c += 1
|
2024-05-05 18:38:59 +00:00
|
|
|
# author not found in the list, so add the new author with the updated stat field
|
|
|
|
followed_author_followers.append(author)
|
2024-05-06 07:25:09 +00:00
|
|
|
await redis.execute(
|
2024-05-06 21:06:31 +00:00
|
|
|
"SET",
|
|
|
|
f"author:{author_id}:followers",
|
|
|
|
json.dumps(followed_author_followers, cls=CustomJSONEncoder),
|
2024-05-06 07:25:09 +00:00
|
|
|
)
|
2024-02-27 12:40:53 +00:00
|
|
|
|
|
|
|
|
2024-04-19 15:22:07 +00:00
|
|
|
async def cache_follows(follower: dict, entity_type: str, entity: dict, is_insert=True):
|
2024-04-09 08:17:32 +00:00
|
|
|
# prepare
|
2024-03-11 10:37:35 +00:00
|
|
|
follows = []
|
2024-04-19 15:22:07 +00:00
|
|
|
follower_id = follower.get("id")
|
|
|
|
if follower_id:
|
|
|
|
redis_key = f"author:{follower_id}:follows-{entity_type}s"
|
|
|
|
follows_str = await redis.execute("GET", redis_key)
|
|
|
|
if isinstance(follows_str, str):
|
|
|
|
follows = json.loads(follows_str)
|
|
|
|
if is_insert:
|
|
|
|
follows.append(entity)
|
|
|
|
else:
|
|
|
|
entity_id = entity.get("id")
|
|
|
|
if not entity_id:
|
|
|
|
raise Exception("wrong entity")
|
|
|
|
# Remove the entity from follows
|
|
|
|
follows = [e for e in follows if e["id"] != entity_id]
|
2024-04-09 08:17:32 +00:00
|
|
|
|
2024-04-19 15:22:07 +00:00
|
|
|
# update follows cache
|
|
|
|
payload = json.dumps(follows, cls=CustomJSONEncoder)
|
|
|
|
await redis.execute("SET", redis_key, payload)
|
2024-04-09 08:17:32 +00:00
|
|
|
|
2024-04-19 15:22:07 +00:00
|
|
|
# update follower's stats everywhere
|
2024-05-06 21:02:15 +00:00
|
|
|
follower_str = await redis.execute("GET", f"author:{follower_id}")
|
|
|
|
if isinstance(follower_str, str):
|
|
|
|
follower = json.loads(follower_str)
|
|
|
|
follower["stat"][f"{entity_type}s"] = len(follows)
|
|
|
|
await cache_author(follower)
|
2024-02-29 21:51:49 +00:00
|
|
|
return follows
|
|
|
|
|
|
|
|
|
2024-05-06 21:02:15 +00:00
|
|
|
async def cache_follow_author_change(follower: dict, author: dict, is_insert=True):
|
2024-04-19 15:22:07 +00:00
|
|
|
author_id = author.get("id")
|
|
|
|
follower_id = follower.get("id")
|
2024-03-12 05:00:42 +00:00
|
|
|
followers = []
|
2024-04-19 15:22:07 +00:00
|
|
|
if author_id and follower_id:
|
|
|
|
redis_key = f"author:{author_id}:followers"
|
|
|
|
followers_str = await redis.execute("GET", redis_key)
|
2024-05-06 07:29:50 +00:00
|
|
|
followers = json.loads(followers_str) if isinstance(followers_str, str) else []
|
2024-05-06 21:02:15 +00:00
|
|
|
|
|
|
|
# Remove the author from the list of followers, if present
|
|
|
|
followers = [f for f in followers if f["id"] != author_id]
|
|
|
|
|
|
|
|
# If inserting, add the new follower to the list if not already present
|
|
|
|
if is_insert and not any(f["id"] == follower_id for f in followers):
|
2024-04-19 15:22:07 +00:00
|
|
|
followers.append(follower)
|
2024-05-06 21:02:15 +00:00
|
|
|
|
|
|
|
# Remove the follower from the list if not inserting and present
|
2024-05-06 07:25:09 +00:00
|
|
|
else:
|
2024-05-06 21:02:15 +00:00
|
|
|
followers = [f for f in followers if f["id"] != follower_id]
|
|
|
|
|
|
|
|
# Ensure followers are unique based on their 'id' field
|
|
|
|
followers = list({f["id"]: f for f in followers}.values())
|
|
|
|
|
|
|
|
# Update follower's stats everywhere
|
|
|
|
follower_str = await redis.execute("GET", f"author:{follower_id}")
|
|
|
|
if isinstance(follower_str, str):
|
|
|
|
follower = json.loads(follower_str)
|
|
|
|
follower["stat"]["followers"] = len(followers)
|
|
|
|
await cache_author(follower)
|
|
|
|
|
2024-05-06 07:25:09 +00:00
|
|
|
payload = json.dumps(followers, cls=CustomJSONEncoder)
|
|
|
|
await redis.execute("SET", redis_key, payload)
|
2024-05-06 21:02:15 +00:00
|
|
|
|
2024-02-29 21:51:49 +00:00
|
|
|
return followers
|
2024-05-03 11:12:57 +00:00
|
|
|
|
|
|
|
|
2024-05-05 15:46:16 +00:00
|
|
|
async def cache_topic(topic_dict: dict):
|
2024-05-03 11:12:57 +00:00
|
|
|
# update stat all field for followers' caches in <topics> list
|
|
|
|
followers = (
|
|
|
|
local_session()
|
|
|
|
.query(TopicFollower)
|
|
|
|
.filter(TopicFollower.topic == topic_dict.get("id"))
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
for tf in followers:
|
|
|
|
follower_id = tf.follower
|
|
|
|
follower_follows_topics = []
|
|
|
|
follower_follows_topics_str = await redis.execute(
|
|
|
|
"GET", f"author:{follower_id}:follows-topics"
|
|
|
|
)
|
2024-05-05 17:16:45 +00:00
|
|
|
if isinstance(follower_follows_topics_str, str):
|
2024-05-03 11:12:57 +00:00
|
|
|
follower_follows_topics = json.loads(follower_follows_topics_str)
|
|
|
|
c = 0
|
|
|
|
for old_topic in follower_follows_topics:
|
|
|
|
if int(old_topic.get("id")) == int(topic_dict.get("id", 0)):
|
|
|
|
follower_follows_topics[c] = topic_dict
|
|
|
|
break # exit the loop since we found and updated the topic
|
|
|
|
c += 1
|
|
|
|
else:
|
|
|
|
# topic not found in the list, so add the new topic with the updated stat field
|
|
|
|
follower_follows_topics.append(topic_dict)
|
|
|
|
|
2024-05-05 17:17:07 +00:00
|
|
|
await redis.execute(
|
2024-05-03 11:12:57 +00:00
|
|
|
"SET",
|
|
|
|
f"author:{follower_id}:follows-topics",
|
2024-05-06 17:30:49 +00:00
|
|
|
json.dumps(follower_follows_topics, cls=CustomJSONEncoder),
|
2024-05-03 11:12:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# update topic's stat
|
|
|
|
topic_dict["stat"]["followers"] = len(followers)
|
|
|
|
|
|
|
|
# save in cache
|
|
|
|
payload = json.dumps(topic_dict, cls=CustomJSONEncoder)
|
|
|
|
await redis.execute("SET", f'topic:{topic_dict.get("slug")}', payload)
|