From 611b9aa100d1135f04cde572115a5ea1f680ad72 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sun, 14 Jul 2024 15:13:20 +0200 Subject: Add user search. --- server.js | 38 +++++++++++++++++++++++++++++++++++--- views/contacts.pug | 4 ++++ views/search_user.pug | 31 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 views/search_user.pug diff --git a/server.js b/server.js index ec8b96d..ed931be 100644 --- a/server.js +++ b/server.js @@ -320,6 +320,7 @@ const SQL_SELECT_LOGIN_BY_NAME = SQL("SELECT * FROM user_login_view WHERE name=? const SQL_SELECT_USER_PROFILE = SQL("SELECT * FROM user_profile_view WHERE name=?") const SQL_SELECT_USER_DYNAMIC = SQL("select * from user_dynamic_view where user_id=?") const SQL_SELECT_USER_ID = SQL("SELECT user_id FROM users WHERE name=?").pluck() +const SQL_SELECT_USER_BY_SEARCH = SQL("select name, atime from users left join user_last_seen using(user_id) where name like ? order by name") const SQL_SELECT_USER_NOTIFY = SQL("SELECT notify FROM users WHERE user_id=?").pluck() const SQL_SELECT_USER_VERIFIED = SQL("SELECT is_verified FROM users WHERE user_id=?").pluck() @@ -724,7 +725,10 @@ app.get("/contacts/remove/:who_name", must_be_logged_in, function (req, res) { if (!who) return res.status(404).send("User not found.") SQL_DELETE_CONTACT.run(req.user.user_id, who.user_id) - return res.redirect("/user/" + who.name) + if (req.headers.referer) + return res.redirect(req.headers.referer) + else + return res.redirect("/user/" + who.name) }) app.get("/contacts/add-friend/:who_name", must_be_logged_in, function (req, res) { @@ -732,7 +736,32 @@ app.get("/contacts/add-friend/:who_name", must_be_logged_in, function (req, res) if (!who) return res.status(404).send("User not found.") SQL_INSERT_CONTACT.run(req.user.user_id, who.user_id, 1) - return res.redirect("/user/" + who.name) + if (req.headers.referer) + return res.redirect(req.headers.referer) + else + return res.redirect("/user/" + who.name) +}) + +app.get("/contacts/search", must_be_logged_in, function (req, res) { + let q = req.query.q + if (q && q.length > 0) { + if (!q.includes("%")) + q = "%" + q + "%" + let results = SQL_SELECT_USER_BY_SEARCH.all(q) + for (let item of results) + item.atime = human_date(item.atime) + res.render("search_user.pug", { + user: req.user, + search: req.query.q, + results + }) + } else { + res.render("search_user.pug", { + user: req.user, + search: null, + results: null, + }) + } }) app.get("/contacts/add-enemy/:who_name", must_be_logged_in, function (req, res) { @@ -740,7 +769,10 @@ app.get("/contacts/add-enemy/:who_name", must_be_logged_in, function (req, res) if (!who) return res.status(404).send("User not found.") SQL_INSERT_CONTACT.run(req.user.user_id, who.user_id, -1) - return res.redirect("/user/" + who.name) + if (req.headers.referer) + return res.redirect(req.headers.referer) + else + return res.redirect("/user/" + who.name) }) /* diff --git a/views/contacts.pug b/views/contacts.pug index 8b42e73..a3ca8d0 100644 --- a/views/contacts.pug +++ b/views/contacts.pug @@ -51,3 +51,7 @@ html else tr td Nobody + + p + form(method="get" action="/contacts/search") + input(type="text" name="q" size=40 placeholder="Find user..." required) diff --git a/views/search_user.pug b/views/search_user.pug new file mode 100644 index 0000000..bc67cf6 --- /dev/null +++ b/views/search_user.pug @@ -0,0 +1,31 @@ +//- vim:ts=4:sw=4: +doctype html +html + head + include head + title User Search Results + body + include header + article + + h1 User Search Results + + if results && search + if results.length > 0 + table.half.striped + thead + tr + th Name + th Last seen + tbody + each who in results + tr + td + a.black(href="/user/"+who.name)= who.name + td.w= who.atime + else + p.error Nobody found matching "#{search}". + + p + form(method="get" action="/contacts/search") + input(type="text" name="q" size=40 placeholder="Find user..." value=search required) -- cgit v1.2.3