Skip to content

Commit d2c213b

Browse files
committed
Updates Update Updates
- 4.0.0 - 2 New Data Types -Notification Object -BoolStatus - 3 New Method Handles - Authorization (See Docs) - Commenting - Notification - 2 New Methods in User Methods - GetUserIdFromName - GetUserFromUserId
1 parent a534af9 commit d2c213b

File tree

11 files changed

+464
-4
lines changed

11 files changed

+464
-4
lines changed

.vs/ScriptBloxAPI/v17/.suo

-142 KB
Binary file not shown.

Backend Functions/ScriptBloxException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ namespace ScriptBloxAPI
99
{
1010
internal class ScriptBloxException : Exception
1111
{
12-
internal ScriptBloxException(string message) : base(message) => Console.WriteLine("CustomException was thrown with message: " + message);
12+
internal ScriptBloxException(string message) : base(message) => Console.WriteLine("ScriptBloxException was thrown with message: " + message);
1313
}
1414
}

DataTypes/BoolStatus.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ScriptBloxAPI.DataTypes
8+
{
9+
public class BoolStatus
10+
{
11+
private bool _value = false;
12+
private string _status = string.Empty;
13+
14+
public bool Value => _value;
15+
public string Status => _status;
16+
17+
public BoolStatus(bool value, string status)
18+
{
19+
_value = value;
20+
_status = status;
21+
}
22+
}
23+
}

DataTypes/NotificationObject.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace ScriptBloxAPI.DataTypes
9+
{
10+
public class NotificationObject
11+
{
12+
private string _id = string.Empty;
13+
private DateTime _createdAt = DateTime.MinValue;
14+
private NotificationType _notificationType = NotificationType.CommentLiked;
15+
private bool isRead = false;
16+
17+
private UserObject userSender = null;
18+
private ScriptObject scriptSender = null;
19+
20+
public string Id => _id;
21+
public DateTime CreatedAt => _createdAt;
22+
public NotificationType Type => _notificationType;
23+
public bool IsRead => isRead;
24+
public ScriptObject ScriptObjectSender => scriptSender;
25+
public UserObject UserObjectSender => userSender;
26+
27+
public enum NotificationType
28+
{
29+
CommentLiked,
30+
CommentDisliked,
31+
ScriptLiked,
32+
ScriptDisliked,
33+
CommentAddedToScript,
34+
Followed,
35+
}
36+
37+
public NotificationObject(string id, DateTime createdAt, NotificationType notificationType, bool isRead, UserObject userSender = null, ScriptObject scriptSender = null)
38+
{
39+
if (userSender == null && scriptSender == null) throw new ScriptBloxException("Must provide either a UserObject or ScriptObject");
40+
41+
_id = id;
42+
_createdAt = createdAt;
43+
_notificationType = notificationType;
44+
this.isRead = isRead;
45+
this.userSender = userSender;
46+
this.scriptSender = scriptSender;
47+
}
48+
}
49+
}

