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( let sortId = Reaction.getSortId(
db, db,
interactionId: cellViewModel.id, interactionId: cellViewModel.id,
emoji: emoji emoji: emoji,
timestamp: sentTimestamp
) )
try Reaction( try Reaction(
interactionId: cellViewModel.id, interactionId: cellViewModel.id,

View File

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

View File

@ -369,7 +369,7 @@ public extension Message {
let reactors = rawReaction.reactors let reactors = rawReaction.reactors
{ {
var count = rawReaction.count 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 { for reactor in reactors {
if reactor == blindedUserPublicKey { continue } // Will add a reaction for this case outside of the loop if reactor == blindedUserPublicKey { continue } // Will add a reaction for this case outside of the loop
let reaction = Reaction( let reaction = Reaction(

View File

@ -38,11 +38,13 @@ extension OpenGroupAPI {
case count case count
case reactors case reactors
case you case you
case first
} }
public let count: Int64 public let count: Int64
public let reactors: [String]? public let reactors: [String]?
public let you: Bool public let you: Bool
public let first: TimeInterval
} }
public let reactions: [String:Reaction]? public let reactions: [String:Reaction]?
@ -114,7 +116,8 @@ extension OpenGroupAPI.Message.Reaction {
self = OpenGroupAPI.Message.Reaction( self = OpenGroupAPI.Message.Reaction(
count: try container.decode(Int64.self, forKey: .count), count: try container.decode(Int64.self, forKey: .count),
reactors: try? container.decode([String].self, forKey: .reactors), 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( let sortId = Reaction.getSortId(
db, db,
interactionId: interactionId, interactionId: interactionId,
emoji: reaction.emoji emoji: reaction.emoji,
timestamp: Int64(messageSentTimestamp * 1000)
) )
switch reaction.kind { switch reaction.kind {

View File

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