Class: NewsmastMastodon::Api::V1::ChannelsController

Inherits:
Api::BaseController
  • Object
show all
Includes:
Redisable
Defined in:
app/controllers/newsmast_mastodon/api/v1/channels_controller.rb

Instance Method Summary collapse

Instance Method Details

#starter_packs_channelsObject

GET /api/v1/channels/starter_packs_channels Returns the list of starter pack channels from JSON data



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/newsmast_mastodon/api/v1/channels_controller.rb', line 11

def starter_packs_channels
  file_path = starter_pack_data_path('starter_pack_list.json')
  full_path = ::NewsmastMastodon::Engine.root.join('config', 'data', file_path)

  # Return empty response if file doesn't exist
  unless File.exist?(full_path)
    render json: { data: [] } and return
  end

  # Set HTTP caching headers based on file modification time
  last_modified = File.mtime(full_path)
  if stale?(last_modified: last_modified, etag: "#{starter_pack_namespace}-#{last_modified.to_i}")
    starter_packs_channels = load_json_data(file_path)

    expires_in 24.hours, public: true

    render json: { data: starter_packs_channels }
  end
end

#starter_packs_detailObject

GET /api/v1/channels/:id/starter_packs_detail Returns details of a specific starter pack channel including followers



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/newsmast_mastodon/api/v1/channels_controller.rb', line 33

def starter_packs_detail
  channel_id = params[:id]
  list_file = starter_pack_data_path('starter_pack_list.json')
  list_path = ::NewsmastMastodon::Engine.root.join('config', 'data', list_file)

  unless File.exist?(list_path)
    render json: { error: "Channel not found" }, status: :not_found and return
  end

  followers_file = starter_pack_data_path("starter_pack_#{channel_id}.json")
  followers_path = ::NewsmastMastodon::Engine.root.join('config', 'data', followers_file)

  list_mtime = File.mtime(list_path)
  followers_mtime = File.exist?(followers_path) ? File.mtime(followers_path) : list_mtime
  last_modified = [list_mtime, followers_mtime].max

  if stale?(last_modified: last_modified, etag: "#{starter_pack_namespace}-#{channel_id}-#{last_modified.to_i}")
    starter_packs_channels = load_json_data(list_file)
    channel = starter_packs_channels.find { |ch| ch["id"] == channel_id }

    unless channel
      render json: { error: "Channel not found" }, status: :not_found and return
    end

    followers = load_json_data(followers_file)

    expires_in 24.hours, public: true

    render json: {
      channel: channel,
      followers: followers
    }
  end
end