From f9e0c58f3c0e90b987b7763f020ea5dcbf366779 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 9 Mar 2020 17:41:53 +0100 Subject: Fix revUserId (messenger) and implement search (messenger only for now) --- external/messenger.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'external/messenger.py') diff --git a/external/messenger.py b/external/messenger.py index 9874b5b..6da4808 100755 --- a/external/messenger.py +++ b/external/messenger.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 -# @FIXME: make revUserId work correctly (e.g.: talking to a contact not in your top 20 recent threads will fail) - could we do this with a search request? # @FIXME: store the client pickle in the config automatically -# @FIXME: add a way to search for users and initiate a discussion import sys import json @@ -31,6 +29,7 @@ SET_ROOM_INFO = "set_room_info" JOIN = "join" INVITE = "invite" LEAVE = "leave" +SEARCH = "search" SEND = "send" CLOSE = "close" @@ -47,6 +46,7 @@ CACHE_GET = "cache_get" # ezbr -> external: all must wait for a reply! # external -> ezbr: only CACHE_GET produces a reply REP_OK = "rep_ok" +REP_SEARCH_RESULTS = "rep_search_results" REP_ERROR = "rep_error" # Event types @@ -168,13 +168,14 @@ class MessengerBridge: return self.getUserId(user) def revUserId(self, user_id): - if user_id in self.rev_uid: - return self.rev_uid[user_id] - else: - # TODO this doesn't work always... - # we should try to find the api endpoint that resolves usernames ? - # or do a search request and try to find a user that has that username - return user_id + if user_id not in self.rev_uid: + for user in self.client.searchForUsers(user_id): + self.getUserId(user) + + if user_id not in self.rev_uid: + raise ValueError("User not found: {}".format(user_id)) + + return self.rev_uid[user_id] def getUserShortName(self, user): if user.first_name != None: @@ -277,6 +278,16 @@ class MessengerBridge: uid = self.revUserId(cmd["user"]) self.client.addUsersToGroup([uid], cmd["room"]) + elif ty == SEARCH: + users = self.client.searchForUsers(cmd["data"]) + rep = [] + for user in users: + rep.append({ + "id": self.getUserId(user), + "display_name": user.name, + }) + return {"_type": REP_SEARCH_RESULTS, "data": rep} + elif ty == SEND: event = cmd["data"] if event["type"] in [EVENT_MESSAGE, EVENT_ACTION]: -- cgit v1.2.3