Permalink
Browse files

Finished more twitter integration(follow/unfollow)

  • Loading branch information...
1 parent 8e07b49 commit 7e0015f85384702d2425df0d7abda17b7d6b236d Pixl-Coder committed Oct 28, 2010
Showing with 101 additions and 55 deletions.
  1. +12 −9 em/TadpoleConnection.rb
  2. +21 −0 em/em-twitter.rb
  3. +5 −3 em/twitterOAuth.rb
  4. +1 −0 public/auth.html
  5. +8 −12 public/js/App.js
  6. +2 −2 public/js/Colors.js
  7. +39 −23 public/js/ContextMenu.js
  8. +11 −3 public/js/Tadpole.js
  9. +2 −3 public/js/WebSocketService.js
View
@@ -93,7 +93,7 @@ def authorize_handler(json)
@storage.authorized(auth.user_id,auth.screen_name)
Syslog.info("Authenticated ##{@tadpole.id } as #{@tadpole.authorized}")
-
+
@tadpole.handle = @tadpole.authorized
broadcast @tadpole.to_json
@@ -123,16 +123,19 @@ def twitter_handler(json)
if @auth && @auth.authorized?
case json["request"]
when "follow"
- result = @auth.post("/friendships/create/#{json['id']}.json")
- twitter_response(json,result.to_json)
+ EM::Twitter.post(@auth,"/friendships/create/#{json['id']}.json") { |result|
+ twitter_response(json,result.to_json)
+ }
when "unfollow"
- result = @auth.post("/friendships/destroy/#{json['id']}.json")
- twitter_response(json,result.to_json)
+ EM::Twitter.post(@auth,"/friendships/destroy/#{json['id']}.json") { |result|
+ twitter_response(json,result.to_json)
+ }
when "friends"
- result = @auth.get("/friends/ids.json")
- if not result.to_json["error"]
- @socket.send(%({"type":"twitter","request":"friends","result":#{result}}));
- end
+ EM::Twitter.get(@auth,"/friends/ids.json") { |result|
+ if not result.to_json["error"]
+ @socket.send(%({"type":"twitter","request":"friends","result":#{result}}));
+ end
+ }
end
end
end
View
@@ -34,6 +34,27 @@ def self.verifyRequest(token,verifier,&block)
},block
)
end
+
+ def self.rememberSession
+ end
+
+ def self.get(auth,path,&block)
+ EventMachine.defer(
+ proc {
+ result = auth.get(path)
+ result
+ },block
+ )
+ end
+ def self.post(auth,path,&block)
+ EventMachine.defer(
+ proc {
+ result = auth.post(path)
+ result
+ },block
+ )
+ end
+
end
end
View
@@ -58,13 +58,13 @@ def authorized?
def get(path, headers={})
headers.merge!("User-Agent" => "rumpetroll")
- oauth_response = access_token.get("/1#{path}", headers)
+ oauth_response = @access_token.get("/1#{path}", headers)
return oauth_response.body
end
def post(path, body = '', headers={})
headers.merge!("User-Agent" => "rumpetroll")
- oauth_response = access_token.post("/1#{path}",body, headers)
+ oauth_response = @access_token.post("/1#{path}",body, headers)
return oauth_response.body
end
@@ -84,7 +84,9 @@ def access_token
@tokens.access_token = at.token
@tokens.access_secret = at.secret
@screen_name = at.params[:screen_name]
- @user_id = at.params[:user_id]
+ @user_id = at.params[:user_id]
+ puts(@tokens.access_token)
+ puts(@tokens.access_secret)
end
at
View
@@ -13,6 +13,7 @@
uri = parseUri(document.location)
if ( uri.queryKey.oauth_token ) {
window.opener.app.authorize(uri.queryKey.oauth_token, uri.queryKey.oauth_verifier);
+ localStorage[""] = uri.queryKey.oauth_token, uri.queryKey.oauth_verifier
window.close()
}
</script>
View
@@ -10,8 +10,7 @@ var App = function(aSettings, aCanvas) {
webSocketService,
mouse = {x: 0, y: 0, worldx: 0, worldy: 0, tadpole:null},
keyNav = {x:0,y:0},
- messageQuota = 5,
- contextMenu
+ messageQuota = 5
;
app.update = function() {
@@ -37,7 +36,7 @@ var App = function(aSettings, aCanvas) {
// Update tadpoles
for(id in model.tadpoles) {
- model.tadpoles[id].update(mouse,model.userTadpole.followers);
+ model.tadpoles[id].update(mouse,model.userTadpole.friends);
}
// Update waterParticles
@@ -118,11 +117,9 @@ var App = function(aSettings, aCanvas) {
app.mousedown = function(e) {
mouse.clicking = true;
- if(contextMenu.opened) {
- if(e.target.className != "item") {
- contextMenu.close();
- }
- return;
+ if(model.userTadpole.contextMenu && model.userTadpole.contextMenu.opened && e.target.className != "item") {
+ model.userTadpole.contextMenu.close();
+ return false;
}
if(mouse.tadpole && mouse.tadpole.hover && mouse.tadpole.onclick(e)) {
@@ -136,8 +133,8 @@ var App = function(aSettings, aCanvas) {
};
app.oncontextmenu = function(e) {
- if(mouse.tadpole && mouse.tadpole.hover) {
- contextMenu.open(e.clientX,e.clientY,mouse.tadpole);
+ if(mouse.tadpole && mouse.tadpole.hover && model.userTadpole.authorized) {
+ model.userTadpole.contextMenu.open(e.clientX,e.clientY,mouse.tadpole);
return false;
}
};
@@ -249,6 +246,7 @@ var App = function(aSettings, aCanvas) {
model.userTadpole = new Tadpole();
model.userTadpole.id = -1;
+ model.userTadpole.friends = [];
model.tadpoles[model.userTadpole.id] = model.userTadpole;
@@ -268,7 +266,5 @@ var App = function(aSettings, aCanvas) {
webSocket.onmessage = app.onSocketMessage;
webSocketService = new WebSocketService(model, webSocket);
-
- contextMenu = new ContextMenu(webSocketService);
})();
}
View
@@ -42,6 +42,6 @@ var FadingColor = function(color,rate) {
var statusColors = {
none: new Color(226,219,226),
- hover: new Color(192, 253, 247),
- follower: new Color(255,110,50)
+ hover: new Color(182, 243, 237),
+ follower: new Color(255,200,126)
};
View
@@ -1,8 +1,8 @@
-var ContextMenu = function(webSocketService) {
+var ContextMenu = function(webSocketService,userTadpole) {
var contextMenu = this;
- var options = {
+ var functions = {
follow : function() {
webSocketService.sendTwitterRequest("follow",{id:contextMenu.tadpole.twitter_id});
},
@@ -14,40 +14,56 @@ var ContextMenu = function(webSocketService) {
this.element;
this.opened = false;
this.tadpole = null;
+ this.userTadpole = userTadpole;
this.init = function() {
if(!contextMenu.element) {
-
contextMenu.element = document.createElement("div");
contextMenu.element.id = "contextMenu";
- for(var i in options) {
- var currentOption = document.createElement("div");
-
- currentOption.id = currentOption.innerText = i;
- currentOption.className = "item";
+ document.getElementById("ui").appendChild(contextMenu.element);
+ }
+ };
- currentOption.onclick = function(e) {
- options[this.id]();
- contextMenu.close();
- e.stopPropagation();
- return false;
- };
+ var loadFunction = function(functionID) {
+ var func = document.createElement("div");
+ func.id = func.innerText = functionID;
+ func.className = "item";
+ func.onclick = function(e) {
+ functions[this.id]();
+ contextMenu.close();
+ e.preventDefault();
+ e.stopPropagation();
+ }
+ contextMenu.element.appendChild(func);
+ };
- contextMenu.element.appendChild(currentOption);
- }
+ this.loadFunctions = function() {
+ contextMenu.element.innerText = "";
- document.getElementById("ui").appendChild(contextMenu.element);
+ if(contextMenu.userTadpole.twitter_id == contextMenu.tadpole.twitter_id) {
+ return false;
+ }
+ else if(contextMenu.tadpole.isFriend) {
+ loadFunction("unfollow");
+ return true;
+ }
+ else {
+ loadFunction("follow");
+ return true;
}
};
+
this.open = function(x,y,tadpole) {
- contextMenu.element.style.left = x + "px";
- contextMenu.element.style.top = y + "px";
- contextMenu.element.style.display = "block";
-
- contextMenu.opened = true;
contextMenu.tadpole = tadpole;
- }
+ if(contextMenu.userTadpole.authorized && contextMenu.tadpole.authorized && contextMenu.loadFunctions()) {
+ contextMenu.element.style.left = x + "px";
+ contextMenu.element.style.top = y + "px";
+ contextMenu.element.style.display = "block";
+
+ contextMenu.opened = true;
+ }
+ };
this.close = function() {
contextMenu.element.style.display = "none";
View
@@ -6,8 +6,8 @@ var Tadpole = function() {
this.size = 4;
this.authorized = false;
- this.friends = [];
this.twitter_id = 0;
+ this.isFriend = false;
this.name = '';
this.age = 0;
@@ -29,7 +29,7 @@ var Tadpole = function() {
this.changed = 0;
this.timeSinceLastServerUpdate = 0;
- this.update = function(mouse,followers) {
+ this.update = function(mouse,friends) {
tadpole.timeSinceLastServerUpdate++;
tadpole.x += Math.cos(tadpole.angle) * tadpole.momentum;
@@ -63,13 +63,21 @@ var Tadpole = function() {
// Update tadpole color
if(tadpole.authorized && !tadpole.hover) {
- if(tadpole.friends.indexOf(tadpole.twitter_id) != -1) {
+ if(friends.indexOf(tadpole.twitter_id) > -1) {
tadpole.color.targetColor = statusColors.follower.clone();
+ tadpole.isFriend = true;
+ }
+ else {
+ tadpole.isFriend = false;
}
}
tadpole.tail.update();
};
+
+ this.onauthorized = function(webSocketService) {
+ tadpole.contextMenu = new ContextMenu(webSocketService, tadpole);
+ };
this.onclick = function(e) {
if(e.ctrlKey && (e.which == 1 || e.which == 3)) {
@@ -31,9 +31,8 @@ var WebSocketService = function(model, webSocket) {
if(tadpole.id == model.userTadpole.id) {
if(tadpole.authorized != data.authorized) {
- //ladies and gentlemen we have just been authorized
- //....
- //..so we have to get our friends
+ // We have just been authorized
+ tadpole.onauthorized(webSocketService);
webSocketService.sendTwitterRequest("friends");
}
tadpole.authorized = data.authorized;

0 comments on commit 7e0015f

Please sign in to comment.