close

  
<p> /*
URL: /r/
Template: rec_template
Params: None

This controller displays the recommendation page. It prominently displays the user's recomendation 
as well as basic information about the recommended topic. It allows logged-in users to recommend
or bookmark the topic as well.

If the the user didn't write a recommendation then we redirect to the topic page for now.

The tiny-rec-guid is not a standard guid, but instead a tiny guid that has been compressed.
*/

var General = acre.require('lib_general');
var Tinyify = acre.require('/user/namesbc/tinyify/tinyify', 'stable');
var Recommend = acre.require('lib_recommend');
var Template;

General.ensure_proper_url();

var visitor = acre.freebase.get_user_info();

var tiny_rec_id = acre.request.path_info.substring(1);
var rec_id = Tinyify.full_guid(tiny_rec_id)
var rec = Recommend.get_rec(rec_id);

if (rec) {
  // We have a rec, so decide which topic page to display
  if (rec.related_type.id == '/book/book') {
    Template = acre.require('book_template');
  } else if (rec.related_type.id == '/music/artist') {
    Template = acre.require('musical-artist_template');
  } else if (rec.related_type.id == '/film/film') {
    Template = acre.require('film_template');
  }
  
  var topic = rec.topic;
  var all_recs = Recommend.recent_recommendations(null, null, rec.topic.id);
  var recommendations = [r for each (r in all_recs.items) if (r.guid != rec.guid)];
  
} else {
  //No rec was found with this tiny guid:
  // Either this rec never existed or the user deleted it. 
  // We want to show the user something since they are probably
  //   coming from a tweeted link.
  // So if it was a deleted rec then redirect to the topic page
  //   otherwise redirect to the homepage.
  var dead_query = {
    "id": rec_id,
    "/freebase/list_entry/list" : {
      "id" : null,
      "related_type": null,
      "link" : {
        "timestamp" : null,
        "valid" : null
      },
      "limit" : 1,
      "sort" : "-link.timestamp",
      "optional": true
    },
    "/freebase/list_entry/topic" : {
      "id" : null,
      "link" : {
        "timestamp" : null,
        "valid" : null
      },
      "limit" : 1,
      "sort" : "-link.timestamp",
      "optional": true
    },
    "creator": {
      "type": "/type/attribution",
      "creator": {
        "id": null,
        "type": "/type/user"
      }
    }
  };
  var dead_rec = acre.freebase.mqlread(dead_query).result;
  if (dead_rec &amp;&amp; dead_rec['/freebase/list_entry/topic'] &amp;&amp; dead_rec['/freebase/list_entry/topic'].id
      &amp;&amp; dead_rec['/freebase/list_entry/list'] &amp;&amp; dead_rec['/freebase/list_entry/list'].related_type) {
    // Deleted rec with a topic, redirect to topic page
    var topic_id = dead_rec['/freebase/list_entry/topic'].id;
    var type_id = dead_rec['/freebase/list_entry/list'].related_type;
    var url = General.topic_url(topic_id, General.tm_types[type_id]);
    acre.start_response(303, {"Location" : url});
    acre.exit();     
  } else if (dead_rec &amp;&amp; dead_rec.creator &amp;&amp; dead_rec.creator.creator &amp;&amp; dead_rec.creator.creator.id) {
    // Deleted rec with a creator but no topic (I don't know how this happened!), redirect to user page
    acre.start_response(303, {"Location" : dead_rec.creator.creator.id});
    acre.exit();
  } else {
    // No rec ever existed, redirect to homepage
    acre.start_response(303, {"Location" : "/"});
    acre.exit();
  }
}

if (!Template) {
  // No template exists for this list type, redirect to homepage instead of barfing
  acre.start_response(303, {"Location" : "/"});
  acre.exit();
}

acre.response.set_header("content-type", "text/html");
acre.write(acre.markup.stringify(Template.master(topic, recommendations, rec))); </p>

Comments

Hide