palm-client: channel history follows the channel's key when its slot changes, and is set aside when it's left; the new-messages marker survives old messages dropping off
This commit is contained in:
1 parent
da92a158bb
commit
35663f62af
4 files changed
+139
-10
No files matched your search
@@ -2,10 +2,13 @@
|
||||
#include "protocol.h"
|
||||
#include "session.h"
|
||||
#include "channels.h"
|
||||
#include "messages.h"
|
||||
#include "ui/text.h"
|
||||
|
||||
/* In the second code segment: see sections.h for the rules. */
|
||||
static Boolean InUse(const VmChannel *c) VM_SECT2;
|
||||
static void RefileConv(VmConv *conv, void *ctx) VM_SECT2;
|
||||
static void RefileMessages(UInt16 oldCount, UInt8 maxChannels) VM_SECT2;
|
||||
|
||||
/* data arrays, not literals: this module is in the second code segment (sections.h) */
|
||||
static Char DB_NAME[] = "VisorMeshChannels";
|
||||
@@ -82,6 +85,74 @@ static Boolean InUse(const VmChannel *c)
|
||||
return c->name[0] != 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Messages are filed by slot, so when a channel moves to another slot or is
|
||||
* left, its history follows it by secret, or is set aside (VM_SLOT_GONE) and
|
||||
* comes back if the channel is added again, rather than turning up under
|
||||
* whatever channel takes the slot next.
|
||||
*/
|
||||
#define KEY_LEN 6 /* the secret prefix messages carry (messages.h) */
|
||||
|
||||
typedef struct {
|
||||
UInt16 nOld, nNew;
|
||||
UInt8 *oldKeys; /* by slot, nOld slots; zero: not in the old cache */
|
||||
UInt8 *newKeys; /* slot, then key, nNew entries */
|
||||
} Refile;
|
||||
|
||||
static void RefileConv(VmConv *conv, void *ctx)
|
||||
{
|
||||
Refile *r = ctx;
|
||||
UInt8 zero[KEY_LEN];
|
||||
UInt16 i;
|
||||
|
||||
MemSet(zero, sizeof(zero), 0);
|
||||
if (MemCmp(conv->key, zero, KEY_LEN) == 0) {
|
||||
/* stored before messages carried the key: the old cache knows it */
|
||||
if (conv->channel >= r->nOld || MemCmp(r->oldKeys + conv->channel * KEY_LEN, zero, KEY_LEN) == 0)
|
||||
return;
|
||||
MemMove(conv->key, r->oldKeys + conv->channel * KEY_LEN, KEY_LEN);
|
||||
}
|
||||
for (i = 0; i < r->nNew; i++)
|
||||
if (MemCmp(r->newKeys + i * (1 + KEY_LEN) + 1, conv->key, KEY_LEN) == 0) {
|
||||
conv->channel = r->newKeys[i * (1 + KEY_LEN)];
|
||||
return;
|
||||
}
|
||||
conv->channel = VM_SLOT_GONE;
|
||||
}
|
||||
|
||||
/* Records before oldCount are the previous cache, the rest the new one. */
|
||||
static void RefileMessages(UInt16 oldCount, UInt8 maxChannels)
|
||||
{
|
||||
VmChannel c;
|
||||
Refile r;
|
||||
UInt16 i, n = DmNumRecords(gDb);
|
||||
|
||||
r.nOld = maxChannels;
|
||||
r.nNew = 0;
|
||||
r.oldKeys = MemPtrNew(maxChannels * KEY_LEN + 1);
|
||||
r.newKeys = MemPtrNew((n - oldCount) * (1 + KEY_LEN) + 1);
|
||||
if (r.oldKeys && r.newKeys) {
|
||||
MemSet(r.oldKeys, maxChannels * KEY_LEN, 0);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!ChannelsGet(i, &c))
|
||||
continue;
|
||||
if (i < oldCount) {
|
||||
if (c.idx < maxChannels)
|
||||
MemMove(r.oldKeys + c.idx * KEY_LEN, c.secret, KEY_LEN);
|
||||
} else {
|
||||
r.newKeys[r.nNew * (1 + KEY_LEN)] = c.idx;
|
||||
MemMove(r.newKeys + r.nNew * (1 + KEY_LEN) + 1, c.secret, KEY_LEN);
|
||||
r.nNew++;
|
||||
}
|
||||
}
|
||||
MessagesRefileChannels(RefileConv, &r);
|
||||
}
|
||||
if (r.oldKeys)
|
||||
MemPtrFree(r.oldKeys);
|
||||
if (r.newKeys)
|
||||
MemPtrFree(r.newKeys);
|
||||
}
|
||||
|
||||
Err ChannelsRefresh(UInt8 maxChannels)
|
||||
{
|
||||
VmChannel c;
|
||||
@@ -112,6 +183,7 @@ Err ChannelsRefresh(UInt8 maxChannels)
|
||||
MemHandleUnlock(h);
|
||||
DmReleaseRecord(gDb, index, true);
|
||||
}
|
||||
RefileMessages(oldCount, maxChannels);
|
||||
for (i = 0; i < oldCount; i++)
|
||||
DmRemoveRecord(gDb, 0);
|
||||
return errNone;
|
||||
|
||||
@@ -27,7 +27,7 @@ static void Build(void) VM_SECT2;
|
||||
static Int16 TextWithMentions(const Char *text, UInt16 len, Coord x, Coord y, Coord hitX) VM_SECT2;
|
||||
static void DrawLine(UInt16 row, const RectangleType *b, void *ctx) VM_SECT2;
|
||||
static void DrawCounter(void) VM_SECT2;
|
||||
static UInt16 FirstUnread(void) VM_SECT2;
|
||||
static UInt32 FirstUnread(void) VM_SECT2;
|
||||
static void ScrollToNew(void) VM_SECT2;
|
||||
static void Refresh(Boolean toEnd) VM_SECT2;
|
||||
static void Send(FormType *form) VM_SECT2;
|
||||
@@ -45,7 +45,7 @@ static Boolean TappedEmoji(UInt16 row, Coord penX) VM_SECT2;
|
||||
#define L_CHIP 1
|
||||
#define L_TEXT 2
|
||||
#define L_NEW 3 /* "── new ──" before the first message unread when the screen opened */
|
||||
#define NO_NEW 0xFFFF
|
||||
#define NO_NEW 0
|
||||
|
||||
typedef struct {
|
||||
UInt8 kind;
|
||||
@@ -60,7 +60,7 @@ static Line *gLines = NULL;
|
||||
static UInt16 gCount = 0;
|
||||
static RowList gList;
|
||||
static Char gTitle[VM_NAME_SIZE + 2];
|
||||
static UInt16 gNewFrom = NO_NEW; /* message record of the first unread one at open */
|
||||
static UInt32 gNewFrom = NO_NEW; /* unique ID of the first message unread at open */
|
||||
|
||||
static const Char *kDays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
||||
static const Char *kMonths[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
@@ -161,7 +161,7 @@ static void Build(void)
|
||||
lastDay = day;
|
||||
lastSender[0] = 0;
|
||||
}
|
||||
if (m == gNewFrom) {
|
||||
if (gNewFrom != NO_NEW && MessagesUniqueId(m) == gNewFrom) {
|
||||
AddLine(L_NEW, m, 0, 0, false, false, 0);
|
||||
lastSender[0] = 0; /* start the new part with a sender chip */
|
||||
}
|
||||
@@ -310,16 +310,17 @@ static void DrawCounter(void)
|
||||
}
|
||||
|
||||
/* The oldest unread message of this conversation, or NO_NEW. */
|
||||
static UInt16 FirstUnread(void)
|
||||
static UInt32 FirstUnread(void)
|
||||
{
|
||||
UInt16 i, n = MessagesCount(), found = NO_NEW;
|
||||
UInt16 i, n = MessagesCount();
|
||||
UInt32 found = NO_NEW;
|
||||
|
||||
for (i = n; i > 0; i--) {
|
||||
const VmMsgHeader *h = MessagesLock(i - 1);
|
||||
|
||||
if (h) {
|
||||
if (h->unread && ConvEqual(&h->conv, &gAppConv))
|
||||
found = i - 1;
|
||||
found = MessagesUniqueId(i - 1);
|
||||
MessagesRelease(i - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
#include "ui/text.h"
|
||||
#include "messages.h"
|
||||
#include "contacts.h"
|
||||
#include "channels.h"
|
||||
|
||||
/* In the second code segment: see sections.h for the rules. */
|
||||
static Err Append(const VmMsgHeader *hdr, const Char *text, UInt16 *index) VM_SECT2;
|
||||
static void WriteHeader(UInt16 i, const VmMsgHeader *hdr) VM_SECT2;
|
||||
static Boolean ReadHeader(UInt16 i, VmMsgHeader *hdr) VM_SECT2;
|
||||
static void TagChannel(VmConv *conv) VM_SECT2;
|
||||
|
||||
/* a data array, not a literal: this module is in the second code segment (sections.h) */
|
||||
static Char DB_NAME[] = "VisorMeshMessages";
|
||||
@@ -69,6 +71,15 @@ void MessagesRelease(UInt16 i)
|
||||
MemHandleUnlock(h);
|
||||
}
|
||||
|
||||
UInt32 MessagesUniqueId(UInt16 i)
|
||||
{
|
||||
UInt32 id;
|
||||
|
||||
if (!gDb || DmRecordInfo(gDb, i, NULL, &id, NULL) != errNone)
|
||||
return 0;
|
||||
return id;
|
||||
}
|
||||
|
||||
Boolean ConvEqual(const VmConv *a, const VmConv *b)
|
||||
{
|
||||
if (a->type != b->type)
|
||||
@@ -78,6 +89,16 @@ Boolean ConvEqual(const VmConv *a, const VmConv *b)
|
||||
return MemCmp(a->key, b->key, 6) == 0;
|
||||
}
|
||||
|
||||
/* Notes the channel's secret prefix on a channel message, for MessagesRefileChannels. */
|
||||
static void TagChannel(VmConv *conv)
|
||||
{
|
||||
VmChannel c;
|
||||
|
||||
MemSet(conv->key, sizeof(conv->key), 0);
|
||||
if (ChannelsFindSlot(conv->channel, &c))
|
||||
MemMove(conv->key, c.secret, sizeof(conv->key));
|
||||
}
|
||||
|
||||
static Err Append(const VmMsgHeader *hdr, const Char *text, UInt16 *index)
|
||||
{
|
||||
UInt16 i = dmMaxRecordIndex;
|
||||
@@ -115,6 +136,7 @@ Err MessagesAddIncoming(const VmMessage *m, UInt32 now)
|
||||
if (m->isChannel) {
|
||||
hdr.conv.type = VM_CONV_CHANNEL;
|
||||
hdr.conv.channel = m->channelIdx;
|
||||
TagChannel(&hdr.conv);
|
||||
nameLen = TxtSplitSender(text, len);
|
||||
if (nameLen) {
|
||||
if (nameLen > VM_NAME_SIZE - 1)
|
||||
@@ -153,6 +175,8 @@ Err MessagesAddOutgoing(const VmConv *conv, const Char *text, UInt32 now, UInt16
|
||||
return err;
|
||||
MemSet(&hdr, sizeof(hdr), 0);
|
||||
hdr.conv = *conv;
|
||||
if (conv->type == VM_CONV_CHANNEL)
|
||||
TagChannel(&hdr.conv);
|
||||
hdr.dir = VM_DIR_OUT;
|
||||
hdr.status = VM_ST_PENDING;
|
||||
hdr.timestamp = now;
|
||||
@@ -260,3 +284,22 @@ void MessagesMarkRead(const VmConv *conv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MessagesRefileChannels(MessagesConvFn fn, void *ctx)
|
||||
{
|
||||
VmMsgHeader hdr;
|
||||
VmConv was;
|
||||
UInt16 i, n;
|
||||
|
||||
if (MessagesOpen() != errNone)
|
||||
return;
|
||||
n = DmNumRecords(gDb);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!ReadHeader(i, &hdr) || hdr.conv.type != VM_CONV_CHANNEL)
|
||||
continue;
|
||||
was = hdr.conv;
|
||||
fn(&hdr.conv, ctx);
|
||||
if (MemCmp(&was, &hdr.conv, sizeof(VmConv)) != 0)
|
||||
WriteHeader(i, &hdr);
|
||||
}
|
||||
}
|
||||
@@ -12,14 +12,19 @@
|
||||
|
||||
#define VM_MAX_MESSAGES 500
|
||||
|
||||
/* A conversation: a channel slot, or a contact (by key prefix). */
|
||||
/*
|
||||
* A conversation: a channel slot, or a contact (by key prefix). Stored
|
||||
* channel messages also carry the channel's secret prefix in key, so their
|
||||
* history follows the channel when its slot changes (channels.c).
|
||||
*/
|
||||
#define VM_CONV_CHANNEL 0
|
||||
#define VM_CONV_DIRECT 1
|
||||
#define VM_SLOT_GONE 0xFF
|
||||
|
||||
typedef struct {
|
||||
UInt8 type; /* VM_CONV_CHANNEL / VM_CONV_DIRECT */
|
||||
UInt8 channel; /* channel slot */
|
||||
UInt8 key[6]; /* contact key prefix */
|
||||
UInt8 channel; /* channel slot; VM_SLOT_GONE: a channel that was left */
|
||||
UInt8 key[6]; /* contact key prefix; a stored channel message: the secret's */
|
||||
} VmConv;
|
||||
|
||||
#define VM_DIR_IN 0
|
||||
@@ -54,6 +59,8 @@ UInt16 MessagesCount(void) VM_SECT2;
|
||||
/* Locks record i; returns its header (text right after it). Call MessagesRelease(i) when done. */
|
||||
const VmMsgHeader *MessagesLock(UInt16 i) VM_SECT2;
|
||||
void MessagesRelease(UInt16 i) VM_SECT2;
|
||||
/* Record i's unique ID (0 if none): unlike the index, it stays put when old messages drop off. */
|
||||
UInt32 MessagesUniqueId(UInt16 i) VM_SECT2;
|
||||
|
||||
Boolean ConvEqual(const VmConv *a, const VmConv *b) VM_SECT2;
|
||||
|
||||
@@ -70,5 +77,11 @@ Boolean MessagesAcked(UInt32 ack) VM_SECT2;
|
||||
Boolean MessagesExpireAcks(UInt32 now) VM_SECT2;
|
||||
/* Clears the unread flag on a conversation's messages. */
|
||||
void MessagesMarkRead(const VmConv *conv) VM_SECT2;
|
||||
/*
|
||||
* Calls fn for each channel message's conversation; fn may change it (its
|
||||
* slot, its key) and the record is rewritten if it did.
|
||||
*/
|
||||
typedef void (*MessagesConvFn)(VmConv *conv, void *ctx);
|
||||
void MessagesRefileChannels(MessagesConvFn fn, void *ctx) VM_SECT2;
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user