1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# -*- coding: utf-8 -*-
# Copyright 2014 matrix.org
#
# 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 synapse.types import UserID
from synapse.api.constants import Membership
from synapse.api.events.room import RoomMemberEvent
from ._base import SQLBaseStore, Table
import collections
import json
import logging
logger = logging.getLogger(__name__)
class RoomMemberStore(SQLBaseStore):
def get_room_member(self, user_id, room_id):
"""Retrieve the current state of a room member.
Args:
user_id (str): The member's user ID.
room_id (str): The room the member is in.
Returns:
namedtuple: The room member from the database, or None if this
member does not exist.
"""
query = RoomMemberTable.select_statement(
"room_id = ? AND user_id = ? ORDER BY id DESC LIMIT 1")
return self._execute(
RoomMemberTable.decode_single_result,
query, room_id, user_id,
)
def store_room_member(self, user_id, sender, room_id, membership, content):
"""Store a room member in the database.
Args:
user_id (str): The member's user ID.
room_id (str): The room in relation to the member.
membership (synapse.api.constants.Membership): The new membership
state.
content (dict): The content of the membership (JSON).
"""
content_json = json.dumps(content)
return self._simple_insert(RoomMemberTable.table_name, dict(
user_id=user_id,
sender=sender,
room_id=room_id,
membership=membership,
content=content_json,
))
@defer.inlineCallbacks
def get_room_members(self, room_id, membership=None):
"""Retrieve the current room member list for a room.
Args:
room_id (str): The room to get the list of members.
membership (synapse.api.constants.Membership): The filter to apply
to this list, or None to return all members with some state
associated with this room.
Returns:
list of namedtuples representing the members in this room.
"""
query = RoomMemberTable.select_statement(
"id IN (SELECT MAX(id) FROM " + RoomMemberTable.table_name
+ " WHERE room_id = ? GROUP BY user_id)"
)
res = yield self._execute(
RoomMemberTable.decode_results, query, room_id,
)
# strip memberships which don't match
if membership:
res = [entry for entry in res if entry.membership == membership]
defer.returnValue(res)
def get_rooms_for_user_where_membership_is(self, user_id, membership_list):
""" Get all the rooms for this user where the membership for this user
matches one in the membership list.
Args:
user_id (str): The user ID.
membership_list (list): A list of synapse.api.constants.Membership
values which the user must be in.
Returns:
A list of dicts with "room_id" and "membership" keys.
"""
if not membership_list:
return defer.succeed(None)
args = [user_id]
membership_placeholder = ["membership=?"] * len(membership_list)
where_membership = "(" + " OR ".join(membership_placeholder) + ")"
for membership in membership_list:
args.append(membership)
query = ("SELECT room_id, membership FROM room_memberships"
+ " WHERE user_id=? AND " + where_membership
+ " GROUP BY room_id ORDER BY id DESC")
return self._execute(
self.cursor_to_dict, query, *args
)
@defer.inlineCallbacks
def get_joined_hosts_for_room(self, room_id):
query = RoomMemberTable.select_statement(
"id IN (SELECT MAX(id) FROM " + RoomMemberTable.table_name
+ " WHERE room_id = ? GROUP BY user_id)"
)
res = yield self._execute(
RoomMemberTable.decode_results, query, room_id,
)
def host_from_user_id_string(user_id):
domain = UserID.from_string(entry.user_id, self.hs).domain
return domain
# strip memberships which don't match
hosts = [
host_from_user_id_string(entry.user_id)
for entry in res
if entry.membership == Membership.JOIN
]
logger.debug("Returning hosts: %s from results: %s", hosts, res)
defer.returnValue(hosts)
def get_max_room_member_id(self):
return self._simple_max_id(RoomMemberTable.table_name)
class RoomMemberTable(Table):
table_name = "room_memberships"
fields = [
"id",
"user_id",
"sender",
"room_id",
"membership",
"content"
]
class EntryType(collections.namedtuple("RoomMemberEntry", fields)):
def as_event(self, event_factory):
return event_factory.create_event(
etype=RoomMemberEvent.TYPE,
room_id=self.room_id,
target_user_id=self.user_id,
user_id=self.sender,
content=json.loads(self.content),
)
|