Skip to content

Commit 3e1abf6

Browse files
committed
feat: add feed rules models
- Add PlacementCriteria model - Add EngagementRule model - Add SuggestionRule model
1 parent 16b008e commit 3e1abf6

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import 'package:equatable/equatable.dart';
2+
import 'package:ht_shared/src/models/auth/user_role.dart';
3+
import 'package:ht_shared/src/models/feed_extras/feed_template_types.dart';
4+
import 'package:json_annotation/json_annotation.dart';
5+
6+
part 'feed_rules.g.dart';
7+
8+
/// {@template placement_criteria}
9+
/// Defines where and how often an injected item should appear in a feed.
10+
/// {@endtemplate}
11+
@JsonSerializable(explicitToJson: true)
12+
class PlacementCriteria extends Equatable {
13+
/// {@macro placement_criteria}
14+
const PlacementCriteria({
15+
this.afterPrimaryItemIndex,
16+
this.relativePosition,
17+
this.minPrimaryItemsRequired,
18+
});
19+
20+
/// Creates a [PlacementCriteria] from JSON data.
21+
factory PlacementCriteria.fromJson(Map<String, dynamic> json) =>
22+
_$PlacementCriteriaFromJson(json);
23+
24+
/// Inject after this many primary feed items (0-indexed).
25+
/// If null, no specific index is preferred.
26+
final int? afterPrimaryItemIndex;
27+
28+
/// A string indicating a general position, e.g., "middle", "end_quarter".
29+
/// The decorator will interpret this.
30+
final String? relativePosition;
31+
32+
/// Minimum number of primary items required on the page for this injection
33+
/// to be considered. Prevents injecting into very short lists.
34+
final int? minPrimaryItemsRequired;
35+
36+
/// Converts this [PlacementCriteria] instance to JSON data.
37+
Map<String, dynamic> toJson() => _$PlacementCriteriaToJson(this);
38+
39+
@override
40+
List<Object?> get props => [
41+
afterPrimaryItemIndex,
42+
relativePosition,
43+
minPrimaryItemsRequired,
44+
];
45+
}
46+
47+
/// {@template engagement_rule}
48+
/// Defines the rules for triggering a specific engagement prompt.
49+
/// {@endtemplate}
50+
@JsonSerializable(explicitToJson: true)
51+
class EngagementRule extends Equatable {
52+
/// {@macro engagement_rule}
53+
const EngagementRule({
54+
required this.templateType,
55+
required this.userRoles,
56+
this.minDaysSinceAccountCreation,
57+
this.maxTimesToShow,
58+
this.minDaysSinceLastShown,
59+
this.placement,
60+
});
61+
62+
/// Creates an [EngagementRule] from JSON data.
63+
factory EngagementRule.fromJson(Map<String, dynamic> json) =>
64+
_$EngagementRuleFromJson(json);
65+
66+
/// Type of engagement template to use.
67+
final EngagementTemplateType templateType;
68+
69+
/// Roles this rule applies to.
70+
final List<UserRole> userRoles;
71+
72+
/// Minimum days since user account was created for this rule to apply.
73+
final int? minDaysSinceAccountCreation;
74+
75+
/// Overall maximum number of times this specific engagement can be shown to a user.
76+
final int? maxTimesToShow;
77+
78+
/// Minimum days since this specific engagement was last shown to the user.
79+
final int? minDaysSinceLastShown;
80+
81+
/// How to place this in the feed.
82+
final PlacementCriteria? placement;
83+
84+
/// Converts this [EngagementRule] instance to JSON data.
85+
Map<String, dynamic> toJson() => _$EngagementRuleToJson(this);
86+
87+
@override
88+
List<Object?> get props => [
89+
templateType,
90+
userRoles,
91+
minDaysSinceAccountCreation,
92+
maxTimesToShow,
93+
minDaysSinceLastShown,
94+
placement,
95+
];
96+
}
97+
98+
/// {@template suggestion_rule}
99+
/// Defines the rules for triggering a specific content suggestion block.
100+
/// {@endtemplate}
101+
@JsonSerializable(explicitToJson: true)
102+
class SuggestionRule extends Equatable {
103+
/// {@macro suggestion_rule}
104+
const SuggestionRule({
105+
required this.templateType,
106+
required this.userRoles,
107+
this.placement,
108+
});
109+
110+
/// Creates a [SuggestionRule] from JSON data.
111+
factory SuggestionRule.fromJson(Map<String, dynamic> json) =>
112+
_$SuggestionRuleFromJson(json);
113+
114+
/// Type of suggestion template to use.
115+
final SuggestionTemplateType templateType;
116+
117+
/// Roles this rule applies to.
118+
final List<UserRole> userRoles;
119+
120+
/// How to place this in the feed.
121+
final PlacementCriteria? placement;
122+
123+
/// Converts this [SuggestionRule] instance to JSON data.
124+
Map<String, dynamic> toJson() => _$SuggestionRuleToJson(this);
125+
126+
@override
127+
List<Object?> get props => [templateType, userRoles, placement];
128+
}

lib/src/models/remote_config/feed_rules.g.dart

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)