1
0
Fork 0
forked from gitea/nas

Wip: antenna

This commit is contained in:
KMY 2023-04-23 14:20:07 +09:00
parent 07ea091320
commit 2eb5ffb9b3
19 changed files with 610 additions and 1 deletions

203
app/models/antenna.rb Normal file
View file

@ -0,0 +1,203 @@
# == Schema Information
#
# Table name: antennas
#
# id :bigint(8) not null, primary key
# account_id :bigint(8) not null
# list_id :bigint(8) not null
# title :string default(""), not null
# keywords :jsonb
# exclude_keywords :jsonb
# any_domains :boolean default(TRUE), not null
# any_tags :boolean default(TRUE), not null
# any_accounts :boolean default(TRUE), not null
# any_keywords :boolean default(TRUE), not null
# available :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# expires_at :datetime
#
class Antenna < ApplicationRecord
include Expireable
has_many :antenna_domains, inverse_of: :antenna, dependent: :destroy
has_many :antenna_tags, inverse_of: :antenna, dependent: :destroy
has_many :antenna_accounts, inverse_of: :antenna, dependent: :destroy
belongs_to :account
belongs_to :list
scope :all_keywords, -> { where(any_keywords: true) }
scope :all_domains, -> { where(any_domains: true) }
scope :all_accounts, -> { where(any_accounts: true) }
scope :all_tags, -> { where(any_tags: true) }
scope :availables, -> { where(available: true) }
def enabled?
available && !(any_keywords && any_domains && any_accounts && any_tags) && !expires?
end
def expires_in
return @expires_in if defined?(@expires_in)
return nil if expires_at.nil?
[30.minutes, 1.hour, 6.hours, 12.hours, 1.day, 1.week].find { |expires_in| expires_in.from_now >= expires_at }
end
def expires?
expires_at.present? && expires_at < Time.now.utc
end
def context
context = []
context << 'domain' if !any_domains
context << 'tag' if !any_tags
context << 'keyword' if !any_keywords
context << 'account' if !any_accounts
context
end
def list=(list_id)
list_id = list_id.to_i if list_id.is_a?(String)
if list_id.is_a?(Numeric)
self[:list_id] = list_id
else
self[:list] = list_id
end
end
def keywords_raw
return '' if !keywords.present?
keywords.join("\n")
end
def keywords_raw=(raw)
keywords = raw.split(/\R/).filter { |r| r.present? }
self[:keywords] = keywords
self[:any_keywords] = !keywords.any? && !exclude_keywords&.any?
end
def exclude_keywords_raw
return '' if !exclude_keywords.present?
exclude_keywords.join("\n")
end
def exclude_keywords_raw=(raw)
exclude_keywords = raw.split(/\R/).filter { |r| r.present? }
self[:exclude_keywords] = exclude_keywords
self[:any_keywords] = !keywords&.any? && !exclude_keywords.any?
end
def tags_raw
antenna_tags.where(exclude: false).map(&:tag).map(&:name).join("\n")
end
def tags_raw=(raw)
return if tags_raw == raw
tag_names = raw.split(/\R/).filter { |r| r.present? }
antenna_tags.where(exclude: false).destroy_all!
Tag.find_or_create_by_names(tag_names).each do |tag|
antenna_tags.create!(tag: tag, exclude: false)
end
self[:any_tags] = !tag_names.any? && !exclude_tags&.any?
end
def exclude_tags_raw
antenna_tags.where(exclude: true).map(&:tag).map(&:name).join("\n")
end
def exclude_tags_raw=(raw)
return if exclude_tags_raw == raw
tag_names = raw.split(/\R/).filter { |r| r.present? }
antenna_tags.where(exclude: true).destroy_all!
Tag.find_or_create_by_names(tag_names).each do |tag|
antenna_tags.create!(tag: tag, exclude: true)
end
self[:any_tags] = !tag_names.any? && !tags&.any?
end
def domains_raw
antenna_domains.where(exclude: false).map(&:name).join("\n")
end
def domains_raw=(raw)
return if domains_raw == raw
domain_names = raw.split(/\R/).filter { |r| r.present? }
antenna_domains.where(exclude: false).destroy_all!
domain_names.each do |domain|
antenna_domains.create!(name: domain, exclude: false)
end
self[:any_domains] = !domain_names.any? && !exclude_domains&.any?
end
def exclude_domains_raw
antenna_domains.where(exclude: true).map(&:name).join("\n")
end
def exclude_domains_raw=(raw)
return if exclude_domains_raw == raw
domain_names = raw.split(/\R/).filter { |r| r.present? }
antenna_domains.where(exclude: true).destroy_all!
domain_names.each do |domain|
antenna_domains.create!(name: domain, exclude: true)
end
self[:any_domains] = !domain_names.any? && !domains&.any?
end
def accounts_raw
antenna_accounts.where(exclude: false).map(&:account).map { |account| account.domain ? "@#{account.username}@#{account.domain}" : "@#{account.username}" }.join("\n")
end
def accounts_raw=(raw)
return if accounts_raw == raw
account_names = raw.split(/\R/).filter { |r| r.present? }
hit = false
antenna_accounts.where(exclude: false).destroy_all!
account_names.each do |name|
name = name[1..-1] if name.start_with?('@')
username, domain = name.split('@')
account = Account.find_by(username: username, domain: domain)
if account.present?
antenna_accounts.create!(account: account, exclude: false)
hit = true
end
end
self[:any_accounts] = !hit && !exclude_accounts&.any?
end
def exclude_accounts_raw
antenna_accounts.where(exclude: true).map(&:account).map { |account| account.domain ? "@#{account.username}@#{account.domain}" : "@#{account.username}" }.join("\n")
end
def exclude_accounts_raw=(raw)
return if exclude_accounts_raw == raw
account_names = raw.split(/\R/).filter { |r| r.present? }
hit = false
antenna_accounts.where(exclude: true).destroy_all!
account_names.each do |name|
name = name[1..-1] if name.start_with?('@')
username, domain = name.split('@')
account = Account.find_by(username: username, domain: domain)
if account.present?
antenna_accounts.create!(account: account, exclude: true)
hit = true
end
end
self[:any_accounts] = !hit && !accounts&.any?
end
end

View file

@ -0,0 +1,17 @@
# == Schema Information
#
# Table name: antenna_accounts
#
# id :bigint(8) not null, primary key
# antenna_id :bigint(8) not null
# account_id :bigint(8) not null
# exclude :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class AntennaAccount < ApplicationRecord
belongs_to :antenna
belongs_to :account
end

View file

@ -0,0 +1,16 @@
# == Schema Information
#
# Table name: antenna_domains
#
# id :bigint(8) not null, primary key
# antenna_id :bigint(8) not null
# name :string
# exclude :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class AntennaDomain < ApplicationRecord
belongs_to :antenna
end

17
app/models/antenna_tag.rb Normal file
View file

@ -0,0 +1,17 @@
# == Schema Information
#
# Table name: antenna_tags
#
# id :bigint(8) not null, primary key
# antenna_id :bigint(8) not null
# tag_id :bigint(8) not null
# exclude :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class AntennaTag < ApplicationRecord
belongs_to :antenna
belongs_to :tag
end

View file

@ -39,6 +39,8 @@ module AccountAssociations
has_many :report_notes, dependent: :destroy
has_many :custom_filters, inverse_of: :account, dependent: :destroy
has_many :antennas, inverse_of: :account, dependent: :destroy
has_many :antenna_accounts, inverse_of: :account, dependent: :destroy
# Moderation notes
has_many :account_moderation_notes, dependent: :destroy, inverse_of: :account

View file

@ -23,6 +23,7 @@ class List < ApplicationRecord
has_many :list_accounts, inverse_of: :list, dependent: :destroy
has_many :accounts, through: :list_accounts
has_many :antennas, inverse_of: :list, dependent: :destroy
validates :title, presence: true

View file

@ -27,6 +27,8 @@ class Tag < ApplicationRecord
has_many :featured_tags, dependent: :destroy, inverse_of: :tag
has_many :followers, through: :passive_relationships, source: :account
has_one :antenna_tag, dependent: :destroy, inverse_of: :tag
HASHTAG_SEPARATORS = "_\u00B7\u30FB\u200c"
HASHTAG_FIRST_SEQUENCE_CHUNK_ONE = "[[:word:]_][[:word:]#{HASHTAG_SEPARATORS}]*[[:alpha:]#{HASHTAG_SEPARATORS}]"
HASHTAG_FIRST_SEQUENCE_CHUNK_TWO = "[[:word:]#{HASHTAG_SEPARATORS}]*[[:word:]_]"