diff options
author | Chaoyi Zha <summermontreal@gmail.com> | 2017-04-11 19:51:40 +0300 |
---|---|---|
committer | Chaoyi Zha <summermontreal@gmail.com> | 2017-04-11 19:51:40 +0300 |
commit | d8dd8d6b0b622365942b3910fb2044d9ac8dc7f5 (patch) | |
tree | 3cb61c67788aaace4cf67347a08efcd83995d73f /app | |
parent | 4286d209d5bd459275a0e4ab8bf706b3ee1da890 (diff) |
Refactor use of link lookups in new URL shortening fix #311
Diffstat (limited to 'app')
-rw-r--r-- | app/Factories/LinkFactory.php | 4 | ||||
-rw-r--r-- | app/Helpers/LinkHelper.php | 22 |
2 files changed, 20 insertions, 6 deletions
diff --git a/app/Factories/LinkFactory.php b/app/Factories/LinkFactory.php index 2320613..14e5463 100644 --- a/app/Factories/LinkFactory.php +++ b/app/Factories/LinkFactory.php @@ -55,10 +55,10 @@ class LinkFactory { looks like a shortened URL.'); } - if (!$is_secret && (!isset($custom_ending) || $custom_ending === '') && (LinkHelper::longLinkExists($long_url) !== false)) { + if (!$is_secret && (!isset($custom_ending) || $custom_ending === '') && (LinkHelper::longLinkExists($long_url, $creator) !== false)) { // if link is not specified as secret, is non-custom, and // already exists in Polr, lookup the value and return - $existing_link = LinkHelper::longLinkExists($long_url); + $existing_link = LinkHelper::longLinkExists($long_url, $creator); return self::formatLink($existing_link); } diff --git a/app/Helpers/LinkHelper.php b/app/Helpers/LinkHelper.php index b876d77..5abd675 100644 --- a/app/Helpers/LinkHelper.php +++ b/app/Helpers/LinkHelper.php @@ -51,16 +51,30 @@ class LinkHelper { } } - static public function longLinkExists($long_url) { + static public function longLinkExists($long_url, $username=false) { /** * Provided a long link (string), * check whether the link is in the DB. + * If a username is provided, only search for links created by the + * user. * @return boolean */ - $link = Link::longUrl($long_url) + $link_base = Link::longUrl($long_url) ->where('is_custom', 0) - ->where('secret_key', '') - ->first(); + ->where('secret_key', ''); + + if (is_null($username)) { + // Search for links without a creator only + $link = $link_base->where('creator', '')->first(); + } + else if (($username !== false)) { + // Search for links created by $username only + $link = $link_base->where('creator', $username)->first(); + } + else { + // Search for links created by any user + $link = $link_base->first(); + } if ($link == null) { return false; |