Skip to content

Commit

Permalink
Fixing conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Nov 29, 2023
2 parents f5b898c + 651feb9 commit 4fd7fb9
Show file tree
Hide file tree
Showing 123 changed files with 6,402 additions and 2,051 deletions.
19 changes: 15 additions & 4 deletions app/controllers/api/v1/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Api::V1::AdminController < Api::V1::BaseApiController
before_action :authenticate_from_token!, except: [:add_publisher_to_project, :save_twitter_credentials_for_smooch_bot, :save_facebook_credentials_for_smooch_bot]
before_action :authenticate_from_token!, except: [:add_publisher_to_project, :save_twitter_credentials_for_smooch_bot, :save_messenger_credentials_for_smooch_bot, :save_instagram_credentials_for_smooch_bot]

# GET /api/admin/project/add_publisher?token=:project-token
def add_publisher_to_project
Expand Down Expand Up @@ -51,8 +51,19 @@ def save_twitter_credentials_for_smooch_bot
render template: 'message', formats: :html, status: status
end

# GET /api/admin/smooch_bot/:bot-installation-id/authorize/facebook?token=:bot-installation-token
def save_facebook_credentials_for_smooch_bot
# GET /api/admin/smooch_bot/:bot-installation-id/authorize/messenger?token=:bot-installation-token
def save_messenger_credentials_for_smooch_bot
self.save_facebook_credentials_for_smooch_bot('messenger')
end

# GET /api/admin/smooch_bot/:bot-installation-id/authorize/instagram?token=:bot-installation-token
def save_instagram_credentials_for_smooch_bot
self.save_facebook_credentials_for_smooch_bot('instagram')
end

private

def save_facebook_credentials_for_smooch_bot(platform) # "platform" is either "instagram" or "messenger"
tbi = TeamBotInstallation.find(params[:id])
auth = session['check.facebook.authdata']
status = nil
Expand All @@ -68,7 +79,7 @@ def save_facebook_credentials_for_smooch_bot
'appSecret' => CheckConfig.get('smooch_facebook_app_secret'),
'pageAccessToken' => pages[0]['access_token']
}
tbi.smooch_add_integration('messenger', params)
tbi.smooch_add_integration(platform, params)
@message = I18n.t(:smooch_facebook_success)
status = 200
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/facebook_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def setup_facebook
# pages_manage_metadata is for Facebook API > 7
# manage_pages is for Facebook API < 7
# An error will be displayed for Facebook users that are admins of the Facebook app, but should be transparent for other users
request.env['omniauth.strategy'].options[:scope] = 'pages_manage_metadata,manage_pages,pages_messaging' if params[:context] == 'smooch'
request.env['omniauth.strategy'].options[:scope] = 'pages_manage_metadata,manage_pages,pages_messaging,instagram_manage_messages,instagram_basic' if params[:context] == 'smooch'
prefix = facebook_context == 'smooch' ? 'smooch_' : ''
request.env['omniauth.strategy'].options[:client_id] = CheckConfig.get("#{prefix}facebook_app_id")
request.env['omniauth.strategy'].options[:client_secret] = CheckConfig.get("#{prefix}facebook_app_secret")
Expand Down
44 changes: 44 additions & 0 deletions app/graph/mutations/feed_invitation_mutations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module FeedInvitationMutations
MUTATION_TARGET = 'feed_invitation'.freeze
PARENTS = ['feed'].freeze

class Create < Mutations::CreateMutation
argument :email, GraphQL::Types::String, required: true
argument :feed_id, GraphQL::Types::Int, required: true, camelize: false
end

class Destroy < Mutations::DestroyMutation; end

class Accept < Mutations::UpdateMutation
argument :id, GraphQL::Types::Int, required: true
argument :team_id, GraphQL::Types::Int, required: true, camelize: false

field :success, GraphQL::Types::Boolean, null: true

def resolve(id:, team_id:)
success = false
feed_invitation = FeedInvitation.find_if_can(id, context[:ability])
if User.current && Team.current && User.current.team_ids.include?(team_id) && feed_invitation.email == User.current.email
feed_invitation.accept!(team_id)
success = true
end
{ success: success }
end
end

