def selectUserToChatWith(self): resp = self.stub.CheckRequest(users_pb2.User(username=self.uname)) if resp.username != "NONE": tempInput = input("[SPARTAN] {} is requesting to chat with you. Enter 'yes' to accept or different user: "******"yes": anotherUser = resp.username else: anotherUser = tempInput else: anotherUser = input("[SPARTAN] Enter user whom you want to chat with: ") anotherUser.lower() if anotherUser != self.uname: request = users_pb2.OneOnOneChat( user1 = users_pb2.User(username=self.uname), user2 = users_pb2.User(username=anotherUser) ) response = self.stub.ConnectWithUser(request) if not response.isAvailable: print("[SPARTAN] Cannot find the user you specified. Please try again.") return self.selectUserToChatWith() else: print("[SPARTAN] You are now ready to chat with {}.".format(anotherUser)) self.conversation = self.uname + '-' + anotherUser return True else: print("[SPARTAN] Cannot chat with yourself. Please try again") return self.selectUserToChatWith()
def Update(self, request, context): user = [user for user in users if user['id'] == request.id][0] user['id'] = request.id user['name'] = request.name user['address'] = request.address user['age'] = request.age return pb2.User(**user)
def CreateUser(self, request, context): """ This function is called whenever a new user registers for chat. """ # Create user object of the request user = users_pb2.User(username=request.user) if user not in self.registeredUsers: return users_pb2.RegistrationResponse(isAvailable = False) return users_pb2.RegistrationResponse(isAvailable = True)
def Insert(self, request, context): user = request user.id = str(uuid.uuid4()) users.append(user) return pb2.User( id=user.id, name=user.name, age=user.age, address=user.address )
def ConnectWithUser (self,request, context): conversing_users = [request.user1.username, request.user2.username] self.userRequesting[request.user2.username] = users_pb2.User(username=request.user1.username) conversing_users.sort() new_key = conversing_users[0] + "-" + conversing_users[1] for online_user in self.registeredUsers: if (online_user.username == request.user2.username): if new_key not in self.message_list: self.message_list[new_key] = lrucache.Cache(LRU_CACHE_SIZE) return users_pb2.RegistrationResponse(isAvailable=True) return users_pb2.RegistrationResponse(isAvailable=False)
def __init__(self): """ This is default functions which initializes few variables on when called. """ self.message_list = {} self.user = '' self.registeredUsers = [] self.userRequesting = {} self.unreadMessageCount = {} # dict of dict. {key(user), {key(otherUser in coversation), count of unread msgs}} self.number_of_messages_sent = {} for usr in DATA['users']: self.registeredUsers.append(users_pb2.User(username=usr)) self.unreadMessageCount[usr] = {} self.number_of_messages_sent[usr] = []
def create_user(self, phone_number, firstname, lastname): user = message.User(phone_number=phone_number, firstname=firstname, lastname=lastname) req = message.UserReq(user=user) return self.stub.CreateUser(req)
}, ] users_grpc = [] for user in users: payments = [] for payment in user['payments']: payments.append(users_pb2.Payment( created=payment['created'], amount=payment['amount'], )) users_grpc.append(users_pb2.User( id=user['id'], name=user['name'], surname=user['surname'], phone=user['phone'], created=user['created'], active=user['active'], lat=user['lat'], lng=user['lng'], country=users_pb2.Country( name=user['country']['name'], currency=user['country']['currency'], code=user['country']['code'], ), payments=payments, )) users_reply = users_pb2.UsersReply(total=len(users_grpc), users=users_grpc)
def Listen_for_messages(self): for msg in self.stub.StreamMessages(users_pb2.User(username=self.conversation)): print('[{}] > {}'.format(msg.sender, self.decryptMessage(msg.content)))
def insert_user(stub, first_name, last_name): user = users_pb2.User(first_name=first_name, last_name=last_name) response = stub.AddUser(user) print(f"Inserted user:\n{textwrap.indent(str(response), ' ')}")
def CheckRequest (self, request, context): if request.username in self.userRequesting: return self.userRequesting[request.username] else: return users_pb2.User(username="******")