Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Backend fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeGEINE committed Jul 24, 2024
1 parent 3577a71 commit 258cd29
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 97 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@

.DS_Store
.idea/

.env.dev
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ SHELL=/bin/sh
UID := $(shell id -u)
GID := $(shell id -g)

include .env
export
# include .env
# export

setup: build db-prepare

Expand Down
50 changes: 27 additions & 23 deletions app/api/vk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def self.exchange_code(code, code_verifier, device_id, state)
code:,
code_verifier:,
device_id:,
redirect_uri: "#{ENV[HOST]}/auth/vkontakte/callback/",
redirect_uri: "#{ENV['HOST']}/auth/vkontakte/callback/",
state:,
client_id: '51989509' },
client: vkid_client)
Expand All @@ -56,19 +56,22 @@ def self.get_friends(user)
'/method/friends.get',
{ access_token: user.access_token,
v: '5.199',
fields: 'photo_100 first_name last_name' },
fields: %w[photo_50 first_name last_name] },
{ 'Authorization': "Bearer #{user.access_token}" })
response_hash = resp.body['response']['items']
friends_id_list_str = response_hash.map(&:to_s).join(', ')
puts 'FRIENDLY FIRE'
puts resp.body
# friends_id_list_str = response_hash.map(&:to_s).join(', ')

response_info = try_request(:get,
'/method/users.get',
{ access_token: user.access_token,
v: '5.199',
user_ids: friends_id_list_str,
fields: 'photo_100 first_name last_name' },
client:)
response_info.body['response']['items']
# response_info = try_request(:get,
# '/method/users.get',
# { access_token: user.access_token,
# v: '5.199',
# user_ids: friends_id_list_str,
# fields: 'photo_100 first_name last_name' },
# { 'Authorization': "Bearer #{user.access_token}" }
# client:)
response_hash
end

def self.get_followers(user)
Expand Down Expand Up @@ -106,7 +109,6 @@ def self.get_profile_info(user)
end

def self.get_data(user) # rubocop:disable Metrics/AbcSize
posts = []
resp = try_request(:get,
'/method/wall.get',
{ access_token: user.access_token,
Expand All @@ -116,7 +118,7 @@ def self.get_data(user) # rubocop:disable Metrics/AbcSize
filter: 'all' },
client:)
post_data_all = resp.body['response']['items']
post_data_all.each do |post_data|
post_data_all.map do |post_data|
like_count = post_data['likes']['count']
comment_count = post_data['comments']['count']

Expand All @@ -125,23 +127,23 @@ def self.get_data(user) # rubocop:disable Metrics/AbcSize
else
get_likers(post_data['id'], like_count.to_i, user)
end
post_commentators = if comment_count.zero?
[]
else
get_commentators(post_data['id'], comment_count.to_i, user)
end
post = {
# post_commentators = if comment_count.zero?
# []
# else
# get_commentators(post_data['id'], comment_count.to_i, user)
# end
{
post_id: post_data['id'],
date: post_data['date'],
image_url: get_image_from_post(post_data['attachments']),
count_likes: post_data['likes']['count'],
count_comments: post_data['comments']['count'],
# count_comments: post_data['comments']['count'],
count_comments: 0,
likers: post_likers,
commentators: post_commentators
# commentators: post_commentators
commentators: []
}
posts << post
end
posts
end

# проверить утром на работо способность!!!!!!!!!!!
Expand Down Expand Up @@ -266,6 +268,8 @@ def self.get_commentators(post_id, comments_count, user)
count: 100,
offset: 100 * i },
client:)
puts 'RESPONSE'
puts resp.body
comments = resp.body['response']['items']
current_commentators = comments.map { |comment| comment['from_id'] }
commentators << current_commentators
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

class HomeController < ApplicationController
def index
if current_user
redirect_to root_path
else
unless current_user
pkce_challenge = PkceChallenge.challenge
session[:code_verifier] = pkce_challenge.code_verifier
@data_attributes = {
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def create # rubocop:disable Metrics/AbcSize
unless user_signed_in?
response = Vk.exchange_code(params['code'], session[:code_verifier], params['device_id'], params['state'])
# поиск пользователя или создание его
user = User.user_create(response.merge({ deviceid: params['device_id'], state: params['state'],
expires_in: params['expires_in'] }))
user = User.user_create(response.merge({ deviceid: params['device_id'], state: params['state'] }))
user.link_friends Vk.get_friends(user).map { |h| Friend.from_vk h }
Vk.get_data(user).map { |h| Post.from_vk h, user }
sign_in user
end
redirect_to root_path
Expand Down
21 changes: 18 additions & 3 deletions app/models/friend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@ class Friend < ApplicationRecord
has_many :relationships
has_many :users, through: :relationships

validates :first_name, presence: true
validates :last_name, presence: true
validates :vk_uid, presence: true, uniqueness: true
# validates :first_name, presence: true
# validates :last_name, presence: true
# validates :vk_uid, presence: true, uniqueness: true

def self.from_vk(payload)
existing_friend=find_or_create_by(vk_uid: payload["id"]) do |friend|
# friend.vk_uid = payload[:id],
friend.first_name =payload['first_name'],
friend.last_name = payload['last_name'],
friend.image_url = payload['photo_50']
end
existing_friend.update(
first_name: payload['first_name'],
last_name: payload['last_name'],
image_url: payload['photo_50']
)
existing_friend
end
end
23 changes: 23 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,27 @@ class Post < ApplicationRecord
belongs_to :user

validates :vk_uid, presence: true, uniqueness: true

def self.from_vk(payload, owner)
puts 'PAYLOAD!!!'
puts payload
existing_post= find_or_create_by(vk_uid: payload[:post_id]) do |post|
# post.vk_uid = payload[:post_id],
post.date = payload[:date],
post.image_url = payload[:image_url],
# post.count_likes = payload[:count_likes],
# post.count_comments = payload[:count_comments],
post.likes = payload[:likers],
post.comments = payload[:commentators]
post.user = owner
end
existing_post.update(
date: payload[:date],
image_url: payload[:image_url],
# count_likes: payload[:count_likes],
# count_comments: payload[:count_comments],
likes: payload[:likers],
comments: payload[:commentators])
existing_post
end
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ def self.user_create(response) # rubocop:disable Metrics/AbcSize
end
user
end

def link_friends(friends_array)
self.friends = friends_array
end
end
122 changes: 60 additions & 62 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
context: .
dockerfile: Dockerfile.dev
env_file:
- .env
- .env.dev
stdin_open: true
tty: true
volumes:
Expand All @@ -15,6 +15,7 @@ services:
- 12345:12345
depends_on:
- postgres
command: ash -c "rm -f tmp/pids/server.pid && bin/dev"
healthcheck:
test: curl --fail http://localhost:3000/up || exit 1
interval: 30s
Expand All @@ -27,7 +28,7 @@ services:
image: postgres:16-alpine
restart: always
env_file:
- .env
- .env.dev
ports:
- 5432:5432
volumes:
Expand Down

0 comments on commit 258cd29

Please sign in to comment.