Class: ICalPal::Reminder

Inherits:
Object
  • Object
show all
Includes:
ICalPal
Defined in:
lib/reminder.rb

Overview

Class representing items from the Reminders database

Constant Summary collapse

DEFAULT_COLOR =
'#1BADF8'.freeze
DEFAULT_SYMBOLIC_COLOR =
'blue'.freeze
LONG_PRIORITY =
[
  'High priority',
  'Medium priority',
  'Low priority',
  'No priority',
].freeze
DB_PATH =
"#{Dir.home}/Library/Group Containers/group.com.apple.reminders/Container_v1/Stores".freeze
QUERY =
<<~SQL.freeze
SELECT DISTINCT

r1.zTitle as title,
r1.zAllday as all_day,
r1.zDueDate as due_date,
r1.zFlagged as flagged,
r1.zNotes as notes,
r1.zPriority as priority,
r1.zContactHandles as messaging,
r1.zDueDateDeltaAlertsData as alert,
r1.zckIdentifier as id,
r1.zCompleted as completed,
r1.zDisplayDateUpdatedForSecondsFromGMT as utc_offset,

bl1.zBadgeEmblem as badge,
bl1.zColor as color,
bl1.zName as list_name,
bl1.zParentList as parent,
bl1.zSharingStatus as shared,
bl1.zShouldCategorizeGroceryItems as grocery,

json(bl1.ZMembershipsOfRemindersInSectionsAsData) -> '$.memberships' AS members,

(SELECT zName
 FROM zremcdBaseList bl2
 WHERE bl2.z_pk = bl1.zParentList) AS 'group',

(SELECT json_group_array(zremcdObject.zTitle)
FROM zremcdObject
WHERE zremcdObject.z_pk IN (
 SELECT zTrigger
 FROM zremcdObject
 WHERE zremcdObject.zReminder = r1.z_pk
)) AS location,

(SELECT json_group_array(zremcdObject.zProximity)
 FROM zremcdObject
 WHERE zremcdObject.z_pk IN (
  SELECT zTrigger
  FROM zremcdObject
  WHERE zremcdObject.zReminder = r1.z_pk
 )) AS proximity,

(SELECT json_group_array(zremcdObject.zRadius)
 FROM zremcdObject
 WHERE zremcdObject.z_pk IN (
  SELECT zTrigger
  FROM zremcdObject
  WHERE zremcdObject.zReminder = r1.z_pk
 )) AS radius,

(SELECT json_group_array(zName)
 FROM zremcdHashtagLabel
 WHERE zremcdHashtagLabel.z_pk IN (
	SELECT zremcdObject.zHashtagLabel
	FROM zremcdObject
	JOIN zremcdReminder ON zremcdObject.zReminder3 = r1.z_pk
	WHERE zremcdObject.zReminder3 = r1.z_pk
 )) AS tags,

(SELECT
 json_array(zNickname, zFirstName, zLastName, zAddress1)
 FROM zremcdObject
 WHERE z_pk = (
  SELECT zAssignee
  FROM zremcdObject
  WHERE zReminder1 = r1.z_pk
)) AS assignee,

(SELECT zURL
 FROM zremcdObject
 WHERE zReminder2 = r1.z_pk) AS url

FROM zremcdReminder r1

LEFT OUTER JOIN zremcdBaseList bl1 ON r1.zList = bl1.z_pk

WHERE r1.zMarkedForDeletion = 0

SQL
SECTIONS_QUERY =

Load sections

<<~SQL.freeze
SELECT DISTINCT

zckIdentifier AS id,
zDisplayName AS name

FROM zremcdBaseSection

SQL

Constants included from ICalPal

DOW, ITIME, NAME, VERSION

Instance Attribute Summary

Attributes included from ICalPal

#self

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ICalPal

#[]=, call, #dump, #keys, nth, #to_csv, #to_xml, #values

Constructor Details

#initialize(obj) ⇒ Reminder

Returns a new instance of Reminder.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/reminder.rb', line 73

