Signal-iOS/SignalMessaging/Subscriptions/CachedBadge.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

75 lines
2.6 KiB
Swift

//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
public final class CachedBadge: Equatable, Dependencies {
let badgeLevel: OneTimeBadgeLevel
init(level: OneTimeBadgeLevel) {
self.badgeLevel = level
}
public enum Value: Equatable {
case notFound
case profileBadge(ProfileBadge)
}
// If set, the badge and its assets are populated and ready to use.
private var _cachedValue = AtomicOptional<Value>(nil)
public var cachedValue: Value? { self._cachedValue.get() }
// If set, there's an ongoing request to populate the badge. New callers
// should join this chain to know when the shared request has finished.
private var fetchPromise: Promise<Value>?
public static func == (lhs: CachedBadge, rhs: CachedBadge) -> Bool {
// Cached badges are considered equivalent if the underlying badge has the
// same level. We expect the resulting ProfileBadge to be the same if the
// levels match.
return lhs.badgeLevel == rhs.badgeLevel
}
@discardableResult
public func fetchIfNeeded() -> Promise<Value> {
// Run on a stable queue to avoid race conditions.
return firstly(on: .main) { () -> Promise<Value> in
// If we already have a cached value, do nothing.
if let cachedValue = self.cachedValue {
return Promise.value(cachedValue)
}
// If we're already fetching, chain onto that fetch.
if let fetchPromise = self.fetchPromise {
return fetchPromise
}
// Otherwise, kick off a new fetch.
let fetchPromise: Promise<Value> = firstly {
SubscriptionManager.getBadge(level: self.badgeLevel)
}.then { (profileBadge) -> Promise<Value> in
switch profileBadge {
case .none:
return Promise.value(.notFound)
case .some(let profileBadge):
return firstly {
self.profileManager.badgeStore.populateAssetsOnBadge(profileBadge)
}.map { _ in
return .profileBadge(profileBadge)
}
}
}.map { (value) -> Value in
self._cachedValue.set(value)
return value
}.ensure {
self.fetchPromise = nil
}
// no need to catch -- network request errors are logged elsewhere
self.fetchPromise = fetchPromise
return fetchPromise
}
}
}