Permalink
Browse files

Improved script to get entry from preview url

  • Loading branch information...
davidmerfield committed Jan 9, 2019
1 parent 94b2536 commit 4296f5c2a4da7f58c0ce8d5d548d54fb3d699e83
Showing with 14 additions and 12 deletions.
  1. +7 −0 scripts/get/blog.js
  2. +7 −12 scripts/get/entry.js
@@ -7,6 +7,13 @@ var parseUrl = require("url").parse;
module.exports = function get(url, callback) {
url = parseUrl(url);

// Map 'preview.default.foo.blot.im' -> 'foo.blot.im'
if (url.host.indexOf("preview.") === 0)
url.host = url.host
.split(".")
.slice(-3)
.join(".");

Blog.get({ domain: url.host }, function(err, blog) {
if (err || !blog) return callback(err || new Error("No blog"));

@@ -1,24 +1,19 @@
var User = require("../../app/models/user");
var Entry = require("../../app/models/entry");
var Blog = require("../../app/models/blog");
var blog = require("./blog");
var parseUrl = require("url").parse;

// Takes a URL and fetches the blog, user and entry

module.exports = function get(url, callback) {
url = parseUrl(url);
blog(url, function(err, user, blog) {
if (err) return callback(err);

Blog.get({ domain: url.host }, function(err, blog) {
if (err || !blog) return callback(err || new Error("No blog"));
url = parseUrl(url);

User.getById(blog.owner, function(err, user) {
if (err || !user) return callback(err || new Error("No user"));
Entry.getByUrl(blog.id, url.path, function(entry) {
if (!entry) return callback(new Error("No entry"));

Entry.getByUrl(blog.id, url.path, function(entry) {
if (!entry) return callback(new Error("No entry"));

callback(err, user, blog, entry);
});
callback(err, user, blog, entry);
});
});
};

0 comments on commit 4296f5c

Please sign in to comment.