From 9b5460304dfd89b08ec6695123b005613a446426 Mon Sep 17 00:00:00 2001 From: lukasdenk Date: Mon, 3 Jan 2022 10:22:44 +0100 Subject: [PATCH 1/2] Solve issue #10699 Adds a flag to the synapse_review_recent_signups script to ignore and filter appservice users. Signed-off-by: Lukas Denk --- changelog.d/11675.feature | 1 + synapse/_scripts/review_recent_signups.py | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 changelog.d/11675.feature diff --git a/changelog.d/11675.feature b/changelog.d/11675.feature new file mode 100644 index 000000000000..c14b1023be33 --- /dev/null +++ b/changelog.d/11675.feature @@ -0,0 +1 @@ +Adds a flag to the `synapse_review_recent_signups` script to ignore and filter appservice users. diff --git a/synapse/_scripts/review_recent_signups.py b/synapse/_scripts/review_recent_signups.py index 093af4327ae0..921dec719666 100644 --- a/synapse/_scripts/review_recent_signups.py +++ b/synapse/_scripts/review_recent_signups.py @@ -46,7 +46,9 @@ class UserInfo: ips: List[str] = attr.Factory(list) -def get_recent_users(txn: LoggingTransaction, since_ms: int) -> List[UserInfo]: +def get_recent_users( + txn: LoggingTransaction, since_ms: int, exclude_app_service: bool +) -> List[UserInfo]: """Fetches recently registered users and some info on them.""" sql = """ @@ -56,6 +58,9 @@ def get_recent_users(txn: LoggingTransaction, since_ms: int) -> List[UserInfo]: AND deactivated = 0 """ + if exclude_app_service: + sql += "\n AND appservice_id IS NOT NULL" + txn.execute(sql, (since_ms / 1000,)) user_infos = [UserInfo(user_id, creation_ts) for user_id, creation_ts in txn] @@ -121,6 +126,9 @@ def main() -> None: action="store_true", help="Only print user IDs that match.", ) + parser.add_argument( + "-a", "--exclude-app-service", help="Exclude appservice users.", default=False + ) config = ReviewConfig() @@ -133,6 +141,7 @@ def main() -> None: since_ms = time.time() * 1000 - Config.parse_duration(config_args.since) exclude_users_with_email = config_args.exclude_emails + exclude_users_with_appservice = config_args.exclude_app_service include_context = not config_args.only_users for database_config in config.database.databases: @@ -143,7 +152,7 @@ def main() -> None: with make_conn(database_config, engine, "review_recent_signups") as db_conn: # This generates a type of Cursor, not LoggingTransaction. - user_infos = get_recent_users(db_conn.cursor(), since_ms) # type: ignore[arg-type] + user_infos = get_recent_users(db_conn.cursor(), since_ms, exclude_users_with_appservice) # type: ignore[arg-type] for user_info in user_infos: if exclude_users_with_email and user_info.emails: From 62ea8ea51ef5027c4f545b6ffcf79a693e1ba48f Mon Sep 17 00:00:00 2001 From: lukasdenk Date: Mon, 10 Jan 2022 16:08:31 +0100 Subject: [PATCH 2/2] - Apply proposed changes (see https://github.com/matrix-org/synapse/pull/11675#pullrequestreview-843389959). - Fix option exclude appservice to _exclude_ appservice users. Before, the option _included_ appservice users. --- changelog.d/11675.feature | 2 +- synapse/_scripts/review_recent_signups.py | 9 ++++++--- synapse/handlers/register.py | 12 +++++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/changelog.d/11675.feature b/changelog.d/11675.feature index c14b1023be33..9a276f9542cf 100644 --- a/changelog.d/11675.feature +++ b/changelog.d/11675.feature @@ -1 +1 @@ -Adds a flag to the `synapse_review_recent_signups` script to ignore and filter appservice users. +Add a flag to the `synapse_review_recent_signups` script to ignore and filter appservice users. diff --git a/synapse/_scripts/review_recent_signups.py b/synapse/_scripts/review_recent_signups.py index 921dec719666..e207f154f3aa 100644 --- a/synapse/_scripts/review_recent_signups.py +++ b/synapse/_scripts/review_recent_signups.py @@ -59,7 +59,7 @@ def get_recent_users( """ if exclude_app_service: - sql += "\n AND appservice_id IS NOT NULL" + sql += " AND appservice_id IS NULL" txn.execute(sql, (since_ms / 1000,)) @@ -118,7 +118,7 @@ def main() -> None: "-e", "--exclude-emails", action="store_true", - help="Exclude users that have validated email addresses", + help="Exclude users that have validated email addresses.", ) parser.add_argument( "-u", @@ -127,7 +127,10 @@ def main() -> None: help="Only print user IDs that match.", ) parser.add_argument( - "-a", "--exclude-app-service", help="Exclude appservice users.", default=False + "-a", + "--exclude-app-service", + help="Exclude appservice users.", + action="store_true", ) config = ReviewConfig() diff --git a/synapse/handlers/register.py b/synapse/handlers/register.py index f08a516a7588..68dbae591618 100644 --- a/synapse/handlers/register.py +++ b/synapse/handlers/register.py @@ -979,16 +979,18 @@ async def _register_email_threepid( if ( self.hs.config.email.email_enable_notifs and self.hs.config.email.email_notif_for_new_users - and token ): # Pull the ID of the access token back out of the db # It would really make more sense for this to be passed # up when the access token is saved, but that's quite an # invasive change I'd rather do separately. - user_tuple = await self.store.get_user_by_access_token(token) - # The token better still exist. - assert user_tuple - token_id = user_tuple.token_id + if token: + user_tuple = await self.store.get_user_by_access_token(token) + # The token better still exist. + assert user_tuple + token_id = user_tuple.token_id + else: + token_id = None await self.pusher_pool.add_pusher( user_id=user_id,