def initialize(obj)
  super

  # Convert JSON arrays to Arrays
  %w[ assignee tags ].each do |a|
    @self[a] = JSON.parse(obj[a]) if obj[a]
  end

  %w[ location proximity radius ].each do |a|
    @self[a] = JSON.parse(obj[a]).compact.uniq[0] if obj[a]
  end

  # Section
  if @self['members']
    j = JSON.parse(@self['members']).select { |i| i['memberID'] == @self['id'] }
    s = $sections.select { |i| i['id'] == j[0]['groupID'] } if j[0]
    @self['section'] = s[0]['name'] if s && s[0]

    # Drop the internal membership scratch but keep `id` (zckIdentifier),
    # which downstream consumers rely on for joins/dedup.
    @self.delete('members')
  end

  # Priority
  # rubocop: disable Style/NumericPredicate
  @self['prio'] = 0 if @self['priority'] == 1 # high
  @self['prio'] = 1 if @self['priority'] == 5 # medium
  @self['prio'] = 2 if @self['priority'] == 9 # low
  @self['prio'] = 3 if @self['priority'] == 0 # none
  # rubocop: enable Style/NumericPredicate

  @self['long_priority'] = LONG_PRIORITY[@self['prio']] if @self['prio']

  # Due date
  if @self['due_date']
    @self['due_date'] += ITIME
    @self['due_date'] -= obj['utc_offset'] if obj['utc_offset']
    @self['due'] = RDT.from_time(Time.at(@self['due_date']))
  end

  # Notes
  @self['notes'] = '' unless @self['notes']

  # Color
  @self['color'] = nil unless $opts[:palette]

  if @self['color']
    plist = plconvert(@self['color'])

    # Get color and symbolic color name
    plist.each do |p|
      @self['color'] = plist[p['daHexString']['CF$UID']] if p['daHexString']
      @self['symbolic_color_name'] = plist[p['ckSymbolicColorName']['CF$UID']] if p['ckSymbolicColorName']
    end
  else
    @self['color'] = DEFAULT_COLOR
    @self['symbolic_color_name'] = DEFAULT_SYMBOLIC_COLOR
  end

  # Contacts
  messaging = []

  plist = plconvert(@self['messaging'])
  plist.each do |p|
    %w[ emails phones ].each do |field|
      next unless p[field]

      offset = p[field].values[0]
      targets = plist[offset]['NS.objects']
      targets.each { |t| messaging.push(plist[t['CF$UID']]) }
    end
  end if plist

  @self['messaging'] = messaging
end

Class Method Details

.load_data(db_file, q) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/reminder.rb', line 6

def self.load_data(db_file, q)
  # Load items
  ICalPal.load_data(db_file, q)

rescue SQLite3::SQLException => e
  # Data-local.sqlite does not have zremcdBaseList
  raise e unless e.message =~ /no such table/

  []
end

Instance Method Details

#<=>(other) ⇒ Object

When comparing sections, “Others” always goes last

See Also:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/reminder.rb', line 152

def <=>(other)
  $sort_attrs.each do |s|
    next if self[s] == other[s]

    # nil is always less than
    return -1 if other[s].nil?
    return 1 if self[s].nil?

    if s == 'section'
      # Section "Others" always goes last
      return -1 if other[s] == 'Others'
      return 1 if self[s] == 'Others'
    end

    return -1 if self[s] < other[s]
    return 1 if self[s] > other[s]
  end

  0
end

#[](k) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
67
68
69
70
71
# File 'lib/reminder.rb', line 17

def [](k)
  case k
  when 'alert'              # -N (minutes|hours|days|weeks|months)
    if @self['alert']
      alert = JSON.parse(@self['alert'])['dueDateDeltaAlerts'][0]
      count = alert['dueDateDeltaCount'] * -1
      unit = EventKit::EKReminderDueDateDeltaUnit[alert['dueDateDeltaUnit']]
      "#{count} #{unit}"
    end

  when 'assignee'           # [ nickname, firstname, lastname, address ]
    if @self['assignee']
      a = @self['assignee']
      t = (a[0])? a[0] : "#{a[1]} #{a[2]}"
      t += " (#{a[3][7..]})" if a[3]
      t
    end

  when 'due'                # date[ at time]
    if @self['due']
      t = @self['due'].to_s
      t += " at #{@self['due'].strftime($opts[:tf])}" unless @self['all_day'] == 1
      t
    end

  when 'group'              # (group|"(no group)")
    (@self['group'])? @self['group'] : '(no group)'

  when 'priority'           # Integer -> String
    EventKit::EKReminderPriority[@self['priority']] if @self['priority'].positive?

  when 'proximity'          # (arriving|leaving)
    EventKit::EKReminderProximity[@self['proximity']] if @self['proximity']

  when 'radius'             # Float -> Integer
    "#{Integer(@self['radius'])}m" if @self['radius']

  when 'sdate'              # For sorting
    @self['due_date']

  when 'sday'
    @self['due'].day_start(0) if @self['due']

  when 'section'
    (@self['section'])? @self['section'] : 'Others'

  when 'name', 'reminder', 'task' # Aliases
    @self['title']

  when 'UUID'               # For consistency with other commands
    @self['id']

  else super
  end
end