Methods/Authorization.cs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using ScriptBloxAPI.DataTypes;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace ScriptBloxAPI.Methods
11+
{
12+
public class Authorization
13+
{
14+
/// <summary>
15+
/// Sends a ping request to the idle endpoint.
16+
/// </summary>
17+
/// <param name="authorization">The authorization token.</param>
18+
/// <returns>A BoolStatus object indicating if the request was successful and the response status code.</returns>
19+
public static BoolStatus PingIdle(string authorization)
20+
{
21+
MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization);
22+
HttpResponseMessage response = MiscFunctions.HttpClient.GetAsync("https://scriptblox.com/api/ping/idle").Result;
23+
MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization");
24+
25+
return new BoolStatus(response.IsSuccessStatusCode, response.StatusCode.ToString());
26+
}
27+
28+
/// <summary>
29+
/// Changes the username of the user.
30+
/// </summary>
31+
/// <param name="authorization">The authorization token.</param>
32+
/// <param name="newUsername">The new username.</param>
33+
/// <returns>A BoolStatus object indicating if the username change was successful and the response text.</returns>
34+
public static BoolStatus ChangeUsername(string authorization, string newUsername)
35+
{
36+
HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/update", authorization, $"{{\"username\":\"{newUsername}\"}}");
37+
38+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password updated!"), response.Content.ReadAsStringAsync().Result);
39+
}
40+
41+
/// <summary>
42+
/// Changes the password of the user.
43+
/// </summary>
44+
/// <param name="authorization">The authorization token.</param>
45+
/// <param name="oldPassword">The old password.</param>
46+
/// <param name="newPassword">The new password.</param>
47+
/// <returns>A BoolStatus object indicating if the password change was successful and the response text.</returns>
48+
public static BoolStatus ChangePassword(string authorization, string oldPassword, string newPassword)
49+
{
50+
HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/change-password", authorization, $"{{\"oldPassword\":\"{oldPassword}\",\"newPassword\":\"{newPassword}\"}}");
51+
52+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password updated!"), response.Content.ReadAsStringAsync().Result);
53+
}
54+
55+
/// <summary>
56+
/// Updates the bio of the user.
57+
/// </summary>
58+
/// <param name="authorization">The authorization token.</param>
59+
/// <param name="newBio">The new bio.</param>
60+
/// <returns>A BoolStatus object indicating if the bio update was successful and the response text.</returns>
61+
public static BoolStatus UpdateBio(string authorization, string newBio)
62+
{
63+
HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/update", authorization, $"{{\"bio\":\"{newBio}\"}}");
64+
65+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("User updated!"), response.Content.ReadAsStringAsync().Result);
66+
}
67+
68+
/// <summary>
69+
/// Follows a user on ScriptBlox.
70+
/// </summary>
71+
/// <param name="authorization">The authorization token.</param>
72+
/// <param name="username">The username of the user to follow.</param>
73+
/// <returns>A BoolStatus object indicating if the follow operation was successful and the response text.</returns>
74+
public static BoolStatus FollowUser(string authorization, string username)
75+
{
76+
string UserId = UserMethods.GetUserIdFromName(username);
77+
HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/follow", authorization, $"{{\"userId\":\"{UserId}\"}}");
78+
79+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("You're now following "), response.Content.ReadAsStringAsync().Result);
80+
}
81+
82+
/// <summary>
83+
/// Unfollows a user on ScriptBlox.
84+
/// </summary>
85+
/// <param name="authorization">The authorization token.</param>
86+
/// <param name="username">The username of the user to unfollow.</param>
87+
/// <returns>A BoolStatus object indicating if the unfollow operation was successful and the response text.</returns>
88+
public static BoolStatus UnFollowUser(string authorization, string username)
89+
{
90+
string UserId = UserMethods.GetUserIdFromName(username);
91+
HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/unfollow", authorization, $"{{\"userId\":\"{UserId}\"}}");
92+
93+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Unfollowed user "), response.Content.ReadAsStringAsync().Result);
94+
}
95+
96+
/// <summary>
97+
/// Sends a password reset request.
98+
/// </summary>
99+
/// <param name="emailToReset">The email address of the user to reset the password for.</param>
100+
/// <returns>A BoolStatus object indicating if the password reset request was successful and the response text.</returns>
101+
public static BoolStatus SendPasswordReset(string emailToReset)
102+
{
103+
HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/reset-password", "", $"{{\"email\":\"{emailToReset}\"}}", false);
104+
105+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password reset link has been sent to your email!"), response.Content.ReadAsStringAsync().Result);
106+
}
107+
108+
internal static HttpResponseMessage SendRequest(string url, string authorization, string data, bool WithAuth = true)
109+
{
110+
if (WithAuth)
111+
MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization);
112+
113+
HttpResponseMessage response = MiscFunctions.HttpClient.PostAsync(url, new StringContent(data)).Result;
114+
115+
if (WithAuth)
116+
MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization");
117+
118+
return response;
119+
}
120+
121+
}
122+
}

Methods/Commenting.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using Newtonsoft.Json.Linq;
2+
using ScriptBloxAPI.DataTypes;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Net.Http;
8+
using System.Security.Policy;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace ScriptBloxAPI.Methods
13+
{
14+
public class Commenting
15+
{
16+
/// <summary>
17+
/// Adds a comment to the specified script using ScriptBlox.
18+
/// </summary>
19+
/// <param name="script">The script object to add the comment to.</param>
20+
/// <param name="authorization">The authorization token.</param>
21+
/// <param name="comment">The text of the comment.</param>
22+
/// <returns>The newly added CommentObject.</returns>
23+
public static CommentObject AddComment(ScriptObject script, string authorization, string comment)
24+
{
25+
HttpResponseMessage response = SendRequest("https://scriptblox.com/api/comment/add", authorization, $"{{\"scriptId\":\"{script.Id}\",\"text\":\"{comment}\"}}");
26+
27+
JToken jsonReturn = JToken.Parse(response.Content.ReadAsStringAsync().Result);
28+
29+
if (jsonReturn == null)
30+
throw new ScriptBloxException("An error has occured while fetching the json, please submit a bug report.");
31+
if (jsonReturn["message"] == null)
32+
33+
34+
if (jsonReturn.Value<string>("message") != "Commented!")
35+
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
36+
37+
JToken commentData = jsonReturn["comment"];
38+
39+
return new CommentObject(
40+
commentData.Value<string>("_id"),
41+
commentData.Value<string>("text"),
42+
0,
43+
0,
44+
UserMethods.GetUserFromUserId(commentData.Value<string>("commentBy"))
45+
);
46+
}
47+
48+
/// <summary>
49+
/// Deletes a comment with the specified comment ID using the ScriptBlox API.
50+
/// </summary>
51+
/// <param name="authorization">The authorization token.</param>
52+
/// <param name="commentId">The ID of the comment to delete.</param>
53+
/// <returns>A BoolStatus indicating whether the comment deletion was successful.</returns>
54+
public static BoolStatus DeleteComment(string authorization, string commentId)
55+
{
56+
MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization);
57+
HttpResponseMessage response = MiscFunctions.HttpClient.DeleteAsync($"https://scriptblox.com/api/comment/{commentId}").Result;
58+
MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization");
59+
60+
return new BoolStatus(response.IsSuccessStatusCode, response.Content.ReadAsStringAsync().Result);
61+
}
62+
63+
/// <summary>
64+
/// Retrieves the comments associated with a script from ScriptBlox.
65+
/// </summary>
66+
/// <param name="script">The ScriptObject representing the script.</param>
67+
/// <returns>A list of CommentObject representing the comments associated with the script.</returns>
68+
/// <exception cref="ScriptBloxException">
69+
/// Thrown when an error occurs while fetching the JSON, when the API returns an error message, or when a backend error occurs.
70+
/// </exception>
71+
public static List<CommentObject> GetCommentsFromScript(ScriptObject script)
72+
{
73+
List<CommentObject> comments = new List<CommentObject>();
74+
75+
JToken jsonReturn = JToken.Parse(MiscFunctions.HttpClient.GetStringAsync($"https://scriptblox.com/api/comment/{script.Id}?page=1&max=999").Result);
76+
77+
if (jsonReturn == null)
78+
throw new ScriptBloxException("An error has occured while fetching the json, please submit a bug report.");
79+
if (jsonReturn["message"] != null)
80+
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
81+
if (jsonReturn["comments"] == null)
82+
throw new ScriptBloxException("Backend error occured.");
83+
84+
foreach (JToken comment in jsonReturn["comments"])
85+
{
86+
comments.Add(new CommentObject(
87+
comment.Value<string>("_id"),
88+
comment.Value<string>("text"),
89+
comment.Value<int>("likeCount"),
90+
comment.Value<int>("dislikeCount"),
91+
UserMethods.GetUserFromUsername(comment["commentBy"].Value<string>("username"))
92+
));
93+
}
94+
95+
return comments;
96+
}
97+
98+
internal static HttpResponseMessage SendRequest(string url, string authorization, string data, bool WithAuth = true)
99+
{
100+
if (WithAuth)
101+
MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization);
102+
103+
HttpResponseMessage response = MiscFunctions.HttpClient.PostAsync(url, new StringContent(data)).Result;
104+
105+
if (WithAuth)
106+
MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization");
107+
108+
return response;
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)