Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Subscribe/Unsubscribe #2482

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions Classes/Issues/GithubClient+Issues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ extension GithubClient {
milestone: milestoneModel,
targetBranch: targetBranchModel,
timelinePages: [newPage] + (prependResult?.timelinePages ?? []),
viewerIsSubscribed: issueType.viewerSubscribed,
viewerCanUpdate: issueType.viewerCanUpdate,
hasIssuesEnabled: repository.hasIssuesEnabled,
viewerCanAdminister: canAdmin,
Expand Down Expand Up @@ -297,6 +298,25 @@ extension GithubClient {
}
}


func setSubscription(
previous: IssueResult,
subscribed: Bool,
completion: ((Result<Bool>) -> Void)? = nil
) {

let cache = self.cache

client.send(V3SubscribeThreadRequest(id: previous.id, ignore: subscribed)) { result in
switch result {
case .success:
completion?(.success(true))
case .failure(let error):
cache.set(value: previous)
}
}
}

enum CollaboratorPermission: String {
case admin
case write
Expand Down
4 changes: 4 additions & 0 deletions Classes/Issues/Issue+IssueType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import IGListKit

extension IssueOrPullRequestQuery.Data.Repository.IssueOrPullRequest.AsIssue: IssueType {

var viewerSubscribed: Bool {
return viewerSubscription?.rawValue != "IGNORED"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We should use a constant for this value here: SubscriptionState.ignored.rawValue

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shall fix this!

}

var pullRequest: Bool {
return false
}
Expand Down
27 changes: 26 additions & 1 deletion Classes/Issues/IssueManagingContextController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ final class IssueManagingContextController: NSObject, ContextMenuDelegate {
case lock
case reopen
case close
case subscribe
case unsubscribe
}

var actions: [Action] {
Expand All @@ -97,6 +99,15 @@ final class IssueManagingContextController: NSObject, ContextMenuDelegate {
if result.pullRequest {
actions.append(.reviewers)
}
}

if result.viewerIsSubscribed {
actions.append(.unsubscribe)
} else {
actions.append(.subscribe)
}

if case .collaborator = permissions {
if result.labels.locked {
actions.append(.unlock)
} else {
Expand Down Expand Up @@ -144,13 +155,19 @@ final class IssueManagingContextController: NSObject, ContextMenuDelegate {
case .close:
title = Constants.Strings.close
iconName = "x"
case .subscribe:
title = Constants.Strings.subscribe
iconName = "unmute"
case .unsubscribe:
title = Constants.Strings.unsubscribe
iconName = "mute"
}

// Lock always has the divider above it assuming you're a collaborator.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update this comment.

// If you aren't a collaborator (Lock does not show), close has the divider above it.
let separator: Bool
switch action {
case .lock, .unlock: separator = true
case .subscribe, .unsubscribe: separator = true
case .reopen, .close: separator = permissions != .collaborator
default: separator = false
}
Expand Down Expand Up @@ -186,6 +203,8 @@ final class IssueManagingContextController: NSObject, ContextMenuDelegate {
case .lock: strongSelf.lock(true)
case .reopen: strongSelf.close(false)
case .close: strongSelf.close(true)
case .subscribe: strongSelf.subscribe(true)
case .unsubscribe: strongSelf.subscribe(false)
}
}
}
Expand Down Expand Up @@ -271,6 +290,12 @@ final class IssueManagingContextController: NSObject, ContextMenuDelegate {
)
}

func subscribe(_ doSubscribe: Bool) {
guard let previous = result else { return }
delegate?.willMutateModel(from: self)
client.setSubscription(previous: previous, subscribed: doSubscribe)
Haptic.triggerNotification(.success)
}
func close(_ doClose: Bool) {
guard let previous = result else { return }
delegate?.willMutateModel(from: self)
Expand Down
5 changes: 5 additions & 0 deletions Classes/Issues/IssueResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct IssueResult: Cachable {
let targetBranch: IssueTargetBranchModel?
// end optionals
let timelinePages: [IssueTimelinePage]
let viewerIsSubscribed: Bool
let viewerCanUpdate: Bool
let hasIssuesEnabled: Bool
let viewerCanAdminister: Bool
Expand Down Expand Up @@ -73,6 +74,7 @@ struct IssueResult: Cachable {
labels: IssueLabelsModel? = nil,
assignee: IssueAssigneesModel? = nil,
timelinePages: [IssueTimelinePage]? = nil,
viewerIsSubscribed: Bool? = nil,
viewerCanUpdate: Bool? = nil,
hasIssuesEnabled: Bool? = nil,
viewerCanAdminister: Bool? = nil,
Expand All @@ -90,6 +92,7 @@ struct IssueResult: Cachable {
milestone: self.milestone,
targetBranch: self.targetBranch,
timelinePages: timelinePages ?? self.timelinePages,
viewerIsSubscribed: viewerIsSubscribed ?? self.viewerIsSubscribed,
viewerCanUpdate: viewerCanUpdate ?? self.viewerCanUpdate,
hasIssuesEnabled: hasIssuesEnabled ?? self.hasIssuesEnabled,
viewerCanAdminister: viewerCanAdminister ?? self.viewerCanAdminister,
Expand All @@ -114,6 +117,7 @@ struct IssueResult: Cachable {
milestone: milestone,
targetBranch: self.targetBranch,
timelinePages: timelinePages ?? self.timelinePages,
viewerIsSubscribed: self.viewerIsSubscribed,
viewerCanUpdate: self.viewerCanUpdate,
hasIssuesEnabled: self.hasIssuesEnabled,
viewerCanAdminister: self.viewerCanAdminister,
Expand All @@ -138,6 +142,7 @@ struct IssueResult: Cachable {
milestone: self.milestone,
targetBranch: self.targetBranch,
timelinePages: timelinePages ?? self.timelinePages,
viewerIsSubscribed: self.viewerIsSubscribed,
viewerCanUpdate: self.viewerCanUpdate,
hasIssuesEnabled: self.hasIssuesEnabled,
viewerCanAdminister: self.viewerCanAdminister,
Expand Down
1 change: 1 addition & 0 deletions Classes/Issues/IssueType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protocol IssueType {
var targetBranch: String? { get }
var locked: Bool { get }
var headPaging: HeadPaging { get }
var viewerSubscribed: Bool { get }
var viewerCanUpdate: Bool { get }
var fileChanges: FileChanges? { get }

Expand Down
4 changes: 4 additions & 0 deletions Classes/Issues/PullRequest+IssueType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import StyledTextKit

extension IssueOrPullRequestQuery.Data.Repository.IssueOrPullRequest.AsPullRequest: IssueType {

var viewerSubscribed: Bool {
return viewerSubscription?.rawValue != "IGNORED"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We should use a constant for this value here: SubscriptionState.ignored.rawValue

}

var pullRequest: Bool {
return true
}
Expand Down
2 changes: 2 additions & 0 deletions Classes/Views/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ enum Constants {
static let reviewers = NSLocalizedString("Reviewers", comment: "")
static let clear = NSLocalizedString("Clear", comment: "")
static let preview = NSLocalizedString("Preview", comment: "")
static let subscribe = NSLocalizedString("Subscribe", comment: "")
static let unsubscribe = NSLocalizedString("Unsubscribe", comment: "")
}
}
101 changes: 82 additions & 19 deletions gql/API.swift

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions gql/IssueOrPullRequest.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ query IssueOrPullRequest($owner: String!, $repo: String!, $number: Int!, $page_s
}
issueOrPullRequest(number: $number) {
... on Issue {
viewerSubscription
timeline(last: $page_size, before: $before) {
pageInfo{...headPaging}
nodes {
Expand Down Expand Up @@ -182,6 +183,7 @@ query IssueOrPullRequest($owner: String!, $repo: String!, $number: Int!, $page_s
title
}
... on PullRequest {
viewerSubscription
timeline(last: $page_size, before: $before) {
pageInfo{...headPaging}
nodes {
Expand Down