Don't forget to lycansubscribe
at master 959 B view raw
1require 'active_record' 2 3require_relative 'user' 4 5class Post < ActiveRecord::Base 6 validates_presence_of :time, :data, :rkey 7 validates :text, length: { minimum: 0, allow_nil: false } 8 9 validates_length_of :rkey, is: 13 10 validates_length_of :text, maximum: 1000 11 validates_length_of :data, maximum: 10000 12 13 validate :check_null_bytes 14 15 belongs_to :user 16 17 has_many :likes, dependent: :delete_all 18 has_many :reposts, dependent: :delete_all 19 has_many :pins, dependent: :delete_all 20 has_many :quotes, dependent: :delete_all 21 22 def self.find_by_at_uri(uri) 23 uri = AT_URI(uri) unless uri.is_a?(AT_URI) 24 return nil unless uri.is_post? 25 26 Post.joins(:user).find_by(user: { did: uri.repo }, rkey: uri.rkey) 27 end 28 29 def at_uri 30 "at://#{user.did}/app.bsky.feed.post/#{rkey}" 31 end 32 33 def check_null_bytes 34 # Postgres doesn't allow null bytes in strings 35 errors.add(:text, 'must not contain a null byte') if text.include?("\u0000") 36 end 37end