added usercache search endpoint

This commit is contained in:
Yessiest 2023-05-11 13:06:28 +04:00
parent 6e459c36bc
commit 43f82df65a
3 changed files with 77 additions and 7 deletions

View File

@ -38,7 +38,7 @@ Thread.new do
next next
end end
messages["messages"].each { |x| messages["messages"].each { |x|
puts "#{x["from"]} says: #{x["content"]}" puts "#{x["user"]["username"]} says: #{x["content"]}"
} }
end end
end end
@ -57,13 +57,39 @@ while buf = Readline.readline("> ", true)
puts "/help - this message" puts "/help - this message"
puts "/send <protcol_id> - direct messages to somebody" puts "/send <protcol_id> - direct messages to somebody"
puts "/exit - quit program" puts "/exit - quit program"
puts "/find <username> - find a username by pattern"
puts "/find-protoid <protoid> - find by a protocol id"
next
end end
if buf == "/exit" then if buf == "/exit" then
post("/user/delete",{ post("/user/delete",{
"protocol_id"=> "heimdall-"+nickname "protocol_id"=> "heimdall-"+nickname
}) })
exit exit
end 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 if target then
post("/user/send", { post("/user/send", {
"to" => target, "to" => target,
@ -71,7 +97,4 @@ while buf = Readline.readline("> ", true)
"from" => "heimdall-"+nickname "from" => "heimdall-"+nickname
}) })
end end
if buf.match(/^\/send .*$/) then
target = buf.match(/^\/send ([^\s]*)$/)[1]
end
end end

View File

@ -1,7 +1,7 @@
UIDS = {} UIDS = {}
module Heimdall module Heimdall
VERSION = "0.1 alpha" VERSION = "0.4 alpha"
attr_reader :VERSION attr_reader :VERSION
class ProtocolError < StandardError class ProtocolError < StandardError
@ -31,6 +31,15 @@ module Heimdall
raise ProtocolError, "user not found" if not @users[protoid] raise ProtocolError, "user not found" if not @users[protoid]
return @users[protoid] return @users[protoid]
end 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) def delete(protoid)
@users.delete protoid @users.delete protoid
end end
@ -44,6 +53,12 @@ module Heimdall
@direct = DirectChannel.new @direct = DirectChannel.new
super() super()
end end
def to_card()
return {
"username" => @username,
"protoid" => @protoid
}
end
attr_reader :username attr_reader :username
attr_reader :protoid attr_reader :protoid
attr_reader :direct attr_reader :direct

View File

@ -39,6 +39,38 @@ server = Hyde::Server.new Port: 8000 do
puts ctx.request.body.inspect puts ctx.request.body.inspect
end 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| get "exists" do |ctx|
req,res = ctx.request,ctx.response req,res = ctx.request,ctx.response
begin begin
@ -112,7 +144,7 @@ server = Hyde::Server.new Port: 8000 do
_send_json(res, { _send_json(res, {
messages: messages.map { |x| messages: messages.map { |x|
x = x.to_struct x = x.to_struct
x["username"] = Users.get(x["from"]).username x["user"] = Users.get(x["from"]).to_card
x x
} }
}) })
@ -133,7 +165,7 @@ server = Hyde::Server.new Port: 8000 do
_send_json(res, { _send_json(res, {
messages: messages.map { |x| messages: messages.map { |x|
x = x.to_struct x = x.to_struct
x["username"] = Users.get(x["from"]).username x["user"] = Users.get(x["from"]).to_card
x x
} }
}) })