constructor facility added
[statusbox:statusbox.git] / src / api / user.js
1 net.status.js.User=function(p){
2         this.user_id=null;
3         this.screen_name=null;
4         this.id=null;
5         this.API=p;
6         
7         this.construct=function(){
8                 //calls the super class's contruct()
9                 this.__proto__.construct();
10         };
11         
12         
13         this.init=function(identifier){
14                 //this is must to do.
15                 //do the initaition of the values
16                 this.construct();
17                 
18                 if(isNaN(identifier)){
19                         this.screen_name=identifier;
20                 }
21                 else{
22                         this.user_id=identifier;
23                 }
24                 this.id=identifier;
25                 
26                 this.method="/users/show.json";
27                 this.params={id:this.id};
28                 this.type="net.status.js.User";
29                 
30         };
31         
32         
33         this.favorites=function(){
34                 this.method="/favorites.json";
35                 this.params={id:this.screen_name};
36                 this.type="net.status.js.Status";
37                 return this;
38         };
39         
40         this.userTimeline=function(){
41                 this.method="/statuses/user_timeline.json";
42                 this.params=this.getUserParams();
43                 this.type="net.status.js.Status"; 
44                 return this;
45         };
46         
47         this.followers=function(){
48                 this.method="/statuses/followers.json";
49                 this.params=this.getUserParams();
50                 this.type="net.status.js.User";
51                 return this;
52         };
53         
54         this.friends=function(){
55                 this.method="/statuses/friends.json";
56                 this.params=this.getUserParams();
57                 this.type="net.status.js.User";
58                 return this;
59         };
60         
61         this.isFollowing=function(){
62                 this.validateOnline();
63                 
64                 this.method="/friendships/exists.json";
65                 this.params={user_b:this.id,user_a:net.status.js.User.current().screen_name};
66                 this.type="null";
67                 return this;
68         };
69         
70         this.isFriend=function(){
71                 this.validateOnline();
72                 
73                 this.method="/friendships/exists.json";
74                 this.params={user_a:this.id,user_b:net.status.js.User.current().screen_name};
75                 this.type="null";
76                 return this;
77         };
78         
79         this.isMutual=function(){
80                 this.validateOnline();
81                 
82                 this.alteration=
83                         "var isMutual=false;"+
84                         "if(response.relationship && response.relationship.source.following && response.relationship.source.followed_by) {"+
85                         "       isMutual=true;"+
86                         "}"+
87                         "response=isMutual;";
88                 this.method="/friendships/show.json";
89                 
90                 if(this.screen_name){
91                         this.params=
92                         {source_screen_name:this.screen_name,target_screen_name:net.status.js.User.current().screen_name};
93                 }
94                 else if(this.user_id){
95                         this.params=
96                         {source_id:this.user_id,target_screen_name:net.status.js.User.current().screen_name};
97                 }
98                 
99                 this.type="null";
100                 return this;
101         };
102         
103         this.block=function(){
104                 this.validateOnline();
105                 this.proxy='POST';
106                 
107                 this.method="/blocks/create.json";
108                 this.params={id:this.id};
109                 this.type="null";
110                 return this;
111         };
112         
113         this.unblock=function(){
114                 this.validateOnline();
115                 this.proxy='POST';
116                 this.method="/blocks/destroy.json";
117                 this.params={id:this.id};
118                 this.type="null";
119                 return this;
120         };
121         
122         this.directMessage=this.dm=function(messageText){
123                 return net.status.js.DirectMessage.send(this.id,messageText);
124         };
125         
126         this.follow=function(){
127                 this.validateOnline();
128                 this.proxy='POST';
129                 this.method="/friendships/create.json";
130                 this.params={id:this.id};
131                 this.type="net.status.js.User";
132                 return this;
133         };      
134         
135         this.unfollow=function(){
136                 this.validateOnline();
137                 this.proxy='POST';
138                 this.method="/friendships/destroy.json";
139                 this.params={id:this.id};
140                 this.type="net.status.js.User";
141                 return this;
142         };      
143         
144         this.getUserParams=function(params){
145                 var rtn= {screen_name:this.screen_name,user_id:this.user_id};
146                 jQuery.extend(rtn,params);
147                 return rtn;
148         };
149
150 };
151
152 //inheritance
153 net.status.js.User.prototype=new net.status.js.APIBase();
154
155 net.status.js.User.find=function(identifier){
156         if(!identifier) throw "Please enter an ID";
157         var user=new net.status.js.User(this.parent);
158         user.init(identifier);
159         return user;
160 };
161
162 //supports only screen names
163 //not yet user_id's
164 net.status.js.User.findAll=function(identifiers){
165         console.error("Not Implemented Yet! Finding a API endpoing");
166 };
167
168 net.status.js.User.current=function(){
169         var curr=this.parent.currentUser;
170         //curr.api(this.parent);
171         return curr;
172 };
173