add "dm/messages/get" request
This commit is contained in:
parent
442add0ee4
commit
bb57c26dea
3 changed files with 60 additions and 7 deletions
|
|
@ -29,6 +29,49 @@ public class Requests
|
|||
string memberPath = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members/{id};{DOMAIN}";
|
||||
return Fs.Exists(memberPath);
|
||||
}
|
||||
public static async Task<string> GetDmMessages(string id, string dmId, string startOffset, string countStr)
|
||||
{
|
||||
if (!await IsMemberOfDm(id, dmId)) return "error:forbidden";
|
||||
|
||||
string msgDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/messages";
|
||||
if (!Fs.Exists(msgDir)) return "{}";
|
||||
|
||||
long last = 0;
|
||||
if (Fs.Exists($"{msgDir}/last"))
|
||||
{
|
||||
string lastStr = Encoding.UTF8.GetString(await Fs.ReadFile($"{msgDir}/last"));
|
||||
long.TryParse(lastStr, out last);
|
||||
}
|
||||
|
||||
long start = last;
|
||||
if (!string.IsNullOrEmpty(startOffset) && long.TryParse(startOffset, out long s))
|
||||
{
|
||||
start = s;
|
||||
}
|
||||
|
||||
int count = 50;
|
||||
if (!string.IsNullOrEmpty(countStr) && int.TryParse(countStr, out int c))
|
||||
{
|
||||
count = Math.Min(c, 100);
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder("{");
|
||||
int fetched = 0;
|
||||
for (long i = start; i >= 0 && fetched < count; i--)
|
||||
{
|
||||
string p = $"{msgDir}/{i}";
|
||||
if (Fs.Exists(p))
|
||||
{
|
||||
if (fetched > 0) sb.Append(',');
|
||||
sb.Append($"\"{i}\":");
|
||||
sb.Append(Encoding.UTF8.GetString(await Fs.ReadFile(p)));
|
||||
fetched++;
|
||||
}
|
||||
}
|
||||
sb.Append("}");
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
||||
public static async Task<string> GetDmKey(string id, string dmId)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue