-
Notifications
You must be signed in to change notification settings - Fork 296
Add NIP-88 polls support #3253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alltheseas
wants to merge
3
commits into
master
Choose a base branch
from
feature/nip-88-polls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add NIP-88 polls support #3253
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// PollDraft.swift | ||
// damus | ||
// | ||
// Created by ChatGPT on 2025-04-02. | ||
// | ||
|
||
import Foundation | ||
|
||
struct PollDraftOption: Identifiable, Equatable { | ||
let id: UUID | ||
var text: String | ||
} | ||
|
||
struct PollDraft: Equatable { | ||
var options: [PollDraftOption] | ||
var pollType: PollType | ||
var endsAt: Date? | ||
|
||
static let minimumOptions: Int = 2 | ||
static let maximumOptions: Int = 6 | ||
|
||
static func makeDefault() -> PollDraft { | ||
PollDraft( | ||
options: (0..<PollDraft.minimumOptions).map { _ in PollDraftOption(id: UUID(), text: "") }, | ||
pollType: .singleChoice, | ||
endsAt: nil | ||
) | ||
} | ||
|
||
mutating func addOption() { | ||
guard options.count < PollDraft.maximumOptions else { return } | ||
options.append(PollDraftOption(id: UUID(), text: "")) | ||
} | ||
|
||
mutating func removeOption(id: UUID) { | ||
guard options.count > PollDraft.minimumOptions else { return } | ||
options.removeAll { $0.id == id } | ||
} | ||
|
||
mutating func ensureMinimumOptions() { | ||
if options.count < PollDraft.minimumOptions { | ||
let missing = PollDraft.minimumOptions - options.count | ||
for _ in 0..<missing { | ||
options.append(PollDraftOption(id: UUID(), text: "")) | ||
} | ||
} | ||
} | ||
|
||
var normalizedOptionLabels: [String] { | ||
options | ||
.map { $0.text.trimmingCharacters(in: .whitespacesAndNewlines) } | ||
.filter { !$0.isEmpty } | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,159 @@ | ||||||
// | ||||||
// PollModels.swift | ||||||
// damus | ||||||
// | ||||||
// Created by ChatGPT on 2025-04-02. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The creation date 2025-04-02 appears to be in the future relative to when this was likely created. Consider updating to the actual creation date.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
// | ||||||
|
||||||
import Foundation | ||||||
|
||||||
enum PollType: String { | ||||||
case singleChoice = "singlechoice" | ||||||
case multipleChoice = "multiplechoice" | ||||||
|
||||||
static var `default`: PollType { .singleChoice } | ||||||
} | ||||||
|
||||||
struct PollOption: Identifiable, Hashable { | ||||||
let id: String | ||||||
let label: String | ||||||
} | ||||||
|
||||||
struct PollEvent { | ||||||
let id: NoteId | ||||||
let author: Pubkey | ||||||
let createdAt: UInt32 | ||||||
let question: String | ||||||
let options: [PollOption] | ||||||
let pollType: PollType | ||||||
let relayHints: [RelayURL] | ||||||
let endsAt: UInt32? | ||||||
|
||||||
private let optionIdSet: Set<String> | ||||||
|
||||||
init?(event: NostrEvent) { | ||||||
guard event.known_kind == .poll else { return nil } | ||||||
|
||||||
var options: [PollOption] = [] | ||||||
var optionIds: Set<String> = [] | ||||||
var relayHints: [RelayURL] = [] | ||||||
var pollType: PollType = .default | ||||||
var endsAt: UInt32? = nil | ||||||
|
||||||
for tag in event.tags { | ||||||
guard tag.count >= 2 else { continue } | ||||||
var iterator = tag.makeIterator() | ||||||
guard let keyElem = iterator.next() else { continue } | ||||||
|
||||||
switch keyElem.string() { | ||||||
case "option": | ||||||
guard tag.count >= 3, | ||||||
let idElem = iterator.next(), | ||||||
let labelElem = iterator.next() | ||||||
else { continue } | ||||||
let optionId = idElem.string() | ||||||
let optionLabel = labelElem.string() | ||||||
guard !optionId.isEmpty, !optionLabel.isEmpty, !optionIds.contains(optionId) else { continue } | ||||||
optionIds.insert(optionId) | ||||||
options.append(PollOption(id: optionId, label: optionLabel)) | ||||||
|
||||||
case "relay": | ||||||
guard let relayElem = iterator.next(), | ||||||
let relay = RelayURL(relayElem.string()) | ||||||
else { continue } | ||||||
if !relayHints.contains(relay) { | ||||||
relayHints.append(relay) | ||||||
} | ||||||
|
||||||
case "polltype": | ||||||
guard let typeElem = iterator.next() else { continue } | ||||||
pollType = PollType(rawValue: typeElem.string()) ?? .default | ||||||
|
||||||
case "endsAt": | ||||||
guard let endsElem = iterator.next() else { continue } | ||||||
if let raw = endsElem.u64() { | ||||||
if raw > UInt64(UInt32.max) { | ||||||
endsAt = UInt32.max | ||||||
} else { | ||||||
endsAt = UInt32(raw) | ||||||
} | ||||||
} | ||||||
|
||||||
default: | ||||||
continue | ||||||
} | ||||||
} | ||||||
|
||||||
guard options.count >= 2 else { return nil } | ||||||
|
||||||
self.id = event.id | ||||||
self.author = event.pubkey | ||||||
self.createdAt = event.created_at | ||||||
self.question = event.content | ||||||
self.options = options | ||||||
self.pollType = pollType | ||||||
self.relayHints = relayHints | ||||||
self.endsAt = endsAt | ||||||
self.optionIdSet = optionIds | ||||||
} | ||||||
|
||||||
func containsOption(_ optionId: String) -> Bool { | ||||||
optionIdSet.contains(optionId) | ||||||
} | ||||||
|
||||||
func isExpired(at timestamp: UInt32) -> Bool { | ||||||
guard let endsAt else { return false } | ||||||
return timestamp > endsAt | ||||||
} | ||||||
|
||||||
func isExpired(now: Date = .now) -> Bool { | ||||||
guard let endsAt else { return false } | ||||||
return UInt32(now.timeIntervalSince1970) > endsAt | ||||||
} | ||||||
} | ||||||
|
||||||
struct PollResponse { | ||||||
let pollId: NoteId | ||||||
let responseId: NoteId | ||||||
let responder: Pubkey | ||||||
let createdAt: UInt32 | ||||||
let optionIds: [String] | ||||||
|
||||||
init?(event: NostrEvent) { | ||||||
guard event.known_kind == .poll_response else { return nil } | ||||||
|
||||||
guard let pollReference = event.referenced_ids.first(where: { ref in | ||||||
if case .event = ref { | ||||||
return true | ||||||
} | ||||||
return false | ||||||
}), | ||||||
case .event(let pollId) = pollReference | ||||||
else { | ||||||
return nil | ||||||
} | ||||||
|
||||||
var responses: [String] = [] | ||||||
for tag in event.tags { | ||||||
guard tag.count >= 2 else { continue } | ||||||
var iterator = tag.makeIterator() | ||||||
guard let keyElem = iterator.next(), keyElem.string() == "response", | ||||||
let responseElem = iterator.next() | ||||||
else { | ||||||
continue | ||||||
} | ||||||
let optionId = responseElem.string() | ||||||
guard !optionId.isEmpty else { continue } | ||||||
responses.append(optionId) | ||||||
} | ||||||
|
||||||
guard !responses.isEmpty else { return nil } | ||||||
|
||||||
self.pollId = pollId | ||||||
self.responseId = event.id | ||||||
self.responder = event.pubkey | ||||||
self.createdAt = event.created_at | ||||||
self.optionIds = responses | ||||||
} | ||||||
} | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The creation date 2025-04-02 appears to be in the future relative to when this was likely created. Consider updating to the actual creation date.
Copilot uses AI. Check for mistakes.