Method: OpenC3::ActivityModel.destroy

Defined in:
lib/openc3/models/activity_model.rb

.destroy(name:, scope:, score:, uuid: nil, recurring: nil) ⇒ Integer

Remove one member from a sorted set.

Returns:

  • (Integer)

    count of the members removed, 0 indicates the member was not found



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
# File 'lib/openc3/models/activity_model.rb', line 91

def self.destroy(name:, scope:, score:, uuid: nil, recurring: nil)
  result = 0

  # Delete all recurring activities
  if recurring
    activity = self.score(name: name, score: score, scope: scope)
    if activity and activity.recurring['end'] and activity.recurring['uuid']
      json = Store.zrangebyscore("#{scope}#{PRIMARY_KEY}__#{name}", activity.recurring['start'], activity.recurring['end'])
      parsed = json.map { |value| ActivityModel.from_json(value, name: name, scope: scope) }
      parsed.each_with_index do |value, index|
        if value.recurring['uuid'] == uuid
          Store.zrem("#{scope}#{PRIMARY_KEY}__#{name}", json[index])
          result += 1
        end
      end
    end
  end

  # First find all the activities at the score
  json = Store.zrangebyscore("#{scope}#{PRIMARY_KEY}__#{name}", score, score, :limit => [0, 100])
  parsed = json.map { |value| JSON.parse(value, :allow_nan => true, :create_additions => true) }
  parsed.each_with_index do |value, index|
    if uuid
      # If the uuid is given then only delete activities that match the uuid
      if value['uuid'] == uuid
        Store.zrem("#{scope}#{PRIMARY_KEY}__#{name}", json[index])
        result += 1
        break
      end
    else
      # If the uuid is not given (backwards compatibility) then delete all activities
      # at the score that do NOT have a uuid
      next if value['uuid']
      Store.zrem("#{scope}#{PRIMARY_KEY}__#{name}", json[index])
      result += 1
    end
  end

  notification = {
    # start / stop to match SortedModel
    'data' => JSON.generate({'start' => score, 'uuid' => uuid}),
    'kind' => 'deleted',
    'type' => 'activity',
    'timeline' => name
  }
  TimelineTopic.write_activity(notification, scope: scope)
  return result
end