1
0
Fork 1
mirror of https://github.com/mastodon/mastodon.git synced 2024-12-23 01:25:36 +00:00
mastodon/app/models/invite.rb
Eugen Rochko cdb101340a
Keyword/phrase filtering ()
* Add keyword filtering

    GET|POST       /api/v1/filters
    GET|PUT|DELETE /api/v1/filters/:id

- Irreversible filters can drop toots from home or notifications
- Other filters can hide toots through the client app
- Filters use a phrase valid in particular contexts, expiration

* Make sure expired filters don't get applied client-side

* Add missing API methods

* Remove "regex filter" from column settings

* Add tests

* Add test for FeedManager

* Add CustomFilter test

* Add UI for managing filters

* Add streaming API event to allow syncing filters

* Fix tests
2018-06-29 15:34:36 +02:00

40 lines
984 B
Ruby

# frozen_string_literal: true
# == Schema Information
#
# Table name: invites
#
# id :bigint(8) not null, primary key
# user_id :bigint(8) not null
# code :string default(""), not null
# expires_at :datetime
# max_uses :integer
# uses :integer default(0), not null
# created_at :datetime not null
# updated_at :datetime not null
# autofollow :boolean default(FALSE), not null
#
class Invite < ApplicationRecord
include Expireable
belongs_to :user
has_many :users, inverse_of: :invite
scope :available, -> { where(expires_at: nil).or(where('expires_at >= ?', Time.now.utc)) }
before_validation :set_code
def valid_for_use?
(max_uses.nil? || uses < max_uses) && !expired?
end
private
def set_code
loop do
self.code = ([*('a'..'z'), *('A'..'Z'), *('0'..'9')] - %w(0 1 I l O)).sample(8).join
break if Invite.find_by(code: code).nil?
end
end
end