forked from mastodon/mastodon
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scheduled statuses (mastodon#9706)
Fix mastodon#340
- Loading branch information
1 parent
4bb1391
commit 86d6e88
Showing
29 changed files
with
432 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::ScheduledStatusesController < Api::BaseController | ||
include Authorization | ||
|
||
before_action -> { doorkeeper_authorize! :read, :'read:statuses' }, except: [:update, :destroy] | ||
before_action -> { doorkeeper_authorize! :write, :'write:statuses' }, only: [:update, :destroy] | ||
|
||
before_action :set_statuses, only: :index | ||
before_action :set_status, except: :index | ||
|
||
after_action :insert_pagination_headers, only: :index | ||
|
||
def index | ||
render json: @statuses, each_serializer: REST::ScheduledStatusSerializer | ||
end | ||
|
||
def show | ||
render json: @status, serializer: REST::ScheduledStatusSerializer | ||
end | ||
|
||
def update | ||
@status.update!(scheduled_status_params) | ||
render json: @status, serializer: REST::ScheduledStatusSerializer | ||
end | ||
|
||
def destroy | ||
@status.destroy! | ||
render_empty | ||
end | ||
|
||
private | ||
|
||
def set_statuses | ||
@statuses = current_account.scheduled_statuses.paginate_by_id(limit_param(DEFAULT_STATUSES_LIMIT), params_slice(:max_id, :since_id, :min_id)) | ||
end | ||
|
||
def set_status | ||
@status = current_account.scheduled_statuses.find(params[:id]) | ||
end | ||
|
||
def scheduled_status_params | ||
params.permit(:scheduled_at) | ||
end | ||
|
||
def pagination_params(core_params) | ||
params.slice(:limit).permit(:limit).merge(core_params) | ||
end | ||
|
||
def insert_pagination_headers | ||
set_pagination_headers(next_path, prev_path) | ||
end | ||
|
||
def next_path | ||
if records_continue? | ||
api_v1_scheduled_statuses_url pagination_params(max_id: pagination_max_id) | ||
end | ||
end | ||
|
||
def prev_path | ||
unless @statuses.empty? | ||
api_v1_scheduled_statuses_url pagination_params(min_id: pagination_since_id) | ||
end | ||
end | ||
|
||
def records_continue? | ||
@statuses.size == limit_param(DEFAULT_STATUSES_LIMIT) | ||
end | ||
|
||
def pagination_max_id | ||
@statuses.last.id | ||
end | ||
|
||
def pagination_since_id | ||
@statuses.first.id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: scheduled_statuses | ||
# | ||
# id :bigint(8) not null, primary key | ||
# account_id :bigint(8) | ||
# scheduled_at :datetime | ||
# params :jsonb | ||
# | ||
|
||
class ScheduledStatus < ApplicationRecord | ||
include Paginable | ||
|
||
TOTAL_LIMIT = 300 | ||
DAILY_LIMIT = 25 | ||
|
||
belongs_to :account, inverse_of: :scheduled_statuses | ||
has_many :media_attachments, inverse_of: :scheduled_status, dependent: :destroy | ||
|
||
validate :validate_future_date | ||
validate :validate_total_limit | ||
validate :validate_daily_limit | ||
|
||
private | ||
|
||
def validate_future_date | ||
errors.add(:scheduled_at, I18n.t('scheduled_statuses.too_soon')) if scheduled_at.present? && scheduled_at <= Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET | ||
end | ||
|
||
def validate_total_limit | ||
errors.add(:base, I18n.t('scheduled_statuses.over_total_limit', limit: TOTAL_LIMIT)) if account.scheduled_statuses.count >= TOTAL_LIMIT | ||
end | ||
|
||
def validate_daily_limit | ||
errors.add(:base, I18n.t('scheduled_statuses.over_daily_limit', limit: DAILY_LIMIT)) if account.scheduled_statuses.where('scheduled_at::date = ?::date', scheduled_at).count >= DAILY_LIMIT | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class REST::ScheduledStatusSerializer < ActiveModel::Serializer | ||
attributes :id, :scheduled_at | ||
|
||
has_many :media_attachments, serializer: REST::MediaAttachmentSerializer | ||
|
||
def id | ||
object.id.to_s | ||
end | ||
end |
Oops, something went wrong.