1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

icalendar source sync: create/update/delete ical events

This commit is contained in:
Sylvain 2019-12-03 12:16:07 +01:00
parent 538b5cef78
commit 64fe68b2b0
3 changed files with 21 additions and 12 deletions

View File

@ -36,8 +36,7 @@ class API::ICalendarController < API::ApiController
end end
def sync def sync
worker = ICalendarImportWorker.new ICalendarImportWorker.perform_async([params[:id]])
worker.perform([params[:id]])
render json: { processing: [params[:id]] }, status: :created render json: { processing: [params[:id]] }, status: :created
end end

View File

@ -3,4 +3,10 @@
# iCalendar (RFC 5545) event, belonging to an ICalendar object (its source) # iCalendar (RFC 5545) event, belonging to an ICalendar object (its source)
class ICalendarEvent < ActiveRecord::Base class ICalendarEvent < ActiveRecord::Base
belongs_to :i_calendar belongs_to :i_calendar
def self.update_or_create_by(args, attributes)
obj = find_or_create_by(args)
obj.update(attributes)
obj
end
end end

View File

@ -7,25 +7,29 @@ class ICalendarImportService
require 'uri' require 'uri'
require 'icalendar' require 'icalendar'
events = [] uids = []
i_cal = ICalendar.find(i_calendar_id) i_cal = ICalendar.find(i_calendar_id)
ics = Net::HTTP.get(URI.parse(i_cal.url)) ics = Net::HTTP.get(URI.parse(i_cal.url))
cals = Icalendar::Calendar.parse(ics) cals = Icalendar::Calendar.parse(ics)
# create new events and update existings
cals.each do |cal| cals.each do |cal|
cal.events.each do |evt| cal.events.each do |evt|
events.push( uids.push(evt.uid.to_s)
uid: evt.uid, ICalendarEvent.update_or_create_by(
dtstart: evt.dtstart, { uid: evt.uid.to_s },
dtend: evt.dtend, {
summary: evt.summary, dtstart: evt.dtstart,
description: evt.description, dtend: evt.dtend,
i_calendar_id: i_calendar_id summary: evt.summary,
description: evt.description,
i_calendar_id: i_calendar_id
}
) )
end end
end end
# remove deleted events
ICalendarEvent.create!(events) ICalendarEvent.where(i_calendar_id: i_calendar_id).where.not(uid: uids).destroy_all
end end
end end