friends-of-friends
Add Friendship Management to any Mongoose Schema
friends-of-friends makes it nearly painless to develop custom social-networking applications by allowing you send/accept/deny friend requests, and search for friends, pending friends, friends of friends, and non-friends from a Model or Document.
For details, see the API and Usage sections below.
How does it work?
friends-of-friends defines a friendships
collection that is used to store 1-to-1 friendships with pending/accepted status between users. It also provides a mongoose plugin to add functionality to your existing user model providing support to send/get/accept/deny friendship requests as well as query for friends/non-friends/pending-friends/friends-of-friends using mongoose's rich Model.find() syntax.
Do I need to modify my user schema to use friends-of-friends?
friends-of-friends currently (in the future, this will be configurable) depends on the _id
field to be an ObjectId
to track who is friends with who, but is otherwise unopinionated and leaves all design decisions up to you. So long as you have not implemented fields/properties with the same name as any of the static/instance methods provided by the plugin, all functionality should work out-of-box.
Contributing
I'm sure there are bugs, please help me find/fix them! If you make valuable contributions, I'll make you a collaborator :)
See the Contribution Guide for more information on how to contribute, run tests, and generate coverage reports.
API
API documentation is located in doc/
Installation
$ npm install --save friends-of-friends
Usage
Create a new FriendsOfFriends Object
// mongoose is requiredvar mongoose = ; var FriendsOfFriends = ;var fof = mongoose;// works with or without 'new'var FriendsOfFriends = mongoose;
Default Configuration Options
var defaults = // define the name for your Users model. personModelName: 'Person' // define the name for the Friendship model friendshipModelName: 'Friendship' // define the name of the Friendship collection. friendshipCollectionName: undefined
Specifying Configuration Options
var options = personModelName: 'Player' friendshipModelName: 'Friend_Relationships' friendshipCollectionName: 'foo_bar_userRelationships'; var FriendsOfFriends = ;var fof = mongoose options;// again, works with or without 'new'var FriendsOfFriends = mongoose options;
Plug-in friends-of-friends to User Schema
// ... // your User schemavar UserSchema = username: String; // apply the friends-of-friends mongoose plugin to your User schemaUserSchema; // compile your user modelvar User = mongoose; // create usersvar Jeff = username: "Jeff" Zane = username: "Zane" Sam = username: "Sam"; console;console;console; // Jeff: { __v: 0, username: 'Jeff', _id: 54c6eb7cf2f9fe9672b90ba2 }// Zane: { __v: 0, username: 'Zane', _id: 54c6eb7cf2f9fe9672b90ba3 }// Sam: { __v: 0, username: 'Sam', _id: 54c6eb7cf2f9fe9672b90ba4 } // connect to dbmongoose;
Use Plugged-in functionality
Send Friend Requests
Jeff can ask Zane to be friends
Jeff;
Deny Friend Requests
Zane could deny Jeff's Request...
Zane;
Accept Friend Requests
... or Zane could accept Jeff's Request
Zane;
Get Friends
Now Jeff can get a list of his friends
// Zane is now Jeff's friendJeff;
Get Friends Of Friends
When two users are not friends, but have at least one friend in common, they are friends-of-friends
Zane;
License
Copyright (c) 2014-2015 Jeff Harris
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.