class Reject < Mutations::BaseMutation
argument :id, GraphQL::Types::Int, required: true

field :success, GraphQL::Types::Boolean, null: true

def resolve(id:)
success = false
feed_invitation = FeedInvitation.find_if_can(id, context[:ability])
if User.current && Team.current && feed_invitation.email == User.current.email && feed_invitation.state == 'invited'
feed_invitation.reject!
success = true
end
{ success: success }
end
end
end
2 changes: 2 additions & 0 deletions app/graph/mutations/feed_mutations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ class Update < Mutations::UpdateMutation

argument :name, GraphQL::Types::String, required: false
end

class Destroy < Mutations::DestroyMutation; end
end
2 changes: 2 additions & 0 deletions app/graph/mutations/feed_team_mutations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ class Update < Mutations::UpdateMutation
argument :shared, GraphQL::Types::Boolean, required: false
argument :requests_filters, JsonStringType, required: false, camelize: false
end

class Destroy < Mutations::DestroyMutation; end
end
50 changes: 50 additions & 0 deletions app/graph/mutations/tipline_messages_pagination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class TiplineMessagesPagination < GraphQL::Pagination::ArrayConnection
# def cursor_for(item)
# encode(item.id.to_i.to_s)
# end
#
# def load_nodes
# @nodes ||= begin
# sliced_nodes = if before && after
# end_idx = index_from_cursor(before)
# start_idx = index_from_cursor(after)
# items.where(id: start_idx..end_idx)
# elsif before
# end_idx = index_from_cursor(before)
# items.where('id < ?', end_idx)
# elsif after
# start_idx = index_from_cursor(after)
# items.where('id > ?', start_idx)
# else
# items
# end
#
# @has_previous_page = if last
# # There are items preceding the ones in this result
# sliced_nodes.count > last
# elsif after
# # We've paginated into the Array a bit, there are some behind us
# index_from_cursor(after) > items.map(&:id).min
# else
# false
# end
#
# @has_next_page = if first
# # There are more items after these items
# sliced_nodes.count > first
# elsif before
# # The original array is longer than the `before` index
# index_from_cursor(before) < items.map(&:id).max
# else
# false
# end
#
# limited_nodes = sliced_nodes
#
# limited_nodes = limited_nodes.first(first) if first
# limited_nodes = limited_nodes.last(last) if last
#
# limited_nodes
# end
# end
end
13 changes: 13 additions & 0 deletions app/graph/types/feed_invitation_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class FeedInvitationType < DefaultObject
description "Feed invitation type"

implements GraphQL::Types::Relay::Node

field :dbid, GraphQL::Types::Int, null: false
field :feed_id, GraphQL::Types::Int, null: false
field :feed, FeedType, null: false
field :user_id, GraphQL::Types::Int, null: false
field :user, UserType, null: false
field :state, GraphQL::Types::String, null: false
field :email, GraphQL::Types::String, null: false
end
11 changes: 11 additions & 0 deletions app/graph/types/feed_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ class FeedType < DefaultObject
def requests(**args)
object.search(args)
end

field :feed_invitations, FeedInvitationType.connection_type, null: false

def feed_invitations
ability = context[:ability] || Ability.new
return FeedInvitation.none unless ability.can?(:read_feed_invitations, object)
object.feed_invitations
end

field :teams, TeamType.connection_type, null: false
field :feed_teams, FeedTeamType.connection_type, null: false
end
7 changes: 7 additions & 0 deletions app/graph/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,15 @@ class MutationType < BaseObject

field :createFeed, mutation: FeedMutations::Create
field :updateFeed, mutation: FeedMutations::Update
field :destroyFeed, mutation: FeedMutations::Destroy

field :updateFeedTeam, mutation: FeedTeamMutations::Update
field :destroyFeedTeam, mutation: FeedTeamMutations::Destroy

field :createFeedInvitation, mutation: FeedInvitationMutations::Create
field :destroyFeedInvitation, mutation: FeedInvitationMutations::Destroy
field :acceptFeedInvitation, mutation: FeedInvitationMutations::Accept
field :rejectFeedInvitation, mutation: FeedInvitationMutations::Reject

field :createTiplineNewsletter, mutation: TiplineNewsletterMutations::Create
field :updateTiplineNewsletter, mutation: TiplineNewsletterMutations::Update
Expand Down
2 changes: 1 addition & 1 deletion app/graph/types/project_media_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def comments
end

field :requests,
DynamicAnnotationFieldType.connection_type,
TiplineRequestType.connection_type,
null: true

def requests
Expand Down
22 changes: 22 additions & 0 deletions app/graph/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,27 @@ def dynamic_annotation_field(query:, only_cache: nil)
end
end

field :feed_invitation, FeedInvitationType, description: 'Information about a feed invitation, given its database ID or feed database ID (and then the current user email is used)', null: true do
argument :id, GraphQL::Types::Int, required: false
argument :feed_id, GraphQL::Types::Int, required: false
end

def feed_invitation(id: nil, feed_id: nil)
feed_invitation_id = id || FeedInvitation.where(feed_id: feed_id, email: User.current.email).last&.id
GraphqlCrudOperations.load_if_can(FeedInvitation, feed_invitation_id, context)
end

field :feed_team, FeedTeamType, description: 'Information about a feed team, given its database ID or the combo feed database ID plus team slug', null: true do
argument :id, GraphQL::Types::Int, required: false
argument :feed_id, GraphQL::Types::Int, required: false
argument :team_slug, GraphQL::Types::String, required: false
end

def feed_team(id: nil, feed_id: nil, team_slug: nil)
feed_team_id = id || FeedTeam.where(feed_id: feed_id, team_id: Team.find_by_slug(team_slug).id).last&.id
GraphqlCrudOperations.load_if_can(FeedTeam, feed_team_id, context)
end

# Getters by ID
%i[
source
Expand All @@ -214,6 +235,7 @@ def dynamic_annotation_field(query:, only_cache: nil)
cluster
feed
request
tipline_message
].each do |type|
field type,
"#{type.to_s.camelize}Type",
Expand Down
3 changes: 2 additions & 1 deletion app/graph/types/team_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def shared_teams
field :saved_searches, SavedSearchType.connection_type, null: true
field :project_groups, ProjectGroupType.connection_type, null: true
field :feeds, FeedType.connection_type, null: true
field :feed_teams, FeedTeamType.connection_type, null: false
field :tipline_newsletters, TiplineNewsletterType.connection_type, null: true
field :tipline_resources, TiplineResourceType.connection_type, null: true

Expand All @@ -301,6 +302,6 @@ def shared_teams
end

def tipline_messages(uid:)
object.tipline_messages.where(uid: uid).order('sent_at DESC')
TiplineMessagesPagination.new(object.tipline_messages.where(uid: uid).order('sent_at DESC'))
end
end
5 changes: 5 additions & 0 deletions app/graph/types/tipline_message_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ class TiplineMessageType < DefaultObject
field :team, TeamType, null: true
field :sent_at, GraphQL::Types::String, null: true, camelize: false
field :media_url, GraphQL::Types::String, null: true
field :cursor, GraphQL::Types::String, null: true

def sent_at
object.sent_at.to_i.to_s
end

# def cursor
# GraphQL::Schema::Base64Encoder.encode(object.id.to_i.to_s)
# end
end
19 changes: 19 additions & 0 deletions app/graph/types/tipline_request_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class TiplineRequestType < DefaultObject
description "TiplineRequest type"

implements GraphQL::Types::Relay::Node

field :dbid, GraphQL::Types::Int, null: true
field :value_json, JsonStringType, null: true
field :annotation, AnnotationType, null: true
field :annotation_id, GraphQL::Types::Int, null: true
field :associated_graphql_id, GraphQL::Types::String, null: true
field :smooch_user_slack_channel_url, GraphQL::Types::String, null: true
field :smooch_user_external_identifier, GraphQL::Types::String, null: true
field :smooch_report_received_at, GraphQL::Types::Int, null: true
field :smooch_report_update_received_at, GraphQL::Types::Int, null: true
field :smooch_user_request_language, GraphQL::Types::String, null: true
field :smooch_report_sent_at, GraphQL::Types::Int, null: true
field :smooch_report_correction_sent_at, GraphQL::Types::Int, null: true
field :smooch_request_type, GraphQL::Types::String, null: true
end
7 changes: 7 additions & 0 deletions app/graph/types/user_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,11 @@ def assignments(team_id: nil)
# pms.reject { |pm| pm.is_finished? }
pms
end

