orphan control

pull/3/head
db0 2023-06-22 16:29:58 +02:00
parent 97e475157f
commit 6978bcc5ed
4 changed files with 28 additions and 4 deletions

View File

@ -38,7 +38,7 @@ class Endorsements(Resource):
@api.marshal_with(models.response_model_model_Whitelist_get, code=200, description='Instances', skip_none=True) @api.marshal_with(models.response_model_model_Whitelist_get, code=200, description='Instances', skip_none=True)
@api.response(404, 'Instance not registered', models.response_model_error) @api.response(404, 'Instance not registered', models.response_model_error)
def get(self, domain): def get(self, domain):
'''Display all endorsements given by a specific domain '''Display all endorsements received by a specific domain
''' '''
self.args = self.get_parser.parse_args() self.args = self.get_parser.parse_args()
instance = database.find_instance_by_domain(domain) instance = database.find_instance_by_domain(domain)

View File

@ -103,6 +103,7 @@ class Guarantees(Resource):
orphan_ids = database.get_guarantee_chain(target_instance.id) orphan_ids = database.get_guarantee_chain(target_instance.id)
for orphan in database.get_instances_by_ids(orphan_ids): for orphan in database.get_instances_by_ids(orphan_ids):
pm_instance(orphan.domain, f"Phew! You guarantor chain has been repaired as {instance.domain} has guaranteed for {domain}.") pm_instance(orphan.domain, f"Phew! You guarantor chain has been repaired as {instance.domain} has guaranteed for {domain}.")
orphan.unset_as_orphan()
logger.info(f"{instance.domain} Guaranteed for {domain}") logger.info(f"{instance.domain} Guaranteed for {domain}")
return {"message":'Changed'}, 200 return {"message":'Changed'}, 200
@ -141,9 +142,15 @@ class Guarantees(Resource):
db.session.delete(endorsement) db.session.delete(endorsement)
db.session.delete(guarantee) db.session.delete(guarantee)
db.session.commit() db.session.commit()
pm_instance(target_instance.domain, f"Attention! You guarantor instance {instance.domain} has withdrawn their backing.\n\nIMPORTANT: All your guarantees will be deleted unless you manage to find a new guarantor within 24hours!") pm_instance(target_instance.domain,
f"Attention! You guarantor instance {instance.domain} has withdrawn their backing.\n\n"
"IMPORTANT: You are still considered guaranteed for the next 24hours, but you cannot further endorse or guarantee others."
"If you find a new guarantor then your guarantees will be reactivated!.\n\n"
"Note that if you do not find a guarantor within 7 days, all your endorsements will be removed."
)
orphan_ids = database.get_guarantee_chain(target_instance.id) orphan_ids = database.get_guarantee_chain(target_instance.id)
for orphan in database.get_instances_by_ids(orphan_ids): for orphan in database.get_instances_by_ids(orphan_ids):
pm_instance(orphan.domain, f"Attention! You guarantor chain has been b broken because {instance.domain} has withdrawn their backing from {domain}.\n\nIMPORTANT: All your guarantees will be deleted unless the chain is repaired or you find a new guarantor within 24hours!") pm_instance(orphan.domain, f"Attention! You guarantor chain has been broken because {instance.domain} has withdrawn their backing from {domain}.\n\nIMPORTANT: All your guarantees will be deleted unless the chain is repaired or you find a new guarantor within 24hours!")
orphan.set_as_oprhan()
logger.info(f"{instance.domain} Withdrew guarantee from {domain}") logger.info(f"{instance.domain} Withdrew guarantee from {domain}")
return {"message":'Changed'}, 200 return {"message":'Changed'}, 200

View File

@ -19,6 +19,7 @@ class Guarantee(db.Model):
guarantor_instance = db.relationship("Instance", back_populates="guarantees", foreign_keys=[guarantor_id]) guarantor_instance = db.relationship("Instance", back_populates="guarantees", foreign_keys=[guarantor_id])
guaranteed_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), unique=True, nullable=False) guaranteed_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), unique=True, nullable=False)
guaranteed_instance = db.relationship("Instance", back_populates="guarantors", foreign_keys=[guaranteed_id]) guaranteed_instance = db.relationship("Instance", back_populates="guarantors", foreign_keys=[guaranteed_id])
created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
class Endorsement(db.Model): class Endorsement(db.Model):
@ -29,6 +30,7 @@ class Endorsement(db.Model):
approving_instance = db.relationship("Instance", back_populates="approvals", foreign_keys=[approving_id]) approving_instance = db.relationship("Instance", back_populates="approvals", foreign_keys=[approving_id])
endorsed_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), nullable=False) endorsed_id = db.Column(db.Integer, db.ForeignKey("instances.id", ondelete="CASCADE"), nullable=False)
endorsed_instance = db.relationship("Instance", back_populates="endorsements", foreign_keys=[endorsed_id]) endorsed_instance = db.relationship("Instance", back_populates="endorsements", foreign_keys=[endorsed_id])
created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
class Instance(db.Model): class Instance(db.Model):
@ -39,6 +41,7 @@ class Instance(db.Model):
api_key = db.Column(db.String(100), unique=True, nullable=False, index=True) api_key = db.Column(db.String(100), unique=True, nullable=False, index=True)
created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
updated = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
oprhan_since = db.Column(db.DateTime, nullable=True)
open_registrations = db.Column(db.Boolean, unique=False, nullable=False, index=True) open_registrations = db.Column(db.Boolean, unique=False, nullable=False, index=True)
email_verify = db.Column(db.Boolean, unique=False, nullable=False, index=True) email_verify = db.Column(db.Boolean, unique=False, nullable=False, index=True)
@ -81,3 +84,12 @@ class Instance(db.Model):
def get_guarantor_domain(self): def get_guarantor_domain(self):
guarantor = self.get_guarantor() guarantor = self.get_guarantor()
return guarantor.domain if guarantor else None return guarantor.domain if guarantor else None
def set_as_oprhan(self):
self.oprhan_since = datetime.utcnow()
db.session.commit()
def unset_as_orphan(self):
self.oprhan_since = None
db.session.commit()

View File

@ -21,6 +21,11 @@ def get_all_instances(min_endorsements = 0, min_guarantors = 1):
joinedload(Instance.endorsements), joinedload(Instance.endorsements),
).group_by( ).group_by(
Instance.id Instance.id
).filter(
or_(
Instance.oprhan_since == None,
Instance.oprhan_since > datetime.utcnow() - timedelta(hours=24)
)
).having( ).having(
db.func.count(Instance.endorsements) >= min_endorsements, db.func.count(Instance.endorsements) >= min_endorsements,
).having( ).having(