From 43f82df65a1c2cfdb2c1188350d94731ec5843dd Mon Sep 17 00:00:00 2001 From: Yessiest Date: Thu, 11 May 2023 13:06:28 +0400 Subject: [PATCH] added usercache search endpoint --- client.rb | 31 +++++++++++++++++++++++++++---- proto.rb | 17 ++++++++++++++++- server.rb | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/client.rb b/client.rb index 9781b2b..a92bb46 100755 --- a/client.rb +++ b/client.rb @@ -38,7 +38,7 @@ Thread.new do next end messages["messages"].each { |x| - puts "#{x["from"]} says: #{x["content"]}" + puts "#{x["user"]["username"]} says: #{x["content"]}" } end end @@ -57,13 +57,39 @@ while buf = Readline.readline("> ", true) puts "/help - this message" puts "/send - direct messages to somebody" puts "/exit - quit program" + puts "/find - find a username by pattern" + puts "/find-protoid - find by a protocol id" + next end + if buf == "/exit" then post("/user/delete",{ "protocol_id"=> "heimdall-"+nickname }) exit end + + if buf.match(/^\/send .*$/) then + target = buf.match(/^\/send ([^\s]*)$/)[1] + next + end + + if buf.match(/^\/find .*$/) then + uname = (buf.match /^\/find (.*)$/)[1] + users = get("/user/find/by-username?username=#{uname}")["results"] + puts "Found #{users.length} results: " + users.each { |x| puts x[0] } + next + end + + if buf.match(/^\/find-protoid .*$/) then + pid = (buf.match /^\/find-protoid (.*)$/)[1] + users = get("/user/find/by-protoid?protocol_id=#{pid}")["results"] + puts "Found #{users.length} results: " + users.each { |x| puts x[0] } + next + end + if target then post("/user/send", { "to" => target, @@ -71,7 +97,4 @@ while buf = Readline.readline("> ", true) "from" => "heimdall-"+nickname }) end - if buf.match(/^\/send .*$/) then - target = buf.match(/^\/send ([^\s]*)$/)[1] - end end diff --git a/proto.rb b/proto.rb index af50e41..5d75cdb 100644 --- a/proto.rb +++ b/proto.rb @@ -1,7 +1,7 @@ UIDS = {} module Heimdall - VERSION = "0.1 alpha" + VERSION = "0.4 alpha" attr_reader :VERSION class ProtocolError < StandardError @@ -31,6 +31,15 @@ module Heimdall raise ProtocolError, "user not found" if not @users[protoid] return @users[protoid] end + def search(name,n,&block) + @users.select(&block).take(n) + end + def search_by_username(name, n = 10) + search(name,n) { |k,v| v.username.match? name } + end + def search_by_protoid(name,n = 10) + search(name,n) { |k,v| k.match? name } + end def delete(protoid) @users.delete protoid end @@ -44,6 +53,12 @@ module Heimdall @direct = DirectChannel.new super() end + def to_card() + return { + "username" => @username, + "protoid" => @protoid + } + end attr_reader :username attr_reader :protoid attr_reader :direct diff --git a/server.rb b/server.rb index b4fa739..b505dde 100644 --- a/server.rb +++ b/server.rb @@ -39,6 +39,38 @@ server = Hyde::Server.new Port: 8000 do puts ctx.request.body.inspect end + path "find" do + index ["by-username"] + + get "by-username" do |ctx| + req,res = ctx.request,ctx.response + begin + _require_keys(req.query, { + username: String + }) + _send_json(res, { + "results": Users.search_by_username(req.query['username']) + }) + rescue KeyError => keyerror + _throw_error(res,keyerror) + end + end + + get "by-protoid" do |ctx| + req,res = ctx.request,ctx.response + begin + _require_keys(req.query, { + protocol_id: String + }) + _send_json(res, { + "results": Users.search_by_protoid(req.query['protocol_id']) + }) + rescue KeyError => keyerror + _throw_error(res,keyerror) + end + end + end + get "exists" do |ctx| req,res = ctx.request,ctx.response begin @@ -112,7 +144,7 @@ server = Hyde::Server.new Port: 8000 do _send_json(res, { messages: messages.map { |x| x = x.to_struct - x["username"] = Users.get(x["from"]).username + x["user"] = Users.get(x["from"]).to_card x } }) @@ -133,7 +165,7 @@ server = Hyde::Server.new Port: 8000 do _send_json(res, { messages: messages.map { |x| x = x.to_struct - x["username"] = Users.get(x["from"]).username + x["user"] = Users.get(x["from"]).to_card x } })