fixed getting posts
This commit is contained in:
parent
4c17a7d019
commit
5bf8ca3fb5
8 changed files with 105 additions and 54 deletions
|
@ -16,6 +16,7 @@
|
|||
if (self) {
|
||||
// Initialization code here.
|
||||
d = [NSUserDefaults standardUserDefaults];
|
||||
//[d removeObjectForKey:@"user_access_token"];
|
||||
}
|
||||
|
||||
return self;
|
||||
|
|
|
@ -44,14 +44,12 @@
|
|||
andSelector:@selector(handleGetURLEvent:withReplyEvent:)
|
||||
forEventClass:kInternetEventClass
|
||||
andEventID:kAEGetURL];
|
||||
|
||||
|
||||
viewDelegate = [[ViewDelegate alloc] init];
|
||||
|
||||
|
||||
accessToken = [[AccessToken alloc] init];
|
||||
|
||||
//accessToken.accessToken = nil;
|
||||
NSLog(@"%@", [accessToken stringForKey:@"user_access_token"]);
|
||||
if (![accessToken stringForKey:@"user_access_token"]) {
|
||||
[self initOauth];
|
||||
} else {
|
||||
|
|
93
Core.js
93
Core.js
|
@ -7,17 +7,20 @@
|
|||
//
|
||||
|
||||
function Core(action) {
|
||||
|
||||
this.max_length = 200;
|
||||
this.since_id;
|
||||
this.timeout = 2 * 60 * 1000;
|
||||
// this.timeout = 2 * 60 * 1000;
|
||||
this.timeout = 10 * 1000; // every 10 seconds
|
||||
this.action = action;
|
||||
this.getNewData();
|
||||
this.unread_mentions = 0;
|
||||
this.since_id = null;
|
||||
this.since_id_entity = null;
|
||||
this.since_time = 0;
|
||||
|
||||
this.body = document.createElement("ol");
|
||||
this.body.className = this.action;
|
||||
this.cache = {};
|
||||
this.is_not_init = false;
|
||||
|
||||
/*
|
||||
if (action == "home_timeline") {
|
||||
|
@ -29,7 +32,8 @@ function Core(action) {
|
|||
}
|
||||
|
||||
Core.prototype.newStatus = function(status, supress_new_with_timeout) {
|
||||
if(status != null) {
|
||||
if(status != null && status.length > 0) {
|
||||
this.since_id = status[0]["id"];
|
||||
for(var i = status.length-1, c=0; i>=c; --i) {
|
||||
if(this.body.childNodes.length > 0) {
|
||||
if(this.body.childNodes.length > this.max_length) {
|
||||
|
@ -54,10 +58,11 @@ Core.prototype.newStatus = function(status, supress_new_with_timeout) {
|
|||
}
|
||||
|
||||
Core.prototype.getItem = function(status) {
|
||||
alert(JSON.stringify(status))
|
||||
|
||||
var _this = this;
|
||||
this.since_id = status.id;
|
||||
this.since_id_entity = status.entity;
|
||||
if (this.since_time < status.published_at) this.since_time = status.published_at;
|
||||
|
||||
var original_status = null;
|
||||
/*
|
||||
|
@ -76,13 +81,17 @@ Core.prototype.getItem = function(status) {
|
|||
template.username.href = status.entity; // FIXME open profile
|
||||
|
||||
findProfileURL(status.entity, function(profile_url) {
|
||||
getURL(profile_url, "GET", function(resp) {
|
||||
var profile = JSON.parse(resp.responseText);
|
||||
var basic = profile["https://tent.io/types/info/basic/v0.1.0"];
|
||||
alert(JSON.stringify(basic))
|
||||
template.username.innerText = basic.name;
|
||||
template.image.src = basic.avatar_url;
|
||||
});
|
||||
if (profile_url) {
|
||||
getURL(profile_url, "GET", function(resp) {
|
||||
var profile = JSON.parse(resp.responseText);
|
||||
var basic = profile["https://tent.io/types/info/basic/v0.1.0"];
|
||||
|
||||
if (profile && basic) {
|
||||
if(basic.name) template.username.innerText = basic.name;
|
||||
if(basic.avatar_url) template.image.src = basic.avatar_url;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
|
@ -107,20 +116,22 @@ Core.prototype.getItem = function(status) {
|
|||
template.message.innerHTML = replaceTwitterLinks(replaceURLWithHTMLLinks(status.content.text, status.entities, template.message));
|
||||
|
||||
var time = document.createElement("abbr");
|
||||
time.innerText = status.published_at;
|
||||
time.title = status.published_at;
|
||||
time.innerText = ISODateString(new Date(status.published_at * 1000));
|
||||
time.title = time.innerText;
|
||||
time.className = "timeago";
|
||||
$(time).timeago();
|
||||
template.ago.appendChild(time);
|
||||
//template.ago.href = WEBSITE_PATH + status.user.screen_name + "/status/" + status.id_str;
|
||||
|
||||
// {"type":"Point","coordinates":[57.10803113,12.25854746]}
|
||||
if (status.geo && status.geo.type == "Point") {
|
||||
template.geo.href = "http://maps.google.com/maps?q=" + status.geo.coordinates[0] + "," + status.geo.coordinates[1];
|
||||
if (status.content && status.content.location && status.content.location.type == "Point") {
|
||||
template.geo.href = "http://maps.google.com/maps?q=" + status.content.location.coordinates[0] + "," + status.content.location.coordinates[1];
|
||||
template.geo.style.display = "";
|
||||
}
|
||||
|
||||
template.source.innerHTML = status.source;
|
||||
template.source.href = status.app.url;
|
||||
template.source.innerHTML = status.app.name;
|
||||
template.source.title = status.app.url;
|
||||
/*
|
||||
if(status.entities.media) {
|
||||
|
||||
|
@ -187,11 +198,12 @@ Core.prototype.getTemplate = function() {
|
|||
retweet.className = "retweet";
|
||||
retweet.innerText = " ";
|
||||
retweet.href = "#";
|
||||
item.appendChild(retweet);
|
||||
// item.appendChild(retweet); // FIXME
|
||||
|
||||
|
||||
var image = document.createElement("img");
|
||||
image.className = "image";
|
||||
image.src = "default-avatar.png";
|
||||
image.onmousedown = function(e) { e.preventDefault(); };
|
||||
item.appendChild(image);
|
||||
|
||||
|
@ -248,7 +260,7 @@ Core.prototype.getTemplate = function() {
|
|||
var from = document.createTextNode(" from ");
|
||||
date.appendChild(from)
|
||||
|
||||
var source = document.createElement("span");
|
||||
var source = document.createElement("a");
|
||||
source.className = "source";
|
||||
date.appendChild(source)
|
||||
|
||||
|
@ -272,28 +284,47 @@ Core.prototype.getTemplate = function() {
|
|||
Core.prototype.getNewData = function(supress_new_with_timeout) {
|
||||
|
||||
var those = this;
|
||||
var url = controller.stringForKey_("api_root") + "/posts";
|
||||
//if(this.since_id) url += "?since_id=" this.since_id;
|
||||
|
||||
var url = URI(controller.stringForKey_("api_root"));
|
||||
url.path("posts");
|
||||
url.addSearch("post_types", "https://tent.io/types/post/status/v0.1.0");
|
||||
url.addSearch("limit", this.max_length);
|
||||
if(this.since_id) {
|
||||
url.addSearch("since_id", this.since_id);
|
||||
url.addSearch("since_id_entity", this.since_id_entity);
|
||||
}
|
||||
|
||||
if (this.action == "mentions") {
|
||||
url.addSearch("mentioned_entity", controller.stringForKey_("entity"));
|
||||
}
|
||||
|
||||
var http_method = "GET";
|
||||
var callback = function(resp) {
|
||||
those.newStatus(JSON.parse(resp.responseText), supress_new_with_timeout);
|
||||
|
||||
try {
|
||||
var json = JSON.parse(resp.responseText)
|
||||
} catch (e) {
|
||||
//alert(resp.responseText);
|
||||
alert(url + " JSON parse error");
|
||||
throw e;
|
||||
}
|
||||
|
||||
those.newStatus(json, supress_new_with_timeout);
|
||||
}
|
||||
|
||||
var data = null;
|
||||
|
||||
getURL(
|
||||
url,
|
||||
url.toString(),
|
||||
http_method,
|
||||
callback,
|
||||
data,
|
||||
makeAuthHeader(
|
||||
url,
|
||||
url.toString(),
|
||||
http_method,
|
||||
controller.stringForKey_("user_mac_key"),
|
||||
controller.stringForKey_("user_access_token")
|
||||
)
|
||||
);
|
||||
); // FIXME: error callback
|
||||
|
||||
/*
|
||||
$.ajax(
|
||||
|
@ -596,4 +627,14 @@ function replaceShortened(url, message_node) {
|
|||
});
|
||||
}
|
||||
|
||||
function ISODateString(d){
|
||||
function pad(n){return n<10 ? '0'+n : n}
|
||||
return d.getUTCFullYear()+'-'
|
||||
+ pad(d.getUTCMonth()+1)+'-'
|
||||
+ pad(d.getUTCDate())+'T'
|
||||
+ pad(d.getUTCHours())+':'
|
||||
+ pad(d.getUTCMinutes())+':'
|
||||
+ pad(d.getUTCSeconds())+'Z'
|
||||
}
|
||||
|
||||
var tentia_instance;
|
||||
|
|
|
@ -73,7 +73,7 @@
|
|||
}
|
||||
|
||||
- (void)inReplyTo:(NSString *)userName statusId:(NSString *)statusId {
|
||||
[textField setStringValue:[NSString stringWithFormat:@"@%@ ", userName]];
|
||||
[textField setStringValue:[NSString stringWithFormat:@"^%@ ", userName]];
|
||||
NSRange range = {[[textField stringValue] length] , 0};
|
||||
[[textField currentEditor] setSelectedRange:range];
|
||||
[inReplyTostatusId release];
|
||||
|
|
|
@ -48,16 +48,9 @@ OauthImplementation.prototype.apiRoot = function() {
|
|||
|
||||
OauthImplementation.prototype.requestProfileURL = function (entity) {
|
||||
var those = this;
|
||||
getURL(entity, "HEAD", function(resp) {
|
||||
var headers = resp.getAllResponseHeaders();
|
||||
var regex = /Link: <([^>]*)>; rel="https:\/\/tent.io\/rels\/profile"/; // FIXME: parse it!
|
||||
var profile_url = headers.match(regex)[1];
|
||||
if (profile_url == "/profile") {
|
||||
profile_url = entity + "/profile";
|
||||
}
|
||||
findProfileURL(entity, function(profile_url) {
|
||||
those.register(profile_url);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.register = function (url) {
|
||||
|
@ -113,7 +106,6 @@ OauthImplementation.prototype.requestAccessToken = function(responseBody) {
|
|||
those.requestAccessTokenTicketFinished(resp.responseText);
|
||||
};
|
||||
|
||||
alert(controller.stringForKey_("app_mac_key"))
|
||||
var auth_header = makeAuthHeader(url, "POST", controller.stringForKey_("app_mac_key"), controller.stringForKey_("app_mac_key_id"));
|
||||
getURL(url, "POST", callback, requestBody, auth_header);
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
1F9D655F163B63F700B7282B /* URI.min.js in Resources */ = {isa = PBXBuildFile; fileRef = 1F9D655D163B63E300B7282B /* URI.min.js */; };
|
||||
1F9D6561163C222400B7282B /* hmac-helper.js in Sources */ = {isa = PBXBuildFile; fileRef = 1F9D6560163C222400B7282B /* hmac-helper.js */; };
|
||||
1F9D6562163C26FC00B7282B /* hmac-helper.js in Resources */ = {isa = PBXBuildFile; fileRef = 1F9D6560163C222400B7282B /* hmac-helper.js */; };
|
||||
1F9D6564163F35C600B7282B /* default-avatar.png in Resources */ = {isa = PBXBuildFile; fileRef = 1F9D6563163F35C600B7282B /* default-avatar.png */; };
|
||||
1FA09847144602530079E258 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FA09846144602530079E258 /* libicucore.dylib */; };
|
||||
1FC254941427BC050035D84B /* index_oauth.html in Resources */ = {isa = PBXBuildFile; fileRef = 1FC254931427BC050035D84B /* index_oauth.html */; };
|
||||
1FC254951427BF150035D84B /* OauthImplementation.js in Resources */ = {isa = PBXBuildFile; fileRef = 1FC254911427ADF90035D84B /* OauthImplementation.js */; };
|
||||
|
@ -90,6 +91,7 @@
|
|||
1F98DC9D124BFFD7004289ED /* pin.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pin.png; sourceTree = "<group>"; };
|
||||
1F9D655D163B63E300B7282B /* URI.min.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = URI.min.js; sourceTree = "<group>"; };
|
||||
1F9D6560163C222400B7282B /* hmac-helper.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "hmac-helper.js"; sourceTree = "<group>"; };
|
||||
1F9D6563163F35C600B7282B /* default-avatar.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "default-avatar.png"; sourceTree = "<group>"; };
|
||||
1FA09846144602530079E258 /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = usr/lib/libicucore.dylib; sourceTree = SDKROOT; };
|
||||
1FC254911427ADF90035D84B /* OauthImplementation.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; lineEnding = 0; path = OauthImplementation.js; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.javascript; };
|
||||
1FC254931427BC050035D84B /* index_oauth.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = index_oauth.html; sourceTree = "<group>"; };
|
||||
|
@ -186,6 +188,7 @@
|
|||
1F4673E21180F519006CC37C /* jQuery.js */,
|
||||
1F4673E41180F590006CC37C /* jQuery-Plugins.js */,
|
||||
1F9D6560163C222400B7282B /* hmac-helper.js */,
|
||||
1F9D6563163F35C600B7282B /* default-avatar.png */,
|
||||
);
|
||||
name = WebKit;
|
||||
sourceTree = "<group>";
|
||||
|
@ -334,6 +337,7 @@
|
|||
1F122D49118E1DE100E83B77 /* Icon.icns in Resources */,
|
||||
1F98DC9E124BFFD7004289ED /* pin.png in Resources */,
|
||||
1FC254941427BC050035D84B /* index_oauth.html in Resources */,
|
||||
1F9D6564163F35C600B7282B /* default-avatar.png in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
BIN
default-avatar.png
Normal file
BIN
default-avatar.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.1 KiB |
|
@ -5,13 +5,8 @@
|
|||
function getURL(url, http_method, callback, data, auth_header) {
|
||||
$.ajax({
|
||||
beforeSend: function(xhr) {
|
||||
if (data) {
|
||||
xhr.setRequestHeader("Content-Length", data.length);
|
||||
}
|
||||
|
||||
if (auth_header) {
|
||||
xhr.setRequestHeader("Authorization", auth_header);
|
||||
}
|
||||
if (data) xhr.setRequestHeader("Content-Length", data.length);
|
||||
if (auth_header) xhr.setRequestHeader("Authorization", auth_header);
|
||||
},
|
||||
url: url,
|
||||
accepts: "application/vnd.tent.v0+json",
|
||||
|
@ -39,7 +34,7 @@ function makeAuthHeader(url, http_method, mac_key, mac_key_id) {
|
|||
+ time_stamp + '\n'
|
||||
+ nonce + '\n'
|
||||
+ http_method + '\n'
|
||||
+ url.path() + '\n'
|
||||
+ url.path() + url.search() + url.hash() + '\n'
|
||||
+ url.hostname() + '\n'
|
||||
+ url.port() + '\n'
|
||||
+ '\n' ;
|
||||
|
@ -66,13 +61,33 @@ function makeid(len) {
|
|||
}
|
||||
|
||||
function findProfileURL(entity, callback) {
|
||||
getURL(entity, "HEAD", function(resp) {
|
||||
var headers = resp.getAllResponseHeaders();
|
||||
var regex = /Link: <([^>]*)>; rel="https:\/\/tent.io\/rels\/profile"/; // FIXME: parse it!
|
||||
var profile_url = headers.match(regex)[1];
|
||||
if (profile_url == "/profile") {
|
||||
profile_url = entity + "/profile";
|
||||
$.ajax({
|
||||
url: entity,
|
||||
type: "HEAD",
|
||||
complete: function(resp) {
|
||||
if(resp) {
|
||||
var headers = resp.getAllResponseHeaders();
|
||||
var regex = /Link: <([^>]*)>; rel="https:\/\/tent.io\/rels\/profile"/; // FIXME: parse it!
|
||||
var ret = headers.match(regex);
|
||||
var profile_url = entity + "/profile";
|
||||
if(ret.length > 1) {
|
||||
var profile_url = ret[1];
|
||||
if (profile_url == "/profile") {
|
||||
profile_url = entity + "/profile";
|
||||
}
|
||||
}
|
||||
callback(profile_url);
|
||||
}
|
||||
},
|
||||
error: function(xhr, ajaxOptions, thrownError) {
|
||||
alert("getURL ERROR (" + url + ") (" + http_method + "):");
|
||||
alert(xhr.statusText);
|
||||
alert(ajaxOptions);
|
||||
alert(thrownError);
|
||||
}
|
||||
callback(profile_url);
|
||||
});
|
||||
}
|
||||
|
||||
function debug(string) {
|
||||
alert("DEBUG: " + string);
|
||||
}
|
Reference in a new issue