doc: storage

oneRain 2021-04-19 16:28:31 +08:00
parent 70d2e2e422
commit a4be2b8bb5
19 changed files with 544 additions and 19 deletions

View File

@ -3,7 +3,15 @@ using LeanCloud.Storage;
using LeanCloud.Storage.Internal.Persistence;
namespace LeanCloud {
/// <summary>
/// LCApplication contains static functions that handle global configuration
/// for LeanCloud services.
/// </summary>
public class LCApplication {
/// <summary>
/// Whether or not ths SDK using master key.
/// Default is false.
/// </summary>
public static bool UseMasterKey {
get => LCCore.UseMasterKey;
set {
@ -11,6 +19,13 @@ namespace LeanCloud {
}
}
/// <summary>
/// Initialize LeanCloud services.
/// </summary>
/// <param name="appId">The application id provided in LeanCloud dashboard.</param>
/// <param name="appKey">The application key provided in LeanCloud dashboard.</param>
/// <param name="server">The server url bound by yourself.</param>
/// <param name="masterKey">The application master key provided in LeanCloud dashboard.</param>
public static void Initialize(string appId,
string appKey,
string server = null,

View File

@ -3,21 +3,28 @@ using System.Collections.Generic;
namespace LeanCloud.Storage {
/// <summary>
/// LeanCloud Access Control Lists.
/// LCACL is used to control which users and roles can access or modify
/// a particular object. Each LCObject can have its own LCObject.
/// </summary>
public class LCACL {
const string PublicKey = "*";
const string RoleKeyPrefix = "role:";
public Dictionary<string, bool> ReadAccess {
internal Dictionary<string, bool> ReadAccess {
get;
} = new Dictionary<string, bool>();
public Dictionary<string, bool> WriteAccess {
internal Dictionary<string, bool> WriteAccess {
get;
} = new Dictionary<string, bool>();
/// <summary>
/// Creates a LCACL that is allowed to read and write this object
/// for the user.
/// </summary>
/// <param name="owner">The user.</param>
/// <returns></returns>
public static LCACL CreateWithOwner(LCUser owner) {
if (owner == null) {
throw new ArgumentNullException(nameof(owner));
@ -28,6 +35,9 @@ namespace LeanCloud.Storage {
return acl;
}
/// <summary>
/// Gets or sets whether the public is allowed to read this object.
/// </summary>
public bool PublicReadAccess {
get {
return GetAccess(ReadAccess, PublicKey);
@ -36,6 +46,9 @@ namespace LeanCloud.Storage {
}
}
/// <summary>
/// Gets or sets whether the public is allowed to write this object.
/// </summary>
public bool PublicWriteAccess {
get {
return GetAccess(WriteAccess, PublicKey);
@ -44,6 +57,14 @@ namespace LeanCloud.Storage {
}
}
/// <summary>
/// Gets whether the given user id is *explicitly* allowed to read this
/// object. Even if this returns false, the user may still be able to read
/// it if <see cref="PublicReadAccess"/> is true or a role that the user
/// belongs to has read access.
/// </summary>
/// <param name="userId">The user ObjectId to check.</param>
/// <returns></returns>
public bool GetUserIdReadAccess(string userId) {
if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId));
@ -51,6 +72,11 @@ namespace LeanCloud.Storage {
return GetAccess(ReadAccess, userId);
}
/// <summary>
/// Set whether the given user id is allowed to read this object.
/// </summary>
/// <param name="userId">The ObjectId of the user.</param>
/// <param name="value">Whether the user has permission.</param>
public void SetUserIdReadAccess(string userId, bool value) {
if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId));
@ -58,6 +84,13 @@ namespace LeanCloud.Storage {
SetAccess(ReadAccess, userId, value);
}
/// <summary>
/// Gets whether the given user id is *explicitly* allowed to write this
/// object. Even if this returns false, the user may still be able to write
/// it if <see cref="PublicWriteAccess"/> is true or a role that the user
/// belongs to has read access.
/// </summary>
/// <param name="userId">T
public bool GetUserIdWriteAccess(string userId) {
if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId));
@ -65,6 +98,11 @@ namespace LeanCloud.Storage {
return GetAccess(WriteAccess, userId);
}
/// <summary>
/// Set whether the given user id is allowed to write this object.
/// </summary>
/// <param name="userId">The ObjectId of the user.</param>
/// <param name="value">Whether the user has permission.</param>
public void SetUserIdWriteAccess(string userId, bool value) {
if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId));
@ -72,6 +110,14 @@ namespace LeanCloud.Storage {
SetAccess(WriteAccess, userId, value);
}
/// <summary>
/// Gets whether the given user is *explicitly* allowed to read this
/// object. Even if this returns false, the user may still be able to read
/// it if <see cref="PublicReadAccess"/> is true or a role that the user
/// belongs to has read access.
/// </summary>
/// <param name="user">The user to check.</param>
/// <returns></returns>
public bool GetUserReadAccess(LCUser user) {
if (user == null) {
throw new ArgumentNullException(nameof(user));
@ -79,6 +125,11 @@ namespace LeanCloud.Storage {
return GetUserIdReadAccess(user.ObjectId);
}
/// <summary>
/// Set whether the given user is allowed to read this object.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="value">Whether the user has permission.</param>
public void SetUserReadAccess(LCUser user, bool value) {
if (user == null) {
throw new ArgumentNullException(nameof(user));
@ -86,6 +137,14 @@ namespace LeanCloud.Storage {
SetUserIdReadAccess(user.ObjectId, value);
}
/// <summary>
/// Gets whether the given user is *explicitly* allowed to write this
/// object. Even if this returns false, the user may still be able to write
/// it if <see cref="PublicReadAccess"/> is true or a role that the user
/// belongs to has write access.
/// </summary>
/// <param name="userId">The user to check.</param>
/// <returns></returns>
public bool GetUserWriteAccess(LCUser user) {
if (user == null) {
throw new ArgumentNullException(nameof(user));
@ -93,6 +152,11 @@ namespace LeanCloud.Storage {
return GetUserIdWriteAccess(user.ObjectId);
}
/// <summary>
/// Set whether the given user is allowed to write this object.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="value">Whether the user has permission.</param>
public void SetUserWriteAccess(LCUser user, bool value) {
if (user == null) {
throw new ArgumentNullException(nameof(user));
@ -100,6 +164,11 @@ namespace LeanCloud.Storage {
SetUserIdWriteAccess(user.ObjectId, value);
}
/// <summary>
/// Get whether the given role are allowed to read this object.
/// </summary>
/// <param name="role">The role to check.</param>
/// <returns></returns>
public bool GetRoleReadAccess(LCRole role) {
if (role == null) {
throw new ArgumentNullException(nameof(role));
@ -108,6 +177,11 @@ namespace LeanCloud.Storage {
return GetAccess(ReadAccess, roleKey);
}
/// <summary>
/// Sets whether the given role are allowed to read this object.
/// </summary>
/// <param name="role">The role.</param>
/// <param name="value">Whether the role has access.</param>
public void SetRoleReadAccess(LCRole role, bool value) {
if (role == null) {
throw new ArgumentNullException(nameof(role));
@ -116,6 +190,11 @@ namespace LeanCloud.Storage {
SetAccess(ReadAccess, roleKey, value);
}
/// <summary>
/// Get whether the given role are allowed to write this object.
/// </summary>
/// <param name="role">The role to check.</param>
/// <returns></returns>
public bool GetRoleWriteAccess(LCRole role) {
if (role == null) {
throw new ArgumentNullException(nameof(role));
@ -124,6 +203,11 @@ namespace LeanCloud.Storage {
return GetAccess(WriteAccess, roleKey);
}
/// <summary>
/// Sets whether the given role are allowed to write this object.
/// </summary>
/// <param name="role">The role.</param>
/// <param name="value">Whether the role has access.</param>
public void SetRoleWriteAccess(LCRole role, bool value) {
if (role == null) {
throw new ArgumentNullException(nameof(role));

View File

@ -37,6 +37,13 @@ namespace LeanCloud.Storage {
return response;
}
/// <summary>
/// Invokes a cloud function.
/// </summary>
/// <typeparam name="T">The type of return value.</typeparam>
/// <param name="name">Cloud function name.</param>
/// <param name="parameters">Parameters of cloud function.</param>
/// <returns></returns>
public static async Task<T> Run<T>(string name,
Dictionary<string, object> parameters = null) {
Dictionary<string, object> response = await Run(name, parameters);

View File

@ -7,9 +7,15 @@ using LeanCloud.Storage.Internal.File;
using LeanCloud.Storage.Internal.Object;
namespace LeanCloud.Storage {
/// <summary>
/// LCFile is a local representation of a file that is saved to LeanCloud.
/// </summary>
public class LCFile : LCObject {
public const string CLASS_NAME = "_File";
/// <summary>
/// Gets the name of the file.
/// </summary>
public string Name {
get {
return this["name"] as string;
@ -18,6 +24,9 @@ namespace LeanCloud.Storage {
}
}
/// <summary>
/// Gets the MIME type of the file.
/// </summary>
public string MimeType {
get {
return this["mime_type"] as string;
@ -26,6 +35,9 @@ namespace LeanCloud.Storage {
}
}
/// <summary>
/// Gets the url of the file.
/// </summary>
public string Url {
get {
return this["url"] as string;
@ -34,6 +46,9 @@ namespace LeanCloud.Storage {
}
}
/// <summary>
/// Gets the metadata of the file.
/// </summary>
public Dictionary<string, object> MetaData {
get {
return this["metaData"] as Dictionary<string, object>;
@ -44,30 +59,58 @@ namespace LeanCloud.Storage {
readonly Stream stream;
/// <summary>
/// Creates a new file.
/// </summary>
public LCFile() : base(CLASS_NAME) {
MetaData = new Dictionary<string, object>();
}
/// <summary>
/// Creates a new file from a byte array and a name.
/// </summary>
/// <param name="name"></param>
/// <param name="bytes"></param>
public LCFile(string name, byte[] bytes) : this() {
Name = name;
stream = new MemoryStream(bytes);
}
/// <summary>
/// Creates a new file from a local file and a name.
/// </summary>
/// <param name="name"></param>
/// <param name="path"></param>
public LCFile(string name, string path) : this() {
Name = name;
MimeType = LCMimeTypeMap.GetMimeType(path);
stream = new FileStream(path, FileMode.Open);
}
/// <summary>
/// Creates a new file from an url and a name.
/// </summary>
/// <param name="name"></param>
/// <param name="url"></param>
public LCFile(string name, Uri url) : this() {
Name = name;
Url = url.AbsoluteUri;
}
/// <summary>
/// Add metadata.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddMetaData(string key, object value) {
MetaData[key] = value;
}
/// <summary>
/// Saves the file to LeanCloud.
/// </summary>
/// <param name="onProgress"></param>
/// <returns></returns>
public async Task<LCFile> Save(Action<long, long> onProgress = null) {
if (!string.IsNullOrEmpty(Url)) {
// 外链方式
@ -108,6 +151,10 @@ namespace LeanCloud.Storage {
return this;
}
/// <summary>
/// Deletes the file from LeanCloud.
/// </summary>
/// <returns></returns>
public new async Task Delete() {
if (string.IsNullOrEmpty(ObjectId)) {
return;
@ -116,6 +163,15 @@ namespace LeanCloud.Storage {
await LCCore.HttpClient.Delete(path);
}
/// <summary>
/// Gets the thumbnail url of image file.
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="quality"></param>
/// <param name="scaleToFit"></param>
/// <param name="format"></param>
/// <returns></returns>
public string GetThumbnailUrl(int width, int height, int quality = 100, bool scaleToFit = true, string format = "png") {
int mode = scaleToFit ? 2 : 1;
return $"{Url}?imageView/{mode}/w/{width}/h/{height}/q/{quality}/format/{format}";
@ -132,6 +188,10 @@ namespace LeanCloud.Storage {
return await LCCore.HttpClient.Post<Dictionary<string, object>>("fileTokens", data: data);
}
/// <summary>
/// Gets LCQuery of LCFile.
/// </summary>
/// <returns></returns>
public static LCQuery<LCFile> GetQuery() {
return new LCQuery<LCFile>(CLASS_NAME);
}

View File

@ -1,19 +1,34 @@
using System.Collections.Generic;
namespace LeanCloud.Storage {
/// <summary>
/// LCFollowersAndFollowees is a result that contains followers and followees.
/// </summary>
public class LCFollowersAndFollowees {
/// <summary>
/// The followers.
/// </summary>
public List<LCObject> Followers {
get; internal set;
}
/// <summary>
/// The followees.
/// </summary>
public List<LCObject> Followees {
get; internal set;
}
/// <summary>
/// The count of followers.
/// </summary>
public int FollowersCount {
get; internal set;
}
/// <summary>
/// The count of followees.
/// </summary>
public int FolloweesCount {
get; internal set;
}

View File

@ -5,7 +5,16 @@ using LeanCloud.Common;
using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Storage {
/// <summary>
/// LCFriendship contains static functions that handle the friendship of LeanCloud.
/// </summary>
public static class LCFriendship {
/// <summary>
/// Requests to add friend.
/// </summary>
/// <param name="userId">The user id to add.</param>
/// <param name="attributes">The additional attributes for the friendship.</param>
/// <returns></returns>
public static async Task Request(string userId, Dictionary<string, object> attributes = null) {
LCUser user = await LCUser.GetCurrent();
if (user == null) {
@ -23,6 +32,12 @@ namespace LeanCloud.Storage {
await LCCore.HttpClient.Post<Dictionary<string, object>>(path, data: data);
}
/// <summary>
/// Accepts the request of add friend.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="attributes">The additional attributes for the friendship.</param>
/// <returns></returns>
public static async Task AcceptRequest(LCFriendshipRequest request, Dictionary<string, object> attributes = null) {
if (request == null) {
throw new ArgumentNullException(nameof(request));
@ -37,6 +52,11 @@ namespace LeanCloud.Storage {
await LCCore.HttpClient.Put<Dictionary<string, object>>(path, data: data);
}
/// <summary>
/// Declines the request of add friend.
/// </summary>
/// <param name="request">The request.</param>
/// <returns></returns>
public static async Task DeclineRequest(LCFriendshipRequest request) {
if (request == null) {
throw new ArgumentNullException(nameof(request));

View File

@ -1,4 +1,8 @@
namespace LeanCloud.Storage {
/// <summary>
/// LCFriendshipRequest is a local representation of a friend request that
/// is saved to LeanCloud.
/// </summary>
public class LCFriendshipRequest : LCObject {
public const string CLASS_NAME = "_FriendshipRequest";

View File

@ -1,26 +1,49 @@
using System;
namespace LeanCloud.Storage {
/// <summary>
/// LCGeoPoint represents a latitude and longitude point that may be associated
/// with a key in a LCObject or used as a reference point for queries.
/// </summary>
public class LCGeoPoint {
/// <summary>
/// Gets the latitude of the LCGeoPoint.
/// </summary>
public double Latitude {
get;
}
/// <summary>
/// Gets the longitude of the LCGeoPoint.
/// </summary>
public double Longitude {
get;
}
/// <summary>
/// Constructs a LCGeoPoint with the specified latitude and longitude.
/// </summary>
/// <param name="latitude"></param>
/// <param name="longtitude"></param>
public LCGeoPoint(double latitude, double longtitude) {
Latitude = latitude;
Longitude = longtitude;
}
/// <summary>
/// The original LCGeoPoint.
/// </summary>
public static LCGeoPoint Origin {
get {
return new LCGeoPoint(0, 0);
}
}
/// <summary>
/// Calculate the distance in kilometer between this point and another GeoPoint.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public double KilometersTo(LCGeoPoint point) {
if (point == null) {
throw new ArgumentNullException(nameof(point));
@ -28,6 +51,11 @@ namespace LeanCloud.Storage {
return RadiansTo(point) * 6371.0;
}
/// <summary>
/// Calculate the distance in mile between this point and another GeoPoint.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public double MilesTo(LCGeoPoint point) {
if (point == null) {
throw new ArgumentNullException(nameof(point));
@ -35,6 +63,11 @@ namespace LeanCloud.Storage {
return RadiansTo(point) * 3958.8;
}
/// <summary>
/// Calculate the distance in radians between this point and another GeoPoint.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public double RadiansTo(LCGeoPoint point) {
if (point == null) {
throw new ArgumentNullException(nameof(point));

View File

@ -5,6 +5,9 @@ using System.Linq;
using LeanCloud.Storage.Internal.Operation;
namespace LeanCloud.Storage {
/// <summary>
/// LCClassHook represents the hooks for saving / updating / deleting LCObject.
/// </summary>
public static class LCClassHook {
public const string BeforeSave = "beforeSave";
public const string AfterSave = "afterSave";
@ -17,6 +20,9 @@ namespace LeanCloud.Storage {
public partial class LCObject {
internal const string IgnoreHooksKey = "__ignore_hooks";
/// <summary>
/// Disable to hook before saving / updating / deleting LCObject.
/// </summary>
public void DisableBeforeHook() {
Ignore(
LCClassHook.BeforeSave,
@ -24,6 +30,9 @@ namespace LeanCloud.Storage {
LCClassHook.BeforeDelete);
}
/// <summary>
/// Disable to hook after saving / updating / deleting LCObject.
/// </summary>
public void DisableAfterHook() {
Ignore(
LCClassHook.AfterSave,
@ -31,6 +40,10 @@ namespace LeanCloud.Storage {
LCClassHook.AfterDelete);
}
/// <summary>
/// Ignores the hook for this LCObject.
/// </summary>
/// <param name="hookName"></param>
public void IgnoreHook(string hookName) {
if (hookName != LCClassHook.BeforeSave && hookName != LCClassHook.AfterSave &&
hookName != LCClassHook.BeforeUpdate && hookName != LCClassHook.AfterUpdate &&
@ -46,6 +59,10 @@ namespace LeanCloud.Storage {
ApplyOperation(IgnoreHooksKey, op);
}
/// <summary>
/// Gets the updated keys of this LCObject.
/// </summary>
/// <returns></returns>
public ReadOnlyCollection<string> GetUpdatedKeys() {
if (this["_updatedKeys"] == null) {
return null;

View File

@ -11,51 +11,60 @@ using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Storage {
/// <summary>
/// LeanCloud Object
/// The LCObject is a local representation of data that can be saved and
/// retrieved from the LeanCloud.
/// </summary>
public partial class LCObject {
/// <summary>
/// Last synced data.
/// </summary>
internal LCObjectData data;
/// <summary>
/// Estimated data.
/// </summary>
internal Dictionary<string, object> estimatedData;
/// <summary>
/// Operations.
/// </summary>
internal Dictionary<string, ILCOperation> operationDict;
static readonly Dictionary<Type, LCSubclassInfo> subclassTypeDict = new Dictionary<Type, LCSubclassInfo>();
static readonly Dictionary<string, LCSubclassInfo> subclassNameDict = new Dictionary<string, LCSubclassInfo>();
/// <summary>
/// Gets the class name for the LCObject.
/// </summary>
public string ClassName {
get {
return data.ClassName;
}
}
/// <summary>
/// Gets the object id. An object id is assigned as son as an object is
/// saved to the server. The combination of a <see cref="ClassName"/> and
/// an <see cref="ObjectId"/> uniquely identifies an object in your application.
/// </summary>
public string ObjectId {
get {
return data.ObjectId;
}
}
/// <summary>
/// Gets the first time of this object was saved the server.
/// </summary>
public DateTime CreatedAt {
get {
return data.CreatedAt;
}
}
/// <summary>
/// Gets the last time of this object was updated the server.
/// </summary>
public DateTime UpdatedAt {
get {
return data.UpdatedAt;
}
}
/// <summary>
/// Gets or sets the LCACL governing this object.
/// </summary>
public LCACL ACL {
get {
return this["ACL"] as LCACL ;
@ -72,6 +81,12 @@ namespace LeanCloud.Storage {
}
}
/// <summary>
/// Constructs a new LCObject with no data in it. A LCObject constructed
/// in this way will not have an ObjectedId and will not persist to database
/// until <see cref="Save(bool, LCQuery{LCObject}))"/> is called.
/// </summary>
/// <param name="className">The className for the LCObject.</param>
public LCObject(string className) {
if (string.IsNullOrEmpty(className)) {
throw new ArgumentNullException(nameof(className));
@ -84,6 +99,13 @@ namespace LeanCloud.Storage {
isNew = true;
}
/// <summary>
/// Creates a reference to an existing LCObject for use in creating
/// associations between LCObjects.
/// </summary>
/// <param name="className">The className for the LCObject.</param>
/// <param name="objectId">The object id for the LCObject.</param>
/// <returns></returns>
public static LCObject CreateWithoutData(string className, string objectId) {
if (string.IsNullOrEmpty(objectId)) {
throw new ArgumentNullException(nameof(objectId));
@ -94,6 +116,12 @@ namespace LeanCloud.Storage {
return obj;
}
/// <summary>
/// Creates a reference to an existing LCObject for use in creating
/// associations between LCObjects.
/// </summary>
/// <param name="className">The className for the LCObject.</param>
/// <returns></returns>
public static LCObject Create(string className) {
if (subclassNameDict.TryGetValue(className, out LCSubclassInfo subclassInfo)) {
return subclassInfo.Constructor.Invoke();
@ -108,6 +136,12 @@ namespace LeanCloud.Storage {
return null;
}
/// <summary>
/// Gets or sets a value on the object. It is forbidden to name keys
/// with '_'.
/// </summary>
/// <param name="key">The value for key.</param>
/// <returns></returns>
public object this[string key] {
get {
if (estimatedData.TryGetValue(key, out object value)) {
@ -148,6 +182,11 @@ namespace LeanCloud.Storage {
}
/// <summary>
/// Creates a <see cref="LCRelation{T}"/> value for a key.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddRelation(string key, LCObject value) {
if (string.IsNullOrEmpty(key)) {
throw new ArgumentNullException(nameof(key));
@ -159,6 +198,11 @@ namespace LeanCloud.Storage {
ApplyOperation(key, op);
}
/// <summary>
/// Removes a <see cref="LCRelation{T}"/> value for a key.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void RemoveRelation(string key, LCObject value) {
if (string.IsNullOrEmpty(key)) {
throw new ArgumentNullException(nameof(key));
@ -324,6 +368,12 @@ namespace LeanCloud.Storage {
}
}
/// <summary>
/// Saves this object to the server.
/// </summary>
/// <param name="fetchWhenSave">Whether or not fetch data when saved.</param>
/// <param name="query">The condition for saving.</param>
/// <returns></returns>
public async Task<LCObject> Save(bool fetchWhenSave = false, LCQuery<LCObject> query = null) {
if (LCBatch.HasCircleReference(this, new HashSet<LCObject>())) {
throw new ArgumentException("Found a circle dependency when save.");
@ -350,6 +400,11 @@ namespace LeanCloud.Storage {
return this;
}
/// <summary>
/// Saves each object in the provided list.
/// </summary>
/// <param name="objects">The objects to save.</param>
/// <returns></returns>
public static async Task<List<LCObject>> SaveAll(IEnumerable<LCObject> objects) {
if (objects == null) {
throw new ArgumentNullException(nameof(objects));
@ -364,6 +419,10 @@ namespace LeanCloud.Storage {
return objects.ToList();
}
/// <summary>
/// Deletes this object on the server.
/// </summary>
/// <returns></returns>
public async Task Delete() {
if (ObjectId == null) {
return;
@ -372,6 +431,11 @@ namespace LeanCloud.Storage {
await LCCore.HttpClient.Delete(path);
}
/// <summary>
/// Deletes each object in the provided list.
/// </summary>
/// <param name="objects"></param>
/// <returns></returns>
public static async Task DeleteAll(IEnumerable<LCObject> objects) {
if (objects == null || objects.Count() == 0) {
throw new ArgumentNullException(nameof(objects));
@ -390,6 +454,12 @@ namespace LeanCloud.Storage {
await LCCore.HttpClient.Post<List<object>>("batch", data: data);
}
/// <summary>
/// Fetches this object from server.
/// </summary>
/// <param name="keys">The keys for fetching.</param>
/// <param name="includes">The include keys for fetching.</param>
/// <returns></returns>
public async Task<LCObject> Fetch(IEnumerable<string> keys = null, IEnumerable<string> includes = null) {
Dictionary<string, object> queryParams = new Dictionary<string, object>();
if (keys != null) {
@ -405,6 +475,11 @@ namespace LeanCloud.Storage {
return this;
}
/// <summary>
/// Fetches all of the objects in the provided list.
/// </summary>
/// <param name="objects">The objects for fetching.</param>
/// <returns></returns>
public static async Task<IEnumerable<LCObject>> FetchAll(IEnumerable<LCObject> objects) {
if (objects == null || objects.Count() == 0) {
throw new ArgumentNullException(nameof(objects));
@ -442,6 +517,13 @@ namespace LeanCloud.Storage {
return objects;
}
/// <summary>
/// Registers a custom subclass type with LeanCloud SDK, enabling strong-typing
/// of those LCObjects whenever they appear.
/// </summary>
/// <typeparam name="T">The LCObject subclass type to register.</typeparam>
/// <param name="className">The className on server.</param>
/// <param name="constructor">The constructor for creating an object.</param>
public static void RegisterSubclass<T>(string className, Func<T> constructor) where T : LCObject {
Type classType = typeof(T);
LCSubclassInfo subclassInfo = new LCSubclassInfo(className, classType, constructor);

View File

@ -9,7 +9,13 @@ using LeanCloud.Storage.Internal.Query;
using LeanCloud.Storage.Internal.Object;
namespace LeanCloud.Storage {
/// <summary>
/// The LCQuery class defines a query that is used to fetch LCObjects.
/// </summary>
public class LCQuery {
/// <summary>
/// The classname of this query.
/// </summary>
public string ClassName {
get; internal set;
}
@ -18,6 +24,10 @@ namespace LeanCloud.Storage {
get; internal set;
}
/// <summary>
/// Constructs a LCQuery for class.
/// </summary>
/// <param name="className"></param>
public LCQuery(string className) {
ClassName = className;
Condition = new LCCompositionalCondition();
@ -172,16 +182,35 @@ namespace LeanCloud.Storage {
return this;
}
/// <summary>
/// The value corresponding to key is near the point.
/// </summary>
/// <param name="key"></param>
/// <param name="point"></param>
/// <returns></returns>
public LCQuery<T> WhereNear(string key, LCGeoPoint point) {
Condition.WhereNear(key, point);
return this;
}
/// <summary>
/// The value corresponding to key is in the given rectangular geographic bounding box.
/// </summary>
/// <param name="key"></param>
/// <param name="southwest"></param>
/// <param name="northeast"></param>
/// <returns></returns>
public LCQuery<T> WhereWithinGeoBox(string key, LCGeoPoint southwest, LCGeoPoint northeast) {
Condition.WhereWithinGeoBox(key, southwest, northeast);
return this;
}
/// <summary>
/// The value corresponding to key is related to the parent.
/// </summary>
/// <param name="parent"></param>
/// <param name="key"></param>
/// <returns></returns>
public LCQuery<T> WhereRelatedTo(LCObject parent, string key) {
Condition.WhereRelatedTo(parent, key);
return this;
@ -255,13 +284,21 @@ namespace LeanCloud.Storage {
return this;
}
/// <summary>
/// Sorts the results in ascending order by the given key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public LCQuery<T> OrderByAscending(string key) {
Condition.OrderByAscending(key);
return this;
}
/// <summary>
/// Sorts the results in descending order by the given key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public LCQuery<T> OrderByDescending(string key) {
Condition.OrderByDescending(key);
return this;
@ -338,6 +375,10 @@ namespace LeanCloud.Storage {
return this;
}
/// <summary>
/// Counts the number of objects that match this query.
/// </summary>
/// <returns></returns>
public async Task<int> Count() {
string path = $"classes/{ClassName}";
Dictionary<string, object> parameters = BuildParams();
@ -347,6 +388,12 @@ namespace LeanCloud.Storage {
return (int)ret["count"];
}
/// <summary>
/// Constructs a LCObject whose id is already known by fetching data
/// from the server.
/// </summary>
/// <param name="objectId"></param>
/// <returns></returns>
public async Task<T> Get(string objectId) {
if (string.IsNullOrEmpty(objectId)) {
throw new ArgumentNullException(nameof(objectId));
@ -363,6 +410,10 @@ namespace LeanCloud.Storage {
return DecodeLCObject(response);
}
/// <summary>
/// Retrieves a list of LCObjects that satisfy the query from Server.
/// </summary>
/// <returns></returns>
public async Task<ReadOnlyCollection<T>> Find() {
string path = $"classes/{ClassName}";
Dictionary<string, object> parameters = BuildParams();
@ -376,6 +427,10 @@ namespace LeanCloud.Storage {
return list.AsReadOnly();
}
/// <summary>
/// Retrieves at most one LCObject that satisfies this query.
/// </summary>
/// <returns></returns>
public async Task<T> First() {
Limit(1);
ReadOnlyCollection<T> results = await Find();
@ -385,6 +440,11 @@ namespace LeanCloud.Storage {
return null;
}
/// <summary>
/// Constructs a query that is the and of the given queries.
/// </summary>
/// <param name="queries"></param>
/// <returns></returns>
public static LCQuery<T> And(IEnumerable<LCQuery<T>> queries) {
if (queries == null || queries.Count() < 1) {
throw new ArgumentNullException(nameof(queries));
@ -402,6 +462,11 @@ namespace LeanCloud.Storage {
return compositionQuery;
}
/// <summary>
/// Constructs a query that is the or of the given queries.
/// </summary>
/// <param name="queries"></param>
/// <returns></returns>
public static LCQuery<T> Or(IEnumerable<LCQuery<T>> queries) {
if (queries == null || queries.Count() < 1) {
throw new ArgumentNullException(nameof(queries));

View File

@ -1,21 +1,40 @@
namespace LeanCloud.Storage {
/// <summary>
/// LCRelation provides access to all of the children of a many-to-many
/// relationship.
/// </summary>
/// <typeparam name="T">The type of the child objects.</typeparam>
public class LCRelation<T> where T : LCObject {
/// <summary>
/// The key of this LCRelation.
/// </summary>
public string Key {
get; set;
}
/// <summary>
/// The parent of this LCRelation.
/// </summary>
public LCObject Parent {
get; set;
}
/// <summary>
/// The className of this LCRelation.
/// </summary>
public string TargetClass {
get; set;
}
/// <summary>
/// Constructs a LCRelation.
/// </summary>
public LCRelation() {
}
/// <summary>
/// Gets a query that can be used to query the objects in this relation.
/// </summary>
public LCQuery<T> Query {
get {
LCQuery<T> query = new LCQuery<T>(TargetClass);

View File

@ -5,7 +5,9 @@
public class LCRole : LCObject {
public const string CLASS_NAME = "_Role";
/// <summary>
/// The name of LCRole.
/// </summary>
public string Name {
get {
return this["name"] as string;
@ -40,9 +42,18 @@
}
}
/// <summary>
/// Constructs a LCRole.
/// </summary>
public LCRole() : base(CLASS_NAME) {
}
/// <summary>
/// Constructs a LCRole with a name and a LCACL.
/// </summary>
/// <param name="name"></param>
/// <param name="acl"></param>
/// <returns></returns>
public static LCRole Create(string name, LCACL acl) {
LCRole role = new LCRole() {
Name = name,

View File

@ -61,6 +61,12 @@ namespace LeanCloud.Storage {
await LCCore.HttpClient.Post<Dictionary<string, object>>(path, data: data);
}
/// <summary>
/// Verifies the mobile number with the verification code.
/// </summary>
/// <param name="mobile"></param>
/// <param name="code"></param>
/// <returns></returns>
public static async Task VerifyMobilePhone(string mobile, string code) {
string path = $"verifySmsCode/{code}";
Dictionary<string, object> data = new Dictionary<string, object> {

View File

@ -7,6 +7,9 @@ using LeanCloud.Storage.Internal.Object;
using LC.Newtonsoft.Json;
namespace LeanCloud.Storage {
/// <summary>
/// LCStatus is a local representation of a status in LeanCloud.
/// </summary>
public class LCStatus : LCObject {
public const string CLASS_NAME = "_Status";
@ -22,25 +25,42 @@ namespace LeanCloud.Storage {
public const string OwnerKey = "owner";
public const string MessageIdKey = "messageId";
/// <summary>
/// The id of this status.
/// </summary>
public int MessageId {
get; internal set;
}
/// <summary>
/// The inboxType of this status.
/// </summary>
public string InboxType {
get; internal set;
}
private LCQuery query;
/// <summary>
/// The data of this status.
/// </summary>
public Dictionary<string, object> Data {
get; set;
}
/// <summary>
/// Constructs a LCStatus.
/// </summary>
public LCStatus() : base(CLASS_NAME) {
InboxType = InboxTypeDefault;
Data = new Dictionary<string, object>();
}
/// <summary>
/// Sends the status to the followers of this user.
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
public static async Task<LCStatus> SendToFollowers(LCStatus status) {
if (status == null) {
throw new ArgumentNullException(nameof(status));
@ -62,6 +82,12 @@ namespace LeanCloud.Storage {
return await status.Send();
}
/// <summary>
/// Sends the status to the user with targetId privatedly.
/// </summary>
/// <param name="status"></param>
/// <param name="targetId"></param>
/// <returns></returns>
public static async Task<LCStatus> SendPrivately(LCStatus status, string targetId) {
if (status == null) {
throw new ArgumentNullException(nameof(status));
@ -84,6 +110,10 @@ namespace LeanCloud.Storage {
return await status.Send();
}
/// <summary>
/// Send this status.
/// </summary>
/// <returns></returns>
public async Task<LCStatus> Send() {
LCUser user = await LCUser.GetCurrent();
if (user == null) {
@ -118,6 +148,10 @@ namespace LeanCloud.Storage {
return this;
}
/// <summary>
/// Deletes this status.
/// </summary>
/// <returns></returns>
public new async Task Delete() {
LCUser user = await LCUser.GetCurrent();
if (user == null) {
@ -137,6 +171,11 @@ namespace LeanCloud.Storage {
}
}
/// <summary>
/// Gets the count of the status with inboxType.
/// </summary>
/// <param name="inboxType"></param>
/// <returns></returns>
public static async Task<LCStatusCount> GetCount(string inboxType) {
LCUser user = await LCUser.GetCurrent();
if (user == null) {
@ -158,6 +197,11 @@ namespace LeanCloud.Storage {
return statusCount;
}
/// <summary>
/// Reset the count of the status to be zero.
/// </summary>
/// <param name="inboxType"></param>
/// <returns></returns>
public static async Task ResetUnreadCount(string inboxType = null) {
LCUser user = await LCUser.GetCurrent();
if (user == null) {

View File

@ -1,9 +1,18 @@
namespace LeanCloud.Storage {
/// <summary>
/// LCStatusCount is a result that contains the count of status.
/// </summary>
public class LCStatusCount {
/// <summary>
/// The total count.
/// </summary>
public int Total {
get; set;
}
/// <summary>
/// The unread count.
/// </summary>
public int Unread {
get; set;
}

View File

@ -9,25 +9,45 @@ using LeanCloud.Storage.Internal.Codec;
using LeanCloud.Storage.Internal.Object;
namespace LeanCloud.Storage {
/// <summary>
/// LCStatusQuery is the query of LCStatus.
/// </summary>
public class LCStatusQuery : LCQuery<LCStatus> {
/// <summary>
/// The inboxType for query.
/// </summary>
public string InboxType {
get; set;
}
/// <summary>
/// The id since the query.
/// </summary>
public int SinceId {
get; set;
}
/// <summary>
/// The max id for the query.
/// </summary>
public int MaxId {
get; set;
}
/// <summary>
/// Constructs a LCStatusQuery with inboxType.
/// </summary>
/// <param name="inboxType"></param>
public LCStatusQuery(string inboxType = LCStatus.InboxTypeDefault) : base("_Status") {
InboxType = inboxType;
SinceId = 0;
MaxId = 0;
}
/// <summary>
/// Retrieves a list of LCStatus that satisfy the query from Server.
/// </summary>
/// <returns></returns>
public new async Task<ReadOnlyCollection<LCStatus>> Find() {
LCUser user = await LCUser.GetCurrent();
if (user == null) {

View File

@ -1,5 +1,4 @@
using System;
using LeanCloud.Common;
using LeanCloud.Common;
namespace LeanCloud.Storage {
public class LCStorage {

View File

@ -6,6 +6,9 @@ using LeanCloud.Common;
using LeanCloud.Storage.Internal.Object;
namespace LeanCloud.Storage {
/// <summary>
/// LCUser represents a user for a LeanCloud application.
/// </summary>
public class LCUser : LCObject {
public const string CLASS_NAME = "_User";
@ -79,6 +82,11 @@ namespace LeanCloud.Storage {
static LCUser currentUser;
/// <summary>
/// Gets the currently logged in LCUser with a valid session, either from
/// memory or disk if necessary.
/// </summary>
/// <returns></returns>
public static async Task<LCUser> GetCurrent() {
if (currentUser != null) {
return currentUser;
@ -700,6 +708,13 @@ namespace LeanCloud.Storage {
.Include("followee");
}
/// <summary>
/// Gets the followers and followees of the currently logged in user.
/// </summary>
/// <param name="includeFollower"></param>
/// <param name="includeFollowee"></param>
/// <param name="returnCount"></param>
/// <returns></returns>
public async Task<LCFollowersAndFollowees> GetFollowersAndFollowees(bool includeFollower = false,
bool includeFollowee = false, bool returnCount = false) {
Dictionary<string, object> queryParams = new Dictionary<string, object>();