summary refs log tree commit diff
path: root/synapse/storage/profile.py
blob: a432b1eaabd4354cfebdbbb697ebd49a09f91448 (plain) (blame)
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
# -*- coding: utf-8 -*-
# Copyright 2014-2016 OpenMarket 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


class ProfileStore(SQLBaseStore):
    def create_profile(self, user_localpart):
        return defer.succeed(None)

    @defer.inlineCallbacks
    def get_profile_displayname(self, user_id):
        profile = yield self.get_profile_key(
            user_id, "default", "m.display_name"
        )

        if profile:
            try:
                display_name = profile["rows"][0]["display_name"]
            except (KeyError, IndexError):
                display_name = None
        else:
            display_name = None

        defer.returnValue(display_name)

    def set_profile_displayname(self, user_id, new_displayname):
        if new_displayname:
            content = {"rows": [{
                "display_name": new_displayname
            }]}
        else:
            # TODO: Delete in this case
            content = {}

        return self.update_profile_key(
            user_id, "default", "m.display_name", content
        )

    @defer.inlineCallbacks
    def get_profile_avatar_url(self, user_id):
        profile = yield self.get_profile_key(
            user_id, "default", "m.avatar_url"
        )

        if profile:
            try:
                avatar_url = profile["rows"][0]["avatar_url"]
            except (KeyError, IndexError):
                avatar_url = None
        else:
            avatar_url = None

        defer.returnValue(avatar_url)

    def set_profile_avatar_url(self, user_id, new_avatar_url):
        if new_avatar_url:
            content = {"rows": [{
                "avatar_url": new_avatar_url
            }]}
        else:
            # TODO: Delete in this case
            content = {}

        return self.update_profile_key(
            user_id, "default", "m.avatar_url", content
        )

    @defer.inlineCallbacks
    def get_full_profile(self, user_id):
        rows = yield self._simple_select_list(
            table="profiles_extended",
            keyvalues={"user_id": user_id},
            retcols=("persona", "key", "content",),
        )

        personas = {}
        profile = {"personas": personas}
        for row in rows:
            content = ujson.loads(row["content"])
            personas.setdefault(
                row["persona"], {"rows": {}}
            )["rows"][row["key"]] = content

        defer.returnValue(profile)

    @defer.inlineCallbacks
    def get_persona_profile(self, user_id, persona):
        rows = yield self._simple_select_list(
            table="profiles_extended",
            keyvalues={
                "user_id": user_id,
                "persona": persona,
            },
            retcols=("key", "content",),
        )

        persona = {"properties": {
            row["key"]: ujson.loads(row["content"])
            for row in rows
        }}

        defer.returnValue(persona)

    @defer.inlineCallbacks
    def get_profile_key(self, user_id, persona, key):
        content_json = yield self._simple_select_one_onecol(
            table="profiles_extended",
            keyvalues={
                "user_id": user_id,
                "persona": persona,
                "key": key,
            },
            retcol="content",
            allow_none=True,
        )

        if content_json:
            content = ujson.loads(content_json)
        else:
            content = None

        defer.returnValue(content)

    def update_profile_key(self, user_id, persona, key, content):
        content_json = ujson.dumps(content)

        def _update_profile_key_txn(txn, stream_id):
            self._simple_delete_txn(
                txn,
                table="profiles_extended",
                keyvalues={
                    "user_id": user_id,
                    "persona": persona,
                    "key": key,
                }
            )

            self._simple_insert_txn(
                txn,
                table="profiles_extended",
                values={
                    "stream_id": stream_id,
                    "user_id": user_id,
                    "persona": persona,
                    "key": key,
                    "content": content_json,
                }
            )

        with self._profiles_id_gen.get_next() as stream_id:
            return self.runInteraction(
                "update_profile_key", _update_profile_key_txn,
                stream_id,
            )