Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add basic search functionality

  • Loading branch information...
commit e374b399572e56e0445643bf8a55a216a17392b5 1 parent 7c167c8
@brianshaler brianshaler authored
View
172 controllers/SearchController.js
@@ -0,0 +1,172 @@
+/**
+ * Search Controller
+ **/
+
+var mongoose = require('mongoose'),
+ Identity = mongoose.model('Identity'),
+ Topic = mongoose.model('Topic'),
+ Characteristic = mongoose.model('Characteristic'),
+ ActivityItem = mongoose.model('ActivityItem');
+
+exports.controller = function(req, res, next) {
+ Controller.call(this, req, res, next);
+ var self = this;
+
+ self.index = function () {
+ res.render("dashboard/search", {
+ layout: "dashboard/dashboard-layout"
+ });
+ }
+
+ self.all = function () {
+ var q = String(req.query.q);
+ var words = q.split(" ");
+ var results = {};
+
+ // Flow control
+ var step = 0;
+ next_step();
+ function next_step (err, _results) {
+ if (err) throw err;
+ if (_results) {
+ for (key in _results) {
+ results[key] = _results[key];
+ }
+ }
+ step++;
+ switch (step) {
+ case 1:
+ self._find_items(words, next_step);
+ break;
+ case 2:
+ self._find_users(words, next_step);
+ break;
+ case 3:
+ self._find_topics(words, next_step);
+ break;
+ default:
+ finished();
+ }
+ }
+
+ function finished (err) {
+ if (err) {
+ console.log(err);
+ res.send(err);
+ } else {
+ res.send(results);
+ }
+ }
+ }
+
+ self.users = function () {
+ var q = String(req.query.q);
+ var words = q.split(" ");
+ var results = {};
+ self._find_users(words, finish);
+
+ function finish (err, results) {
+ if (err) {
+ console.log(err);
+ res.send(err);
+ } else {
+ res.send(results);
+ }
+ }
+ }
+
+
+ self._find_items = function (words, cb) {
+ var results = {items: []};
+ var where = {};
+ var _and = get_where_array(words, "message");
+ if (_and.length == 0) {
+ return cb(null, results);
+ } else
+ if (_and.length == 1) {
+ where = _and[0];
+ } else {
+ where["$and"] = _and;
+ }
+ ActivityItem.find(where)
+ .sort("posted_at", -1)
+ .limit(20)
+ .populate("user")
+ .populate("topics", ["text", "ratings", "instances"])
+ .populate("characteristics", ["text", "ratings"])
+ .run(function (err, items) {
+ if (err) {
+ return cb(err);
+ }
+
+ items.forEach(function (item) {
+ item.data = undefined;
+ });
+
+ results.items = items;
+ return cb(null, results);
+ });
+ }
+
+ self._find_users = function (words, cb) {
+ var results = {users: []};
+ var where = {};
+ var _or = get_where_array(words, "display_name").concat(get_where_array(words, "user_name"));
+ if (_or.length == 0) {
+ return cb(null, results);
+ } else
+ if (_or.length == 1) {
+ where = _or[0];
+ } else {
+ where["$or"] = _or;
+ }
+ Identity.find(where)
+ .sort("ratings.overall", -1, "updated_at", -1)
+ .limit(20)
+ .run(function (err, users) {
+ if (err) {
+ return cb(err);
+ }
+
+ results.users = users;
+ return cb(null, results);
+ });
+ }
+
+ self._find_topics = function (words, cb) {
+ var results = {topics: []};
+ var where = {};
+ var _and = get_where_array(words, "text");
+ if (_and.length == 0) {
+ return cb(null, results);
+ } else
+ if (_and.length == 1) {
+ where = _and[0];
+ } else {
+ where["$and"] = _and;
+ }
+ Topic.find(where)
+ .sort("instances", -1)
+ .limit(20)
+ .run(function (err, topics) {
+ if (err) {
+ return cb(err);
+ }
+
+ results.topics = topics;
+ return cb(null, results);
+ });
+ }
+}
+
+function get_where_array (words, key) {
+ var arr = [];
+ words.forEach(function (w) {
+ if (w.length > 0) {
+ var obj = {};
+ obj[key] = new RegExp(w, "i");
+ arr.push(obj);
+ }
+ });
+ return arr;
+}
View
8 public/js/saturn.js
@@ -90,6 +90,7 @@ function get_view (_name) {
function ActivityView (_name, _url) {
var self = this;
+ self.active = true;
self.name = _name;
self.url = _url;
self.new_items = [];
@@ -131,6 +132,8 @@ ActivityView.prototype.set_sort = function (sort) {
ActivityView.prototype.fetch_items = function () {
var self = this;
+ if (!self.active) { return; }
+
$.getJSON(self.url+"?since="+(self.newest_item-60), function (data) {
if (data && data.length > 0) {
data.forEach(function (item) {
@@ -204,7 +207,10 @@ ActivityView.prototype.show_new_items = function (e) {
return false;
}
-
+ActivityView.prototype.deactivate = function () {
+ var self = this;
+ self.active = false;
+}
View
2  views/dashboard/dashboard-layout.ejs
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>saturn.io admin</title>
+ <title>saturn.io dashboard</title>
<link rel="shortcut icon" type="image/x-icon" href="/images/saturn/favicon.ico">
<link rel="stylesheet" href="/css/style.css" />
<link rel="stylesheet" type="text/css" media="all" href="/css/960/reset.css" />
View
84 views/dashboard/search.ejs
@@ -0,0 +1,84 @@
+<div class="grid_9 alpha omega">
+ Search: <input type="text" name="search_text" id="search_text" /> <input type="button" id="search_button" value="go" />
+</div>
+
+<div class="grid_5 alpha">
+ <div class="stream-results"></div>
+</div>
+<div class="grid_4 omega">
+ <div class="user-results"></div>
+</div>
+<script>
+
+$("#search_text").bind("keyup", update_search);
+$("#search_button").bind("click", update_search);
+
+var searching = false;
+var next_search = false;
+var last_q = "";
+function update_search (e) {
+ var q = $("#search_text").val();
+ if (q == last_q || searching) return;
+
+ if (q.indexOf(last_q) == -1) {
+ if (q.length >= 2) {
+ $(".stream-results").html("Loading...");
+ } else {
+ $(".stream-results").html("");
+ }
+ }
+ if (q.length >= 2) {
+ if (current_view) {
+ current_view.deactivate();
+ }
+ search_url = "/search/all?q="+encodeURI(q);
+ searching = true;
+ $.getJSON(search_url, function (data) {
+ searching = false;
+ $(".stream-results").html("");
+ $(".user-results").html("");
+
+ current_view = new ActivityView("results", search_url);
+ current_view.minimum_rating = -100;
+ current_view.collapse_below = 1;
+ $.each(data.items, function (i, item) {
+ current_view.add_item(item);
+ });
+
+ $.each(data.users, function (i, user) {
+ var div = $("<div class='bd media'>");
+ var img_div = $("<div class='img'>");
+ var author_span = $("<span class='activity-item-author'>");
+
+ var avatar_url = "/images/saturn/logo_t.png";
+ if (user.photo && user.photo.length > 0 && user.photo[0] && user.photo[0].url) {
+ avatar_url = user.photo[0].url;
+ }
+
+ var img = $("<img class='activity-item-avatar'>").attr("src", avatar_url);
+ var link = $("<a>").attr("href", "/identity/view/"+user._id);
+ var img_link = link.clone();
+ var author_link = link.clone();
+ img_link.append(img);
+ img_div.append(img_link);
+ author_link.html(user.display_name);
+ author_span.append(author_link);
+
+ div.append(img_div);
+ div.append(author_span);
+
+ $(".user-results").append(div);
+ });
+
+ if (q != $("#search_text").val()) {
+ update_search();
+ }
+ });
+ } else {
+ $(".stream-results").html("");
+ $(".user-results").html("");
+ }
+ last_q = q;
+}
+
+</script>
View
15 views/objects/nav.ejs
@@ -1,12 +1,13 @@
<div class="grid_3">
<ul class="nav nav-list">
- <li class="nav-header">Friends</li>
- <li><a href="/dashboard">Recent Posts</a></li>
- <li><a href="/dashboard/top">Top Posts</a></li>
+ <li class="nav-header">Dashboard</li>
+ <li><a href="/dashboard">Recent Posts</a></li>
+ <li><a href="/dashboard/top">Top Posts</a></li>
+ <li><a href="/search">Search</a></li>
<li class="nav-header">Topics</li>
- <li><a href="/topic/trending">Trending</a></li>
- <li><a href="/topic/popular">Popular</a></li>
+ <li><a href="/topic/trending">Trending</a></li>
+ <li><a href="/topic/popular">Popular</a></li>
<li class="nav-header">Settings</li>
- <li><a href="/admin">Admin</a></li>
+ <li><a href="/admin">Admin</a></li>
</ul>
-</div>
+</div>
Please sign in to comment.
Something went wrong with that request. Please try again.