-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from cardeons/develop
Release 0.6
- Loading branch information
Showing
55 changed files
with
2,694 additions
and
11,943 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,6 @@ Style/GuardClause: | |
Enabled: false | ||
|
||
AllCops: | ||
SuggestExtensions: false | ||
SuggestExtensions: false | ||
Excludes: | ||
- db/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
class FriendlistChannel < ApplicationCable::Channel | ||
def subscribed | ||
stream_for current_user | ||
|
||
current_user.update(status: :online) | ||
|
||
Friendship.broadcast_friends(current_user) | ||
Friendship.broadcast_pending_requests(current_user) | ||
end | ||
|
||
def unsubscribed | ||
current_user.update(status: :offline) | ||
end | ||
|
||
def send_friend_request(data) | ||
future_friend = User.find_by('id=?', data['friend']) | ||
|
||
Friendship.add_friend(current_user, future_friend) | ||
|
||
broadcast_to(current_user, { type: 'FRIEND_LOG', params: { message: "You sent a friendrequest to #{future_friend.name}" } }) | ||
broadcast_to(future_friend, { type: 'FRIEND_LOG', params: { message: "#{current_user.name} sent you a friendrequest" } }) | ||
broadcast_to(future_friend, { type: 'FRIEND_REQUEST', params: { inquirer: current_user.id, inquirer_name: current_user.name } }) | ||
end | ||
|
||
def accept_friend_request(data) | ||
inquirer = User.find_by('id=?', data['inquirer']) | ||
|
||
Friendship.accept(current_user, inquirer) | ||
|
||
broadcast_to(current_user, { type: 'FRIEND_LOG', params: { message: "You accepted a friendrequest from #{inquirer.name}" } }) | ||
broadcast_to(inquirer, { type: 'FRIEND_LOG', params: { message: "#{current_user.name} accepted your friendrequest" } }) | ||
end | ||
|
||
def decline_friend_request(data) | ||
inquirer = User.find_by('id=?', data['inquirer']) | ||
|
||
Friendship.remove_friend(current_user, inquirer) | ||
broadcast_to(current_user, { type: 'FRIEND_LOG', params: { message: "You declined a friendrequest from #{inquirer.name}" } }) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Friend < ApplicationRecord | ||
has_many :friendships | ||
has_many :users, through: :friendships | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class Friendship < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :friend, class_name: 'User' | ||
belongs_to :inquirer, class_name: 'User' | ||
|
||
def self.add_friend(user1, user2) | ||
# user2.friends << user1 | ||
# user1.friends << user2 | ||
|
||
Friendship.create(user: user1, friend: user2, inquirer: user1) | ||
Friendship.create(user: user2, friend: user1, inquirer: user1) | ||
end | ||
|
||
def self.remove_friend(user1, user2) | ||
friendship1 = Friendship.where(['user_id = ? and friend_id = ?', user1.id, user2.id]).first | ||
friendship2 = Friendship.where(['user_id = ? and friend_id = ?', user2.id, user1.id]).first | ||
|
||
friendship1 ? Friendship.destroy(friendship1.id) : nil | ||
friendship2 ? Friendship.destroy(friendship2.id) : nil | ||
end | ||
|
||
def self.accept(user1, user2) | ||
friendship1 = Friendship.where(['user_id = ? and friend_id = ?', user1.id, user2.id]).first | ||
friendship2 = Friendship.where(['user_id = ? and friend_id = ?', user2.id, user1.id]).first | ||
|
||
friendship1.update!(pending: false) | ||
friendship2.update!(pending: false) | ||
end | ||
|
||
def self.broadcast_pending_requests(current_user) | ||
pending_requests = Friendship.where(['user_id = ? and pending = ?', current_user.id, true]) | ||
|
||
pending_requests.each do |request| | ||
FriendlistChannel.broadcast_to(current_user, { type: 'FRIEND_REQUEST', params: { inquirer: request.friend.id, inquirer_name: request.friend.name } }) if request.inquirer != current_user | ||
end | ||
end | ||
|
||
def self.broadcast_friends(current_user) | ||
friendships = Friendship.where(['user_id = ? and pending = ?', current_user.id, false]) | ||
|
||
friends_obj = [] | ||
|
||
friendships.each do |friendship| | ||
friends_obj.push({ name: friendship.friend.name, status: friendship.friend.status }) | ||
end | ||
|
||
FriendlistChannel.broadcast_to(current_user, { type: 'FRIENDLIST', params: { friends: friends_obj } }) | ||
end | ||
end |
Oops, something went wrong.