mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2024-12-24 03:09:24 -05:00
307 lines
9.9 KiB
Python
307 lines
9.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright 2017 Vector Creations Ltd
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from twisted.internet import defer
|
|
|
|
from ._base import SQLBaseStore
|
|
|
|
import ujson as json
|
|
|
|
|
|
class GroupServerStore(SQLBaseStore):
|
|
def get_group(self, group_id):
|
|
return self._simple_select_one(
|
|
table="groups",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
},
|
|
retcols=("name", "short_description", "long_description", "avatar_url",),
|
|
allow_none=True,
|
|
desc="is_user_in_group",
|
|
)
|
|
|
|
def get_users_in_group(self, group_id, include_private=False):
|
|
# TODO: Pagination
|
|
|
|
keyvalues = {
|
|
"group_id": group_id,
|
|
}
|
|
if not include_private:
|
|
keyvalues["is_public"] = True
|
|
|
|
return self._simple_select_list(
|
|
table="group_users",
|
|
keyvalues=keyvalues,
|
|
retcols=("user_id", "is_public",),
|
|
desc="get_users_in_group",
|
|
)
|
|
|
|
def get_rooms_in_group(self, group_id, include_private=False):
|
|
# TODO: Pagination
|
|
|
|
keyvalues = {
|
|
"group_id": group_id,
|
|
}
|
|
if not include_private:
|
|
keyvalues["is_public"] = True
|
|
|
|
return self._simple_select_list(
|
|
table="group_rooms",
|
|
keyvalues=keyvalues,
|
|
retcols=("room_id", "is_public",),
|
|
desc="get_rooms_in_group",
|
|
)
|
|
|
|
def is_user_in_group(self, user_id, group_id):
|
|
return self._simple_select_one_onecol(
|
|
table="group_users",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
retcol="user_id",
|
|
allow_none=True,
|
|
desc="is_user_in_group",
|
|
).addCallback(lambda r: bool(r))
|
|
|
|
def is_user_admin_in_group(self, group_id, user_id):
|
|
return self._simple_select_one_onecol(
|
|
table="group_users",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
retcol="is_admin",
|
|
allow_none=True,
|
|
desc="is_user_adim_in_group",
|
|
)
|
|
|
|
def add_group_invite(self, group_id, user_id):
|
|
"""Record that the group server has invited a user
|
|
"""
|
|
return self._simple_insert(
|
|
table="group_invites",
|
|
values={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
desc="add_group_invite",
|
|
)
|
|
|
|
def is_user_invited_to_local_group(self, group_id, user_id):
|
|
"""Has the group server invited a user?
|
|
"""
|
|
return self._simple_select_one_onecol(
|
|
table="group_invites",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
retcol="user_id",
|
|
desc="is_user_invited_to_local_group",
|
|
allow_none=True,
|
|
)
|
|
|
|
def add_user_to_group(self, group_id, user_id, is_admin=False, is_public=True,
|
|
local_attestation=None, remote_attestation=None):
|
|
"""Add a user to the group server.
|
|
|
|
Args:
|
|
group_id (str)
|
|
user_id (str)
|
|
is_admin (bool)
|
|
is_public (bool)
|
|
local_attestation (dict): The attestation the GS created to give
|
|
to the remote server. Optional if the user and group are on the
|
|
same server
|
|
remote_attestation (dict): The attestation given to GS by remote
|
|
server. Optional if the user and group are on the same server
|
|
"""
|
|
def _add_user_to_group_txn(txn):
|
|
self._simple_insert_txn(
|
|
txn,
|
|
table="group_users",
|
|
values={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
"is_admin": is_admin,
|
|
"is_public": is_public,
|
|
},
|
|
)
|
|
|
|
self._simple_delete_txn(
|
|
txn,
|
|
table="group_invites",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
)
|
|
|
|
if local_attestation:
|
|
self._simple_insert_txn(
|
|
txn,
|
|
table="group_attestations_renewals",
|
|
values={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
"valid_until_ms": local_attestation["valid_until_ms"],
|
|
},
|
|
)
|
|
if remote_attestation:
|
|
self._simple_insert_txn(
|
|
txn,
|
|
table="group_attestations_remote",
|
|
values={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
"valid_until_ms": remote_attestation["valid_until_ms"],
|
|
"attestation_json": json.dumps(remote_attestation),
|
|
},
|
|
)
|
|
|
|
return self.runInteraction(
|
|
"add_user_to_group", _add_user_to_group_txn
|
|
)
|
|
|
|
def remove_user_from_group(self, group_id, user_id):
|
|
def _remove_user_from_group_txn(txn):
|
|
self._simple_delete_txn(
|
|
txn,
|
|
table="group_users",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
)
|
|
self._simple_delete_txn(
|
|
txn,
|
|
table="group_invites",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
)
|
|
self._simple_delete_txn(
|
|
txn,
|
|
table="group_attestations_renewals",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
)
|
|
self._simple_delete_txn(
|
|
txn,
|
|
table="group_attestations_remote",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
)
|
|
return self.runInteraction("remove_user_from_group", _remove_user_from_group_txn)
|
|
|
|
def add_room_to_group(self, group_id, room_id, is_public):
|
|
return self._simple_insert(
|
|
table="group_rooms",
|
|
values={
|
|
"group_id": group_id,
|
|
"room_id": room_id,
|
|
"is_public": is_public,
|
|
},
|
|
desc="add_room_to_group",
|
|
)
|
|
|
|
@defer.inlineCallbacks
|
|
def create_group(self, group_id, user_id, name, avatar_url, short_description,
|
|
long_description,):
|
|
yield self._simple_insert(
|
|
table="groups",
|
|
values={
|
|
"group_id": group_id,
|
|
"name": name,
|
|
"avatar_url": avatar_url,
|
|
"short_description": short_description,
|
|
"long_description": long_description,
|
|
},
|
|
desc="create_group",
|
|
)
|
|
|
|
def get_attestations_need_renewals(self, valid_until_ms):
|
|
"""Get all attestations that need to be renewed until givent time
|
|
"""
|
|
def _get_attestations_need_renewals_txn(txn):
|
|
sql = """
|
|
SELECT group_id, user_id FROM group_attestations_renewals
|
|
WHERE valid_until_ms <= ?
|
|
"""
|
|
txn.execute(sql, (valid_until_ms,))
|
|
return self.cursor_to_dict(txn)
|
|
return self.runInteraction(
|
|
"get_attestations_need_renewals", _get_attestations_need_renewals_txn
|
|
)
|
|
|
|
def update_attestation_renewal(self, group_id, user_id, attestation):
|
|
"""Update an attestation that we have renewed
|
|
"""
|
|
return self._simple_update_one(
|
|
table="group_attestations_renewals",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
updatevalues={
|
|
"valid_until_ms": attestation["valid_until_ms"],
|
|
},
|
|
desc="update_attestation_renewal",
|
|
)
|
|
|
|
def update_remote_attestion(self, group_id, user_id, attestation):
|
|
"""Update an attestation that a remote has renewed
|
|
"""
|
|
return self._simple_update_one(
|
|
table="group_attestations_remote",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
updatevalues={
|
|
"valid_until_ms": attestation["valid_until_ms"],
|
|
"attestation_json": json.dumps(attestation)
|
|
},
|
|
desc="update_remote_attestion",
|
|
)
|
|
|
|
@defer.inlineCallbacks
|
|
def get_remote_attestation(self, group_id, user_id):
|
|
"""Get the attestation that proves the remote agrees that the user is
|
|
in the group.
|
|
"""
|
|
row = yield self._simple_select_one(
|
|
table="group_attestations_remote",
|
|
keyvalues={
|
|
"group_id": group_id,
|
|
"user_id": user_id,
|
|
},
|
|
retcols=("valid_until_ms", "attestation_json"),
|
|
desc="get_remote_attestation",
|
|
allow_none=True,
|
|
)
|
|
|
|
now = int(self._clock.time_msec())
|
|
if row and now < row["valid_until_ms"]:
|
|
defer.returnValue(json.loads(row["attestation_json"]))
|
|
|
|
defer.returnValue(None)
|