core/migration/tables/comments.py

205 lines
7.1 KiB
Python
Raw Normal View History

2022-11-23 14:09:35 +00:00
from datetime import datetime, timezone
2022-08-11 09:14:12 +00:00
from dateutil.parser import parse as date_parse
2022-08-11 09:59:35 +00:00
from base.orm import local_session
2022-08-11 10:06:31 +00:00
from migration.html2text import html2text
2022-09-18 14:29:21 +00:00
from orm.reaction import Reaction, ReactionKind
2022-11-29 12:36:46 +00:00
from orm.shout import ShoutReactionsFollower, Shout
from orm.topic import TopicFollower, Topic
2022-11-09 11:46:00 +00:00
from orm.user import User
2022-11-23 14:09:35 +00:00
ts = datetime.now(tz=timezone.utc)
2022-08-11 09:14:12 +00:00
2022-09-03 10:50:14 +00:00
2022-08-18 06:12:46 +00:00
async def migrate(entry, storage):
2022-09-03 10:50:14 +00:00
"""
{
"_id": "hdtwS8fSyFLxXCgSC",
"body": "<p>",
"contentItem": "mnK8KsJHPRi8DrybQ",
"createdBy": "bMFPuyNg6qAD2mhXe",
"thread": "01/",
"createdAt": "2016-04-19 04:33:53+00:00",
"ratings": [
{ "createdBy": "AqmRukvRiExNpAe8C", "value": 1 },
{ "createdBy": "YdE76Wth3yqymKEu5", "value": 1 }
],
"rating": 2,
"updatedAt": "2020-05-27 19:22:57.091000+00:00",
"updatedBy": "0"
}
->
type Reaction {
id: Int!
shout: Shout!
createdAt: DateTime!
createdBy: User!
updatedAt: DateTime
deletedAt: DateTime
deletedBy: User
range: String # full / 0:2340
kind: ReactionKind!
body: String
replyTo: Reaction
stat: Stat
old_id: String
old_thread: String
}
"""
reaction_dict = {
"createdAt": (
ts if not entry.get("createdAt") else date_parse(entry.get("createdAt"))
),
"body": html2text(entry.get("body", "")),
"oid": entry["_id"],
}
2022-09-03 10:50:14 +00:00
shout_oid = entry.get("contentItem")
2022-09-07 16:30:06 +00:00
if shout_oid not in storage["shouts"]["by_oid"]:
2022-09-03 10:50:14 +00:00
if len(storage["shouts"]["by_oid"]) > 0:
return shout_oid
else:
print("[migration] no shouts migrated yet")
raise Exception
return
else:
with local_session() as session:
author = session.query(User).filter(User.oid == entry["createdBy"]).first()
shout_dict = storage["shouts"]["by_oid"][shout_oid]
if shout_dict:
2022-11-29 12:36:46 +00:00
shout = session.query(
Shout
).where(Shout.slug == shout_dict["slug"]).one()
2022-11-29 19:13:03 +00:00
reaction_dict["shoutId"] = shout.id
2022-11-29 12:36:46 +00:00
reaction_dict["createdBy"] = author.id if author else 1
2022-09-03 10:50:14 +00:00
reaction_dict["kind"] = ReactionKind.COMMENT
# creating reaction from old comment
reaction = Reaction.create(**reaction_dict)
2022-09-18 14:29:21 +00:00
session.add(reaction)
2022-08-11 09:14:12 +00:00
2022-09-18 14:29:21 +00:00
# creating shout's reactions following for reaction author
following1 = session.query(
ShoutReactionsFollower
2022-11-29 12:36:46 +00:00
).join(
User
).join(
Shout
2022-09-18 14:29:21 +00:00
).where(
2022-11-29 12:36:46 +00:00
User.id == reaction_dict["createdBy"]
2022-09-18 14:29:21 +00:00
).filter(
2022-11-29 19:13:03 +00:00
ShoutReactionsFollower.shoutId == reaction.shoutId
2022-09-18 14:29:21 +00:00
).first()
2022-11-29 12:36:46 +00:00
2022-09-18 14:29:21 +00:00
if not following1:
following1 = ShoutReactionsFollower.create(
2022-11-29 19:13:03 +00:00
followerId=reaction_dict["createdBy"],
shoutId=reaction.shoutId,
2022-09-18 14:29:21 +00:00
auto=True
)
session.add(following1)
# creating topics followings for reaction author
for t in shout_dict["topics"]:
tf = session.query(
TopicFollower
2022-11-29 12:36:46 +00:00
).join(
Topic
2022-09-18 14:29:21 +00:00
).where(
2022-11-29 19:13:03 +00:00
TopicFollower.followerId == reaction_dict["createdBy"]
2022-09-18 14:29:21 +00:00
).filter(
2022-11-29 12:36:46 +00:00
Topic.slug == t
2022-09-18 14:29:21 +00:00
).first()
2022-11-29 12:36:46 +00:00
2022-09-18 14:29:21 +00:00
if not tf:
2022-11-29 12:36:46 +00:00
topic = session.query(
Topic
).where(Topic.slug == t).one()
2022-09-18 14:29:21 +00:00
topic_following = TopicFollower.create(
2022-11-29 19:13:03 +00:00
followerId=reaction_dict["createdBy"],
topicId=topic.id,
2022-09-18 14:29:21 +00:00
auto=True
)
session.add(topic_following)
2022-09-03 10:50:14 +00:00
reaction_dict["id"] = reaction.id
for comment_rating_old in entry.get("ratings", []):
rater = (
session.query(User)
2022-11-29 12:36:46 +00:00
.filter(User.oid == comment_rating_old["createdBy"])
.first()
2022-09-03 10:50:14 +00:00
)
re_reaction_dict = {
2022-11-29 19:13:03 +00:00
"shoutId": reaction_dict["shoutId"],
2022-09-03 10:50:14 +00:00
"replyTo": reaction.id,
"kind": ReactionKind.LIKE
if comment_rating_old["value"] > 0
else ReactionKind.DISLIKE,
2022-11-29 12:36:46 +00:00
"createdBy": rater.id if rater else 1,
2022-09-03 10:50:14 +00:00
}
cts = comment_rating_old.get("createdAt")
if cts:
re_reaction_dict["createdAt"] = date_parse(cts)
try:
# creating reaction from old rating
rr = Reaction.create(**re_reaction_dict)
2022-09-18 14:29:21 +00:00
following2 = session.query(
ShoutReactionsFollower
).where(
2022-11-29 19:13:03 +00:00
ShoutReactionsFollower.followerId == re_reaction_dict['createdBy']
2022-09-18 14:29:21 +00:00
).filter(
2022-11-29 19:13:03 +00:00
ShoutReactionsFollower.shoutId == rr.shoutId
2022-09-18 14:29:21 +00:00
).first()
if not following2:
following2 = ShoutReactionsFollower.create(
2022-11-29 19:13:03 +00:00
followerId=re_reaction_dict['createdBy'],
shoutId=rr.shoutId,
2022-09-18 14:29:21 +00:00
auto=True
)
session.add(following2)
session.add(rr)
2022-08-11 09:14:12 +00:00
2022-09-03 10:50:14 +00:00
except Exception as e:
print("[migration] comment rating error: %r" % re_reaction_dict)
raise e
2022-09-18 14:29:21 +00:00
session.commit()
2022-09-03 10:50:14 +00:00
else:
print(
"[migration] error: cannot find shout for comment %r"
% reaction_dict
)
return reaction
2022-08-11 09:14:12 +00:00
def migrate_2stage(rr, old_new_id):
2022-09-03 10:50:14 +00:00
reply_oid = rr.get("replyTo")
if not reply_oid:
return
2022-11-29 12:36:46 +00:00
2022-09-03 10:50:14 +00:00
new_id = old_new_id.get(rr.get("oid"))
if not new_id:
return
2022-11-29 12:36:46 +00:00
2022-09-03 10:50:14 +00:00
with local_session() as session:
comment = session.query(Reaction).filter(Reaction.id == new_id).first()
comment.replyTo = old_new_id.get(reply_oid)
session.add(comment)
2022-09-18 14:29:21 +00:00
srf = session.query(ShoutReactionsFollower).where(
2022-11-29 19:13:03 +00:00
ShoutReactionsFollower.shoutId == comment.shoutId
2022-09-18 14:29:21 +00:00
).filter(
2022-11-29 19:13:03 +00:00
ShoutReactionsFollower.followerId == comment.createdBy
2022-09-18 14:29:21 +00:00
).first()
2022-11-29 12:36:46 +00:00
2022-09-18 14:29:21 +00:00
if not srf:
2022-11-29 19:13:03 +00:00
srf = ShoutReactionsFollower.create(shoutId=comment.shoutId, followerId=comment.createdBy, auto=True)
2022-09-18 14:29:21 +00:00
session.add(srf)
2022-11-29 12:36:46 +00:00
2022-09-03 10:50:14 +00:00
session.commit()
if not rr["body"]:
raise Exception(rr)