using BetterSecondBot.bottypes;
using BetterSecondBot.Static;
using EmbedIO;
using EmbedIO.Routing;
using EmbedIO.WebApi;
using System;
using System.Collections.Generic;
using System.Text;
using OpenMetaverse;
using Newtonsoft.Json;
using BetterSecondBotShared.Static;
namespace BetterSecondBot.HttpService
{
public class HttpApiGroup : WebApiControllerWithTokens
{
public HttpApiGroup(SecondBot mainbot, TokenStorage setuptokens) : base(mainbot, setuptokens) { }
[About("Checks if the given UUID is in the given group
Note: if group membership data is more than 60 secs old this will return Updating
Please wait and retry later")]
[ReturnHints("True|False")]
[ReturnHints("Updating")]
[ReturnHints("Unknown group")]
[ReturnHints("Invaild group UUID")]
[ReturnHints("Unable to find avatar UUID")]
[ArgHints("group", "URLARG", "the UUID of the group")]
[ArgHints("avatar", "URLARG", "the UUID of the avatar you wish to check with")]
[Route(HttpVerbs.Get, "/ismember/{group}/{avatar}/{token}")]
public object ismember(string group,string avatar,string token)
{
if (tokens.Allow(token, "groups", "ismember", getClientIP()) == true)
{
if (UUID.TryParse(group, out UUID groupuuid) == true)
{
if (bot.MyGroups.ContainsKey(groupuuid) == true)
{
if (bot.NeedReloadGroupData(groupuuid) == false)
{
UUID avataruuid = UUID.Zero;
if(UUID.TryParse(avatar,out avataruuid) == false)
{
string lookup = bot.FindAvatarName2Key(avatar);
if (UUID.TryParse(lookup, out avataruuid) == false)
{
return BasicReply("Unable to find avatar UUID");
}
}
bool status = bot.FastCheckInGroup(groupuuid, avataruuid);
return BasicReply(status.ToString());
}
bot.GetClient.Groups.RequestGroupMembers(groupuuid);
return BasicReply("Updating");
}
return BasicReply("Unknown group");
}
return BasicReply("Invaild group UUID");
}
return BasicReply("Token not accepted");
}
[About("fetchs a list of all groups known to the bot")]
[ReturnHints("array UUID=name")]
[Route(HttpVerbs.Get, "/listgroups/{token}")]
public object listgroups(string token)
{
if (tokens.Allow(token, "groups", "listgroups", getClientIP()) == true)
{
Dictionary grouppackage = new Dictionary();
foreach(KeyValuePair entry in bot.MyGroups)
{
grouppackage.Add(entry.Value.ID.ToString(), entry.Value.Name);
}
return BasicReply(JsonConvert.SerializeObject(grouppackage));
}
return BasicReply("Token not accepted");
}
[About("Requests the roles for the selected group
Replys with GroupRoleDetails object formated as follows - UpdateUnderway (Bool)
- RoleDataAge (Int) [default -1]
- Roles (KeyPair array of UUID=Name)
")]
[ReturnHints("GroupRoleDetails object")]
[ReturnHints("Group is not currently known")]
[ReturnHints("Invaild group UUID")]
[ReturnHints("Updating")]
[ArgHints("group", "URLARG", "the UUID of the group")]
[Route(HttpVerbs.Get, "/listgrouproles/{group}/{token}")]
public object listgrouproles(string group, string token)
{
if (tokens.Allow(token, "groups", "listgrouproles", getClientIP()) == true)
{
if (UUID.TryParse(group, out UUID groupuuid) == true)
{
if (bot.MyGroups.ContainsKey(groupuuid) == true)
{
GroupRoleDetails reply = new GroupRoleDetails();
reply.UpdateUnderway = false;
reply.RoleDataAge = -1;
reply.Roles = new Dictionary();
if (bot.MyGroupRolesStorage.ContainsKey(groupuuid) == false)
{
reply.UpdateUnderway = true;
}
else
{
long dif = helpers.UnixTimeNow() - bot.MyGroupRolesStorage[groupuuid].Key;
reply.RoleDataAge = dif;
if (dif >= 120)
{
reply.UpdateUnderway = true;
}
foreach(GroupRole gr in bot.MyGroupRolesStorage[groupuuid].Value)
{
reply.Roles.Add(gr.ID.ToString(), gr.Name);
}
}
if(reply.UpdateUnderway == true)
{
bot.GetClient.Groups.RequestGroupRoles(groupuuid);
if (reply.Roles.Count == 0)
{
return BasicReply("Updating");
}
}
return BasicReply(JsonConvert.SerializeObject(reply));
}
return BasicReply("Group is not currently known");
}
return BasicReply("Invaild group UUID");
}
return BasicReply("Token not accepted");
}
[About("fetchs a list of all groups with unread messages")]
[ReturnHints("array UUID")]
[Route(HttpVerbs.Get, "/listgroupswithunread/{token}")]
public object listgroupswithunread(string token)
{
if (tokens.Allow(token, "groups", "listgroupswithunread", getClientIP()) == true)
{
return BasicReply(JsonConvert.SerializeObject(bot.UnreadGroupchatGroups()));
}
return BasicReply("Token not accepted");
}
[About("checks if there are any groups with unread messages")]
[ReturnHints("True|False")]
[Route(HttpVerbs.Get, "/haveunreadgroupchat/{token}")]
public object haveunreadgroupchat(string token)
{
if (tokens.Allow(token, "groups", "haveunreadgroupchat", getClientIP()) == true)
{
return BasicReply(bot.HasUnreadGroupchats().ToString());
}
return BasicReply("Token not accepted");
}
[About("fetchs the groupchat history")]
[ReturnHints("Group UUID invaild")]
[ReturnHints("Group Chat")]
[ArgHints("group", "URLARG", "the UUID of the group")]
[Route(HttpVerbs.Get, "/getgroupchat/{group}/{token}")]
public object getgroupchat(string group, string token)
{
if (tokens.Allow(token, "groups", "getgroupchat", getClientIP()) == true)
{
if (UUID.TryParse(group, out UUID groupUUID) == true)
{
return BasicReply(JsonConvert.SerializeObject(bot.GetGroupchat(groupUUID)));
}
return BasicReply("Group UUID invaild");
}
return BasicReply("Token not accepted");
}
[About("sends a message to the groupchat")]
[ArgHints("group", "URLARG", "UUID of the group")]
[ArgHints("message", "Text", "the message to send")]
[ReturnHints("Group UUID invaild")]
[ReturnHints("Processing")]
[Route(HttpVerbs.Post, "/sendgroupchat/{group}/{token}")]
public object sendgroupchat(string group, string token, [FormField] string message)
{
if (tokens.Allow(token, "groups", "sendgroupchat", getClientIP()) == true)
{
if (UUID.TryParse(group, out UUID groupUUID) == true)
{
bool status = bot.GetCommandsInterface.Call("Groupchat", group + "~#~" + message, UUID.Zero, "~#~");
return BasicReply("Processing");
}
return BasicReply("Group UUID invaild");
}
return BasicReply("Token not accepted");
}
}
public class GroupRoleDetails
{
public bool UpdateUnderway { get; set; }
public Dictionary Roles { get; set; }
public long RoleDataAge = 0;
}
}