Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/GetStream/stream-core-swift.git", exact: "0.1.0")
.package(url: "https://github.com/GetStream/stream-core-swift.git", exact: "0.2.1")
],
targets: [
.target(
Expand Down
52 changes: 8 additions & 44 deletions Sources/StreamFeeds/Models/ActivityData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public struct ActivityData: Identifiable, Equatable, Sendable {
public let expiresAt: Date?
public let feeds: [String]
public let filterTags: [String]
public let hidden: Bool
public let id: String
public let interestTags: [String]
public private(set) var latestReactions: [FeedsReactionData]
Expand Down Expand Up @@ -106,6 +107,12 @@ extension ActivityData {
}
}

mutating func deleteComment(_ comment: CommentData) {
if comments.remove(byId: comment.id) {
commentCount = max(0, commentCount - 1)
}
}

mutating func updateComment(_ incomingData: CommentData) {
comments.updateFirstElement(where: { $0.id == incomingData.id }, changes: { $0.merge(with: incomingData) })
}
Expand All @@ -130,50 +137,6 @@ extension ActivityData {
changes: { $0.merge(with: incomingData, update: reaction, currentUserId: currentUserId) }
)
}

// MARK: -

mutating func deleteComment(_ comment: CommentData) {
if comments.remove(byId: comment.id) {
commentCount = max(0, commentCount - 1)
}
}

mutating func addBookmark(_ bookmark: BookmarkData, currentUserId: String) {
if bookmark.user.id == currentUserId {
if ownBookmarks.insert(byId: bookmark) {
bookmarkCount += 1
}
} else {
bookmarkCount += 1
}
}

mutating func deleteBookmark(_ bookmark: BookmarkData, currentUserId: String) {
if bookmark.user.id == currentUserId {
if ownBookmarks.remove(byId: bookmark.id) {
bookmarkCount = max(0, bookmarkCount - 1)
}
} else {
bookmarkCount = max(0, bookmarkCount - 1)
}
}

mutating func addReaction(_ reaction: FeedsReactionData, currentUserId: String) {
FeedsReactionData.updateByAdding(reaction: reaction, to: &latestReactions, reactionGroups: &reactionGroups)
reactionCount = reactionGroups.values.reduce(0) { $0 + $1.count }
if reaction.user.id == currentUserId {
ownReactions.insert(byId: reaction)
}
}

mutating func removeReaction(_ reaction: FeedsReactionData, currentUserId: String) {
FeedsReactionData.updateByRemoving(reaction: reaction, from: &latestReactions, reactionGroups: &reactionGroups)
reactionCount = reactionGroups.values.reduce(0) { $0 + $1.count }
if reaction.user.id == currentUserId {
ownReactions.remove(byId: reaction.id)
}
}
}

