Skip to content

Commit

Permalink
Fixing conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed May 9, 2024
2 parents 3c2dfac + 59ee448 commit 98f60fe
Show file tree
Hide file tree
Showing 75 changed files with 5,609 additions and 1,803 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ GIT

GIT
remote: https://github.com/meedan/rails-i18n.git
revision: ba4b3a267ff09b58a29fe8fc9eb002f9b75d7cba
revision: f8dd347402d9de9125af4db05f94f6933147b18f
branch: rails-6-x
specs:
rails-i18n (6.0.0)
Expand Down
16 changes: 14 additions & 2 deletions app/controllers/api/v1/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,22 @@ def save_facebook_credentials_for_smooch_bot(platform) # "platform" is either "i
tbi = TeamBotInstallation.find(params[:id])
auth = session['check.facebook.authdata']
status = nil
if params[:token].to_s.gsub('#_=_', '') == tbi.get_smooch_authorization_token
response = Net::HTTP.get_response(URI("https://graph.facebook.com/me/accounts?client_id=#{CheckConfig.get('smooch_facebook_app_id')}&client_secret=#{CheckConfig.get('smooch_facebook_app_secret')}&access_token=#{auth['token']}&limit=100"))
if auth.blank?
status = 400
@message = I18n.t(:invalid_facebook_authdata)
error_msg = StandardError.new('Could not authenticate Facebook account for tipline Messenger integration.')
CheckSentry.notify(error_msg, team_bot_installation_id: tbi.id, platform: platform)
elsif params[:token].to_s.gsub('#_=_', '') == tbi.get_smooch_authorization_token
q_params = {
client_id: CheckConfig.get('smooch_facebook_app_id'),
client_secret: CheckConfig.get('smooch_facebook_app_secret'),
access_token: auth['token'],
limit: 100,
}
response = Net::HTTP.get_response(URI("https://graph.facebook.com/me/accounts?#{q_params.to_query}"))
pages = JSON.parse(response.body)['data']
if pages.size != 1
CheckSentry.notify(StandardError.new('Unexpected list of Facebook pages returned for tipline Messenger integration'), team_bot_installation_id: tbi.id, response: response.body)
@message = I18n.t(:must_select_exactly_one_facebook_page)
status = 400
else
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/api/v1/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def batch
protected

def parse_graphql_result
log_graphql_activity
context = { ability: @ability, file: parse_uploaded_files }
@output = nil
begin
Expand All @@ -64,6 +65,17 @@ def parse_graphql_result
end
end

def log_graphql_activity
return if User.current.nil?

uid = User.current.id
user_name = User.current.name
team = Team.current || User.current.current_team
team = team.nil? ? '' : team.name
role = User.current.role
Rails.logger.info("[Graphql] Logging activity: uid: #{uid} user_name: #{user_name} team: #{team} role: #{role}")
end

def parse_uploaded_files
file_param = request.params[:file]
file = file_param
Expand Down
11 changes: 11 additions & 0 deletions app/graph/mutations/api_key_mutations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ApiKeyMutations
MUTATION_TARGET = 'api_key'.freeze
PARENTS = ['team'].freeze

class Create < Mutations::CreateMutation
argument :title, GraphQL::Types::String, required: false
argument :description, GraphQL::Types::String, required: false
end

class Destroy < Mutations::DestroyMutation; end
end
25 changes: 25 additions & 0 deletions app/graph/mutations/explainer_mutations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module ExplainerMutations
MUTATION_TARGET = 'explainer'.freeze
PARENTS = ['team'].freeze

module SharedCreateAndUpdateFields
extend ActiveSupport::Concern

included do
argument :title, GraphQL::Types::String, required: true
argument :description, GraphQL::Types::String, required: false
argument :url, GraphQL::Types::String, required: false
argument :language, GraphQL::Types::String, required: false
end
end

class Create < Mutations::CreateMutation
include SharedCreateAndUpdateFields
end

class Update < Mutations::UpdateMutation
include SharedCreateAndUpdateFields
end

class Destroy < Mutations::DestroyMutation; end
end
21 changes: 21 additions & 0 deletions app/graph/mutations/feed_mutations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,25 @@ class Update < Mutations::UpdateMutation
end

class Destroy < Mutations::DestroyMutation; end

class ImportMedia < Mutations::BaseMutation
argument :feed_id, GraphQL::Types::Int, required: true
argument :project_media_id, GraphQL::Types::Int, required: true
argument :parent_id, GraphQL::Types::Int, required: false
argument :claim_title, GraphQL::Types::String, required: false
argument :claim_context, GraphQL::Types::String, required: false

field :project_media, ProjectMediaType, null: false

