2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 10:10:52 -04:00
|
|
|
#
|
|
|
|
# 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.
|
2014-08-12 22:14:34 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2014-09-17 10:05:09 -04:00
|
|
|
from synapse.api.errors import SynapseError, AuthError, CodeMessageException
|
2015-01-06 09:37:00 -05:00
|
|
|
from synapse.api.constants import EventTypes, Membership
|
2015-01-23 06:47:15 -05:00
|
|
|
from synapse.types import UserID
|
2015-05-12 08:14:29 -04:00
|
|
|
from synapse.util import unwrapFirstError
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
from ._base import BaseHandler
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-12-01 15:53:04 -05:00
|
|
|
def changed_presencelike_data(distributor, user, state):
|
|
|
|
return distributor.fire("changed_presencelike_data", user, state)
|
|
|
|
|
|
|
|
|
|
|
|
def collect_presencelike_data(distributor, user, content):
|
|
|
|
return distributor.fire("collect_presencelike_data", user, content)
|
|
|
|
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
class ProfileHandler(BaseHandler):
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(ProfileHandler, self).__init__(hs)
|
|
|
|
|
2014-08-13 12:12:50 -04:00
|
|
|
self.federation = hs.get_replication_layer()
|
|
|
|
self.federation.register_query_handler(
|
|
|
|
"profile", self.on_profile_query
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
distributor = hs.get_distributor()
|
|
|
|
self.distributor = distributor
|
|
|
|
|
2016-02-15 12:10:40 -05:00
|
|
|
distributor.declare("collect_presencelike_data")
|
|
|
|
distributor.declare("changed_presencelike_data")
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
distributor.observe("registered_user", self.registered_user)
|
|
|
|
|
|
|
|
distributor.observe(
|
|
|
|
"collect_presencelike_data", self.collect_presencelike_data
|
|
|
|
)
|
|
|
|
|
|
|
|
def registered_user(self, user):
|
2014-11-20 11:24:00 -05:00
|
|
|
return self.store.create_profile(user.localpart)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-08-13 12:14:42 -04:00
|
|
|
def get_displayname(self, target_user):
|
2014-12-02 05:42:28 -05:00
|
|
|
if self.hs.is_mine(target_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
displayname = yield self.store.get_profile_displayname(
|
|
|
|
target_user.localpart
|
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue(displayname)
|
2014-08-13 12:14:42 -04:00
|
|
|
else:
|
2014-08-12 10:10:52 -04:00
|
|
|
try:
|
2014-08-13 12:12:50 -04:00
|
|
|
result = yield self.federation.make_query(
|
2014-08-12 10:10:52 -04:00
|
|
|
destination=target_user.domain,
|
2014-08-13 12:12:50 -04:00
|
|
|
query_type="profile",
|
|
|
|
args={
|
|
|
|
"user_id": target_user.to_string(),
|
|
|
|
"field": "displayname",
|
|
|
|
}
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
except CodeMessageException as e:
|
|
|
|
if e.code != 404:
|
|
|
|
logger.exception("Failed to get displayname")
|
|
|
|
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
logger.exception("Failed to get displayname")
|
2014-08-13 12:12:50 -04:00
|
|
|
else:
|
|
|
|
defer.returnValue(result["displayname"])
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def set_displayname(self, target_user, auth_user, new_displayname):
|
|
|
|
"""target_user is the user whose displayname is to be changed;
|
|
|
|
auth_user is the user attempting to make this change."""
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(target_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
|
|
|
|
|
|
|
if target_user != auth_user:
|
|
|
|
raise AuthError(400, "Cannot set another user's displayname")
|
|
|
|
|
2015-05-14 09:19:10 -04:00
|
|
|
if new_displayname == '':
|
|
|
|
new_displayname = None
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
yield self.store.set_profile_displayname(
|
|
|
|
target_user.localpart, new_displayname
|
|
|
|
)
|
|
|
|
|
2015-12-01 15:53:04 -05:00
|
|
|
yield changed_presencelike_data(self.distributor, target_user, {
|
|
|
|
"displayname": new_displayname,
|
|
|
|
})
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-09-17 10:05:09 -04:00
|
|
|
yield self._update_join_states(target_user)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
@defer.inlineCallbacks
|
2014-08-13 12:14:42 -04:00
|
|
|
def get_avatar_url(self, target_user):
|
2014-12-02 05:42:28 -05:00
|
|
|
if self.hs.is_mine(target_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
avatar_url = yield self.store.get_profile_avatar_url(
|
|
|
|
target_user.localpart
|
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue(avatar_url)
|
2014-08-13 12:14:42 -04:00
|
|
|
else:
|
2014-08-12 10:10:52 -04:00
|
|
|
try:
|
2014-08-13 12:12:50 -04:00
|
|
|
result = yield self.federation.make_query(
|
|
|
|
destination=target_user.domain,
|
|
|
|
query_type="profile",
|
|
|
|
args={
|
|
|
|
"user_id": target_user.to_string(),
|
|
|
|
"field": "avatar_url",
|
|
|
|
}
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
except CodeMessageException as e:
|
|
|
|
if e.code != 404:
|
|
|
|
logger.exception("Failed to get avatar_url")
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
logger.exception("Failed to get avatar_url")
|
|
|
|
|
|
|
|
defer.returnValue(result["avatar_url"])
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def set_avatar_url(self, target_user, auth_user, new_avatar_url):
|
|
|
|
"""target_user is the user whose avatar_url is to be changed;
|
|
|
|
auth_user is the user attempting to make this change."""
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(target_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
|
|
|
|
|
|
|
if target_user != auth_user:
|
|
|
|
raise AuthError(400, "Cannot set another user's avatar_url")
|
|
|
|
|
|
|
|
yield self.store.set_profile_avatar_url(
|
|
|
|
target_user.localpart, new_avatar_url
|
|
|
|
)
|
|
|
|
|
2015-12-01 15:53:04 -05:00
|
|
|
yield changed_presencelike_data(self.distributor, target_user, {
|
|
|
|
"avatar_url": new_avatar_url,
|
|
|
|
})
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-09-17 10:05:09 -04:00
|
|
|
yield self._update_join_states(target_user)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def collect_presencelike_data(self, user, state):
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(user):
|
2014-08-12 10:10:52 -04:00
|
|
|
defer.returnValue(None)
|
|
|
|
|
2015-05-08 11:32:18 -04:00
|
|
|
(displayname, avatar_url) = yield defer.gatherResults(
|
|
|
|
[
|
|
|
|
self.store.get_profile_displayname(user.localpart),
|
|
|
|
self.store.get_profile_avatar_url(user.localpart),
|
|
|
|
],
|
|
|
|
consumeErrors=True
|
2015-05-12 08:14:29 -04:00
|
|
|
).addErrback(unwrapFirstError)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2016-02-12 11:17:24 -05:00
|
|
|
state["displayname"] = displayname
|
|
|
|
state["avatar_url"] = avatar_url
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
defer.returnValue(None)
|
2014-08-13 12:12:50 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_profile_query(self, args):
|
2015-01-23 06:47:15 -05:00
|
|
|
user = UserID.from_string(args["user_id"])
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(user):
|
2014-08-13 12:12:50 -04:00
|
|
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
|
|
|
|
|
|
|
just_field = args.get("field", None)
|
|
|
|
|
|
|
|
response = {}
|
|
|
|
|
|
|
|
if just_field is None or just_field == "displayname":
|
|
|
|
response["displayname"] = yield self.store.get_profile_displayname(
|
|
|
|
user.localpart
|
|
|
|
)
|
|
|
|
|
|
|
|
if just_field is None or just_field == "avatar_url":
|
|
|
|
response["avatar_url"] = yield self.store.get_profile_avatar_url(
|
|
|
|
user.localpart
|
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue(response)
|
2014-09-17 10:05:09 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _update_join_states(self, user):
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(user):
|
2014-09-17 10:05:09 -04:00
|
|
|
return
|
|
|
|
|
2014-12-19 12:36:33 -05:00
|
|
|
self.ratelimit(user.to_string())
|
|
|
|
|
2015-03-09 12:43:09 -04:00
|
|
|
joins = yield self.store.get_rooms_for_user(
|
2014-09-17 10:05:09 -04:00
|
|
|
user.to_string(),
|
|
|
|
)
|
|
|
|
|
|
|
|
for j in joins:
|
|
|
|
content = {
|
2015-01-06 09:37:00 -05:00
|
|
|
"membership": Membership.JOIN,
|
2014-09-17 10:05:09 -04:00
|
|
|
}
|
|
|
|
|
2015-12-01 15:53:04 -05:00
|
|
|
yield collect_presencelike_data(self.distributor, user, content)
|
2014-09-17 10:05:09 -04:00
|
|
|
|
2014-12-04 10:50:01 -05:00
|
|
|
msg_handler = self.hs.get_handlers().message_handler
|
2015-03-06 11:24:05 -05:00
|
|
|
try:
|
|
|
|
yield msg_handler.create_and_send_event({
|
|
|
|
"type": EventTypes.Member,
|
|
|
|
"room_id": j.room_id,
|
|
|
|
"state_key": user.to_string(),
|
|
|
|
"content": content,
|
|
|
|
"sender": user.to_string()
|
|
|
|
}, ratelimit=False)
|
|
|
|
except Exception as e:
|
|
|
|
logger.warn(
|
|
|
|
"Failed to update join event for room %s - %s",
|
|
|
|
j.room_id, str(e.message)
|
|
|
|
)
|