Skip to content
This repository has been archived by the owner on Mar 2, 2024. It is now read-only.

Commit

Permalink
Remove personal filtering community stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jan 26, 2022
1 parent f395555 commit 31cac6f
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 207 deletions.
1 change: 0 additions & 1 deletion mautrix_facebook/config.py
Expand Up @@ -69,7 +69,6 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
copy("bridge.username_template")
copy("bridge.displayname_template")
copy("bridge.displayname_preference")
copy("bridge.community_template")
copy("bridge.command_prefix")

copy("bridge.initial_chat_sync")
Expand Down
4 changes: 1 addition & 3 deletions mautrix_facebook/db/__init__.py
Expand Up @@ -6,12 +6,11 @@
from .reaction import Reaction
from .upgrade import upgrade_table
from .user import User
from .user_contact import UserContact
from .user_portal import UserPortal


def init(db: Database) -> None:
for table in (Portal, Message, Reaction, User, Puppet, UserPortal, UserContact):
for table in (Portal, Message, Reaction, User, Puppet, UserPortal):
table.db = db


Expand All @@ -25,5 +24,4 @@ def init(db: Database) -> None:
"Puppet",
"User",
"UserPortal",
"UserContact",
]
8 changes: 7 additions & 1 deletion mautrix_facebook/db/upgrade/__init__.py
Expand Up @@ -2,4 +2,10 @@

upgrade_table = UpgradeTable()

from . import v01_initial_revision, v02_message_oti, v03_portal_meta_set, v04_relay_mode
from . import (
v01_initial_revision,
v02_message_oti,
v03_portal_meta_set,
v04_relay_mode,
v05_remove_communities,
)
45 changes: 45 additions & 0 deletions mautrix_facebook/db/upgrade/v05_remove_communities.py
@@ -0,0 +1,45 @@
# mautrix-facebook - A Matrix-Facebook Messenger puppeting bridge.
# Copyright (C) 2022 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from asyncpg import Connection

from . import upgrade_table


@upgrade_table.register(description="Remove community-related fields")
async def upgrade_v5(conn: Connection, scheme: str) -> None:
await conn.execute("DROP TABLE user_contact")
if scheme == "postgres":
await conn.execute("ALTER TABLE user_portal DROP COLUMN in_community")
else:
await conn.execute(
"""CREATE TABLE user_portal_v5 (
"user" BIGINT,
portal BIGINT,
portal_receiver BIGINT,
FOREIGN KEY (portal, portal_receiver) REFERENCES portal(fbid, fb_receiver)
ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY ("user") REFERENCES "user"(fbid) ON UPDATE CASCADE ON DELETE CASCADE,
PRIMARY KEY ("user", portal, portal_receiver)
)"""
)
await conn.execute(
"""
INSERT INTO user_portal_v5 ("user", portal, portal_receiver)
SELECT "user", portal, portal_receiver FROM user_portal
"""
)
await conn.execute("DROP TABLE user_portal")
await conn.execute("ALTER TABLE user_portal_v5 RENAME TO user_portal")
75 changes: 0 additions & 75 deletions mautrix_facebook/db/user_contact.py

This file was deleted.

30 changes: 8 additions & 22 deletions mautrix_facebook/db/user_portal.py
Expand Up @@ -32,7 +32,6 @@ class UserPortal:
user: int
portal: int
portal_receiver: int
in_community: bool

@classmethod
def _from_row(cls, row: Record | None) -> UserPortal | None:
Expand All @@ -42,48 +41,35 @@ def _from_row(cls, row: Record | None) -> UserPortal | None:

@classmethod
async def all(cls, user: int) -> dict[int, UserPortal]:
q = (
'SELECT "user", portal, portal_receiver, in_community FROM user_portal '
'WHERE "user"=$1'
)
q = 'SELECT "user", portal, portal_receiver FROM user_portal WHERE "user"=$1'
rows = await cls.db.fetch(q, user)
return {up.portal: up for up in (cls._from_row(row) for row in rows)}