def resolve(feed_id:, project_media_id:, parent_id: nil, claim_title: nil, claim_context: nil)
ability = context[:ability] || Ability.new
feed = Feed.find(feed_id)
pm = nil
if Team.current&.id && User.current&.id && ability.can?(:import_media, feed)
cluster = Cluster.where(feed_id: feed_id, project_media_id: project_media_id).last
pm = cluster.import_medias_to_team(Team.current, claim_title, claim_context, parent_id)
end
{ project_media: pm }
end
end
end
17 changes: 17 additions & 0 deletions app/graph/types/api_key_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ApiKeyType < DefaultObject
description "ApiKey type"

implements GraphQL::Types::Relay::Node

field :dbid, GraphQL::Types::Int, null: true
field :team_id, GraphQL::Types::Int, null: true
field :user_id, GraphQL::Types::Int, null: true
field :title, GraphQL::Types::String, null: true
field :access_token, GraphQL::Types::String, null: true
field :description, GraphQL::Types::String, null: true
field :application, GraphQL::Types::String, null: true
field :expire_at, GraphQL::Types::String, null: true

field :team, TeamType, null: true
field :user, UserType, null: true
end
6 changes: 6 additions & 0 deletions app/graph/types/article_union.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ArticleUnion < BaseUnion
description 'A union type of all article types we can handle'
possible_types(
ExplainerType,
)
end
5 changes: 3 additions & 2 deletions app/graph/types/cluster_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ def cluster_teams
end

field :project_medias, ProjectMediaType.connection_type, null: true do
argument :team_id, GraphQL::Types::Int, required: true
argument :team_id, GraphQL::Types::Int, required: false
end

def project_medias(team_id:)
def project_medias(team_id: nil)
team_id ||= Team.current.id
return ProjectMedia.none unless object.team_ids.include?(team_id)
object.project_medias.where(team_id: team_id.to_i)
end
Expand Down
21 changes: 21 additions & 0 deletions app/graph/types/explainer_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ExplainerType < DefaultObject
description "Explainer type"

implements GraphQL::Types::Relay::Node

field :dbid, GraphQL::Types::Int, null: true
field :title, GraphQL::Types::String, null: true
field :description, GraphQL::Types::String, null: true
field :url, GraphQL::Types::String, null: true
field :language, GraphQL::Types::String, null: true
field :user_id, GraphQL::Types::Int, null: true
field :team_id, GraphQL::Types::Int, null: true
field :user, UserType, null: true
field :team, PublicTeamType, null: true

field :tags, TagType.connection_type, null: true

def tags
Tag.where(annotation_type: 'tag', annotated_type: object.class.name, annotated_id: object.id)
end
end
8 changes: 8 additions & 0 deletions app/graph/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class MutationType < BaseObject
field :createFeed, mutation: FeedMutations::Create
field :updateFeed, mutation: FeedMutations::Update
field :destroyFeed, mutation: FeedMutations::Destroy
field :feedImportMedia, mutation: FeedMutations::ImportMedia

field :updateFeedTeam, mutation: FeedTeamMutations::Update
field :destroyFeedTeam, mutation: FeedTeamMutations::Destroy
Expand All @@ -141,4 +142,11 @@ class MutationType < BaseObject

field :addNluKeywordToTiplineMenu, mutation: NluMutations::AddKeywordToTiplineMenu
field :removeNluKeywordFromTiplineMenu, mutation: NluMutations::RemoveKeywordFromTiplineMenu

field :createExplainer, mutation: ExplainerMutations::Create
field :updateExplainer, mutation: ExplainerMutations::Update
field :destroyExplainer, mutation: ExplainerMutations::Destroy

field :createApiKey, mutation: ApiKeyMutations::Create
field :destroyApiKey, mutation: ApiKeyMutations::Destroy
end
19 changes: 14 additions & 5 deletions app/graph/types/project_media_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ class ProjectMediaType < DefaultObject
field :custom_title, GraphQL::Types::String, null: true
field :title_field, GraphQL::Types::String, null: true
field :suggestions_count, GraphQL::Types::Int, null: true
field :imported_from_feed_id, GraphQL::Types::Int, null: true
field :imported_from_project_media_id, GraphQL::Types::Int, null: true
field :imported_from_feed, FeedType, null: true

def imported_from_feed
ability = context[:ability] || Ability.new
feed = Feed.find_by_id(object.imported_from_feed_id)
(feed && ability.can?(:read, feed)) ? feed : nil
end

field :claim_description, ClaimDescriptionType, null: true

Expand Down Expand Up @@ -205,12 +214,12 @@ def comments
object.get_annotations("comment").map(&:load)
end

field :requests,
TiplineRequestType.connection_type,
null: true
field :requests, TiplineRequestType.connection_type, null: true do
argument :include_children, GraphQL::Types::Boolean, required: false
end

