1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

import events asyncronously from icalendar

This commit is contained in:
Sylvain 2019-12-02 15:53:24 +01:00
parent 55d2c88134
commit cca6b14f58
13 changed files with 126 additions and 30 deletions

View File

@ -5,7 +5,7 @@ Application.Services.factory('ICalendar', ['$resource', function ($resource) {
{ id: '@id' }, {
events: {
method: 'GET',
url: '/api/i_calendar/events'
url: '/api/i_calendar/:id/events'
},
sync: {
method: 'POST',

View File

@ -27,25 +27,13 @@ class API::ICalendarController < API::ApiController
end
def events
require 'net/http'
require 'uri'
require 'icalendar'
@events = []
@i_cals = ICalendar.all.each do |i_cal|
ics = Net::HTTP.get(URI.parse(i_cal.url))
cals = Icalendar::Calendar.parse(ics)
cals.first.events.each do |evt|
@events.push(calendar: i_cal, event: evt)
end
end
@events = ICalendarEvent.where(i_calendar_id: params[:id]).joins(:i_calendar)
end
def sync
puts '[TODO] run worker'
render json: { processing: true }, status: :created
worker = ICalendarImportWorker.new
worker.perform([params[:id]])
render json: { processing: [params[:id]] }, status: :created
end
private

View File

@ -2,4 +2,14 @@
# iCalendar (RFC 5545) files, stored by URL and kept with their display configuration
class ICalendar < ActiveRecord::Base
has_many :i_calendar_events
after_create sync_events
private
def sync_events
worker = ICalendarImportWorker.new
worker.perform([id])
end
end

View File

@ -0,0 +1,6 @@
# frozen_string_literal: true
# iCalendar (RFC 5545) event, belonging to an ICalendar object (its source)
class ICalendarEvent < ActiveRecord::Base
belongs_to :i_calendar
end

View File

@ -0,0 +1,31 @@
# frozen_string_literal: true
# Import all events from a given remote RFC 5545 iCalendar
class ICalendarImportService
def import(i_calendar_id)
require 'net/http'
require 'uri'
require 'icalendar'
events = []
i_cal = ICalendar.find(i_calendar_id)
ics = Net::HTTP.get(URI.parse(i_cal.url))
cals = Icalendar::Calendar.parse(ics)
cals.each do |cal|
cal.events.each do |evt|
events.push(
uid: evt.uid,
dtstart: evt.dtstart,
dtend: evt.dtend,
summary: evt.summary,
description: evt.description,
i_calendar_id: i_calendar_id
)
end
end
ICalendarEvent.create!(events)
end
end

View File

@ -1,9 +1,9 @@
# frozen_string_literal: true
json.array!(@events) do |event|
json.id event[:event].uid
json.title event[:calendar].text_hidden ? '' : event[:event].summary
json.start event[:event].dtstart.iso8601
json.end event[:event].dtend.iso8601
json.id event.uid
json.title event.i_calendar.text_hidden ? '' : event.summary
json.start event.dtstart.iso8601
json.end event.dtend.iso8601
json.backgroundColor 'white'
end

View File

@ -0,0 +1,14 @@
# frozen_string_literal: true
# Periodically import the iCalendar RFC 5545 events from the configured source
class ICalendarImportWorker
include Sidekiq::Worker
def perform(calendar_ids = ICalendar.all.map(&:id))
service = ICalendarImportService.new
calendar_ids.each do |id|
service.import(id)
end
end
end

View File

@ -113,7 +113,7 @@ Rails.application.routes.draw do
end
resources :i_calendar, only: %i[index create destroy] do
get 'events', on: :collection
get 'events', on: :member
post 'sync', on: :member
end

View File

@ -15,6 +15,11 @@ generate_statistic:
class: "StatisticWorker"
queue: default
i_calendar_import:
cron: "0 2 * * *"
class: "ICalendarImportWorker"
queue: default
open_api_trace_calls_count:
cron: "0 4 * * 0" # every sunday at 4am
class: "OpenAPITraceCallsCountWorker"

View File

@ -0,0 +1,15 @@
class CreateICalendarEvents < ActiveRecord::Migration
def change
create_table :i_calendar_events do |t|
t.string :uid
t.datetime :dtstart
t.datetime :dtend
t.string :summary
t.string :description
t.string :attendee
t.belongs_to :i_calendar, index: true, foreign_key: true
t.timestamps null: false
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20191127153729) do
ActiveRecord::Schema.define(version: 20191202135507) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -251,6 +251,20 @@ ActiveRecord::Schema.define(version: 20191127153729) do
add_index "history_values", ["invoicing_profile_id"], name: "index_history_values_on_invoicing_profile_id", using: :btree
add_index "history_values", ["setting_id"], name: "index_history_values_on_setting_id", using: :btree
create_table "i_calendar_events", force: :cascade do |t|
t.string "uid"
t.datetime "dtstart"
t.datetime "dtend"
t.string "summary"
t.string "description"
t.string "attendee"
t.integer "i_calendar_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "i_calendar_events", ["i_calendar_id"], name: "index_i_calendar_events_on_i_calendar_id", using: :btree
create_table "i_calendars", force: :cascade do |t|
t.string "url"
t.string "name"
@ -933,6 +947,7 @@ ActiveRecord::Schema.define(version: 20191127153729) do
add_foreign_key "exports", "users"
add_foreign_key "history_values", "invoicing_profiles"
add_foreign_key "history_values", "settings"
add_foreign_key "i_calendar_events", "i_calendars"
add_foreign_key "invoices", "coupons"
add_foreign_key "invoices", "invoicing_profiles"
add_foreign_key "invoices", "invoicing_profiles", column: "operator_profile_id"

19
test/fixtures/i_calendar_events.yml vendored Normal file
View File

@ -0,0 +1,19 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
uid: MyString
dtstart: 2019-12-02 14:55:07
dtend: 2019-12-02 14:55:07
summary: MyString
description: MyString
attendee: MyString
i_calendar_id:
two:
uid: MyString
dtstart: 2019-12-02 14:55:07
dtend: 2019-12-02 14:55:07
summary: MyString
description: MyString
attendee: MyString
i_calendar_id:

View File

@ -1,7 +0,0 @@
require 'test_helper'
class ICalendarTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end