@classmethod
async def get(cls, user: int, portal: int, portal_receiver: int) -> UserPortal | None:
q = (
'SELECT "user", portal, portal_receiver, in_community FROM user_portal '
'SELECT "user", portal, portal_receiver FROM user_portal '
'WHERE "user"=$1 AND portal=$2 AND portal_receiver=$3'
)
row = await cls.db.fetchrow(q, user, portal, portal_receiver)
return cls._from_row(row)

async def insert(self) -> None:
q = (
'INSERT INTO user_portal ("user", portal, portal_receiver, in_community) '
"VALUES ($1, $2, $3, $4)"
)
await self.db.execute(q, self.user, self.portal, self.portal_receiver, self.in_community)
q = 'INSERT INTO user_portal ("user", portal, portal_receiver) ' "VALUES ($1, $2, $3)"
await self.db.execute(q, self.user, self.portal, self.portal_receiver)

async def upsert(self) -> None:
q = (
'INSERT INTO user_portal ("user", portal, portal_receiver, in_community) '
"VALUES ($1, $2, $3, $4) "
'ON CONFLICT ("user", portal, portal_receiver) DO UPDATE SET in_community=$4'
'INSERT INTO user_portal ("user", portal, portal_receiver) '
"VALUES ($1, $2, $3) "
'ON CONFLICT ("user", portal, portal_receiver) DO NOTHING'
)
await self.db.execute(q, self.user, self.portal, self.portal_receiver, self.in_community)
await self.db.execute(q, self.user, self.portal, self.portal_receiver)

async def delete(self) -> None:
q = 'DELETE FROM user_portal WHERE "user"=$1 AND portal=$2 AND portal_receiver=$3'
await self.db.execute(q, self.user)

async def save(self) -> None:
q = (
"UPDATE user_portal SET in_community=$1 "
'WHERE "user"=$2 AND portal=$3 AND portal_receiver=$4'
)
await self.db.execute(q, self.in_community, self.user, self.portal, self.portal_receiver)

@classmethod
async def delete_all(cls, user: int) -> None:
await cls.db.execute('DELETE FROM user_portal WHERE "user"=$1', user)
7 changes: 0 additions & 7 deletions mautrix_facebook/example-config.yaml
Expand Up @@ -104,13 +104,6 @@ bridge:
# Localpart template of MXIDs for Facebook users.
# {userid} is replaced with the user ID of the Facebook user.
username_template: "facebook_{userid}"
# Localpart template for per-user room grouping community IDs.
# The bridge will create these communities and add all of the specific user's portals to the community.
# {localpart} is the MXID localpart and {server} is the MXID server part of the user.
# (Note that, by default, non-admins might not have your homeserver's permission to create
# communities. You should set `enable_group_creation: true` in homeserver.yaml to fix this.)
# `facebook_{localpart}={server}` is a good value.
community_template: null
# Displayname template for Facebook users.
# {displayname} is replaced with the display name of the Facebook user
# as defined below in displayname_preference.
Expand Down
21 changes: 5 additions & 16 deletions mautrix_facebook/portal.py
Expand Up @@ -448,20 +448,11 @@ async def _update_matrix_room(
self.log.warning("Canceling _update_matrix_room as update_info didn't return info")
return

up = await UserPortal.get(source.fbid, self.fbid, self.fb_receiver)
if not up:
in_community = await source._community_helper.add_room(source._community_id, self.mxid)
await UserPortal(
user=source.fbid,
portal=self.fbid,
portal_receiver=self.fb_receiver,
in_community=in_community,
).insert()
elif not up.in_community:
up.in_community = await source._community_helper.add_room(
source._community_id, self.mxid
)
await up.save()
await UserPortal(
user=source.fbid,
portal=self.fbid,
portal_receiver=self.fb_receiver,
).upsert()
await self._sync_read_receipts(info.read_receipts.nodes)

async def _sync_read_receipts(self, receipts: list[graphql.ReadReceipt]) -> None:
Expand Down Expand Up @@ -628,12 +619,10 @@ async def _create_matrix_room(
if not self.is_direct:
await self._update_participants(source, info)

in_community = await source._community_helper.add_room(source._community_id, self.mxid)
await UserPortal(
user=source.fbid,
portal=self.fbid,
portal_receiver=self.fb_receiver,
in_community=in_community,
).upsert()

try:
Expand Down

0 comments on commit 31cac6f

Please sign in to comment.