diff --git a/CHANGELOG.md b/CHANGELOG.md index b3162d1..c537d52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +# 0.23.0 + +* Results will return the total count in the DB to help with pagination +* Fixed paginated results not always returning all results +* fixed DECOMMISSIONED typo + # 0.21.1 Allow to display endorsement badge without text diff --git a/fediseer/apis/models/v1.py b/fediseer/apis/models/v1.py index 73bf353..8436c3f 100644 --- a/fediseer/apis/models/v1.py +++ b/fediseer/apis/models/v1.py @@ -58,6 +58,7 @@ class Models: 'instances': fields.List(fields.Nested(self.response_model_instances_visibility)), 'domains': fields.List(fields.String(description="The instance domains as a list.")), 'csv': fields.String(description="The instance domains as a csv."), + 'total': fields.Integer(description="The total amount of results in the database for this query. Use this to know the amount of pages"), }) self.response_model_instances_censured = api.inherit('CensuredInstanceDetails', self.response_model_instances, { 'censure_reasons': fields.List(fields.String(description="The reasons instances have given for censuring this instance")), @@ -69,6 +70,7 @@ class Models: 'instances': fields.List(fields.Nested(self.response_model_instances_censured)), 'domains': fields.List(fields.String(description="The instance domains as a list.")), 'csv': fields.String(description="The instance domains as a csv."), + 'total': fields.Integer(description="The total amount of results in the database for this query. Use this to know the amount of pages"), }) self.response_model_instances_endorsed = api.inherit('EndorsedInstanceDetails', self.response_model_instances, { 'endorsement_reasons': fields.List(fields.String(description="The reasons instances have given for endorsing this instance")), @@ -77,6 +79,7 @@ class Models: 'instances': fields.List(fields.Nested(self.response_model_instances_endorsed)), 'domains': fields.List(fields.String(description="The instance domains as a list.")), 'csv': fields.String(description="The instance domains as a csv."), + 'total': fields.Integer(description="The total amount of results in the database for this query. Use this to know the amount of pages"), }) self.response_model_dubious_instances = api.inherit('DubiousInstanceDetails', self.response_model_instances, { 'hesitation_reasons': fields.List(fields.String(description="The reasons instances have given for hesitating against this instance")), @@ -88,6 +91,7 @@ class Models: 'instances': fields.List(fields.Nested(self.response_model_dubious_instances)), 'domains': fields.List(fields.String(description="The instance domains as a list.")), 'csv': fields.String(description="The instance domains as a csv."), + 'total': fields.Integer(description="The total amount of results in the database for this query. Use this to know the amount of pages"), }) self.input_endorsements_modify = api.model('ModifyEndorsements', { 'reason': fields.String(required=False, description="The reason for this endorsement. No profanity or hate speech allowed!", example="I just think they're neat."), diff --git a/fediseer/apis/v1/censures.py b/fediseer/apis/v1/censures.py index c161c01..71c9b38 100644 --- a/fediseer/apis/v1/censures.py +++ b/fediseer/apis/v1/censures.py @@ -115,11 +115,20 @@ class CensuresGiven(Resource): c_instance_details["censure_count"] = censure_count instance_details.append(c_instance_details) if self.args.csv: - return {"csv": ",".join([instance["domain"] for instance in instance_details])},200 + return { + "csv": ",".join([instance["domain"] for instance in instance_details]), + "total": len(censures) + },200 if self.args.domains: - return {"domains": [instance["domain"] for instance in instance_details]},200 - - return {"instances": instance_details},200 + return { + "domains": [instance["domain"] for instance in instance_details], + "total": len(censures) + },200 + return { + "instances": instance_details, + "total": len(censures) + },200 + class Censures(Resource): get_parser = reqparse.RequestParser() @@ -172,10 +181,19 @@ class Censures(Resource): c_instance_details["rebuttal"] = rebuttals instance_details.append(c_instance_details) if self.args.csv: - return {"csv": ",".join([instance["domain"] for instance in instance_details])},200 + return { + "csv": ",".join([instance["domain"] for instance in instance_details]), + "total": len(censures) + },200 if self.args.domains: - return {"domains": [instance["domain"] for instance in instance_details]},200 - return {"instances": instance_details},200 + return { + "domains": [instance["domain"] for instance in instance_details], + "total": len(censures) + },200 + return { + "instances": instance_details, + "total": len(censures) + },200 decorators = [limiter.limit("45/minute"), limiter.limit("30/minute", key_func = get_request_path)] put_parser = reqparse.RequestParser() diff --git a/fediseer/apis/v1/endorsements.py b/fediseer/apis/v1/endorsements.py index d86efea..dec6365 100644 --- a/fediseer/apis/v1/endorsements.py +++ b/fediseer/apis/v1/endorsements.py @@ -92,10 +92,19 @@ class Approvals(Resource): e_instance_details["endorsement_reasons"] = [endorsement.reason for endorsement in r_endorsements] instance_details.append(e_instance_details) if self.args.csv: - return {"csv": ",".join([instance["domain"] for instance in instance_details])},200 + return { + "csv": ",".join([instance["domain"] for instance in instance_details]), + "total": len(endorsements) + },200 if self.args.domains: - return {"domains": [instance["domain"] for instance in instance_details]},200 - return {"instances": instance_details},200 + return { + "domains": [instance["domain"] for instance in instance_details], + "total": len(endorsements) + },200 + return { + "instances": instance_details, + "total": len(endorsements) + },200 class Endorsements(Resource): get_parser = reqparse.RequestParser() @@ -144,10 +153,20 @@ class Endorsements(Resource): e_instance_details["endorsement_reasons"] = [endorsement.reason for endorsement in endorsements] instance_details.append(e_instance_details) if self.args.csv: - return {"csv": ",".join([instance["domain"] for instance in instance_details])},200 + return { + "csv": ",".join([instance["domain"] for instance in instance_details]), + "total": len(instances) + },200 if self.args.domains: - return {"domains": [instance["domain"] for instance in instance_details]},200 - return {"instances": instance_details},200 + return { + "domains": [instance["domain"] for instance in instance_details], + "total": len(instances) + },200 + return { + "instances": instance_details, + "total": len(instances) + },200 + decorators = [limiter.limit("20/minute", key_func = get_request_path)] put_parser = reqparse.RequestParser() diff --git a/fediseer/apis/v1/hesitations.py b/fediseer/apis/v1/hesitations.py index 73dda18..7f975e4 100644 --- a/fediseer/apis/v1/hesitations.py +++ b/fediseer/apis/v1/hesitations.py @@ -99,11 +99,19 @@ class HesitationsGiven(Resource): c_instance_details["hesitation_count"] = hesitation_count instance_details.append(c_instance_details) if self.args.csv: - return {"csv": ",".join([instance["domain"] for instance in instance_details])},200 + return { + "csv": ",".join([instance["domain"] for instance in instance_details]), + "total": len(hesitations) + },200 if self.args.domains: - return {"domains": [instance["domain"] for instance in instance_details]},200 - - return {"instances": instance_details},200 + return { + "domains": [instance["domain"] for instance in instance_details], + "total": len(hesitations) + },200 + return { + "instances": instance_details, + "total": len(hesitations) + },200 class Hesitations(Resource): get_parser = reqparse.RequestParser() @@ -155,10 +163,19 @@ class Hesitations(Resource): c_instance_details["rebuttal"] = [r.rebuttal for r in rebuttals if r.target_id == c_instance.id] instance_details.append(c_instance_details) if self.args.csv: - return {"csv": ",".join([instance["domain"] for instance in instance_details])},200 + return { + "csv": ",".join([instance["domain"] for instance in instance_details]), + "total": len(hesitations) + },200 if self.args.domains: - return {"domains": [instance["domain"] for instance in instance_details]},200 - return {"instances": instance_details},200 + return { + "domains": [instance["domain"] for instance in instance_details], + "total": len(hesitations) + },200 + return { + "instances": instance_details, + "total": len(hesitations) + },200 decorators = [limiter.limit("20/minute", key_func = get_request_path)] put_parser = reqparse.RequestParser() diff --git a/fediseer/apis/v1/whitelist.py b/fediseer/apis/v1/whitelist.py index bf477fb..024c8d7 100644 --- a/fediseer/apis/v1/whitelist.py +++ b/fediseer/apis/v1/whitelist.py @@ -36,20 +36,30 @@ class Whitelist(Resource): if self.args.software_csv is not None: software = [s.strip() for s in self.args.software_csv.split(',')] instance_details = [] - for instance in database.get_all_instances( + all_instances = database.get_all_instances( min_endorsements=self.args.endorsements, min_guarantors=self.args.guarantors, tags=tags, software=software, page=self.args.page, limit=self.args.limit, - ): + ) + for instance in all_instances: instance_details.append(instance.get_details(show_visibilities=True)) if self.args.csv: - return {"csv": ",".join([instance["domain"] for instance in instance_details])},200 + return { + "csv": ",".join([instance["domain"] for instance in instance_details]), + "total": database.count_all_instances() + },200 if self.args.domains: - return {"domains": [instance["domain"] for instance in instance_details]},200 - return {"instances": instance_details},200 + return { + "domains": [instance["domain"] for instance in instance_details], + "total": database.count_all_instances() + },200 + return { + "instances": instance_details, + "total": database.count_all_instances() + },200 class WhitelistDomain(Resource): get_parser = reqparse.RequestParser() diff --git a/fediseer/consts.py b/fediseer/consts.py index f98449d..77948da 100644 --- a/fediseer/consts.py +++ b/fediseer/consts.py @@ -1,4 +1,4 @@ -FEDISEER_VERSION = "0.22.1" +FEDISEER_VERSION = "0.23.0" SUPPORTED_SOFTWARE = { "lemmy", "mastodon", diff --git a/fediseer/database/functions.py b/fediseer/database/functions.py index d862a02..acfbdde 100644 --- a/fediseer/database/functions.py +++ b/fediseer/database/functions.py @@ -9,14 +9,12 @@ from fediseer.classes.user import Claim, User from fediseer.classes.reports import Report from fediseer import enums -def get_all_instances( +def get_all_instance_query( min_endorsements = 0, min_guarantors = 1, tags = None, software = None, include_decommissioned = True, - page=1, - limit=10, ): query = db.session.query( Instance @@ -48,11 +46,45 @@ def get_all_instances( if software: lower_sw = [sw.lower() for sw in software] query = query.filter(Instance.software.in_(lower_sw)) + return query + +def get_all_instances( + min_endorsements = 0, + min_guarantors = 1, + tags = None, + software = None, + include_decommissioned = True, + page=1, + limit=10, + ): + query = get_all_instance_query( + min_endorsements = min_endorsements, + min_guarantors = min_guarantors, + tags = tags, + software = software, + include_decommissioned = include_decommissioned, + ) page -= 1 if page < 0: page = 0 return query.order_by(Instance.created.desc()).offset(limit * page).limit(limit).all() +def count_all_instances( + min_endorsements = 0, + min_guarantors = 1, + tags = None, + software = None, + include_decommissioned = True, + ): + query = get_all_instance_query( + min_endorsements = min_endorsements, + min_guarantors = min_guarantors, + tags = tags, + software = software, + include_decommissioned = include_decommissioned, + ) + return query.count() + def query_all_endorsed_instances_by_approving_id(approving_ids): return db.session.query( Instance @@ -141,7 +173,6 @@ def get_all_censured_instances_by_censuring_id(censuring_ids,page=1,limit=100): page -= 1 if page < 0: page = 0 - logger.debug([limit * page,limit]) return query.order_by(Instance.domain).offset(limit * page).limit(limit).all() else: return query.all()