feat: add sortId for reaction sorting

This commit is contained in:
ryanzhao 2022-08-05 14:23:28 +10:00
parent 99e4614bf8
commit 073516ae78
6 changed files with 17 additions and 22 deletions

View File

@ -1089,7 +1089,8 @@ extension ConversationVC:
let sortId = Reaction.getSortId(
db,
interactionId: cellViewModel.id,
emoji: emoji
emoji: emoji,
timestamp: sentTimestamp
)
try Reaction(
interactionId: cellViewModel.id,

View File

@ -46,7 +46,7 @@ public struct Reaction: Codable, Equatable, Hashable, FetchableRecord, Persistab
/// regardless of the type of conversation)
public let count: Int64
/// The id for sorting
/// The first timestamp that an emoji is added
public let sortId: Int64
// MARK: - Relationships
@ -108,29 +108,19 @@ public extension Reaction {
static func getSortId(
_ db: Database,
interactionId: Int64,
emoji: String
emoji: String,
timestamp: Int64
) -> Int64 {
let existingSortId: Int64? = try? Reaction
if let existingSortId: Int64 = try? Reaction
.select(Columns.sortId)
.filter(Columns.interactionId == interactionId)
.filter(Columns.emoji == emoji)
.asRequest(of: Int64.self)
.fetchOne(db)
if let sortId = existingSortId {
return sortId
{
return existingSortId
}
let existingLargestSortId: Int64? = try? Reaction
.select(max(Columns.sortId))
.filter(Columns.interactionId == interactionId)
.asRequest(of: Int64.self)
.fetchOne(db)
if let sortId = existingLargestSortId {
return sortId + 1
}
return 0
return timestamp
}
}

View File

@ -369,7 +369,7 @@ public extension Message {
let reactors = rawReaction.reactors
{
var count = rawReaction.count
let sortId: Int64 = 0 // TODO: Need to be modified to the server returned value
let sortId: Int64 = Int64(floor((rawReaction.first * 1000)))
for reactor in reactors {
if reactor == blindedUserPublicKey { continue } // Will add a reaction for this case outside of the loop
let reaction = Reaction(

View File

@ -38,11 +38,13 @@ extension OpenGroupAPI {
case count
case reactors
case you
case first
}
public let count: Int64
public let reactors: [String]?
public let you: Bool
public let first: TimeInterval
}
public let reactions: [String:Reaction]?
@ -114,7 +116,8 @@ extension OpenGroupAPI.Message.Reaction {
self = OpenGroupAPI.Message.Reaction(
count: try container.decode(Int64.self, forKey: .count),
reactors: try? container.decode([String].self, forKey: .reactors),
you: (try? container.decode(Bool.self, forKey: .you)) ?? false
you: (try? container.decode(Bool.self, forKey: .you)) ?? false,
first: ((try? container.decode(TimeInterval.self, forKey: .first)) ?? Date().timeIntervalSince1970)
)
}
}

View File

@ -327,7 +327,8 @@ extension MessageReceiver {
let sortId = Reaction.getSortId(
db,
interactionId: interactionId,
emoji: reaction.emoji
emoji: reaction.emoji,
timestamp: Int64(messageSentTimestamp * 1000)
)
switch reaction.kind {

View File

@ -465,7 +465,7 @@ public extension MessageViewModel {
// MARK: - Comparable
public static func < (lhs: ReactionInfo, rhs: ReactionInfo) -> Bool {
return (lhs.reaction.timestampMs < rhs.reaction.timestampMs)
return (lhs.reaction.sortId < rhs.reaction.sortId)
}
}
}