-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
GleanPlumbMessage.swift
86 lines (65 loc) · 2.34 KB
/
GleanPlumbMessage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import Foundation
protocol MessageDataProtocol {
var surface: MessageSurfaceId { get }
var isControl: Bool { get }
var title: String? { get }
var text: String { get }
var buttonLabel: String? { get }
var experiment: String? { get }
}
extension MessageData: MessageDataProtocol {}
protocol StyleDataProtocol {
var priority: Int { get }
var maxDisplayCount: Int { get }
}
extension StyleData: StyleDataProtocol {}
/// Message is a representation of `MessageData` from `GleanPlumb` that we can better utilize.
struct GleanPlumbMessage {
/// The message Key, a unique identifier.
let id: String
/// An access point to MessageData from Nimbus Messaging.
internal let data: MessageDataProtocol
/// The action to be done when a user positively engages with the message (CTA).
let action: String
/// The conditions that need to be satisfied for a message to be considered eligible to present.
let triggers: [String]
/// The access point to StyleData from Nimbus Messaging.
let style: StyleDataProtocol
/// The minimal data about a message that we should persist.
internal var metadata: GleanPlumbMessageMetaData
var isExpired: Bool {
metadata.isExpired || metadata.impressions >= style.maxDisplayCount
}
var buttonLabel: String? {
data.buttonLabel
}
var text: String {
data.text
}
var title: String? {
data.title
}
var surface: MessageSurfaceId {
data.surface
}
}
/// `MessageMeta` is where we store parts of the message that help us aggregate, query and determine non-expired messages.
class GleanPlumbMessageMetaData: Codable {
/// The message Key.
let id: String
/// The number of times a message was seen by the user.
var impressions: Int
/// The number of times a user intentionally dismissed the message.
var dismissals: Int
/// A message expiry status.
var isExpired: Bool
init(id: String, impressions: Int, dismissals: Int, isExpired: Bool) {
self.id = id
self.impressions = impressions
self.dismissals = dismissals
self.isExpired = isExpired
}
}