// MARK: - Model Conversions
Expand All @@ -193,6 +156,7 @@ extension ActivityResponse {
expiresAt: expiresAt,
feeds: feeds,
filterTags: filterTags,
hidden: hidden ?? false,
id: id,
interestTags: interestTags,
latestReactions: latestReactions.map { $0.toModel() },
Expand Down
3 changes: 3 additions & 0 deletions Sources/StreamFeeds/Models/ActivityPinData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Foundation
public struct ActivityPinData: Equatable, Sendable {
public internal(set) var activity: ActivityData
public let createdAt: Date
public let duration: String?
public let feed: FeedId
public let updatedAt: Date
public let userId: String
Expand All @@ -25,6 +26,7 @@ extension ActivityPinResponse {
ActivityPinData(
activity: activity.toModel(),
createdAt: createdAt,
duration: nil, // ActivityPinResponse doesn't have duration
feed: FeedId(rawValue: feed),
updatedAt: updatedAt,
userId: user.id
Expand All @@ -37,6 +39,7 @@ extension PinActivityResponse {
ActivityPinData(
activity: activity.toModel(),
createdAt: createdAt,
duration: duration,
feed: FeedId(rawValue: feed),
updatedAt: createdAt, // no updatedAt
userId: userId
Expand Down
4 changes: 3 additions & 1 deletion Sources/StreamFeeds/Models/AggregatedActivityData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public struct AggregatedActivityData: Identifiable, Equatable, Sendable {
public var score: Float
public var updatedAt: Date
public var userCount: Int
public var userCountTruncated: Bool

public var id: String {
if let first = activities.first?.id {
Expand All @@ -32,7 +33,8 @@ extension AggregatedActivityResponse {
group: group,
score: score,
updatedAt: updatedAt,
userCount: userCount
userCount: userCount,
userCountTruncated: userCountTruncated
)
}
}
2 changes: 1 addition & 1 deletion Sources/StreamFeeds/Models/BookmarkData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct BookmarkData: Equatable, Sendable {

extension BookmarkData: Identifiable {
public var id: String {
activity.id + user.id
"\(user.id)-\(activity.id)"
}
}

Expand Down
16 changes: 0 additions & 16 deletions Sources/StreamFeeds/Models/CommentData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,6 @@ extension CommentData {
ownReactions.replace(byId: reaction)
}

mutating func addReaction(_ reaction: FeedsReactionData, currentUserId: String) {
FeedsReactionData.updateByAdding(reaction: reaction, to: &latestReactions, reactionGroups: &reactionGroups)
reactionCount = reactionGroups.values.reduce(0) { $0 + $1.count }
if reaction.user.id == currentUserId {
ownReactions.append(reaction)
}
}

mutating func removeReaction(_ reaction: FeedsReactionData, currentUserId: String) {
FeedsReactionData.updateByRemoving(reaction: reaction, from: &latestReactions, reactionGroups: &reactionGroups)
reactionCount = reactionGroups.values.reduce(0) { $0 + $1.count }
if reaction.user.id == currentUserId {
ownReactions.remove(byId: reaction.id)
}
}

mutating func updateUser(_ incomingData: UserData) {
mentionedUsers.updateAll(where: { $0.id == incomingData.id }, changes: { $0 = incomingData })
user = incomingData
Expand Down
10 changes: 7 additions & 3 deletions Sources/StreamFeeds/Models/FeedData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public struct FeedData: Identifiable, Equatable, Sendable {
public let id: String
public let memberCount: Int
public let name: String
public let ownCapabilities: [FeedOwnCapability]?
public let ownFollows: [FollowData]?
public let ownMembership: FeedMemberData?
public let pinCount: Int
public let updatedAt: Date
public let visibility: String?
public let ownCapabilities: [FeedOwnCapability]?

var localFilterData: LocalFilterData?
}
Expand All @@ -45,10 +47,12 @@ extension FeedResponse {
id: id,
memberCount: memberCount,
name: name,
ownCapabilities: ownCapabilities,
ownFollows: ownFollows?.map { $0.toModel() },
ownMembership: ownMembership?.toModel(),
pinCount: pinCount,
updatedAt: updatedAt,
visibility: visibility,
ownCapabilities: ownCapabilities
visibility: visibility
)
}
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/StreamFeeds/Models/FeedMemberData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public struct FeedMemberData: Equatable, Sendable {
public let custom: [String: RawJSON]?
public let inviteAcceptedAt: Date?
public let inviteRejectedAt: Date?
public let membershipLevel: MembershipLevelResponse?
public let role: String
public let status: FeedMemberStatus
public let updatedAt: Date
Expand All @@ -35,6 +36,7 @@ extension FeedMemberResponse {
custom: custom,
inviteAcceptedAt: inviteAcceptedAt,
inviteRejectedAt: inviteRejectedAt,
membershipLevel: membershipLevel,
role: role,
status: status,
updatedAt: updatedAt,
Expand Down
35 changes: 7 additions & 28 deletions Sources/StreamFeeds/Models/FeedsReactionData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import StreamCore

public struct FeedsReactionData: Equatable, Sendable {
public let activityId: String
public let commentId: String?
public let createdAt: Date
public let custom: [String: RawJSON]?
public let type: String
Expand All @@ -16,40 +17,17 @@ public struct FeedsReactionData: Equatable, Sendable {

extension FeedsReactionData: Identifiable {
public var id: String {
"\(user.id)-\(type)-\(activityId)"
if let commentId {
"\(user.id)-\(type)-\(commentId)-\(activityId)"
} else {
"\(user.id)-\(type)-\(activityId)"
}
}
}

// MARK: - Mutating the Data

extension FeedsReactionData {
static func updateByAdding(
reaction: FeedsReactionData,
to latestReactions: inout [FeedsReactionData],
reactionGroups: inout [String: ReactionGroupData]
) {
guard latestReactions.insert(byId: reaction) else { return }
var reactionGroup = reactionGroups[reaction.type] ?? ReactionGroupData(count: 1, firstReactionAt: reaction.createdAt, lastReactionAt: reaction.createdAt)
reactionGroup.increment(with: reaction.createdAt)
reactionGroups[reaction.type] = reactionGroup
}

static func updateByRemoving(
reaction: FeedsReactionData,
from latestReactions: inout [FeedsReactionData],
reactionGroups: inout [String: ReactionGroupData]
) {
guard latestReactions.remove(byId: reaction.id) else { return }
if var reactionGroup = reactionGroups[reaction.type] {
reactionGroup.decrement(with: reaction.createdAt)
if !reactionGroup.isEmpty {
reactionGroups[reaction.type] = reactionGroup
} else {
reactionGroups.removeValue(forKey: reaction.type)
}
}
}

mutating func updateUser(_ incomingData: UserData) {
user = incomingData
}
Expand All @@ -61,6 +39,7 @@ extension FeedsReactionResponse {
func toModel() -> FeedsReactionData {
FeedsReactionData(
activityId: activityId,
commentId: commentId,
createdAt: createdAt,
custom: custom,
type: type,
Expand Down
41 changes: 0 additions & 41 deletions Sources/StreamFeeds/Models/PollData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,47 +74,6 @@ extension PollData {
mutating func updateOption(_ option: PollOptionData) {
options.replace(byId: option)
}

// MARK: - Votes

mutating func castVote(_ vote: PollVoteData, currentUserId: String) {
if enforceUniqueVote, vote.user?.id == currentUserId {
for ownVote in ownVotes {
removeVote(ownVote, currentUserId: currentUserId)
}
ownVotes = [vote]
} else {
ownVotes.insert(byId: vote)
}

var optionVotes = latestVotesByOption[vote.optionId] ?? []
let optionVoteCount = optionVotes.count
optionVotes.insert(byId: vote)
latestVotesByOption[vote.optionId] = optionVotes

if optionVotes.count != optionVoteCount {
let optionVoteCounts = voteCountsByOption[vote.optionId] ?? 0
voteCountsByOption[vote.optionId] = optionVoteCounts + 1
}

voteCount = voteCountsByOption.reduce(0) { $0 + $1.value }
}

mutating func removeVote(_ vote: PollVoteData, currentUserId: String) {
if vote.user?.id == currentUserId {
ownVotes.remove(byId: vote.id)
}

var optionVotes = latestVotesByOption[vote.optionId] ?? []
let optionVoteCount = optionVotes.count
optionVotes.remove(byId: vote.id)
latestVotesByOption[vote.optionId] = optionVotes

if optionVotes.count != optionVoteCount {
let optionVoteCounts = voteCountsByOption[vote.optionId] ?? 0
voteCountsByOption[vote.optionId] = max(0, optionVoteCounts - 1)
}
}
}

// MARK: - Model Conversions
Expand Down
19 changes: 3 additions & 16 deletions Sources/StreamFeeds/Models/ReactionGroupData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,7 @@ public struct ReactionGroupData: Equatable, Sendable {
public private(set) var count: Int
public let firstReactionAt: Date
public private(set) var lastReactionAt: Date
}

extension ReactionGroupData {
var isEmpty: Bool { count <= 0 }

mutating func decrement(with date: Date) {
guard date >= firstReactionAt || date <= lastReactionAt else { return }
count = max(0, count - 1)
}

mutating func increment(with date: Date) {
guard date > firstReactionAt else { return }
count += 1
lastReactionAt = date
}
public let sumScores: Int?
}

// MARK: - Model Conversions
Expand All @@ -33,7 +19,8 @@ extension ReactionGroupResponse {
ReactionGroupData(
count: count,
firstReactionAt: firstReactionAt,
lastReactionAt: lastReactionAt
lastReactionAt: lastReactionAt,
sumScores: sumScores
)
}
}
Loading