Created
December 7, 2012 18:16
-
-
Save prettymuchbryce/4235207 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'ImageUtilities' #ImageUtilities is a module that is used to grab screenshots of a given URL. It is an external API. | |
require 'cgi' | |
require 'LatestCache' #LatestCache is a module that is used to update the cache for entries that have been submitted recently. | |
class EntriesController < ApplicationController | |
before_filter :authenticate, :only => "destroy" #User must have admin privileges to destroy an entry. | |
# This index method is hit via ajax call from javascript. It is the "HOT", or front page entries. | |
def index | |
page = params[:page] #Url Param can tell us which entry to start from. This is in the case that the user is scrolling down the page. Eatch "batch" of entries is a "page". | |
entryArray = Rails.cache.read('entries') | |
submittersArray = Rails.cache.read('submitters') | |
#Catch case that cache is empty. This should never happen so let's log it. | |
if entryArray == nil | |
entryArray = [] | |
submittersArray = [] | |
logger.error "Main Cache is empty." | |
end | |
prepareForRender(entryArray,submittersArray,page) | |
render :partial => "show" | |
end | |
# This method is hit via ajax call from javascript. It is the "latest" entries. | |
def latest | |
page = params[:page] | |
entryArray = Rails.cache.read('entriesLatest') | |
submittersArray = Rails.cache.read('submittersLatest') | |
#catch case that we miss cache | |
if entryArray == nil || entryArray.length == 0 || submittersArray == nil || submittersArray.length == 0 | |
#It's okay if this happens. We can just recreate the cache, however; someday this should be a delayed job. | |
LatestCache.recreate() | |
end | |
prepareForRender(entryArray,submittersArray,page) | |
render :partial => "show" | |
end | |
# The show method gets called when a user clicks on an entry. It redirects them to the entry's link. | |
# This method is becoming deprecated due to the new modal popup style of viewing content. | |
def show | |
#should redirect to site | |
@entry = Entry.find(params[:id]); | |
if !@entry | |
redirect_to :root | |
return | |
end | |
redirect_to CGI.unescape(@entry.link) | |
end | |
#This Controller responds to the User clicking on the bookmarklet, attempting to create a new entry. | |
MAX_POSSIBLE_CHOICES = 20; | |
def new | |
if params.has_key?("u") | |
submissionUrl = params["u"] #Check to see if the bookmarklet passed a param for the URL it's viewing from. | |
end | |
@isLoggedIn = logged_in? | |
str = submissionUrl | |
@documentImage = ImageUtilities.generateUrlPng(str); #ImageUtilities generates a link to a screenshot of this website via an API called url2png. | |
@document = str | |
#Collect all media data from the request and pass it on to the view. | |
@images = [] | |
for i in (0..MAX_POSSIBLE_CHOICES) | |
if params.has_key?("i"+String(i)) | |
@images.push(params["i"+String(i)]) | |
else | |
break | |
end | |
end | |
@entry = Entry.new | |
render :layout => 'bookmarklet' | |
end | |
# The create method is called when a user sends POST request to create a new entry from the bookmarklet. | |
def create | |
#TODO We shouldn't trust the client to send us matching image/urls. | |
#A clever hacker could post the picture that actually leads to the url of an unrelated website. | |
if !params[:externalId] | |
externalId = nil | |
else | |
externalId = params[:externalId] | |
end | |
#Allows users designated as admins to submit content as "fake" users. | |
if params[:isFake] == "true" && current_user.is_admin == true | |
fakeUsers = User.where("is_fake_user = ?",true) | |
user = fakeUsers.sample | |
@id = Entry.createEntry(params[:description],params[:kind],params[:link],params[:imageLink],user,externalId) | |
#Have a few fake users add this entry to make it look real. | |
fakeUsers.sample.addEntryToFlavorites(@id) | |
fakeUsers.sample.addEntryToFlavorites(@id) | |
fakeUsers.sample.addEntryToFlavorites(@id) | |
else | |
#Create the entry. | |
@id = Entry.createEntry(params[:description],params[:kind],params[:link],params[:imageLink],current_user,externalId) | |
#Log that it was created to the BI service. | |
MetricsManager.trackAddEntry(current_user,@id) | |
end | |
render :partial => "id" | |
end | |
#Deletes an entry. | |
def destroy | |
Entry.deleteEntry(params[:id]) | |
LatestCache.recreate() #Recreate the latest cache, otherwise this deleted entry may still show in the site, even though it doesn't exist anymore. | |
render :partial => "empty" | |
end | |
# Prepares the Array of entries for being rendered on the page | |
private | |
def prepareForRender(entryArray,submitterArray,pageNumber) | |
@currentIndex = Integer(params[:page])*ENTRIES_PER_REQUEST #Get the current index based on the "page", or "batch" requested. | |
renderEntriesArray = entryArray.slice(@currentIndex,ENTRIES_PER_REQUEST) | |
renderSubmittersArray = submitterArray.slice(@currentIndex,ENTRIES_PER_REQUEST) | |
@entries = Array.new | |
@submitters = Array.new | |
#Prepare this data for rendering. | |
if renderEntriesArray != nil | |
renderEntriesArray.each do |renderEntry| | |
@entries.push(renderEntry) | |
end | |
renderSubmittersArray.each do |renderSubmitter| | |
@submitters.push(renderSubmitter) | |
end | |
end | |
end | |
private | |
def authenticate | |
authenticate_or_request_with_http_basic do |username, password| | |
username == ADMIN_USERNAME && password == ADMIN_PASS | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment