115 lines
3.7 KiB
C#
115 lines
3.7 KiB
C#
using Habitica.Todoist.Integration.Data;
|
|
using Habitica.Todoist.Integration.Model.Storage;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Linq;
|
|
using Habitica.Todoist.Integration.Model.Todoist;
|
|
using System.Threading.Tasks;
|
|
using System.Collections;
|
|
|
|
namespace Habitica.Todoist.Integration.Services
|
|
{
|
|
public class TodoistIntegrationService
|
|
{
|
|
private TodoistServiceClient todoistClient { get; set; }
|
|
private TableStorageClient storageClient { get; set; }
|
|
|
|
private string userId { get; set; }
|
|
private string latestSyncToken { get; set; } = string.Empty;
|
|
|
|
public TodoistIntegrationService(string todoistApiKey,
|
|
string storageConnectionString,
|
|
string userId)
|
|
{
|
|
this.todoistClient = new TodoistServiceClient(todoistApiKey);
|
|
this.storageClient = new TableStorageClient(storageConnectionString);
|
|
this.userId = userId;
|
|
}
|
|
|
|
public async Task<Items> ReadItemChanges()
|
|
{
|
|
var itemChangesResponse = await todoistClient.GetItemChanges(ReadLatestSyncToken());
|
|
latestSyncToken = itemChangesResponse.Sync_token;
|
|
|
|
return new Items(itemChangesResponse.Items, storageClient, userId);
|
|
}
|
|
|
|
public async Task CommitRead()
|
|
{
|
|
await storageClient.InsertOrUpdate(new TodoistSync(userId, latestSyncToken));
|
|
}
|
|
|
|
private string ReadLatestSyncToken()
|
|
{
|
|
try
|
|
{
|
|
return storageClient.Query<TodoistSync>()
|
|
.Where(x => x.PartitionKey == userId)
|
|
.ToList()
|
|
.OrderByDescending(x => x.Timestamp)
|
|
.First().RowKey;
|
|
}
|
|
catch { }
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
public class Items : IEnumerable<Item>
|
|
{
|
|
private List<Item> changedItems;
|
|
private readonly TableStorageClient storageClient;
|
|
private readonly string userId;
|
|
|
|
internal Items(List<Item> changedItems, TableStorageClient storageClient, string userId)
|
|
{
|
|
this.changedItems = changedItems;
|
|
this.storageClient = storageClient;
|
|
this.userId = userId;
|
|
}
|
|
|
|
public IEnumerable<Item> WhereAdded()
|
|
{
|
|
return changedItems
|
|
.Where(x => !storageClient
|
|
.Exists<TodoHabitLink>(userId, x.Id) && x.Is_deleted == 0)
|
|
.OrderBy(x => x.Parent_Id);
|
|
}
|
|
|
|
public IEnumerable<Item> WhereUpdated()
|
|
{
|
|
return changedItems
|
|
.Where(x => storageClient
|
|
.Exists<TodoHabitLink>(userId, x.Id) && x.Is_deleted == 0)
|
|
.OrderBy(x => x.Parent_Id);
|
|
}
|
|
|
|
public IEnumerable<Item> WhereCompleted()
|
|
{
|
|
return changedItems
|
|
.Where(x => storageClient
|
|
.Exists<TodoHabitLink>(userId, x.Id) && x.Is_deleted == 0 && x.Date_completed != null)
|
|
.OrderBy(x => x.Parent_Id);
|
|
}
|
|
|
|
public IEnumerable<Item> WhereDeleted()
|
|
{
|
|
return changedItems
|
|
.Where(x => storageClient
|
|
.Exists<TodoHabitLink>(userId, x.Id) && x.Is_deleted == 1)
|
|
.OrderBy(x => x.Parent_Id);
|
|
}
|
|
|
|
public IEnumerator<Item> GetEnumerator()
|
|
{
|
|
return changedItems.OrderBy(x => x.Parent_Id).GetEnumerator();
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
}
|
|
}
|
|
}
|