field :feed_invitations, FeedInvitationType.connection_type, null: false

def feed_invitations
return FeedInvitation.none if object.email.blank?
FeedInvitation.where(email: object.email)
end
end
4 changes: 3 additions & 1 deletion app/lib/check_channels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def self.all_channels
"TELEGRAM" => TELEGRAM,
"VIBER" => VIBER,
"LINE" => LINE,
"INSTAGRAM" => INSTAGRAM,
},
"WEB_FORM" => WEB_FORM,
"SHARED_DATABASE" => SHARED_DATABASE
Expand All @@ -30,7 +31,8 @@ def self.all_channels
TELEGRAM = 8
VIBER = 9
LINE = 10
TIPLINE = [WHATSAPP, MESSENGER, TWITTER, TELEGRAM, VIBER, LINE]
INSTAGRAM = 13
TIPLINE = [WHATSAPP, MESSENGER, TWITTER, TELEGRAM, VIBER, LINE, INSTAGRAM]
WEB_FORM = 11
SHARED_DATABASE = 12
ALL = [MANUAL, FETCH, BROWSER_EXTENSION, API, ZAPIER, WEB_FORM, SHARED_DATABASE] + TIPLINE
Expand Down
4 changes: 2 additions & 2 deletions app/lib/check_permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def permissions(ability = nil, klass = self.class)
if self.class.name == 'Team'
role = User.current.role(self)
role ||= 'authenticated'
cache_key = "team_permissions_#{self.private.to_i}_#{role}_role"
cache_key = "team_permissions_#{self.private.to_i}_#{role}_role_202311221222"
perms = Rails.cache.read(cache_key) if Rails.cache.exist?(cache_key)
end
if perms.blank?
Expand All @@ -77,7 +77,7 @@ def set_custom_permissions(ability = nil)

def get_create_permissions
{
'Team' => [Project, Account, TeamUser, User, TagText, ProjectMedia, TiplineNewsletter, Feed],
'Team' => [Project, Account, TeamUser, User, TagText, ProjectMedia, TiplineNewsletter, Feed, FeedTeam, FeedInvitation],
'Account' => [Media, Link, Claim],
'Media' => [ProjectMedia, Comment, Tag, Dynamic, Task],
'Link' => [ProjectMedia, Comment, Tag, Dynamic, Task],
Expand Down
4 changes: 2 additions & 2 deletions app/lib/smooch_nlu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def update_keywords(language, keywords, keyword, operation, doc_id, context)
alegre_params = common_alegre_params.merge({ quiet: true })
end
# FIXME: Add error handling and better logging
Bot::Alegre.request_api(alegre_operation, '/text/similarity/', alegre_params) if alegre_operation && alegre_params
Bot::Alegre.request(alegre_operation, '/text/similarity/', alegre_params) if alegre_operation && alegre_params
keywords
end

Expand Down Expand Up @@ -79,7 +79,7 @@ def self.alegre_matches_from_message(message, language, context, alegre_result_k
language: language,
}.merge(context)
}
response = Bot::Alegre.request_api('get', '/text/similarity/', params)
response = Bot::Alegre.request('post', '/text/similarity/search/', params)

# One approach would be to take the option that has the most matches
# Unfortunately this approach is influenced by the number of keywords per option
Expand Down
2 changes: 2 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def mail(options={})
return if options[:to].blank?
options[:to] = [options[:to]].flatten.collect{ |to| to.gsub(/[\u200B-\u200D\uFEFF]/, '') }
@direction = ApplicationMailer.set_template_direction
attachments.inline['checklogo.png'] = File.read('public/images/checklogo.png')
@logo_url = attachments['checklogo.png'].url
super(options)
end

Expand Down
Loading

0 comments on commit 4fd7fb9

Please sign in to comment.