From 27293f4033c94eca6125c0102d37ae63dc597a6c Mon Sep 17 00:00:00 2001 From: Believethehype <1097224+believethehype@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:34:22 +0200 Subject: [PATCH] update 1984 dvm --- nostr_dvm/tasks/discovery_censor_wot.py | 74 +++++++++++++++++-------- tests/filter.py | 8 +-- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/nostr_dvm/tasks/discovery_censor_wot.py b/nostr_dvm/tasks/discovery_censor_wot.py index 1fca55a..51d92cc 100644 --- a/nostr_dvm/tasks/discovery_censor_wot.py +++ b/nostr_dvm/tasks/discovery_censor_wot.py @@ -4,7 +4,7 @@ from datetime import timedelta from threading import Thread from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKey, NostrSigner, Kind, RelayOptions, \ - RelayLimits + RelayLimits, Event from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils.admin_utils import AdminConfig @@ -23,7 +23,7 @@ Params: None """ -class DiscoverNonFollowers(DVMTaskInterface): +class DiscoverReports(DVMTaskInterface): KIND: Kind = EventDefinitions.KIND_NIP90_PEOPLE_DISCOVERY TASK: str = "people to block" FIX_COST: float = 0 @@ -46,14 +46,22 @@ class DiscoverNonFollowers(DVMTaskInterface): # default values users = [] + sender = event.author().to_hex() + since_days = 360 # users.append(event.author().to_hex()) for tag in event.tags(): if tag.as_vec()[0] == 'i': users.append(tag.as_vec()[1]) + elif tag.as_vec()[0] == 'param': + param = tag.as_vec()[1] + if param == "since_days": # check for param type + since_days = int(tag.as_vec()[2]) options = { "users": users, + "sender": sender, + "since_days": since_days, } request_form['options'] = json.dumps(options) return request_form @@ -63,7 +71,9 @@ class DiscoverNonFollowers(DVMTaskInterface): from types import SimpleNamespace ns = SimpleNamespace() relaylimits = RelayLimits.disable() - opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)).relay_limits(relaylimits)) + opts = ( + Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)).relay_limits( + relaylimits)) sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) keys = Keys.parse(sk.to_hex()) signer = NostrSigner.keys(keys) @@ -71,7 +81,7 @@ class DiscoverNonFollowers(DVMTaskInterface): # cli.add_relay("wss://relay.nostr.band") for relay in self.dvm_config.RELAY_LIST: cli.add_relay(relay) - #add nostr band, too. + # add nostr band, too. ropts = RelayOptions().ping(False) cli.add_relay_with_opts("wss://nostr.band", ropts) @@ -84,7 +94,29 @@ class DiscoverNonFollowers(DVMTaskInterface): for user in options["users"]: pubkeys.append(PublicKey.parse(user)) - kind1984_filter = Filter().authors(pubkeys).kind(Kind(1984)) + # if we don't add users, e.g. by a wot, we check all our followers. + if len(pubkeys) == 0: + followers_filter = Filter().author(PublicKey.parse(options["sender"])).kind(Kind(3)) + followers = cli.get_events_of([followers_filter], timedelta(seconds=5)) + + if len(followers) > 0: + result_list = [] + newest = 0 + best_entry = followers[0] + for entry in followers: + print(len(best_entry.tags())) + print(best_entry.created_at().as_secs()) + if entry.created_at().as_secs() > newest: + newest = entry.created_at().as_secs() + best_entry = entry + for tag in best_entry.tags(): + if tag.as_vec()[0] == "p": + following = PublicKey.parse(tag.as_vec()[1]) + pubkeys.append(following) + + ago = Timestamp.now().as_secs() - 60*60*24*int(options["since_days"]) #TODO make this an option, 180 days for now + since = Timestamp.from_secs(ago) + kind1984_filter = Filter().authors(pubkeys).kind(Kind(1984)).since(since) reports = cli.get_events_of([kind1984_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) bad_actors = [] @@ -97,20 +129,21 @@ class DiscoverNonFollowers(DVMTaskInterface): ns.dic[tag.as_vec()[1]] = 0 for report in reports: - print(report.as_json()) + #print(report.as_json()) for tag in report.tags(): if tag.as_vec()[0] == "p": if len(tag.as_vec()) > 2 and tag.as_vec()[2] in reasons or len(tag.as_vec()) <= 2: ns.dic[tag.as_vec()[1]] += 1 - - #result = {k for (k, v) in ns.dic.items() if v > 0} - #result = sorted(ns.dic.items(), key=lambda x: x[1], reverse=True) + #print(ns.dic.items()) + # result = {k for (k, v) in ns.dic.items() if v > 0} + # result = sorted(ns.dic.items(), key=lambda x: x[1], reverse=True) finallist_sorted = sorted(ns.dic.items(), key=lambda x: x[1], reverse=True) converted_dict = dict(finallist_sorted) print(json.dumps(converted_dict)) - for k in converted_dict: - p_tag = Tag.parse(["p", k]) + for k, v in converted_dict.items(): + print(k) + p_tag = Tag.parse(["p", k, str(v)]) bad_actors.append(p_tag.as_vec()) print(json.dumps(bad_actors)) @@ -138,20 +171,16 @@ def build_example(name, identifier, admin_config): # Add NIP89 nip89info = { "name": name, - "image": "https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg", - "about": "I discover users you follow, but that don't follow you back.", + "image": "https://image.nostr.build/19872a2edd866258fa9eab137631efda89310d52b2c6ea8f99ef057325aa1c7b.jpg", + "about": "I show users that have been reported by either your followers or your Web of Trust.", "encryptionSupported": True, "cashuAccepted": True, + "action": "mute", # follow, unfollow, mute, unmute "nip90Params": { - "user": { - "required": False, - "values": [], - "description": "Do the task for another user" - }, "since_days": { "required": False, "values": [], - "description": "The number of days a user has not been active to be considered inactive" + "description": "The number of days a report is ago in order to be considered " } } } @@ -159,9 +188,10 @@ def build_example(name, identifier, admin_config): nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"]) nip89config.CONTENT = json.dumps(nip89info) - return DiscoverNonFollowers(name=name, dvm_config=dvm_config, nip89config=nip89config, - admin_config=admin_config) + + return DiscoverReports(name=name, dvm_config=dvm_config, nip89config=nip89config, + admin_config=admin_config) if __name__ == '__main__': - process_venv(DiscoverNonFollowers) + process_venv(DiscoverReports) diff --git a/tests/filter.py b/tests/filter.py index 97ed15e..ea8d125 100644 --- a/tests/filter.py +++ b/tests/filter.py @@ -24,12 +24,12 @@ def playground(): admin_config.REBROADCAST_NIP89 = False admin_config.UPDATE_PROFILE = False - #discovery_test_sub = discovery_censor_wot.build_example("Censorship", "discovery_censor", admin_config) - #discovery_test_sub.run() - - discovery_test_sub = discovery_inactive_follows.build_example("Inactive Followings", "discovery_inactive", admin_config) + discovery_test_sub = discovery_censor_wot.build_example("Censorship", "discovery_censor", admin_config) discovery_test_sub.run() + #discovery_test_sub = discovery_inactive_follows.build_example("Inactive Followings", "discovery_inactive", admin_config) + #discovery_test_sub.run() + #keep_alive()