-
Notifications
You must be signed in to change notification settings - Fork 6
/
azeirah:friends.js
174 lines (165 loc) · 6.16 KB
/
azeirah:friends.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// api
Friends = {};
STATUSES = {};
STATUSES.pending = "pending"; // for the user who sent the request
STATUSES.friend = "friend"; // when both users are friends
STATUSES.request = "request"; // for the user on the recieving end of the request
STATUSES.empty = "empty"; // used for denied requests and removed friends
if (Meteor.isServer) {
var _onCreateUser = Accounts.onCreateUser.bind(Accounts);
// Since onCreateUser overrides default behavior, and we don't want to restrict package users
// by removing the onCreateUser function, we override onCreateUser to modify the user document before the regular onCreateUser call.
// TODO: This doesn't work, I'll comment it out until someone finds a solution http://stackoverflow.com/questions/25583543/overriding-a-package-method-in-meteor
// Accounts.onCreateUser = function (func) {
// console.log('onCreateUser definition');
// _onCreateUser(function (options, user) {
// console.log('onCreateUser call, the user should now have a profile');
// if (!user.profile) {
// user.profile = options.profile || {};
// }
// if (!user.profile.friends) {
// user.profile.friends = [];
// }
// return func(options, user);
// });
// };
// TODO: using the dirty way here, now people can't use Accounts.onCreateUser or this package will fail to work. This DOES work though
Accounts.onCreateUser(function (options, user) {
if (!user.profile) {
user.profile = options.profile || {};
}
if (!user.profile.friends) {
user.profile.friends = [];
}
return user;
});
}
Meteor.methods({
addFriend: function (userId, friendId, status) {
Meteor.users.update({_id: userId},
{$addToSet: {"profile.friends": {_id: friendId, status: status}}});
},
/**
* Updates the status of two friends.
* The first user's status will be applied to the second users reference to
* the first user and the other way around. The second user's status wil be
* applied to the first user's reference to the second user.
*
* This might be a bit confusing so here's an example
*
* We have two users, this state resembles that of just after one
* user sent a friend request to someone else
* by user1 to user2
* user1 {profile.friends = [{
* userId: user2,
* status: "pending"
* }]}
* user2 {profile.friends = [{
* userId: user1,
* status: "request"
* }]}
*
* calling updateFriendStatus ({
* userId: user1,
* status: "updatedStatus1" // using fake statuses for clarity
* },{
* userId: user2,
* status: "updatedStatus2"
* })
*
* will result in
* user1 {profile.friends = [{
* userId: user2,
* status: "updatedStatus2"
* }]}
* user2 {profile.friends = [{
* userId: user1,
* status: "updatedStatus1"
* }]}
*
* The userStatus objects are structured like this
* {
* userId: "340usdfh0932ujweh032ur",
* status: "friend" // a status from STATUSES
* }
*/
updateFriendStatus: function (userStatus, userStatus2) {
if (!this.isSimulation) {
var updateStatus = function (userId, friendId, status) {
Meteor.users.update({_id: userId, "profile.friends._id": friendId}, {$set: {"profile.friends.$.status": status}});
};
updateStatus(userStatus.userId, userStatus2.userId, userStatus2.status);
updateStatus(userStatus2.userId, userStatus.userId, userStatus.status);
}
},
friendRequest: function (userId, friendId) {
if (!validations.areRelated(userId, friendId)) {
Meteor.call('addFriend', userId, friendId, STATUSES.pending);
Meteor.call('addFriend', friendId, userId, STATUSES.request);
}
},
confirmRequest: function (userId, friendId) {
var validated = validations.validateUserRelations(
{userId: userId, status: STATUSES.pending},
{userId: friendId, status: STATUSES.request}
);
if (validated) {
Meteor.call('updateFriendStatus',
{userId: userId, status: STATUSES.friend},
{userId: friendId, status: STATUSES.friend}
);
}
},
denyRequest: function (userId, friendId) {
var validated = validations.validateUserRelations(
{userId: userId, status: STATUSES.pending},
{userId: friendId, status: STATUSES.request}
);
// TODO: add option to choose between empty for the request sender or
// pending, for privacy
if (validated) {
Meteor.call('updateFriendStatus',
{userId: userId, status: STATUSES.empty},
{userId: friendId, status: STATUSES.empty}
);
}
},
removeFriend: function (userId, friendId) {
var validated = validations.validateUserRelations(
{userId: userId, status: STATUSES.friend},
{userId: friendId, status: STATUSES.friend}
);
// TODO: add option to choose between empty for the removed friend or
// friend, for privacy
if (validated) {
Meteor.call('updateFriendStatus',
{userId: userId, status: STATUSES.empty},
{userId: friendId, status: STATUSES.empty}
);
}
}
});
/**
* wrapper method for the Meteor friendRequest method
*/
Friends.friendRequest = function (userId, friendId) {
Meteor.call('friendRequest', userId, friendId);
};
/**
* wrapper method for the Meteor confirmRequest method
*/
Friends.confirmRequest = function (userId, friendId) {
Meteor.call('confirmRequest', userId, friendId);
};
/**
* wrapper method for the Meteor denyRequest method
*/
Friends.denyRequest = function (userId, friendId) {
Meteor.call('denyRequest', userId, friendId);
};
/**
* wrapper method for the Meteor removeFriend method
*/
Friends.removeFriend = function (userId, friendId) {
Meteor.call('removeFriend', userId, friendId);
};