概要
Kiicloudではスコープという概念があり,ユーザーに応じて閲覧権限を持たせることができます.つまりこれを用いるとプライベートな空間も作成可能であり,LINEのグループのような機能もできてしまいます.以下の参考にはとても詳しく書いてあるので,詳しくはそちらを閲覧いただければよろしいかと思いますが,表題の機能だけを手っ取り早く実装したい時のために本投稿はございます.
Group Scopeとは
Kiicloudでは Application Scope(ログインユーザー誰でも可)、Group Scope(認証ユーザーのみ可)、User Scope(自分のみ可)という3つのスコープがあります.
結論から
グループへユーザーの追加
手っ取り早く申し上げますと,グループにユーザーを追加する場合は以下のメソッドでユーザーを追加できます.Kiicloudすごいですね.バンザイ!
Uri userUri = Uri.parse("put existing user uri here");
KiiUser user2 = KiiUser.createByUri(userUri);
group.addUser(user2);
group.save(new KiiGroupCallBack() {
@Override
public void onSaveCompleted(int token, KiiGroup group, Exception exception) {
if (exception != null) {
// Error handling
return;
}
}
});
参考:ノンブロッキングのほうがいいと思います
ただし,オーナー(グループ作成者)からしか上記メソッドは使用できません!
ハマりどころなので注意です.
既存グループの取得
上記「group」という変数が出てきていますが,取得するためにはrefreshメソッドが必要な場合もあります.
// Get URI from the existing group.
// You should get the URI just after the creation of the group.
Uri groupUri = group.toUri();
// ... In another situation ...
// Instantiate the group again.
KiiGroup group = KiiGroup.createByUri(groupUri);
// Refresh the instance to make it up-to-date.
group.refresh(new KiiGroupCallBack() {
@Override
public void onRefreshCompleted(int token, KiiGroup group2, Exception exception) {
if (exception != null) {
// Error handling
return;
}
// Do something with the group reference.
}
});
ユーザーからグループに参加したい場合(フォローリクエスト)
オーナーから特定のユーザーをグループへ招待する場合は問題ありませんが,ユーザーからグループに入りたい場合は少し複雑になります.イメージとしてはSNSなどであるフォローリクエストです.
オーナーからしかユーザーをグループに追加することができないため,以下のフローになります.
ユーザー作成時
全体から参加したいグループを検索できるようにするため,作成者もしくはグループのIDをApplication Scope(ログインユーザー誰でも検索可)のbakectへ保存
ユーザーA
Application Scopeから参加したいグループを検索→オーナーAへ参加リクエストの送信(自分のIDなども送信)
オーナーA
グループAを作成→オーナーのuserIDを全体から検索できるようにしておく.
オーナーAがリクエストを受信時
ユーザーAからリクエストされた際は送信されたIDをもとにkiiUserを作成し「group.addUser(user2);」をつかって,グループに追加.
以下IDからUserの追加の方法.
String userID = "put existing user ID here";
final KiiUser user = KiiUser.userWithID(userID);
user.refresh(new KiiUserCallBack() {
@Override
public void onRefreshCompleted(int token, Exception exception) {
if (exception != null) {
// Error handling
return;
}
// check whether user is disabled.
boolean disabled = user.disabled();
if (user.isLinkedWithSocialProvider(Provider.FACEBOOK)) {
// User is linked to the Facebook account.
}
if (user.isLinkedWithSocialProvider(Provider.TWITTER)) {
// User is linked to the Twitter account.
}
}
});
課題
本投稿の結論になります.ユーザー同士で,グループへの参加機能を実装する場合はIDを交換できる仕組みが必要です.なんだよ,それどうすんのよ?と思ったあなた.Kiicloudいたれりつくせりです.
Kiicloudにはユーザー間のプッシュ通知を行う仕組みが提供されています.KiiCloudバンザイ!
つづき(プッシュ通知)