def requests
object.get_requests
def requests(include_children: false)
object.get_requests(include_children)
end

field :last_status, GraphQL::Types::String, null: true
Expand Down
34 changes: 34 additions & 0 deletions app/graph/types/team_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ def get_shorten_outgoing_urls
object.get_shorten_outgoing_urls
end

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

def get_explainers_enabled
object.get_explainers_enabled
end

field :public_team, PublicTeamType, null: true

def public_team
Expand Down Expand Up @@ -281,4 +287,32 @@ def shared_teams
def tipline_messages(uid:)
TiplineMessagesPagination.new(object.tipline_messages.where(uid: uid).order('sent_at DESC'))
end

field :articles, ::ArticleUnion.connection_type, null: true do
argument :article_type, GraphQL::Types::String, required: true, camelize: false
end

def articles(article_type:)
object.explainers if article_type == 'explainer'
end

field :api_key, ApiKeyType, null: true do
argument :dbid, GraphQL::Types::Int, required: true
end

def api_key(dbid:)
ability = context[:ability] || Ability.new
api_key = object.get_api_key(dbid)
ability.can?(:read, api_key) ? api_key : nil
end

field :api_keys, ApiKeyType.connection_type, null: true
def api_keys
ability = context[:ability] || Ability.new
api_keys = object.api_keys.order(created_at: :desc)

api_keys.select do |api_key|
ability.can?(:read, api_key)
end
end
end
2 changes: 2 additions & 0 deletions app/mailers/updated_terms_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class UpdatedTermsMailer < ApplicationMailer
layout nil

rescue_from(Net::SMTPFatalError) { nil }

def notify(recipient, name)
@name = name
@accept_terms_url = CheckConfig.get('tos_url')
Expand Down
7 changes: 6 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def admin_perms
can :destroy, FeedTeam do |obj|
obj.team_id == @context_team.id || obj.feed.team_id == @context_team.id
end
can [:create, :update, :read, :destroy], ApiKey, :team_id => @context_team.id
end

def editor_perms
Expand Down Expand Up @@ -110,7 +111,10 @@ def editor_perms
end
can [:read], FeedTeam, :team_id => @context_team.id
can [:read], FeedInvitation, { feed: { team_id: @context_team.id } }
can [:read, :create, :update], Feed, :team_id => @context_team.id
can [:read, :create, :update, :import_media], Feed, :team_id => @context_team.id
can :import_media, Feed do |obj|
obj.team_ids.include?(@context_team.id)
end
end

def collaborator_perms
Expand Down Expand Up @@ -167,6 +171,7 @@ def collaborator_perms
!v_obj.nil? and v_obj.team_id == @context_team.id and v_obj.media.user_id = @user.id
end
can [:create, :update, :read, :destroy], FactCheck, { claim_description: { project_media: { team_id: @context_team.id } } }
can [:create, :update, :read, :destroy], Explainer, team_id: @context_team.id
can [:create, :update, :read], ClaimDescription, { project_media: { team_id: @context_team.id } }
end

Expand Down
35 changes: 33 additions & 2 deletions app/models/api_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ class Check::TooManyRequestsError < StandardError
end

class ApiKey < ApplicationRecord
belongs_to :team, optional: true
belongs_to :user, optional: true

validates_presence_of :access_token, :expire_at
validates_uniqueness_of :access_token
validates :title, uniqueness: { scope: :team }

before_validation :generate_access_token, on: :create
before_validation :calculate_expiration_date, on: :create
before_validation :set_user_and_team
after_create :create_bot_user

validate :validate_team_api_keys_limit, on: :create

has_one :bot_user
has_one :bot_user, dependent: :destroy

# Reimplement this method in your application
def self.applications
Expand All @@ -34,7 +42,30 @@ def generate_access_token
end
end

def create_bot_user
if self.bot_user.blank? && self.team.present?
bot_name = "#{self.team.slug}-bot-#{self.title}"
new_bot_user = BotUser.new(api_key: self, name: bot_name, login: bot_name)
new_bot_user.skip_check_ability = true
new_bot_user.set_role 'editor'
new_bot_user.save!
end
end

def set_user_and_team
self.user = User.current unless User.current.nil?
self.team = Team.current unless Team.current.nil?
end

def calculate_expiration_date
self.expire_at ||= Time.now.since(30.days)
api_default_expiry_days = CheckConfig.get('api_default_expiry_days', 30).to_i
self.expire_at ||= Time.now.since(api_default_expiry_days.days)
end

def validate_team_api_keys_limit
return unless team

max_team_api_keys = CheckConfig.get('max_team_api_keys', 20).to_i
errors.add(:base, "Maximum number of API keys exceeded") if team.api_keys.count >= max_team_api_keys
end
end
Loading

0 comments on commit 98f60fe

Please sign in to comment.