-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKlusterDataSource.swift
More file actions
132 lines (114 loc) · 5.71 KB
/
KlusterDataSource.swift
File metadata and controls
132 lines (114 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// KlusterDataSource.swift
// Cluster
//
// Created by Michael Fellows on 11/13/15.
// Copyright © 2015 ImagineME. All rights reserved.
//
import Foundation
class KlusterDataSource: NSObject {
class func createKlusterWithParams(params: [NSObject : AnyObject]?, completion:PFIdResultBlock) -> Void
{
PFCloud.callFunctionInBackground("createKluster", withParameters: params) { (object, error) -> Void in
completion(object, error)
}
}
class func joinKluster(klusterId: NSString!, completion:PFIdResultBlock) -> Void {
let params = ["klusterId": klusterId]
PFCloud.callFunctionInBackground("joinKluster", withParameters: params) { (object, error) -> Void in
completion(object, error)
}
}
class func searchForKlusterWithString(searchString: String, completion:PFIdResultBlock) -> Void
{
let lowercaseString = searchString.lowercaseString
PFCloud.callFunctionInBackground("searchForKluster", withParameters: ["searchString": lowercaseString]) { (object, error) -> Void in
completion(object, error)
}
}
class func fetchMainKlusters(params: [NSObject : AnyObject]?, completion: PFIdResultBlock) -> Void {
PFCloud.callFunctionInBackground("fetchMainKlusters", withParameters: params) { (object, error) -> Void in
KlusterStore.sharedInstance.userKlusters = object as? [PFObject]
completion(object, error)
}
}
class func deleteKluster(klusterId: String, completion: PFIdResultBlock) -> Void {
PFCloud.callFunctionInBackground("deleteKluster", withParameters: ["klusterId": klusterId]) { (object, error) -> Void in
completion(object, error)
}
}
class func fetchKlustersForUser(completion:PFIdResultBlock) -> Void {
PFCloud.callFunctionInBackground("fetchKlustersForUser", withParameters: nil) { (object, error) -> Void in
KlusterStore.sharedInstance.userKlusters = object as? [PFObject]
completion(object, error)
}
}
class func fetchMembersForKluster(kluster: Kluster!, completion:PFArrayResultBlock) -> Void {
let query = kluster.memberRelation.query()
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
completion(objects, error)
}
}
// Mark: - Sending messages
class func createMessageInKluster(klusterId: String, text: String, completion: PFIdResultBlock) -> Void {
let params = ["klusterId" : klusterId, "text": text]
PFCloud.callFunctionInBackground("createMessage", withParameters: params) { (object, error) -> Void in
completion(object, error)
}
}
class func fetchMessagesInKluster(klusterId: String, skip: Int, completion: PFIdResultBlock) -> Void {
let params = ["klusterId" : klusterId]
PFCloud.callFunctionInBackground("fetchMessagesForKluster", withParameters: params) { (objects, error) -> Void in
completion(objects, error)
}
}
// Delete user account
class func deleteUserAccount(completion: PFIdResultBlock) -> Void {
PFCloud.callFunctionInBackground("deleteUserAccount", withParameters: nil) { (object, error) -> Void in
completion(object, error)
}
}
class func fetchUsersWithFacebookIds(facebookIDs: [String], kluster: Kluster,completion: PFIdResultBlock) -> Void {
if let query = PFUser.query() {
query.whereKey("facebookId", containedIn: facebookIDs)
// Important to not show Kluster members in the Facebook query. They're already members 😉
// let memberRelation = kluster.memberRelation.query()
// query.whereKey("objectId", doesNotMatchQuery: memberRelation)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
completion(objects, error)
})
}
}
// Takes an array of user ids and invites user to the specified kluster
class func inviteUsersToKluster(userIds: [String]?, klusterId: String?,completion: PFIdResultBlock) -> Void {
if let userIds = userIds, let klusterId = klusterId where userIds.count > 0 && klusterId.length > 0 {
let params = ["klusterId": klusterId, "userIds": userIds]
PFCloud.callFunctionInBackground("inviteUsersToKluster", withParameters: params as [NSObject : AnyObject]) { (object, error) -> Void in
completion(object, error)
}
}
}
class func fetchInvites(completion: PFIdResultBlock) -> Void {
PFCloud.callFunctionInBackground("fetchInvitationsForUser", withParameters: nil) { (object, error) -> Void in
completion(object, error)
}
}
// Accepts an invitation to a Kluster
class func acceptInvitation(klusterId: String?, invitationId: String?, completion: PFIdResultBlock) -> Void {
if let klusterId = klusterId, let invitationId = invitationId {
let params = ["klusterId": klusterId, "invitationId": invitationId]
PFCloud.callFunctionInBackground("acceptInvitationToKluster", withParameters: params, block: { (object, error) -> Void in
completion(object, error)
})
}
}
// Declines an invitation to a Kluster
class func declineKlusterInvitation(invitationId: String?, completion: PFIdResultBlock) -> Void {
if let invitationId = invitationId {
let params = ["invitationId": invitationId]
PFCloud.callFunctionInBackground("declineKlusterInvitationToKluster", withParameters: params, block: { (object, error) -> Void in
completion(object, error)
})
}
}
}